Skip to content

Commit bc732d2

Browse files
jsm174freezy
authored andcommitted
scripting: update set coil unit to accept multiple coils
1 parent 8e32dd7 commit bc732d2

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

Editor/Descriptors/SetCoilUnitDescriptor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public SetCoilUnitDescriptor(SetCoilUnit target) : base(target)
3030

3131
protected override string DefinedSummary()
3232
{
33-
return "This node assigns a given value to a coil defined by its ID.";
33+
return "This node assigns a given value to coils defined by their IDs.";
3434
}
3535

3636
protected override EditorTexture DefinedIcon()
@@ -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(SetCoilUnit.Id):
48-
desc.summary = "The ID of the coil to be set.";
47+
case nameof(SetCoilUnit.Ids):
48+
desc.summary = "The IDs of the coils to be set.";
4949
break;
5050
case nameof(SetCoilUnit.IsEnabled):
51-
desc.summary = "The value to assign to the coil.";
51+
desc.summary = "The value to assign to the coils.";
5252
break;
5353
}
5454
}

Editor/Widgets/SetCoilUnitWidget.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(SetCoilUnit))]
2727
public sealed class SetCoilUnitWidget : GleUnitWidget<SetCoilUnit>
2828
{
29-
private VariableNameInspector _coilIdInspector;
30-
private readonly Func<Metadata, VariableNameInspector> _setCoilInspectorConstructor;
29+
private readonly List<Func<Metadata, VariableNameInspector>> _coilIdInspectorConstructorList;
3130

3231
public SetCoilUnitWidget(FlowCanvas canvas, SetCoilUnit 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ private ControlOutput Process(Flow flow)
8282
return OutputTrigger;
8383
}
8484

85+
var pulseDuration = flow.GetValue<int>(PulseDuration);
86+
8587
foreach (var id in Ids) {
8688
var idValue = flow.GetValue<string>(id);
87-
var pulseDuration = flow.GetValue<int>(PulseDuration);
8889

8990
Gle.SetCoil(idValue, true);
9091
Player.ScheduleAction(pulseDuration, () => Gle.SetCoil(idValue, false));

Runtime/Nodes/Coils/SetCoilUnit.cs

Lines changed: 27 additions & 6 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 SetCoilUnit : 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("Value")]
@@ -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
IsEnabled = ValueInput<bool>(nameof(IsEnabled), false);
5069

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

@@ -59,10 +77,13 @@ private ControlOutput Process(Flow flow)
5977
return OutputTrigger;
6078
}
6179

62-
var id = flow.GetValue<string>(Id);
6380
var isEnabled = flow.GetValue<bool>(IsEnabled);
6481

65-
Gle.SetCoil(id, isEnabled);
82+
foreach (var id in Ids) {
83+
var idValue = flow.GetValue<string>(id);
84+
85+
Gle.SetCoil(idValue, isEnabled);
86+
}
6687

6788
return OutputTrigger;
6889
}

0 commit comments

Comments
 (0)