Skip to content

Commit 5234855

Browse files
jsm174freezy
authored andcommitted
misc: Added IMultiInputUnit support to LampEventUnit
1 parent 31abc9f commit 5234855

File tree

6 files changed

+100
-95
lines changed

6 files changed

+100
-95
lines changed

Editor/Descriptors/GetSwitchUnitDescriptor.cs

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

43-
if (port.key == nameof(GetSwitchUnit.IsEnabled)) {
44-
desc.summary = "Whether the switch is enabled.";
45-
}
46-
else if (int.TryParse(port.key, out int id)) {
47-
id += 1;
48-
49-
desc.label = $"Switch ID {id}";
50-
desc.summary = $"Switch ID {id} to check if enabled.";
43+
switch (port.key) {
44+
case nameof(GetSwitchUnit.Id):
45+
desc.summary = "Switch ID to check if enabled.";
46+
break;
47+
case nameof(GetSwitchUnit.IsEnabled):
48+
desc.summary = "Whether the switch is enabled.";
49+
break;
5150
}
5251
}
5352
}

Editor/Descriptors/LampEventUnitDescriptor.cs

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

41-
switch (port.key) {
42-
case nameof(LampEventUnit.Id):
43-
desc.summary = "The ID of the lamp that changed its intensity.";
44-
break;
45-
case nameof(LampEventUnit.Value):
46-
desc.summary = "The new intensity of the lamp (0-1).";
47-
break;
48-
case nameof(LampEventUnit.IsEnabled):
49-
desc.summary = "Whether the intensity is larger than 0.";
50-
break;
41+
if (port.key == nameof(LampEventUnit.IsEnabled)) {
42+
desc.summary = "Whether the intensity is larger than 0.";
43+
}
44+
else if (port.key == nameof(LampEventUnit.Value)) {
45+
desc.summary = "The new intensity of the lamp (0-1).";
46+
}
47+
else if (int.TryParse(port.key, out int id)) {
48+
id += 1;
49+
50+
desc.label = $"Lamp ID {id}";
51+
desc.summary = $"Lamp ID {id} to look for a change in intensity.";
5152
}
5253
}
5354
}

Editor/Widgets/GetSwitchUnitWidget.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,23 @@ 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-
3129
public GetSwitchUnitWidget(FlowCanvas canvas, GetSwitchUnit unit) : base(canvas, unit)
3230
{
33-
_switchIdInspectorConstructorList = new List<Func<Metadata, VariableNameInspector>>();
31+
_switchIdInspectorConstructor = meta => new VariableNameInspector(meta, GetNameSuggestions);
3432
}
3533

34+
private VariableNameInspector _switchIdInspector;
35+
private readonly Func<Metadata, VariableNameInspector> _switchIdInspectorConstructor;
36+
3637
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3738
{
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-
}
39+
if (port == unit.Id)
40+
{
41+
InspectorProvider.instance.Renew(ref _switchIdInspector, meta, _switchIdInspectorConstructor);
4342

44-
for (var index = 0; index < unit.inputCount; index++) {
45-
if (unit.Items[index] == port) {
46-
VariableNameInspector switchIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
47-
InspectorProvider.instance.Renew(ref switchIdInspector, meta, _switchIdInspectorConstructorList[index]);
48-
49-
return switchIdInspector;
50-
}
43+
return _switchIdInspector;
5144
}
5245

53-
5446
return base.GetPortInspector(port, meta);
5547
}
5648

Editor/Widgets/LampEventUnitWidget.cs

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

34-
private VariableNameInspector _lampIdInspector;
35-
private readonly Func<Metadata, VariableNameInspector> _lampIdInspectorConstructor;
36-
3736
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
3837
{
39-
if (port == unit.Id) {
40-
InspectorProvider.instance.Renew(ref _lampIdInspector, meta, _lampIdInspectorConstructor);
38+
if (_lampIdInspectorConstructorList.Count() < unit.inputCount)
39+
{
40+
for (var index = 0; index < unit.inputCount - _lampIdInspectorConstructorList.Count(); index++)
41+
{
42+
_lampIdInspectorConstructorList.Add(meta => new VariableNameInspector(meta, GetNameSuggestions));
43+
}
44+
}
4145

42-
return _lampIdInspector;
46+
for (var index = 0; index < unit.inputCount; index++)
47+
{
48+
if (unit.multiInputs[index] == port)
49+
{
50+
VariableNameInspector lampIdInspector = new VariableNameInspector(meta, GetNameSuggestions);
51+
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
52+
53+
return lampIdInspector;
54+
}
4355
}
4456

4557
return base.GetPortInspector(port, meta);
@@ -50,7 +62,6 @@ private IEnumerable<string> GetNameSuggestions()
5062
return !GleAvailable
5163
? new List<string>()
5264
: Gle.RequestedLamps.Select(lamp => lamp.Id).ToList();
53-
5465
}
5566
}
5667
}
Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,44 @@
1-
// Visual Pinball Engine
2-
// Copyright (C) 2022 freezy and VPE Team
3-
//
4-
// This program is free software: you can redistribute it and/or modify
5-
// it under the terms of the GNU General Public License as published by
6-
// the Free Software Foundation, either version 3 of the License, or
7-
// (at your option) any later version.
8-
//
9-
// This program is distributed in the hope that it will be useful,
10-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
// GNU General Public License for more details.
13-
//
14-
// You should have received a copy of the GNU General Public License
15-
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16-
1+
// Visual Pinball Engine
2+
// Copyright (C) 2022 freezy and VPE Team
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
using System.Collections.Generic;
18+
using System.Collections.ObjectModel;
1719
using Unity.VisualScripting;
18-
20+
using UnityEngine;
21+
1922
namespace VisualPinball.Unity.VisualScripting
2023
{
2124
[UnitTitle("On Lamp Changed")]
2225
[UnitSurtitle("Gamelogic Engine")]
2326
[UnitCategory("Events\\Visual Pinball")]
24-
public sealed class LampEventUnit : GleEventUnit<LampEventArgs>
27+
public sealed class LampEventUnit : GleEventUnit<LampEventArgs>, IMultiInputUnit
2528
{
29+
[SerializeAs(nameof(inputCount))]
30+
private int _inputCount = 1;
31+
32+
[DoNotSerialize]
33+
[Inspectable, UnitHeaderInspectable("Lamp IDs")]
34+
public int inputCount
35+
{
36+
get => _inputCount;
37+
set => _inputCount = Mathf.Clamp(value, 1, 10);
38+
}
39+
2640
[DoNotSerialize]
27-
[PortLabel("Lamp ID")]
28-
public ValueInput Id { get; private set; }
41+
public ReadOnlyCollection<ValueInput> multiInputs { get; private set; }
2942

3043
[DoNotSerialize]
3144
[PortLabel("Intensity")]
@@ -46,21 +59,33 @@ protected override void Definition()
4659
{
4760
base.Definition();
4861

49-
Id = ValueInput(nameof(Id), string.Empty);
62+
var _multiInputs = new List<ValueInput>();
63+
64+
multiInputs = _multiInputs.AsReadOnly();
65+
66+
for (var i = 0; i < inputCount; i++) {
67+
_multiInputs.Add(ValueInput(i.ToString(), string.Empty));
68+
}
5069

5170
Value = ValueOutput<float>(nameof(Value));
5271
IsEnabled = ValueOutput<bool>(nameof(IsEnabled));
72+
}
73+
74+
protected override bool ShouldTrigger(Flow flow, LampEventArgs args)
75+
{
76+
foreach (var input in multiInputs) {
77+
if (flow.GetValue<string>(input) == args.Id) {
78+
return true;
79+
}
80+
}
81+
82+
return false;
5383
}
5484

5585
protected override void AssignArguments(Flow flow, LampEventArgs args)
5686
{
5787
flow.SetValue(Value, args.Value);
5888
flow.SetValue(IsEnabled, args.Value > 0);
5989
}
60-
61-
protected override bool ShouldTrigger(Flow flow, LampEventArgs args)
62-
{
63-
return args.Id == flow.GetValue<string>(Id);
64-
}
6590
}
6691
}

Runtime/Nodes/Switches/GetSwitchUnit.cs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
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;
1817
using Unity.VisualScripting;
1918
using UnityEngine;
2019

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

4231
[DoNotSerialize]
4332
[PortLabel("Is Enabled")]
4433
public ValueOutput IsEnabled { get; private set; }
4534

4635
protected override void Definition()
4736
{
48-
Items = new List<ValueInput>();
49-
50-
for (var i = 0; i < inputCount; i++) {
51-
var item = ValueInput(i.ToString(), string.Empty);
52-
Items.Add(item);
53-
}
54-
37+
Id = ValueInput(nameof(Id), string.Empty);
5538
IsEnabled = ValueOutput(nameof(IsEnabled), GetEnabled);
5639
}
5740

@@ -62,13 +45,7 @@ private bool GetEnabled(Flow flow)
6245
return false;
6346
}
6447

65-
foreach (var item in Items) {
66-
if (!Gle.GetSwitch(flow.GetValue<string>(item))) {
67-
return false;
68-
}
69-
}
70-
71-
return true;
48+
return Gle.GetSwitch(flow.GetValue<string>(Id));
7249
}
7350
}
7451
}

0 commit comments

Comments
 (0)