7
7
using UnityEngine . UI ;
8
8
using UnityExplorer . CacheObject ;
9
9
using UnityExplorer . UI ;
10
+ using UnityExplorer . UI . Widgets . AutoComplete ;
11
+ using UniverseLib ;
10
12
using UniverseLib . UI ;
11
13
12
14
namespace UnityExplorer . CacheObject . IValues
@@ -20,10 +22,10 @@ public class InteractiveEnum : InteractiveValue
20
22
21
23
public OrderedDictionary CurrentValues ;
22
24
23
- public CachedEnumValue ValueAtIdx ( int idx ) => ( CachedEnumValue ) CurrentValues [ idx ] ;
24
- public CachedEnumValue ValueAtKey ( object key ) => ( CachedEnumValue ) CurrentValues [ key ] ;
25
+ private InputFieldRef inputField ;
26
+ private ButtonRef enumHelperButton ;
27
+ private EnumCompleter enumCompleter ;
25
28
26
- private Dropdown enumDropdown ;
27
29
private GameObject toggleHolder ;
28
30
private readonly List < Toggle > flagToggles = new List < Toggle > ( ) ;
29
31
private readonly List < Text > flagTexts = new List < Text > ( ) ;
@@ -35,38 +37,68 @@ public override void SetValue(object value)
35
37
36
38
if ( lastType != EnumType )
37
39
{
38
- CurrentValues = GetEnumValues ( EnumType , out IsFlags ) ;
40
+ CurrentValues = GetEnumValues ( EnumType ) ;
39
41
42
+ IsFlags = EnumType . GetCustomAttributes ( typeof ( FlagsAttribute ) , true ) is object [ ] fa && fa . Any ( ) ;
40
43
if ( IsFlags )
41
44
SetupTogglesForEnumType ( ) ;
42
45
else
43
- SetupDropdownForEnumType ( ) ;
46
+ {
47
+ inputField . Component . gameObject . SetActive ( true ) ;
48
+ enumHelperButton . Component . gameObject . SetActive ( true ) ;
49
+ toggleHolder . SetActive ( false ) ;
50
+ }
51
+
52
+ enumCompleter . EnumType = EnumType ;
53
+ enumCompleter . CacheEnumValues ( ) ;
44
54
45
55
lastType = EnumType ;
46
56
}
47
57
48
- // setup ui for changes
49
- if ( IsFlags )
50
- SetTogglesForValue ( value ) ;
58
+ if ( ! IsFlags )
59
+ inputField . Text = value . ToString ( ) ;
51
60
else
52
- SetDropdownForValue ( value ) ;
53
- }
61
+ SetTogglesForValue ( value ) ;
54
62
55
- // Setting value to owner
63
+ this . enumCompleter . chosenSuggestion = value . ToString ( ) ;
64
+ AutoCompleteModal . Instance . ReleaseOwnership ( this . enumCompleter ) ;
65
+ }
56
66
57
- private void OnApplyClicked ( )
67
+ private void SetTogglesForValue ( object value )
58
68
{
59
- if ( IsFlags )
60
- SetValueFromFlags ( ) ;
61
- else
62
- SetValueFromDropdown ( ) ;
69
+ try
70
+ {
71
+ var split = value . ToString ( ) . Split ( ',' ) ;
72
+ var set = new HashSet < string > ( ) ;
73
+ foreach ( var s in split )
74
+ set . Add ( s . Trim ( ) ) ;
75
+
76
+ for ( int i = 0 ; i < CurrentValues . Count ; i ++ )
77
+ flagToggles [ i ] . isOn = set . Contains ( ValueAtIdx ( i ) . Name ) ;
78
+ }
79
+ catch ( Exception ex )
80
+ {
81
+ ExplorerCore . LogWarning ( "Exception setting flag toggles: " + ex ) ;
82
+ }
63
83
}
64
84
65
- private void SetValueFromDropdown ( )
85
+ // Setting value to owner
86
+
87
+ private void OnApplyClicked ( )
66
88
{
67
89
try
68
90
{
69
- CurrentOwner . SetUserValue ( ValueAtIdx ( enumDropdown . value ) . ActualValue ) ;
91
+ if ( ! IsFlags )
92
+ {
93
+ if ( ParseUtility . TryParse ( this . inputField . Text , EnumType , out object value , out Exception ex ) )
94
+ CurrentOwner . SetUserValue ( value ) ;
95
+ else
96
+ throw ex ;
97
+ }
98
+ else
99
+ {
100
+ SetValueFromFlags ( ) ;
101
+ }
70
102
}
71
103
catch ( Exception ex )
72
104
{
@@ -93,59 +125,53 @@ private void SetValueFromFlags()
93
125
}
94
126
}
95
127
96
- // setting UI state for value
128
+ // UI Construction
97
129
98
- private void SetDropdownForValue ( object value )
130
+ private void EnumHelper_OnClick ( )
99
131
{
100
- if ( CurrentValues . Contains ( value ) )
101
- {
102
- var cached = ValueAtKey ( value ) ;
103
- enumDropdown . value = cached . EnumIndex ;
104
- enumDropdown . RefreshShownValue ( ) ;
105
- }
106
- else
107
- ExplorerCore . LogWarning ( "CurrentValues does not contain key '" + value ? . ToString ( ) ?? "<null>" + "'" ) ;
132
+ enumCompleter . HelperButtonClicked ( ) ;
108
133
}
109
134
110
- private void SetTogglesForValue ( object value )
135
+ public override GameObject CreateContent ( GameObject parent )
111
136
{
112
- try
113
- {
114
- var split = value . ToString ( ) . Split ( ',' ) ;
115
- var set = new HashSet < string > ( ) ;
116
- foreach ( var s in split )
117
- set . Add ( s . Trim ( ) ) ;
137
+ UIRoot = UIFactory . CreateVerticalGroup ( parent , "InteractiveEnum" , false , false , true , true , 3 , new Vector4 ( 4 , 4 , 4 , 4 ) ,
138
+ new Color ( 0.06f , 0.06f , 0.06f ) ) ;
139
+ UIFactory . SetLayoutElement ( UIRoot , minHeight : 25 , flexibleHeight : 9999 , flexibleWidth : 9999 ) ;
118
140
119
- for ( int i = 0 ; i < CurrentValues . Count ; i ++ )
120
- flagToggles [ i ] . isOn = set . Contains ( ValueAtIdx ( i ) . Name ) ;
121
- }
122
- catch ( Exception ex )
123
- {
124
- ExplorerCore . LogWarning ( "Exception setting flag toggles: " + ex ) ;
125
- }
126
- }
141
+ var hori = UIFactory . CreateUIObject ( "Hori" , UIRoot ) ;
142
+ UIFactory . SetLayoutElement ( hori , minHeight : 25 , flexibleWidth : 9999 ) ;
143
+ UIFactory . SetLayoutGroup < HorizontalLayoutGroup > ( hori , false , false , true , true , 2 ) ;
144
+
145
+ var applyButton = UIFactory . CreateButton ( hori , "ApplyButton" , "Apply" , new Color ( 0.2f , 0.27f , 0.2f ) ) ;
146
+ UIFactory . SetLayoutElement ( applyButton . Component . gameObject , minHeight : 25 , minWidth : 100 ) ;
147
+ applyButton . OnClick += OnApplyClicked ;
127
148
128
- // Setting up the UI for the enum type when it changes or is first set
149
+ inputField = UIFactory . CreateInputField ( hori , "InputField" , "Enter name or underlying value..." ) ;
150
+ UIFactory . SetLayoutElement ( inputField . UIRoot , minHeight : 25 , flexibleHeight : 50 , minWidth : 100 , flexibleWidth : 1000 ) ;
151
+ inputField . Component . lineType = InputField . LineType . MultiLineNewline ;
152
+ inputField . UIRoot . AddComponent < ContentSizeFitter > ( ) . verticalFit = ContentSizeFitter . FitMode . PreferredSize ;
129
153
130
- private void SetupDropdownForEnumType ( )
131
- {
132
- toggleHolder . SetActive ( false ) ;
133
- enumDropdown . gameObject . SetActive ( true ) ;
154
+ enumHelperButton = UIFactory . CreateButton ( hori , "EnumHelper" , "▼" ) ;
155
+ UIFactory . SetLayoutElement ( enumHelperButton . Component . gameObject , minWidth : 25 , minHeight : 25 , flexibleWidth : 0 , flexibleHeight : 0 ) ;
156
+ enumHelperButton . OnClick += EnumHelper_OnClick ;
134
157
135
- // create dropdown entries
136
- enumDropdown . options . Clear ( ) ;
158
+ enumCompleter = new EnumCompleter ( this . EnumType , this . inputField ) ;
137
159
138
- foreach ( CachedEnumValue entry in CurrentValues . Values )
139
- enumDropdown . options . Add ( new Dropdown . OptionData ( entry . Name ) ) ;
160
+ toggleHolder = UIFactory . CreateUIObject ( "ToggleHolder" , UIRoot ) ;
161
+ UIFactory . SetLayoutGroup < VerticalLayoutGroup > ( toggleHolder , false , false , true , true , 4 ) ;
162
+ UIFactory . SetLayoutElement ( toggleHolder , minHeight : 25 , flexibleWidth : 9999 , flexibleHeight : 9999 ) ;
140
163
141
- enumDropdown . value = 0 ;
142
- enumDropdown . RefreshShownValue ( ) ;
164
+ return UIRoot ;
143
165
}
144
166
167
+ public CachedEnumValue ValueAtIdx ( int idx ) => ( CachedEnumValue ) CurrentValues [ idx ] ;
168
+ public CachedEnumValue ValueAtKey ( object key ) => ( CachedEnumValue ) CurrentValues [ key ] ;
169
+
145
170
private void SetupTogglesForEnumType ( )
146
171
{
147
172
toggleHolder . SetActive ( true ) ;
148
- enumDropdown . gameObject . SetActive ( false ) ;
173
+ inputField . Component . gameObject . SetActive ( false ) ;
174
+ enumHelperButton . Component . gameObject . SetActive ( false ) ;
149
175
150
176
// create / set / hide toggles
151
177
for ( int i = 0 ; i < CurrentValues . Count || i < flagToggles . Count ; i ++ )
@@ -180,54 +206,13 @@ private void AddToggleRow()
180
206
flagTexts . Add ( toggleText ) ;
181
207
}
182
208
183
- // UI Construction
184
-
185
- public override GameObject CreateContent ( GameObject parent )
186
- {
187
- UIRoot = UIFactory . CreateVerticalGroup ( parent , "InteractiveEnum" , false , false , true , true , 3 , new Vector4 ( 4 , 4 , 4 , 4 ) ,
188
- new Color ( 0.06f , 0.06f , 0.06f ) ) ;
189
- UIFactory . SetLayoutElement ( UIRoot , minHeight : 25 , flexibleHeight : 9999 , flexibleWidth : 9999 ) ;
190
-
191
- var hori = UIFactory . CreateUIObject ( "Hori" , UIRoot ) ;
192
- UIFactory . SetLayoutElement ( hori , minHeight : 25 , flexibleWidth : 9999 ) ;
193
- UIFactory . SetLayoutGroup < HorizontalLayoutGroup > ( hori , false , false , true , true , 2 ) ;
194
-
195
- var applyButton = UIFactory . CreateButton ( hori , "ApplyButton" , "Apply" , new Color ( 0.2f , 0.27f , 0.2f ) ) ;
196
- UIFactory . SetLayoutElement ( applyButton . Component . gameObject , minHeight : 25 , minWidth : 100 ) ;
197
- applyButton . OnClick += OnApplyClicked ;
198
-
199
- var dropdownObj = UIFactory . CreateDropdown ( hori , out enumDropdown , "not set" , 14 , null ) ;
200
- UIFactory . SetLayoutElement ( dropdownObj , minHeight : 25 , flexibleWidth : 600 ) ;
201
-
202
- toggleHolder = UIFactory . CreateUIObject ( "ToggleHolder" , UIRoot ) ;
203
- UIFactory . SetLayoutGroup < VerticalLayoutGroup > ( toggleHolder , false , false , true , true , 4 ) ;
204
- UIFactory . SetLayoutElement ( toggleHolder , minHeight : 25 , flexibleWidth : 9999 , flexibleHeight : 9999 ) ;
205
-
206
- return UIRoot ;
207
- }
208
-
209
-
210
209
#region Enum cache
211
210
212
- public struct CachedEnumValue
213
- {
214
- public CachedEnumValue ( object value , int index , string name )
215
- {
216
- EnumIndex = index ;
217
- Name = name ;
218
- ActualValue = value ;
219
- }
220
-
221
- public readonly object ActualValue ;
222
- public int EnumIndex ;
223
- public readonly string Name ;
224
- }
225
-
226
211
internal static readonly Dictionary < string , OrderedDictionary > enumCache = new Dictionary < string , OrderedDictionary > ( ) ;
227
212
228
- internal static OrderedDictionary GetEnumValues ( Type enumType , out bool isFlags )
213
+ internal static OrderedDictionary GetEnumValues ( Type enumType )
229
214
{
230
- isFlags = enumType . GetCustomAttributes ( typeof ( FlagsAttribute ) , true ) is object [ ] fa && fa . Any ( ) ;
215
+ // isFlags = enumType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] fa && fa.Any();
231
216
232
217
if ( ! enumCache . ContainsKey ( enumType . AssemblyQualifiedName ) )
233
218
{
@@ -254,4 +239,18 @@ internal static OrderedDictionary GetEnumValues(Type enumType, out bool isFlags)
254
239
255
240
#endregion
256
241
}
242
+
243
+ public struct CachedEnumValue
244
+ {
245
+ public CachedEnumValue ( object value , int index , string name )
246
+ {
247
+ EnumIndex = index ;
248
+ Name = name ;
249
+ ActualValue = value ;
250
+ }
251
+
252
+ public readonly object ActualValue ;
253
+ public int EnumIndex ;
254
+ public readonly string Name ;
255
+ }
257
256
}
0 commit comments