Skip to content

Commit 0c9d2e2

Browse files
jsm174freezy
authored andcommitted
misc: added generic AttributedEnumInspector
1 parent 4dcdc01 commit 0c9d2e2

File tree

6 files changed

+167
-101
lines changed

6 files changed

+167
-101
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
public abstract class AttributedEnumInspector<TEnum> : Inspector
29+
{
30+
private List<TEnum> Enums;
31+
private string[] EnumDescriptions;
32+
33+
public AttributedEnumInspector(Metadata metadata) : base(metadata) {
34+
}
35+
36+
public override void Initialize()
37+
{
38+
Enums = Enum.GetValues(typeof(TEnum)).Cast<TEnum>().ToList();
39+
EnumDescriptions = Enum.GetValues(typeof(TEnum)).Cast<TEnum>().Select(x => GetEnumDescription(x)).ToArray();
40+
41+
metadata.instantiate = true;
42+
43+
base.Initialize();
44+
}
45+
46+
protected override float GetHeight(float width, GUIContent label)
47+
{
48+
return HeightWithLabel(metadata, width, EditorGUIUtility.singleLineHeight, label);
49+
}
50+
51+
protected override void OnGUI(Rect position, GUIContent label)
52+
{
53+
position = BeginLabeledBlock(metadata, position, label);
54+
55+
var fieldPosition = new Rect
56+
(
57+
position.x,
58+
position.y,
59+
position.width,
60+
EditorGUIUtility.singleLineHeight
61+
);
62+
63+
var index = Enums.FindIndex(c => c.Equals(metadata.value));
64+
var newIndex = EditorGUI.Popup(fieldPosition, index, EnumDescriptions);
65+
66+
if (EndBlock(metadata))
67+
{
68+
metadata.RecordUndo();
69+
metadata.value = Enums[newIndex];
70+
}
71+
}
72+
73+
public override float GetAdaptiveWidth()
74+
{
75+
return Mathf.Max(18, EditorStyles.popup.CalcSize(new GUIContent(GetEnumDescription((TEnum)metadata.value))).x);
76+
}
77+
78+
private string GetEnumDescription(TEnum value)
79+
{
80+
FieldInfo field = value.GetType().GetField(value.ToString());
81+
82+
DescriptionAttribute attribute
83+
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
84+
as DescriptionAttribute;
85+
86+
return (attribute == null ? value.ToString() : attribute.Description).Replace('/', '\u2215');
87+
}
88+
}
89+
}

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

Editor/Inspectors/CompareTypeInspector.cs

Lines changed: 3 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,95 +14,15 @@
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;
18-
using System.Collections.Generic;
19-
using System.ComponentModel;
20-
using System.Linq;
21-
using System.Reflection;
2217
using Unity.VisualScripting;
23-
using UnityEditor;
24-
using UnityEngine;
2518

2619
namespace VisualPinball.Unity.VisualScripting.Editor
2720
{
2821
[Inspector(typeof(CompareType))]
29-
public class CompareTypeInspector : Inspector
22+
public class CompareTypeInspector : AttributedEnumInspector<CompareType>
3023
{
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()
24+
public CompareTypeInspector(Metadata metadata) : base(metadata)
7225
{
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');
8526
}
8627
}
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-
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Unity.VisualScripting;
18+
19+
namespace VisualPinball.Unity.VisualScripting.Editor
20+
{
21+
[Inspector(typeof(LampDataType))]
22+
public class LampDataTypeInspector : AttributedEnumInspector<LampDataType>
23+
{
24+
public LampDataTypeInspector(Metadata metadata) : base(metadata)
25+
{
26+
}
27+
}
28+
}

Editor/Inspectors/LampDataTypeInspector.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/LampDataType.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
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-
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+
1719
namespace VisualPinball.Unity.VisualScripting
1820
{
1921
public enum LampDataType
20-
{
21-
OnOff, Status, Intensity, Color,
22+
{
23+
[Description("On/Off")]
24+
OnOff,
25+
26+
Status,
27+
Intensity,
28+
Color,
2229
}
2330
}

0 commit comments

Comments
 (0)