Skip to content

Commit 31abc9f

Browse files
jsm174freezy
authored andcommitted
misc: cleanup. Added IMultiInputUnit to SwitchEvent
1 parent e06660e commit 31abc9f

File tree

9 files changed

+90
-49
lines changed

9 files changed

+90
-49
lines changed

Editor/Descriptors/SwitchEventUnitDescriptor.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
3838
{
3939
base.DefinedPort(port, desc);
4040

41-
switch (port.key) {
42-
case nameof(SwitchEventUnit.Id):
43-
desc.summary = "The ID of the switch that changed its value.";
44-
break;
45-
case nameof(SwitchEventUnit.IsEnabled):
46-
desc.summary = "The new value of the switch, true if enabled, false otherwise.";
47-
break;
41+
if (port.key == nameof(SwitchEventUnit.IsEnabled)) {
42+
desc.summary = "The new value of the switch, true if enabled, false otherwise.";
43+
}
44+
else if (int.TryParse(port.key, out int id)) {
45+
id += 1;
46+
47+
desc.label = $"Switch ID {id}";
48+
desc.summary = $"Switch ID {id} to look for a change status.";
4849
}
4950
}
5051
}

Editor/Widgets/PulseCoilUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4242
}
4343

4444
for (var index = 0; index < unit.inputCount; index++) {
45-
if (unit.Items[index] == port) {
45+
if (unit.multiInputs[index] == port) {
4646
VariableNameInspector coilIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref coilIdInspector, meta, _coilIdInspectorConstructorList[index]);
4848

Editor/Widgets/SetCoilUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4242
}
4343

4444
for (var index = 0; index < unit.inputCount; index++) {
45-
if (unit.Items[index] == port) {
45+
if (unit.multiInputs[index] == port) {
4646
VariableNameInspector coilIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref coilIdInspector, meta, _coilIdInspectorConstructorList[index]);
4848

Editor/Widgets/SwitchEnabledEventUnitWidget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4242
}
4343

4444
for (var index = 0; index < unit.inputCount; index++) {
45-
if (unit.Items[index] == port) {
45+
if (unit.multiInputs[index] == port) {
4646
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);
4848

Editor/Widgets/SwitchEventUnitWidget.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,28 @@ namespace VisualPinball.Unity.VisualScripting.Editor
2626
[Widget(typeof(SwitchEventUnit))]
2727
public sealed class SwitchEventUnitWidget : GleUnitWidget<SwitchEventUnit>
2828
{
29-
private VariableNameInspector _switchIdInspector;
30-
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
29+
private readonly List<Func<Metadata, VariableNameInspector>> _switchIdInspectorConstructorList;
3130

3231
public SwitchEventUnitWidget(FlowCanvas canvas, SwitchEventUnit unit) : base(canvas, unit)
3332
{
34-
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
33+
_switchIdInspectorConstructorList = 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 _switchIdInspector, meta, _switchIdInspectorConstructor);
38+
if (_switchIdInspectorConstructorList.Count() < unit.inputCount) {
39+
for (var index = 0; index < unit.inputCount - _switchIdInspectorConstructorList.Count(); index++) {
40+
_switchIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
41+
}
42+
}
43+
44+
for (var index = 0; index < unit.inputCount; index++) {
45+
if (unit.multiInputs[index] == port) {
46+
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);
4148

42-
return _switchIdInspector;
49+
return switchIdInspector;
50+
}
4351
}
4452

4553
return base.GetPortInspector(port, meta);

Runtime/Nodes/Coils/PulseCoilUnit.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1819
using Unity.VisualScripting;
1920
using UnityEngine;
2021

@@ -23,7 +24,7 @@ namespace VisualPinball.Unity.VisualScripting
2324
[UnitTitle("Pulse Coil")]
2425
[UnitSurtitle("Gamelogic Engine")]
2526
[UnitCategory("Visual Pinball")]
26-
public class PulseCoilUnit : GleUnit
27+
public class PulseCoilUnit : GleUnit, IMultiInputUnit
2728
{
2829
[DoNotSerialize]
2930
[PortLabelHidden]
@@ -45,7 +46,7 @@ public int inputCount
4546
}
4647

4748
[DoNotSerialize]
48-
public List<ValueInput> Items { get; private set; }
49+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
4950

5051
[DoNotSerialize]
5152
[PortLabel("Duration (ms)")]
@@ -56,13 +57,15 @@ protected override void Definition()
5657
InputTrigger = ControlInput(nameof(InputTrigger), Process);
5758
OutputTrigger = ControlOutput(nameof(OutputTrigger));
5859

59-
Items = new List<ValueInput>();
60+
var _multiInputs = new List<ValueInput>();
61+
62+
multiInputs = _multiInputs.AsReadOnly();
6063

6164
for (var i = 0; i < inputCount; i++) {
62-
var item = ValueInput(i.ToString(), string.Empty);
63-
Items.Add(item);
65+
var input = ValueInput(i.ToString(), string.Empty);
66+
_multiInputs.Add(input);
6467

65-
Requirement(item, InputTrigger);
68+
Requirement(input, InputTrigger);
6669
}
6770

6871
PulseDuration = ValueInput(nameof(PulseDuration), 80);
@@ -84,11 +87,10 @@ private ControlOutput Process(Flow flow)
8487

8588
var pulseDuration = flow.GetValue<int>(PulseDuration);
8689

87-
foreach (var item in Items) {
88-
var id = flow.GetValue<string>(item);
89-
90-
Gle.SetCoil(id, true);
91-
Player.ScheduleAction(pulseDuration, () => Gle.SetCoil(id, false));
90+
foreach (var input in multiInputs) {
91+
var coilId = flow.GetValue<string>(input);
92+
Gle.SetCoil(coilId, true);
93+
Player.ScheduleAction(pulseDuration, () => Gle.SetCoil(coilId, false));
9294
}
9395

9496
return OutputTrigger;

Runtime/Nodes/Coils/SetCoilUnit.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1819
using Unity.VisualScripting;
1920
using UnityEngine;
2021

@@ -23,7 +24,7 @@ namespace VisualPinball.Unity.VisualScripting
2324
[UnitTitle("Set Coil")]
2425
[UnitSurtitle("Gamelogic Engine")]
2526
[UnitCategory("Visual Pinball")]
26-
public class SetCoilUnit : GleUnit
27+
public class SetCoilUnit : GleUnit, IMultiInputUnit
2728
{
2829
[DoNotSerialize]
2930
[PortLabelHidden]
@@ -45,7 +46,7 @@ public int inputCount
4546
}
4647

4748
[DoNotSerialize]
48-
public List<ValueInput> Items { get; private set; }
49+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
4950

5051
[DoNotSerialize]
5152
[PortLabel("Value")]
@@ -56,13 +57,15 @@ protected override void Definition()
5657
InputTrigger = ControlInput(nameof(InputTrigger), Process);
5758
OutputTrigger = ControlOutput(nameof(OutputTrigger));
5859

59-
Items = new List<ValueInput>();
60+
var _multiInputs = new List<ValueInput>();
61+
62+
multiInputs = _multiInputs.AsReadOnly();
6063

6164
for (var i = 0; i < inputCount; i++) {
62-
var item = ValueInput(i.ToString(), string.Empty);
63-
Items.Add(item);
65+
var input = ValueInput(i.ToString(), string.Empty);
66+
_multiInputs.Add(input);
6467

65-
Requirement(item, InputTrigger);
68+
Requirement(input, InputTrigger);
6669
}
6770

6871
IsEnabled = ValueInput(nameof(IsEnabled), false);
@@ -79,10 +82,9 @@ private ControlOutput Process(Flow flow)
7982

8083
var isEnabled = flow.GetValue<bool>(IsEnabled);
8184

82-
foreach (var item in Items) {
83-
var id = flow.GetValue<string>(item);
84-
85-
Gle.SetCoil(id, isEnabled);
85+
foreach (var input in multiInputs) {
86+
var coilId = flow.GetValue<string>(input);
87+
Gle.SetCoil(coilId, isEnabled);
8688
}
8789

8890
return OutputTrigger;

Runtime/Nodes/Switches/SwitchEnabledEventUnit.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1819
using Unity.VisualScripting;
1920
using UnityEngine;
2021

@@ -23,7 +24,7 @@ namespace VisualPinball.Unity.VisualScripting
2324
[UnitTitle("On Switch Enabled")]
2425
[UnitSurtitle("Gamelogic Engine")]
2526
[UnitCategory("Events\\Visual Pinball")]
26-
public class SwitchEnabledEventUnit : GleEventUnit<SwitchEventArgs2>
27+
public class SwitchEnabledEventUnit : GleEventUnit<SwitchEventArgs2>, IMultiInputUnit
2728
{
2829
[SerializeAs(nameof(inputCount))]
2930
private int _inputCount = 1;
@@ -37,7 +38,7 @@ public int inputCount
3738
}
3839

3940
[DoNotSerialize]
40-
public List<ValueInput> Items { get; private set; }
41+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
4142

4243
[DoNotSerialize]
4344
protected override bool register => true;
@@ -49,18 +50,19 @@ protected override void Definition()
4950
{
5051
base.Definition();
5152

52-
Items = new List<ValueInput>();
53+
var _multiInputs = new List<ValueInput>();
54+
55+
multiInputs = _multiInputs.AsReadOnly();
5356

5457
for (var i = 0; i < inputCount; i++) {
55-
var item = ValueInput(i.ToString(), string.Empty);
56-
Items.Add(item);
58+
_multiInputs.Add(ValueInput(i.ToString(), string.Empty));
5759
}
5860
}
5961

6062
protected override bool ShouldTrigger(Flow flow, SwitchEventArgs2 args)
6163
{
62-
foreach(var item in Items) {
63-
if (flow.GetValue<string>(item) == args.Id && args.IsEnabled) {
64+
foreach(var input in multiInputs) {
65+
if (flow.GetValue<string>(input) == args.Id && args.IsEnabled) {
6466
return true;
6567
}
6668
}

Runtime/Nodes/Switches/SwitchEventUnit.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,31 @@
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;
18+
using System.Collections.ObjectModel;
1719
using Unity.VisualScripting;
20+
using UnityEngine;
1821

1922
namespace VisualPinball.Unity.VisualScripting
2023
{
2124
[UnitTitle("On Switch Changed")]
2225
[UnitSurtitle("Gamelogic Engine")]
2326
[UnitCategory("Events\\Visual Pinball")]
24-
public class SwitchEventUnit : GleEventUnit<SwitchEventArgs2>
27+
public class SwitchEventUnit : GleEventUnit<SwitchEventArgs2>, IMultiInputUnit
2528
{
29+
[SerializeAs(nameof(inputCount))]
30+
private int _inputCount = 1;
31+
32+
[DoNotSerialize]
33+
[Inspectable, UnitHeaderInspectable("Switch IDs")]
34+
public int inputCount
35+
{
36+
get => _inputCount;
37+
set => _inputCount = Mathf.Clamp(value, 1, 10);
38+
}
39+
2640
[DoNotSerialize]
27-
[PortLabel("Switch ID")]
28-
public ValueInput Id { get; private set; }
41+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
2942

3043
[DoNotSerialize]
3144
[PortLabel("Is Enabled")]
@@ -43,13 +56,26 @@ protected override void Definition()
4356
{
4457
base.Definition();
4558

46-
Id = ValueInput(nameof(Id), string.Empty);
59+
var _multiInputs = new List<ValueInput>();
60+
61+
multiInputs = _multiInputs.AsReadOnly();
62+
63+
for (var i = 0; i < inputCount; i++) {
64+
_multiInputs.Add(ValueInput(i.ToString(), string.Empty));
65+
}
66+
4767
IsEnabled = ValueOutput<bool>(nameof(IsEnabled));
4868
}
4969

5070
protected override bool ShouldTrigger(Flow flow, SwitchEventArgs2 args)
5171
{
52-
return flow.GetValue<string>(Id) == args.Id;
72+
foreach (var input in multiInputs) {
73+
if (flow.GetValue<string>(input) == args.Id) {
74+
return true;
75+
}
76+
}
77+
78+
return false;
5379
}
5480

5581
protected override void AssignArguments(Flow flow, SwitchEventArgs2 args)

0 commit comments

Comments
 (0)