Skip to content

Commit 244121d

Browse files
jsm174freezy
authored andcommitted
switches: update SwitchEnabledEvent to accept multiple ids (OR). update GetSwitch to accept multiple ids (AND)
1 parent 651b553 commit 244121d

File tree

6 files changed

+88
-26
lines changed

6 files changed

+88
-26
lines changed

Editor/Descriptors/GetSwitchUnitDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4141
base.DefinedPort(port, desc);
4242

4343
switch (port.key) {
44-
case nameof(GetSwitchUnit.Id):
45-
desc.summary = "The ID of the switch for which to get current status.";
44+
case nameof(GetSwitchUnit.Ids):
45+
desc.summary = "The IDs of the switches for which to get the current status.";
4646
break;
4747
case nameof(GetSwitchUnit.IsEnabled):
4848
desc.summary = "Whether the switch is enabled.";

Editor/Descriptors/SwitchEnabledEventUnitDescriptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public SwitchEnabledEventUnitDescriptor(SwitchEnabledEventUnit target) : base(ta
2929

3030
protected override string DefinedSummary()
3131
{
32-
return "This node triggers an event when a switch with a given ID is enabled.";
32+
return "This node triggers an event when a switch in the list of given ID is enabled.";
3333
}
3434

3535
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.SwitchEvent);
@@ -39,8 +39,8 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
3939
base.DefinedPort(port, desc);
4040

4141
switch (port.key) {
42-
case nameof(SwitchEnabledEventUnit.Id):
43-
desc.summary = "The ID of the switch that was enabled.";
42+
case nameof(SwitchEnabledEventUnit.Ids):
43+
desc.summary = "The IDs of the switches for which to look for enabled status.";
4444
break;
4545
}
4646
}

Editor/Widgets/GetSwitchUnitWidget.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,30 @@ namespace VisualPinball.Unity.VisualScripting.Editor
2626
[Widget(typeof(GetSwitchUnit))]
2727
public sealed class GetSwitchUnitWidget : GleUnitWidget<GetSwitchUnit>
2828
{
29+
private readonly List<Func<Metadata, VariableNameInspector>> _switchIdInspectorConstructorList;
30+
2931
public GetSwitchUnitWidget(FlowCanvas canvas, GetSwitchUnit unit) : base(canvas, unit)
3032
{
31-
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
33+
_switchIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
3234
}
3335

3436
protected override NodeColorMix baseColor => NodeColorMix.TealReadable;
3537

36-
private VariableNameInspector _switchIdInspector;
37-
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
38-
3938
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
4039
{
41-
if (port == unit.Id) {
42-
InspectorProvider.instance.Renew(ref _switchIdInspector, meta, _switchIdInspectorConstructor);
40+
if (_switchIdInspectorConstructorList.Count() < unit.idCount) {
41+
for (var index = 0; index < unit.idCount - _switchIdInspectorConstructorList.Count(); index++) {
42+
_switchIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
43+
}
44+
}
45+
46+
for (var index = 0; index < unit.idCount; index++) {
47+
if (unit.Ids[index] == port) {
48+
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
49+
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);
4350

44-
return _switchIdInspector;
51+
return switchIdInspector;
52+
}
4553
}
4654

4755
return base.GetPortInspector(port, meta);

Editor/Widgets/SwitchEnabledEventUnitWidget.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(SwitchEnabledEventUnit))]
2727
public sealed class SwitchEnabledEventUnitWidget : GleUnitWidget<SwitchEnabledEventUnit>
2828
{
29-
private VariableNameInspector _switchIdInspector;
30-
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
29+
private readonly List<Func<Metadata, VariableNameInspector>> _switchIdInspectorConstructorList;
3130

3231
public SwitchEnabledEventUnitWidget(FlowCanvas canvas, SwitchEnabledEventUnit 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.idCount) {
39+
for (var index = 0; index < unit.idCount - _switchIdInspectorConstructorList.Count(); index++) {
40+
_switchIdInspectorConstructorList.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 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/Switches/GetSwitchUnit.cs

Lines changed: 26 additions & 4 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

@@ -24,17 +25,32 @@ namespace VisualPinball.Unity.VisualScripting
2425
[UnitCategory("Visual Pinball")]
2526
public class GetSwitchUnit : GleUnit
2627
{
28+
[SerializeAs(nameof(idCount))]
29+
private int _idCount = 1;
30+
31+
[DoNotSerialize]
32+
[Inspectable, UnitHeaderInspectable("Switch IDs")]
33+
public int idCount
34+
{
35+
get => _idCount;
36+
set => _idCount = Mathf.Clamp(value, 1, 10);
37+
}
38+
2739
[DoNotSerialize]
28-
[PortLabel("Switch ID")]
29-
public ValueInput Id { get; private set; }
40+
public List<ValueInput> Ids { get; private set; }
3041

3142
[DoNotSerialize]
3243
[PortLabel("Is Enabled")]
3344
public ValueOutput IsEnabled { get; private set; }
3445

3546
protected override void Definition()
3647
{
37-
Id = ValueInput(nameof(Id), string.Empty);
48+
Ids = new List<ValueInput>();
49+
50+
for (var i = 0; i < idCount; i++) {
51+
var id = ValueInput<string>("Switch ID " + (i + 1), string.Empty);
52+
Ids.Add(id);
53+
}
3854

3955
IsEnabled = ValueOutput(nameof(IsEnabled), GetEnabled);
4056
}
@@ -46,7 +62,13 @@ private bool GetEnabled(Flow flow)
4662
return false;
4763
}
4864

49-
return Gle.GetSwitch(flow.GetValue<string>(Id));
65+
foreach (var id in Ids) {
66+
if (!Gle.GetSwitch(flow.GetValue<string>(id))) {
67+
return false;
68+
}
69+
}
70+
71+
return true;
5072
}
5173
}
5274
}

Runtime/Nodes/Switches/SwitchEnabledEventUnit.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
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;
19+
using UnityEngine;
1820

1921
namespace VisualPinball.Unity.VisualScripting
2022
{
@@ -23,9 +25,19 @@ namespace VisualPinball.Unity.VisualScripting
2325
[UnitCategory("Events\\Visual Pinball")]
2426
public class SwitchEnabledEventUnit : GleEventUnit<SwitchEventArgs2>
2527
{
28+
[SerializeAs(nameof(idCount))]
29+
private int _idCount = 1;
30+
2631
[DoNotSerialize]
27-
[PortLabel("Switch ID")]
28-
public ValueInput Id { get; private set; }
32+
[Inspectable, UnitHeaderInspectable("Switch IDs")]
33+
public int idCount
34+
{
35+
get => _idCount;
36+
set => _idCount = Mathf.Clamp(value, 1, 10);
37+
}
38+
39+
[DoNotSerialize]
40+
public List<ValueInput> Ids { get; private set; }
2941

3042
[DoNotSerialize]
3143
protected override bool register => true;
@@ -36,12 +48,24 @@ public class SwitchEnabledEventUnit : GleEventUnit<SwitchEventArgs2>
3648
protected override void Definition()
3749
{
3850
base.Definition();
39-
Id = ValueInput(nameof(Id), string.Empty);
51+
52+
Ids = new List<ValueInput>();
53+
54+
for (var i = 0; i < idCount; i++) {
55+
var id = ValueInput<string>("Switch ID " + (i + 1), string.Empty);
56+
Ids.Add(id);
57+
}
4058
}
4159

4260
protected override bool ShouldTrigger(Flow flow, SwitchEventArgs2 args)
4361
{
44-
return flow.GetValue<string>(Id) == args.Id && args.IsEnabled;
62+
foreach(var id in Ids) {
63+
if (flow.GetValue<string>(id) == args.Id && args.IsEnabled) {
64+
return true;
65+
}
66+
}
67+
68+
return false;
4569
}
4670
}
4771
}

0 commit comments

Comments
 (0)