Skip to content

Commit 9746825

Browse files
jsm174freezy
authored andcommitted
unit: update LampIdValue to convert to/from JSON
1 parent 6d6e854 commit 9746825

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

Editor/Inspectors/LampIdValueInspector.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected override void OnGUI(Rect position, GUIContent label)
3838

3939
var fieldPosition = position.VerticalSection(ref y, GetFieldHeight(position.width, GUIContent.none));
4040

41-
LampIdValue lampIdValue = (LampIdValue)metadata.value;
41+
LampIdValue lampIdValue = LampIdValue.FromJson((string)metadata.value);
4242

4343
var valueWidth = LudiqGUI.GetTextFieldAdaptiveWidth(lampIdValue.value);
4444

@@ -105,17 +105,17 @@ protected override void OnGUI(Rect position, GUIContent label)
105105
if (EndBlock(metadata))
106106
{
107107
metadata.RecordUndo();
108-
metadata.value = new LampIdValue
109-
{
110-
id = newIdValue,
111-
value = newValue
112-
};
108+
109+
lampIdValue.id = newIdValue;
110+
lampIdValue.value = newValue;
111+
112+
metadata.value = lampIdValue.ToJson();
113113
}
114114
}
115115

116116
public override float GetAdaptiveWidth()
117117
{
118-
LampIdValue lampIdValue = (LampIdValue)metadata.value;
118+
LampIdValue lampIdValue = LampIdValue.FromJson((string)metadata.value);
119119

120120
return Mathf.Max(30, EditorStyles.textField.CalcSize(new GUIContent(lampIdValue.id)).x + 1 + Styles.popup.fixedWidth) +
121121
LudiqGUI.GetTextFieldAdaptiveWidth(lampIdValue.value) + 70;

Runtime/Nodes/Lamps/SwitchLampUnit.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,26 @@
1818
using System.Collections.Generic;
1919
using Unity.VisualScripting;
2020
using UnityEngine;
21-
using UnityObject = UnityEngine.Object;
2221

2322
namespace VisualPinball.Unity.VisualScripting
2423
{
2524
[Serializable]
26-
public class LampIdValue : UnityObject
25+
public struct LampIdValue
2726
{
2827
public string id;
2928
public int value;
3029

31-
public static LampIdValue Empty = new LampIdValue { id = string.Empty, value = 0 };
30+
public static LampIdValue FromJson(string json)
31+
{
32+
return JsonUtility.FromJson<LampIdValue>(json);
33+
}
34+
35+
public string ToJson()
36+
{
37+
return JsonUtility.ToJson(this);
38+
}
39+
40+
public static readonly LampIdValue Empty = new LampIdValue { id = string.Empty, value = 0 };
3241
}
3342

3443
[UnitShortTitle("Switch Lamp")]
@@ -63,6 +72,9 @@ public int idCount
6372
[DoNotSerialize]
6473
public List<ValueInput> LampIdValues { get; private set; }
6574

75+
private List<LampIdValue> _lampIdValueList;
76+
private int _hash;
77+
6678
protected override void Definition()
6779
{
6880
InputTrigger = ControlInput(nameof(InputTrigger), Process);
@@ -72,15 +84,13 @@ protected override void Definition()
7284

7385
LampIdValues = new List<ValueInput>();
7486

75-
for (var i = 0; i < idCount; i++)
76-
{
77-
var lampIdValue = ValueInput<LampIdValue>("Lamp ID " + (i + 1), LampIdValue.Empty);
78-
LampIdValues.Add(lampIdValue);
87+
for (var i = 0; i < idCount; i++) {
88+
var valueInput = ValueInput($"Lamp ID {i + 1}", LampIdValue.Empty.ToJson());
89+
LampIdValues.Add(valueInput);
7990

80-
Requirement(lampIdValue, InputTrigger);
91+
Requirement(valueInput, InputTrigger);
8192
}
8293

83-
8494
Succession(InputTrigger, OutputTrigger);
8595
}
8696

@@ -90,13 +100,12 @@ private ControlOutput Process(Flow flow)
90100
Debug.LogError("Cannot find GLE.");
91101
return OutputTrigger;
92102
}
93-
103+
94104
var value = flow.GetValue<int>(Value);
95105

96-
foreach (var lampIdValue in LampIdValues)
97-
{
98-
var lampIdValueStruct = flow.GetValue<LampIdValue>(lampIdValue);
99-
Gle.SetLamp(lampIdValueStruct.id, lampIdValueStruct.value == value ? 255f : 0f);
106+
foreach (var lampIdValue in LampIdValues) {
107+
var obj = LampIdValue.FromJson(flow.GetValue<string>(lampIdValue));
108+
Gle.SetLamp(obj.id, obj.value == value ? 255f : 0f);
100109
}
101110

102111
return OutputTrigger;

0 commit comments

Comments
 (0)