Skip to content

Commit 8e32dd7

Browse files
jsm174freezy
authored andcommitted
coils: update pulse coil unit to take multiple coils
1 parent 244121d commit 8e32dd7

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

Editor/Descriptors/PulseCoilUnitDescriptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4444
base.DefinedPort(port, desc);
4545

4646
switch (port.key) {
47-
case nameof(PulseCoilUnit.Id):
48-
desc.summary = "The ID of the coil to be pulsed.";
47+
case nameof(PulseCoilUnit.Ids):
48+
desc.summary = "The IDs of the coils to be pulsed.";
4949
break;
5050
case nameof(PulseCoilUnit.PulseDuration):
51-
desc.summary = "The time in milliseconds until the coil is disabled again.";
51+
desc.summary = "The time in milliseconds until the coils are disabled again.";
5252
break;
5353
}
5454
}

Editor/Widgets/PulseCoilUnitWidget.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ namespace VisualPinball.Unity.VisualScripting.Editor
2626
[Widget(typeof(PulseCoilUnit))]
2727
public sealed class PulseCoilUnitWidget : GleUnitWidget<PulseCoilUnit>
2828
{
29-
private VariableNameInspector _coilIdInspector;
30-
private readonly Func<Metadata, VariableNameInspector> _setCoilInspectorConstructor;
29+
private readonly List<Func<Metadata, VariableNameInspector>> _coilIdInspectorConstructorList;
3130

3231
public PulseCoilUnitWidget(FlowCanvas canvas, PulseCoilUnit unit) : base(canvas, unit)
3332
{
34-
_setCoilInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
33+
_coilIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3534
}
3635

3736
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3837
{
39-
if (port == unit.Id) {
40-
InspectorProvider.instance.Renew(ref _coilIdInspector, meta, _setCoilInspectorConstructor);
41-
return _coilIdInspector;
38+
if (_coilIdInspectorConstructorList.Count() < unit.idCount) {
39+
for (var index = 0; index < unit.idCount - _coilIdInspectorConstructorList.Count(); index++) {
40+
_coilIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
41+
}
42+
}
43+
44+
for (var index = 0; index < unit.idCount; index++) {
45+
if (unit.Ids[index] == port) {
46+
VariableNameInspector coilIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref coilIdInspector, meta, _coilIdInspectorConstructorList[index]);
48+
49+
return coilIdInspector;
50+
}
4251
}
4352

4453
return base.GetPortInspector(port, meta);

Runtime/Nodes/Coils/PulseCoilUnit.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
using System.Collections.Generic;
1718
using Unity.VisualScripting;
1819
using UnityEngine;
1920

@@ -32,9 +33,19 @@ public class PulseCoilUnit : GleUnit
3233
[PortLabelHidden]
3334
public ControlOutput OutputTrigger;
3435

36+
[SerializeAs(nameof(idCount))]
37+
private int _idCount = 1;
38+
39+
[DoNotSerialize]
40+
[Inspectable, UnitHeaderInspectable("Coil IDs")]
41+
public int idCount
42+
{
43+
get => _idCount;
44+
set => _idCount = Mathf.Clamp(value, 1, 10);
45+
}
46+
3547
[DoNotSerialize]
36-
[PortLabel("Coil ID")]
37-
public ValueInput Id { get; private set; }
48+
public List<ValueInput> Ids { get; private set; }
3849

3950
[DoNotSerialize]
4051
[PortLabel("Duration (ms)")]
@@ -45,10 +56,17 @@ protected override void Definition()
4556
InputTrigger = ControlInput(nameof(InputTrigger), Process);
4657
OutputTrigger = ControlOutput(nameof(OutputTrigger));
4758

48-
Id = ValueInput<string>(nameof(Id), string.Empty);
59+
Ids = new List<ValueInput>();
60+
61+
for (var i = 0; i < idCount; i++) {
62+
var id = ValueInput<string>("Coil ID " + (i + 1), string.Empty);
63+
Ids.Add(id);
64+
65+
Requirement(id, InputTrigger);
66+
}
67+
4968
PulseDuration = ValueInput<int>(nameof(PulseDuration), 80);
5069

51-
Requirement(Id, InputTrigger);
5270
Succession(InputTrigger, OutputTrigger);
5371
}
5472

@@ -60,15 +78,17 @@ private ControlOutput Process(Flow flow)
6078
}
6179

6280
if (!AssertPlayer(flow)) {
63-
Debug.LogError("Cannot find GLE.");
81+
Debug.LogError("Cannot find Player.");
6482
return OutputTrigger;
6583
}
6684

67-
var id = flow.GetValue<string>(Id);
68-
var pulseDuration = flow.GetValue<int>(PulseDuration);
85+
foreach (var id in Ids) {
86+
var idValue = flow.GetValue<string>(id);
87+
var pulseDuration = flow.GetValue<int>(PulseDuration);
6988

70-
Gle.SetCoil(id, true);
71-
Player.ScheduleAction(pulseDuration, () => Gle.SetCoil(id, false));
89+
Gle.SetCoil(idValue, true);
90+
Player.ScheduleAction(pulseDuration, () => Gle.SetCoil(idValue, false));
91+
}
7292

7393
return OutputTrigger;
7494
}

0 commit comments

Comments
 (0)