Skip to content

Commit 33f48d4

Browse files
jsm174freezy
authored andcommitted
misc: update to SetLampUnit/SwitchLampUnit to use IMultiInputUnit
1 parent 2197b0e commit 33f48d4

File tree

5 files changed

+68
-33
lines changed

5 files changed

+68
-33
lines changed

Editor/Descriptors/SetLampUnitDescriptor.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(SetLampUnit.Id):
45-
desc.summary = "The ID of the lamp for which the intensity is returned.";
46-
break;
47-
case nameof(SetLampUnit.Value):
48-
desc.summary = "The intensity of the lamp (0-1).";
49-
break;
43+
if (port.key == nameof(SetLampUnit.Value)) {
44+
desc.summary = "The intensity of the lamp (0-1).";
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 set the intensity";
5051
}
5152
}
5253
}

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

3231
public SetLampUnitWidget(FlowCanvas canvas, SetLampUnit 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);

Editor/Widgets/SwitchLampUnitWidget.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
LampIdValueInspector lampIdInspector = new LampIdValueInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
4848

Runtime/Nodes/Lamps/SetLampUnit.cs

Lines changed: 33 additions & 11 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, Intensity)")]
2426
[UnitSurtitle("Gamelogic Engine")]
2527
[UnitCategory("Visual Pinball")]
26-
public class SetLampUnit : GleUnit
28+
public class SetLampUnit : GleUnit, IMultiInputUnit
2729
{
2830
[DoNotSerialize]
2931
[PortLabelHidden]
@@ -33,9 +35,19 @@ public class SetLampUnit : GleUnit
3335
[PortLabelHidden]
3436
public ControlOutput OutputTrigger;
3537

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

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

49-
Id = ValueInput<string>(nameof(Id), string.Empty);
50-
Value = ValueInput<float>(nameof(Value), 0f);
61+
var _multiInputs = new List<ValueInput>();
5162

52-
Requirement(Id, InputTrigger);
53-
Succession(InputTrigger, OutputTrigger);
63+
multiInputs = _multiInputs.AsReadOnly();
64+
65+
for (var i = 0; i < inputCount; i++)
66+
{
67+
var input = ValueInput(i.ToString(), string.Empty);
68+
_multiInputs.Add(input);
5469

70+
Requirement(input, InputTrigger);
71+
}
72+
73+
Value = ValueInput(nameof(Value), 0f);
74+
75+
Succession(InputTrigger, OutputTrigger);
5576
}
5677

5778
private ControlOutput Process(Flow flow)
5879
{
59-
6080
if (!AssertGle(flow)) {
6181
Debug.LogError("Cannot find GLE.");
6282
return OutputTrigger;
6383
}
6484

65-
var id = flow.GetValue<string>(Id);
66-
var value = flow.GetValue<float>(Value);
85+
var value = flow.GetValue<float>(Value) * 255f;
6786

68-
Gle.SetLamp(id, value * 255f);
87+
foreach (var input in multiInputs) {
88+
var lampId = flow.GetValue<string>(input);
89+
Gle.SetLamp(lampId, value);
90+
}
6991

7092
return OutputTrigger;
7193
}

Runtime/Nodes/Lamps/SwitchLampUnit.cs

Lines changed: 11 additions & 8 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

@@ -24,7 +25,7 @@ namespace VisualPinball.Unity.VisualScripting
2425
[UnitTitle("Switch Lamp (ID, match value)")]
2526
[UnitSurtitle("Gamelogic Engine")]
2627
[UnitCategory("Visual Pinball")]
27-
public class SwitchLampUnit : GleUnit
28+
public class SwitchLampUnit : GleUnit, IMultiInputUnit
2829
{
2930
[SerializeAs(nameof(inputCount))]
3031
private int _inputCount = 1;
@@ -50,7 +51,7 @@ public int inputCount
5051
public ValueInput SourceValue { get; private set; }
5152

5253
[DoNotSerialize]
53-
public List<ValueInput> Items { get; private set; }
54+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
5455

5556
private Dictionary<int, LampIdValue> _lampIdValueCache = new Dictionary<int, LampIdValue>();
5657

@@ -61,13 +62,15 @@ protected override void Definition()
6162

6263
SourceValue = ValueInput<int>(nameof(SourceValue));
6364

64-
Items = new List<ValueInput>();
65+
var _multiInputs = new List<ValueInput>();
66+
67+
multiInputs = _multiInputs.AsReadOnly();
6568

6669
for (var i = 0; i < inputCount; i++) {
67-
var item = ValueInput(i.ToString(), LampIdValue.Empty.ToJson());
68-
Items.Add(item);
70+
var input = ValueInput(i.ToString(), LampIdValue.Empty.ToJson());
71+
_multiInputs.Add(input);
6972

70-
Requirement(item, InputTrigger);
73+
Requirement(input, InputTrigger);
7174
}
7275

7376
_lampIdValueCache.Clear();
@@ -84,8 +87,8 @@ private ControlOutput Process(Flow flow)
8487

8588
var value = flow.GetValue<int>(SourceValue);
8689

87-
foreach (var item in Items) {
88-
var json = flow.GetValue<string>(item);
90+
foreach (var input in multiInputs) {
91+
var json = flow.GetValue<string>(input);
8992

9093
if (!_lampIdValueCache.ContainsKey(json.GetHashCode())) {
9194
_lampIdValueCache[json.GetHashCode()] = LampIdValue.FromJson(json);

0 commit comments

Comments
 (0)