Skip to content

Commit 6d6e854

Browse files
jsm174freezy
authored andcommitted
unit: add switch lamp unit.
1 parent 8170798 commit 6d6e854

File tree

8 files changed

+409
-0
lines changed

8 files changed

+409
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
// ReSharper disable UnusedType.Global
18+
19+
using Unity.VisualScripting;
20+
using VisualPinball.Unity.Editor;
21+
using IconSize = VisualPinball.Unity.Editor.IconSize;
22+
23+
namespace VisualPinball.Unity.VisualScripting.Editor
24+
{
25+
[Descriptor(typeof(SwitchLampUnit))]
26+
public class SwitchLampUnitDescriptor : UnitDescriptor<SwitchLampUnit>
27+
{
28+
public SwitchLampUnitDescriptor(SwitchLampUnit target) : base(target)
29+
{
30+
}
31+
32+
protected override string DefinedSummary()
33+
{
34+
return "This node triggers an event when a switch in the list of given ID is enabled.";
35+
}
36+
37+
protected override EditorTexture DefinedIcon() => EditorTexture.Single(Unity.Editor.Icons.Light(IconSize.Large, IconColor.Orange));
38+
39+
protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
40+
{
41+
base.DefinedPort(port, desc);
42+
43+
switch (port.key) {
44+
case nameof(SwitchLampUnit.LampIdValues):
45+
desc.summary = "The Lamp ID and value";
46+
break;
47+
}
48+
}
49+
}
50+
}

Editor/Descriptors/SwitchLampUnitDescriptor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using UnityEditor;
5+
using UnityEngine;
6+
using Unity.VisualScripting;
7+
8+
namespace VisualPinball.Unity.VisualScripting.Editor
9+
{
10+
11+
public sealed class LampIdValueInspector : Inspector
12+
{
13+
public LampIdValueInspector(Metadata metadata, Func<IEnumerable<string>> getSuggestions) : base(metadata)
14+
{
15+
Ensure.That(nameof(getSuggestions)).IsNotNull(getSuggestions);
16+
17+
this.getSuggestions = getSuggestions;
18+
}
19+
20+
public Func<IEnumerable<string>> getSuggestions { get; }
21+
22+
23+
protected override float GetHeight(float width, GUIContent label)
24+
{
25+
return HeightWithLabel(metadata, width, GetFieldHeight(width, label), label);
26+
}
27+
28+
private float GetFieldHeight(float width, GUIContent label)
29+
{
30+
return EditorGUIUtility.singleLineHeight;
31+
}
32+
33+
private List<string> suggestions = new List<string>();
34+
35+
protected override void OnGUI(Rect position, GUIContent label)
36+
{
37+
position = BeginLabeledBlock(metadata, position, label);
38+
39+
var fieldPosition = position.VerticalSection(ref y, GetFieldHeight(position.width, GUIContent.none));
40+
41+
LampIdValue lampIdValue = (LampIdValue)metadata.value;
42+
43+
var valueWidth = LudiqGUI.GetTextFieldAdaptiveWidth(lampIdValue.value);
44+
45+
var textFieldPosition = new Rect
46+
(
47+
fieldPosition.x,
48+
fieldPosition.y,
49+
fieldPosition.width - Styles.popup.fixedWidth - valueWidth - 60,
50+
fieldPosition.height
51+
);
52+
53+
var popupPosition = new Rect
54+
(
55+
textFieldPosition.xMax,
56+
fieldPosition.y,
57+
Styles.popup.fixedWidth,
58+
fieldPosition.height
59+
);
60+
61+
var newIdValue = EditorGUI.TextField(textFieldPosition, lampIdValue.id, Styles.textField);
62+
63+
// Micro optimizing memory here because it's a pretty substantial alloc
64+
65+
suggestions.Clear();
66+
suggestions.AddRange(getSuggestions());
67+
68+
EditorGUI.BeginDisabledGroup(suggestions.Count == 0);
69+
70+
var suggestionsArray = getSuggestions().ToArray();
71+
var currentSuggestionIndex = Array.IndexOf(suggestionsArray, lampIdValue.id);
72+
73+
EditorGUI.BeginChangeCheck();
74+
75+
var newSuggestionIndex = EditorGUI.Popup(popupPosition, currentSuggestionIndex, suggestionsArray, Styles.popup);
76+
77+
if (EditorGUI.EndChangeCheck())
78+
{
79+
newIdValue = suggestions[newSuggestionIndex];
80+
}
81+
82+
EditorGUI.EndDisabledGroup();
83+
84+
var valueFieldPosition = new Rect
85+
(
86+
fieldPosition.x + fieldPosition.width - valueWidth,
87+
fieldPosition.y,
88+
valueWidth,
89+
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");
102+
103+
var newValue = LudiqGUI.DraggableIntField(valueFieldPosition, lampIdValue.value);
104+
105+
if (EndBlock(metadata))
106+
{
107+
metadata.RecordUndo();
108+
metadata.value = new LampIdValue
109+
{
110+
id = newIdValue,
111+
value = newValue
112+
};
113+
}
114+
}
115+
116+
public override float GetAdaptiveWidth()
117+
{
118+
LampIdValue lampIdValue = (LampIdValue)metadata.value;
119+
120+
return Mathf.Max(30, EditorStyles.textField.CalcSize(new GUIContent(lampIdValue.id)).x + 1 + Styles.popup.fixedWidth) +
121+
LudiqGUI.GetTextFieldAdaptiveWidth(lampIdValue.value) + 70;
122+
}
123+
124+
public static class Styles
125+
{
126+
static Styles()
127+
{
128+
textField = new GUIStyle(EditorStyles.textField);
129+
130+
popup = new GUIStyle("TextFieldDropDown");
131+
popup.fixedWidth = 18;
132+
popup.clipping = TextClipping.Clip;
133+
popup.normal.textColor = ColorPalette.transparent;
134+
popup.active.textColor = ColorPalette.transparent;
135+
popup.hover.textColor = ColorPalette.transparent;
136+
popup.focused.textColor = ColorPalette.transparent;
137+
popup.onNormal.textColor = ColorPalette.transparent;
138+
popup.onActive.textColor = ColorPalette.transparent;
139+
popup.onHover.textColor = ColorPalette.transparent;
140+
popup.onFocused.textColor = ColorPalette.transparent;
141+
}
142+
143+
public static readonly GUIStyle textField;
144+
public static readonly GUIStyle popup;
145+
}
146+
}
147+
}

Editor/Inspectors/LampIdValueInspector.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// ReSharper disable UnusedType.Global
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Linq;
22+
using Unity.VisualScripting;
23+
24+
namespace VisualPinball.Unity.VisualScripting.Editor
25+
{
26+
[Widget(typeof(SwitchLampUnit))]
27+
public sealed class SwitchLampUnitWidget : GleUnitWidget<SwitchLampUnit>
28+
{
29+
private readonly List<Func<Metadata, LampIdValueInspector>> _lampIdInspectorConstructorList;
30+
31+
public SwitchLampUnitWidget(FlowCanvas canvas, SwitchLampUnit unit) : base(canvas, unit)
32+
{
33+
_lampIdInspectorConstructorList = new List<Func<Metadata, LampIdValueInspector>>();
34+
}
35+
36+
public override Inspector GetPortInspector(IUnitPort port, Metadata meta)
37+
{
38+
if (_lampIdInspectorConstructorList.Count() < unit.idCount) {
39+
for (var index = 0; index < unit.idCount - _lampIdInspectorConstructorList.Count(); index++) {
40+
_lampIdInspectorConstructorList.Add(meta => new LampIdValueInspector(meta, GetNameSuggestions));
41+
}
42+
}
43+
44+
for (var index = 0; index < unit.idCount; index++) {
45+
if (unit.LampIdValues[index] == port) {
46+
LampIdValueInspector lampIdInspector = new LampIdValueInspector(meta, GetNameSuggestions);
47+
InspectorProvider.instance.Renew(ref lampIdInspector, meta, _lampIdInspectorConstructorList[index]);
48+
49+
return lampIdInspector;
50+
}
51+
}
52+
53+
return base.GetPortInspector(port, meta);
54+
}
55+
56+
private IEnumerable<string> GetNameSuggestions()
57+
{
58+
return !GleAvailable
59+
? new List<string>()
60+
: Gle.AvailableLamps.Select(lamp => lamp.Id).ToList();
61+
}
62+
}
63+
}

Editor/Widgets/SwitchLampUnitWidget.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)