Skip to content

Commit 940a6c7

Browse files
committed
Refactor lamp nodes.
1 parent 201d9f6 commit 940a6c7

13 files changed

+177
-315
lines changed

Editor/Descriptors/GetLampUnitDescriptor.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19+
using System;
1920
using Unity.VisualScripting;
2021
using VisualPinball.Unity.Editor;
2122
using IconSize = VisualPinball.Unity.Editor.IconSize;
@@ -45,10 +46,27 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4546
desc.summary = "The ID of the lamp for which the intensity is returned.";
4647
break;
4748
case nameof(GetLampUnit.Value):
48-
desc.summary = "The intensity of the lamp (0-1).";
49-
break;
50-
case nameof(GetLampUnit.IsEnabled):
51-
desc.summary = "Whether the intensity is larger than 0.";
49+
var getLampUnit = port.unit as GetLampUnit;
50+
switch (getLampUnit!.DataType) {
51+
case LampDataType.OnOff:
52+
desc.label = "Lit";
53+
desc.summary = "On or off.";
54+
break;
55+
case LampDataType.Status:
56+
desc.label = "Status";
57+
desc.summary = "On, off or blinking.";
58+
break;
59+
case LampDataType.Intensity:
60+
desc.label = "Intensity";
61+
desc.summary = "The intensity of the lamp (value depends on the maximal intensity of the mapping).";
62+
break;
63+
case LampDataType.Color:
64+
desc.label = "Color";
65+
desc.summary = "The color of the lamp.";
66+
break;
67+
default:
68+
throw new ArgumentOutOfRangeException();
69+
}
5270
break;
5371
}
5472
}

Editor/Descriptors/SetLampColorUnitDescriptor.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

Editor/Descriptors/SetLampColorUnitDescriptor.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Editor/Descriptors/SetLampUnitDescriptor.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19+
using System;
1920
using Unity.VisualScripting;
2021
using VisualPinball.Unity.Editor;
2122
using IconSize = VisualPinball.Unity.Editor.IconSize;
@@ -41,7 +42,28 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4142
base.DefinedPort(port, desc);
4243

4344
if (port.key == nameof(SetLampUnit.Value)) {
44-
desc.summary = "The intensity of the lamp (0-1).";
45+
var setLampUnit = port.unit as SetLampUnit;
46+
switch (setLampUnit!.DataType) {
47+
case LampDataType.OnOff:
48+
desc.label = "Lit?";
49+
desc.summary = "On or off.";
50+
break;
51+
case LampDataType.Status:
52+
desc.label = "Status";
53+
desc.summary = "On, off or blinking.";
54+
break;
55+
case LampDataType.Intensity:
56+
desc.label = "Intensity";
57+
desc.summary = "The intensity of the lamp (0-1).";
58+
break;
59+
case LampDataType.Color:
60+
desc.label = "Color";
61+
desc.summary = "The color of the lamp.";
62+
break;
63+
64+
default:
65+
throw new ArgumentOutOfRangeException();
66+
}
4567
}
4668
else if (int.TryParse(port.key, out int id)) {
4769
id += 1;

Editor/Widgets/SetLampColorUnitWidget.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.

Editor/Widgets/SetLampColorUnitWidget.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public class VisualScriptingGamelogicEngine : MonoBehaviour, IGamelogicEngine, I
6464
public event EventHandler<DisplayFrameData> OnDisplayFrame;
6565
public event EventHandler<LampEventArgs> OnLampChanged;
6666
public event EventHandler<LampsEventArgs> OnLampsChanged;
67-
public event EventHandler<LampColorEventArgs> OnLampColorChanged;
6867
public event EventHandler<CoilEventArgs> OnCoilChanged;
6968
public event EventHandler<SwitchEventArgs2> OnSwitchChanged;
7069
public event EventHandler<EventArgs> OnStarted;
@@ -176,11 +175,6 @@ public void SetLamp(string id, float value, bool isCoil = false, LampSource sour
176175
OnLampChanged?.Invoke(this, new LampEventArgs(id, value, isCoil, source));
177176
}
178177

179-
public void SetLamp(string id, Color color)
180-
{
181-
OnLampColorChanged?.Invoke(this, new LampColorEventArgs(id, color));
182-
}
183-
184178
public LampState GetLamp(string id)
185179
{
186180
return _player.LampStatuses.ContainsKey(id) ? _player.LampStatuses[id] : LampState.Default;

Runtime/Nodes/Lamps/GetLampUnit.cs

Lines changed: 33 additions & 11 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;
1718
using Unity.VisualScripting;
1819
using UnityEngine;
1920

@@ -24,34 +25,45 @@ namespace VisualPinball.Unity.VisualScripting
2425
[UnitCategory("Visual Pinball")]
2526
public class GetLampUnit : GleUnit
2627
{
28+
[Serialize, Inspectable, UnitHeaderInspectable]
29+
public LampDataType DataType { get; set; }
30+
2731
[DoNotSerialize]
2832
[PortLabel("Lamp ID")]
2933
public ValueInput Id { get; private set; }
3034

3135
[DoNotSerialize]
32-
[PortLabel("Value")]
3336
public ValueOutput Value { get; private set; }
3437

35-
[DoNotSerialize]
36-
[PortLabel("Is Enabled")]
37-
public ValueOutput IsEnabled { get; private set; }
38-
3938
protected override void Definition()
4039
{
4140
Id = ValueInput(nameof(Id), string.Empty);
4241

43-
Value = ValueOutput(nameof(Value), GetValue);
44-
IsEnabled = ValueOutput(nameof(IsEnabled), GetEnabled);
42+
switch (DataType) {
43+
case LampDataType.OnOff:
44+
Value = ValueOutput(nameof(Value), GetEnabled);
45+
break;
46+
case LampDataType.Status:
47+
Value = ValueOutput(nameof(Value), GetEnabled);
48+
break;
49+
case LampDataType.Intensity:
50+
Value = ValueOutput(nameof(Value), GetIntensity);
51+
break;
52+
case LampDataType.Color:
53+
Value = ValueOutput(nameof(Value), GetColor);
54+
break;
55+
default:
56+
throw new ArgumentOutOfRangeException();
57+
}
4558
}
4659

47-
private float GetValue(Flow flow)
60+
private float GetIntensity(Flow flow)
4861
{
4962
if (!AssertGle(flow)) {
5063
Debug.LogError("Cannot find GLE.");
5164
return 0;
5265
}
53-
54-
return Gle.GetLamp(flow.GetValue<string>(Id));
66+
return Gle.GetLamp(flow.GetValue<string>(Id)).Intensity;
5567
}
5668

5769
private bool GetEnabled(Flow flow)
@@ -61,7 +73,17 @@ private bool GetEnabled(Flow flow)
6173
return false;
6274
}
6375

64-
return Gle.GetLamp(flow.GetValue<string>(Id)) > 0;
76+
return Gle.GetLamp(flow.GetValue<string>(Id)).IsOn;
77+
}
78+
79+
private Color GetColor(Flow flow)
80+
{
81+
if (!AssertGle(flow)) {
82+
Debug.LogError("Cannot find GLE.");
83+
return Color.black;
84+
}
85+
86+
return Gle.GetLamp(flow.GetValue<string>(Id)).Color.ToUnityColor();
6587
}
6688
}
6789
}

Runtime/Nodes/Lamps/LampDataType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ namespace VisualPinball.Unity.VisualScripting
1818
{
1919
public enum LampDataType
2020
{
21-
Status, Intensity, Color,
21+
OnOff, Status, Intensity, Color,
2222
}
2323
}

0 commit comments

Comments
 (0)