Skip to content

Commit e06660e

Browse files
jsm174freezy
authored andcommitted
misc: updated SetLampEnabledUnit to support IMultiInput
1 parent 33f48d4 commit e06660e

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

Editor/Descriptors/SetLampEnabledUnitDescriptor.cs

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

43-
switch (port.key) {
44-
case nameof(SetLampEnabledUnit.Id):
45-
desc.summary = "The ID of the lamp to toggle.";
46-
break;
47-
case nameof(SetLampEnabledUnit.IsEnabled):
48-
desc.summary = "Whether to turn on or off.";
49-
break;
43+
if (port.key == nameof(SetLampEnabledUnit.IsEnabled)) {
44+
desc.summary = "Whether to turn on or off.";
45+
}
46+
else if (int.TryParse(port.key, out int id)) {
47+
id += 1;
48+
49+
desc.label = $"Lamp ID {id}";
50+
desc.summary = $"Lamp ID {id} of the lamp to toggle.";
5051
}
5152
}
5253
}

Editor/Widgets/SetLampEnabledUnitWidget.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(SetLampEnabledUnit))]
2727
public sealed class SetLampEnabledUnitWidget : GleUnitWidget<SetLampEnabledUnit>
2828
{
29-
private VariableNameInspector _lampIdInspector;
30-
private readonly Func<Metadata, VariableNameInspector> _setLampInspectorConstructor;
29+
private readonly List<Func<Metadata, VariableNameInspector>> _lampIdInspectorConstructorList;
3130

3231
public SetLampEnabledUnitWidget(FlowCanvas canvas, SetLampEnabledUnit unit) : base(canvas, unit)
3332
{
34-
_setLampInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
33+
_lampIdInspectorConstructorList = 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 _lampIdInspector, meta, _setLampInspectorConstructor);
41-
return _lampIdInspector;
38+
if (_lampIdInspectorConstructorList.Count() < unit.inputCount) {
39+
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++) {
40+
_lampIdInspectorConstructorList.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 lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
48+
49+
return lampIdInspector;
50+
}
4251
}
4352

4453
return base.GetPortInspector(port, meta);

Runtime/Nodes/Lamps/SetLampEnabledUnit.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
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;
1820
using UnityEngine;
1921

@@ -23,7 +25,7 @@ namespace VisualPinball.Unity.VisualScripting
2325
[UnitTitle("Set Lamp (ID, on/off)")]
2426
[UnitSurtitle("Gamelogic Engine")]
2527
[UnitCategory("Visual Pinball")]
26-
public class SetLampEnabledUnit : GleUnit
28+
public class SetLampEnabledUnit : GleUnit, IMultiInputUnit
2729
{
2830
[DoNotSerialize]
2931
[PortLabelHidden]
@@ -33,9 +35,19 @@ public class SetLampEnabledUnit : GleUnit
3335
[PortLabelHidden]
3436
public ControlOutput OutputTrigger;
3537

38+
[SerializeAs(nameof(inputCount))]
39+
private int _inputCount = 1;
40+
41+
[DoNotSerialize]
42+
[Inspectable, UnitHeaderInspectable("Lamp IDs")]
43+
public int inputCount
44+
{
45+
get => _inputCount;
46+
set => _inputCount = Mathf.Clamp(value, 1, 10);
47+
}
48+
3649
[DoNotSerialize]
37-
[PortLabel("Lamp ID")]
38-
public ValueInput Id { get; private set; }
50+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
3951

4052
[DoNotSerialize]
4153
[PortLabel("Is Enabled")]
@@ -46,10 +58,19 @@ protected override void Definition()
4658
InputTrigger = ControlInput(nameof(InputTrigger), Process);
4759
OutputTrigger = ControlOutput(nameof(OutputTrigger));
4860

49-
Id = ValueInput(nameof(Id), string.Empty);
61+
var _multiInputs = new List<ValueInput>();
62+
63+
multiInputs = _multiInputs.AsReadOnly();
64+
65+
for (var i = 0; i < inputCount; i++) {
66+
var input = ValueInput(i.ToString(), string.Empty);
67+
_multiInputs.Add(input);
68+
69+
Requirement(input, InputTrigger);
70+
}
71+
5072
IsEnabled = ValueInput(nameof(IsEnabled), false);
5173

52-
Requirement(Id, InputTrigger);
5374
Succession(InputTrigger, OutputTrigger);
5475
}
5576

@@ -60,10 +81,12 @@ private ControlOutput Process(Flow flow)
6081
return OutputTrigger;
6182
}
6283

63-
var id = flow.GetValue<string>(Id);
6484
var isEnabled = flow.GetValue<bool>(IsEnabled);
6585

66-
Gle.SetLamp(id, isEnabled ? 255f : 0f);
86+
foreach (var input in multiInputs) {
87+
var lampId = flow.GetValue<string>(input);
88+
Gle.SetLamp(lampId, isEnabled ? 255f : 0f);
89+
}
6790

6891
return OutputTrigger;
6992
}

Runtime/Nodes/Lamps/SetLampUnit.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ protected override void Definition()
6262

6363
multiInputs = _multiInputs.AsReadOnly();
6464

65-
for (var i = 0; i < inputCount; i++)
66-
{
65+
for (var i = 0; i < inputCount; i++) {
6766
var input = ValueInput(i.ToString(), string.Empty);
6867
_multiInputs.Add(input);
6968

Runtime/Nodes/Switches/AllSwitchesEnabledEventUnit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ protected override bool ShouldTrigger(Flow flow, SwitchEventArgs2 args)
6666
}
6767

6868
var validSwitch = false;
69-
foreach(var item in multiInputs) {
70-
var swId = flow.GetValue<string>(item);
69+
foreach(var input in multiInputs) {
70+
var swId = flow.GetValue<string>(input);
7171
if (swId == args.Id) {
7272
validSwitch = true;
7373
}

0 commit comments

Comments
 (0)