Skip to content

Commit 0ef3389

Browse files
jsm174freezy
authored andcommitted
unit: cleanup switch lamp unit before renaming
1 parent b38e584 commit 0ef3389

File tree

4 files changed

+55
-39
lines changed

4 files changed

+55
-39
lines changed

Editor/Descriptors/SwitchLampUnitDescriptor.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
// ReSharper disable UnusedType.Global
1818

19+
using System;
20+
using System.Text.RegularExpressions;
1921
using Unity.VisualScripting;
2022
using VisualPinball.Unity.Editor;
2123
using IconSize = VisualPinball.Unity.Editor.IconSize;
@@ -31,7 +33,7 @@ public SwitchLampUnitDescriptor(SwitchLampUnit target) : base(target)
3133

3234
protected override string DefinedSummary()
3335
{
34-
return "This node triggers an event when a switch in the list of given ID is enabled.";
36+
return "This node enabled or disables lamps based on matching a source value with a specifed value.";
3537
}
3638

3739
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Light(IconSize.Large, IconColor.Orange));
@@ -40,10 +42,18 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4042
{
4143
base.DefinedPort(port, desc);
4244

43-
switch (port.key) {
44-
case nameof(SwitchLampUnit.LampIdValues):
45-
desc.summary = "The Lamp ID and value";
46-
break;
45+
if (port.key == nameof(SwitchLampUnit.SourceValue)) {
46+
desc.summary = "Source value to use for matching";
47+
}
48+
else {
49+
var match = new Regex("^(item)([0-9]+)$").Match(port.key);
50+
51+
if (match.Success) {
52+
var id = int.Parse(match.Groups[2].Value);
53+
54+
desc.label = $"Lamp ID {id}";
55+
desc.summary = "Lamp ID to enable if specified Value matches source Value, or disable if specified Value does not match source Value";
56+
}
4757
}
4858
}
4959
}

Editor/Inspectors/LampIdValueInspector.cs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace VisualPinball.Unity.VisualScripting.Editor
1010

1111
public sealed class LampIdValueInspector : Inspector
1212
{
13+
private static readonly int LabelPadding = 5;
14+
1315
public LampIdValueInspector(Metadata metadata, Func<IEnumerable<string>> getSuggestions) : base(metadata)
1416
{
1517
Ensure.That(nameof(getSuggestions)).IsNotNull(getSuggestions);
@@ -34,19 +36,20 @@ private float GetFieldHeight(float width, GUIContent label)
3436

3537
protected override void OnGUI(Rect position, GUIContent label)
3638
{
39+
LampIdValue lampIdValue = LampIdValue.FromJson((string)metadata.value);
40+
41+
var valueLabelWidth = EditorStyles.label.CalcSize(new GUIContent("Value")).x;
42+
var valueWidth = EditorStyles.textField.CalcSize(new GUIContent($"{lampIdValue.value}")).x;
43+
3744
position = BeginLabeledBlock(metadata, position, label);
3845

3946
var fieldPosition = position.VerticalSection(ref y, GetFieldHeight(position.width, GUIContent.none));
40-
41-
LampIdValue lampIdValue = LampIdValue.FromJson((string)metadata.value);
42-
43-
var valueWidth = LudiqGUI.GetTextFieldAdaptiveWidth(lampIdValue.value);
4447

4548
var textFieldPosition = new Rect
4649
(
4750
fieldPosition.x,
4851
fieldPosition.y,
49-
fieldPosition.width - Styles.popup.fixedWidth - valueWidth - 60,
52+
fieldPosition.width - valueWidth - LabelPadding - valueLabelWidth - LabelPadding - Styles.popup.fixedWidth,
5053
fieldPosition.height
5154
);
5255

@@ -81,26 +84,25 @@ protected override void OnGUI(Rect position, GUIContent label)
8184

8285
EditorGUI.EndDisabledGroup();
8386

84-
var valueFieldPosition = new Rect
87+
var valueLabelPosition = new Rect
88+
(
89+
fieldPosition.x + fieldPosition.width - valueWidth - LabelPadding - valueLabelWidth,
90+
fieldPosition.y,
91+
valueLabelWidth,
92+
fieldPosition.height
93+
);
94+
95+
EditorGUI.LabelField(valueLabelPosition, "Value");
96+
97+
var valueIntFieldPosition = new Rect
8598
(
8699
fieldPosition.x + fieldPosition.width - valueWidth,
87100
fieldPosition.y,
88101
valueWidth,
89102
fieldPosition.height
90-
);
91-
92-
var valueLabelFieldPosition = new Rect
93-
(
94-
fieldPosition.x + fieldPosition.width - valueWidth - 50,
95-
fieldPosition.y,
96-
50,
97-
fieldPosition.height
98-
);
99-
100-
101-
EditorGUI.LabelField(valueLabelFieldPosition, "Value");
103+
);
102104

103-
var newValue = LudiqGUI.DraggableIntField(valueFieldPosition, lampIdValue.value);
105+
var newValue = LudiqGUI.DraggableIntField(valueIntFieldPosition, lampIdValue.value);
104106

105107
if (EndBlock(metadata))
106108
{
@@ -117,8 +119,12 @@ public override float GetAdaptiveWidth()
117119
{
118120
LampIdValue lampIdValue = LampIdValue.FromJson((string)metadata.value);
119121

120-
return Mathf.Max(30, EditorStyles.textField.CalcSize(new GUIContent(lampIdValue.id)).x + 1 + Styles.popup.fixedWidth) +
121-
LudiqGUI.GetTextFieldAdaptiveWidth(lampIdValue.value) + 70;
122+
return Mathf.Max(30,
123+
EditorStyles.textField.CalcSize(new GUIContent(lampIdValue.id)).x + 1 + Styles.popup.fixedWidth) +
124+
LabelPadding +
125+
EditorStyles.label.CalcSize(new GUIContent("Value")).x +
126+
LabelPadding +
127+
EditorStyles.textField.CalcSize(new GUIContent($"{lampIdValue.value}")).x;
122128
}
123129

124130
public static class Styles

Editor/Widgets/SwitchLampUnitWidget.cs

Lines changed: 2 additions & 2 deletions
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.idCount; index++) {
45-
if (unit.LampIdValues[index] == port) {
45+
if (unit.Items[index] == port) {
4646
LampIdValueInspector lampIdInspector = new LampIdValueInspector(meta, GetNameSuggestions);
4747
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
4848

@@ -57,7 +57,7 @@ private IEnumerable<string> GetNameSuggestions()
5757
{
5858
return !GleAvailable
5959
? new List<string>()
60-
: Gle.RequestedLamps.Select(lamp => lamp.Id).ToList();
60+
: Gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
6161
}
6262
}
6363
}

Runtime/Nodes/Lamps/SwitchLampUnit.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ public int idCount
6666
public ControlOutput OutputTrigger;
6767

6868
[DoNotSerialize]
69-
[PortLabel("Value")]
70-
public ValueInput Value { get; private set; }
69+
[PortLabel("Source Value")]
70+
public ValueInput SourceValue { get; private set; }
7171

7272
[DoNotSerialize]
73-
public List<ValueInput> LampIdValues { get; private set; }
73+
public List<ValueInput> Items { get; private set; }
7474

7575
private Dictionary<int, LampIdValue> _lampIdValueCache = new Dictionary<int, LampIdValue>();
7676

@@ -79,15 +79,15 @@ protected override void Definition()
7979
InputTrigger = ControlInput(nameof(InputTrigger), Process);
8080
OutputTrigger = ControlOutput(nameof(OutputTrigger));
8181

82-
Value = ValueInput<int>(nameof(Value));
82+
SourceValue = ValueInput<int>(nameof(SourceValue));
8383

84-
LampIdValues = new List<ValueInput>();
84+
Items = new List<ValueInput>();
8585

8686
for (var i = 0; i < idCount; i++) {
87-
var valueInput = ValueInput($"Lamp ID {i + 1}", LampIdValue.Empty.ToJson());
88-
LampIdValues.Add(valueInput);
87+
var item = ValueInput($"item{i + 1}", LampIdValue.Empty.ToJson());
88+
Items.Add(item);
8989

90-
Requirement(valueInput, InputTrigger);
90+
Requirement(item, InputTrigger);
9191
}
9292

9393
_lampIdValueCache.Clear();
@@ -102,10 +102,10 @@ private ControlOutput Process(Flow flow)
102102
return OutputTrigger;
103103
}
104104

105-
var value = flow.GetValue<int>(Value);
105+
var value = flow.GetValue<int>(SourceValue);
106106

107-
foreach (var lampIdValue in LampIdValues) {
108-
var json = flow.GetValue<string>(lampIdValue);
107+
foreach (var item in Items) {
108+
var json = flow.GetValue<string>(item);
109109

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

0 commit comments

Comments
 (0)