Skip to content

Commit 984d573

Browse files
jsm174freezy
authored andcommitted
switchlamp: add support for compare type matching
1 parent 9ba0a6a commit 984d573

File tree

6 files changed

+213
-2
lines changed

6 files changed

+213
-2
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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;
18+
using System.Collections.Generic;
19+
using System.ComponentModel;
20+
using System.Linq;
21+
using System.Reflection;
22+
using Unity.VisualScripting;
23+
using UnityEditor;
24+
using UnityEngine;
25+
26+
namespace VisualPinball.Unity.VisualScripting.Editor
27+
{
28+
[Inspector(typeof(CompareType))]
29+
public class CompareTypeInspector : Inspector
30+
{
31+
private List<CompareType> CompareTypes = Enum.GetValues(typeof(CompareType)).Cast<CompareType>().ToList();
32+
private string[] CompareTypeDescriptions = Enum.GetValues(typeof(CompareType)).Cast<CompareType>().Select(x => GetEnumDescription(x)).ToArray();
33+
34+
public CompareTypeInspector(Metadata metadata) : base(metadata) {
35+
}
36+
37+
public override void Initialize()
38+
{
39+
metadata.instantiate = true;
40+
41+
base.Initialize();
42+
}
43+
44+
protected override float GetHeight(float width, GUIContent label)
45+
{
46+
return HeightWithLabel(metadata, width, EditorGUIUtility.singleLineHeight, label);
47+
}
48+
49+
protected override void OnGUI(Rect position, GUIContent label)
50+
{
51+
position = BeginLabeledBlock(metadata, position, label);
52+
53+
var fieldPosition = new Rect
54+
(
55+
position.x,
56+
position.y,
57+
position.width,
58+
EditorGUIUtility.singleLineHeight
59+
);
60+
61+
var index = CompareTypes.FindIndex(c => c == (CompareType)metadata.value);
62+
var newIndex = EditorGUI.Popup(fieldPosition, index, CompareTypeDescriptions);
63+
64+
if (EndBlock(metadata))
65+
{
66+
metadata.RecordUndo();
67+
metadata.value = CompareTypes[newIndex];
68+
}
69+
}
70+
71+
public override float GetAdaptiveWidth()
72+
{
73+
return Mathf.Max(18, EditorStyles.popup.CalcSize(new GUIContent(GetEnumDescription((CompareType)metadata.value))).x + Styles.popup.fixedWidth);
74+
}
75+
76+
private static string GetEnumDescription(Enum value)
77+
{
78+
FieldInfo field = value.GetType().GetField(value.ToString());
79+
80+
DescriptionAttribute attribute
81+
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
82+
as DescriptionAttribute;
83+
84+
return (attribute == null ? value.ToString() : attribute.Description).Replace('/', '\u2215');
85+
}
86+
}
87+
88+
public static class Styles
89+
{
90+
static Styles()
91+
{
92+
popup = new GUIStyle("TextFieldDropDown");
93+
popup.fixedWidth = 10;
94+
popup.clipping = TextClipping.Clip;
95+
popup.normal.textColor = ColorPalette.transparent;
96+
popup.active.textColor = ColorPalette.transparent;
97+
popup.hover.textColor = ColorPalette.transparent;
98+
popup.focused.textColor = ColorPalette.transparent;
99+
popup.onNormal.textColor = ColorPalette.transparent;
100+
popup.onActive.textColor = ColorPalette.transparent;
101+
popup.onHover.textColor = ColorPalette.transparent;
102+
popup.onFocused.textColor = ColorPalette.transparent;
103+
}
104+
105+
public static readonly GUIStyle textField;
106+
public static readonly GUIStyle popup;
107+
}
108+
}

Editor/Inspectors/CompareTypeInspector.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.

Runtime/Nodes/Lamps/SwitchLampUnit.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class SwitchLampUnit : GleUnit, IMultiInputUnit
3737
[Serialize, Inspectable, UnitHeaderInspectable("Non Match")]
3838
public LampDataType NonMatchDataType { get; set; }
3939

40+
[Serialize, Inspectable, UnitHeaderInspectable("Value Compare")]
41+
public CompareType ValueCompareType { get; set; }
42+
4043
[DoNotSerialize]
4144
[Inspectable, UnitHeaderInspectable("Lamp IDs")]
4245
public int inputCount
@@ -134,8 +137,37 @@ private ControlOutput Process(Flow flow)
134137

135138
var lampIdValue = _lampIdValueCache[json.GetHashCode()];
136139

137-
var dataType = lampIdValue.value == sourceValue ? MatchDataType : NonMatchDataType;
138-
var value = lampIdValue.value == sourceValue ? Match : NonMatch;
140+
var match = false;
141+
142+
switch(ValueCompareType)
143+
{
144+
case CompareType.NotEqual:
145+
match = lampIdValue.value != sourceValue;
146+
break;
147+
148+
case CompareType.GreaterThan:
149+
match = lampIdValue.value > sourceValue;
150+
break;
151+
152+
case CompareType.GreaterThanEqual:
153+
match = lampIdValue.value >= sourceValue;
154+
break;
155+
156+
case CompareType.LessThan:
157+
match = lampIdValue.value < sourceValue;
158+
break;
159+
160+
case CompareType.LessThanEqual:
161+
match = lampIdValue.value < sourceValue;
162+
break;
163+
164+
default:
165+
match = lampIdValue.value == sourceValue;
166+
break;
167+
}
168+
169+
var dataType = match ? MatchDataType : NonMatchDataType;
170+
var value = match ? Match : NonMatch;
139171

140172
switch (dataType) {
141173
case LampDataType.OnOff:

Runtime/Nodes/Logic.meta

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

Runtime/Nodes/Logic/CompareType.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.ComponentModel;
18+
19+
namespace VisualPinball.Unity.VisualScripting
20+
{
21+
public enum CompareType
22+
{
23+
[Description("=")]
24+
Equal,
25+
26+
[Description("\u2260")]
27+
NotEqual,
28+
29+
[Description("<")]
30+
LessThan,
31+
32+
[Description("\u2264")]
33+
LessThanEqual,
34+
35+
[Description(">")]
36+
GreaterThan,
37+
38+
[Description("\u2265")]
39+
GreaterThanEqual
40+
}
41+
}

Runtime/Nodes/Logic/CompareType.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)