Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 7b477a8

Browse files
committed
Rewrite EvaluateWidget, add BaseArgumentHandler, use autocomplete for InteractiveEnum
1 parent e585fc6 commit 7b477a8

File tree

13 files changed

+764
-486
lines changed

13 files changed

+764
-486
lines changed

src/CacheObject/CacheMember.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using UnityExplorer.UI;
1212
using UniverseLib;
1313
using UniverseLib.UI;
14+
using UnityExplorer.UI.Widgets;
1415

1516
namespace UnityExplorer.CacheObject
1617
{

src/CacheObject/IValues/InteractiveEnum.cs

Lines changed: 97 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using UnityEngine.UI;
88
using UnityExplorer.CacheObject;
99
using UnityExplorer.UI;
10+
using UnityExplorer.UI.Widgets.AutoComplete;
11+
using UniverseLib;
1012
using UniverseLib.UI;
1113

1214
namespace UnityExplorer.CacheObject.IValues
@@ -20,10 +22,10 @@ public class InteractiveEnum : InteractiveValue
2022

2123
public OrderedDictionary CurrentValues;
2224

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;
2528

26-
private Dropdown enumDropdown;
2729
private GameObject toggleHolder;
2830
private readonly List<Toggle> flagToggles = new List<Toggle>();
2931
private readonly List<Text> flagTexts = new List<Text>();
@@ -35,38 +37,68 @@ public override void SetValue(object value)
3537

3638
if (lastType != EnumType)
3739
{
38-
CurrentValues = GetEnumValues(EnumType, out IsFlags);
40+
CurrentValues = GetEnumValues(EnumType);
3941

42+
IsFlags = EnumType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] fa && fa.Any();
4043
if (IsFlags)
4144
SetupTogglesForEnumType();
4245
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();
4454

4555
lastType = EnumType;
4656
}
4757

48-
// setup ui for changes
49-
if (IsFlags)
50-
SetTogglesForValue(value);
58+
if (!IsFlags)
59+
inputField.Text = value.ToString();
5160
else
52-
SetDropdownForValue(value);
53-
}
61+
SetTogglesForValue(value);
5462

55-
// Setting value to owner
63+
this.enumCompleter.chosenSuggestion = value.ToString();
64+
AutoCompleteModal.Instance.ReleaseOwnership(this.enumCompleter);
65+
}
5666

57-
private void OnApplyClicked()
67+
private void SetTogglesForValue(object value)
5868
{
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+
}
6383
}
6484

65-
private void SetValueFromDropdown()
85+
// Setting value to owner
86+
87+
private void OnApplyClicked()
6688
{
6789
try
6890
{
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+
}
70102
}
71103
catch (Exception ex)
72104
{
@@ -93,59 +125,53 @@ private void SetValueFromFlags()
93125
}
94126
}
95127

96-
// setting UI state for value
128+
// UI Construction
97129

98-
private void SetDropdownForValue(object value)
130+
private void EnumHelper_OnClick()
99131
{
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();
108133
}
109134

110-
private void SetTogglesForValue(object value)
135+
public override GameObject CreateContent(GameObject parent)
111136
{
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);
118140

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;
127148

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;
129153

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;
134157

135-
// create dropdown entries
136-
enumDropdown.options.Clear();
158+
enumCompleter = new EnumCompleter(this.EnumType, this.inputField);
137159

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);
140163

141-
enumDropdown.value = 0;
142-
enumDropdown.RefreshShownValue();
164+
return UIRoot;
143165
}
144166

167+
public CachedEnumValue ValueAtIdx(int idx) => (CachedEnumValue)CurrentValues[idx];
168+
public CachedEnumValue ValueAtKey(object key) => (CachedEnumValue)CurrentValues[key];
169+
145170
private void SetupTogglesForEnumType()
146171
{
147172
toggleHolder.SetActive(true);
148-
enumDropdown.gameObject.SetActive(false);
173+
inputField.Component.gameObject.SetActive(false);
174+
enumHelperButton.Component.gameObject.SetActive(false);
149175

150176
// create / set / hide toggles
151177
for (int i = 0; i < CurrentValues.Count || i < flagToggles.Count; i++)
@@ -180,54 +206,13 @@ private void AddToggleRow()
180206
flagTexts.Add(toggleText);
181207
}
182208

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-
210209
#region Enum cache
211210

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-
226211
internal static readonly Dictionary<string, OrderedDictionary> enumCache = new Dictionary<string, OrderedDictionary>();
227212

228-
internal static OrderedDictionary GetEnumValues(Type enumType, out bool isFlags)
213+
internal static OrderedDictionary GetEnumValues(Type enumType)
229214
{
230-
isFlags = enumType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] fa && fa.Any();
215+
//isFlags = enumType.GetCustomAttributes(typeof(FlagsAttribute), true) is object[] fa && fa.Any();
231216

232217
if (!enumCache.ContainsKey(enumType.AssemblyQualifiedName))
233218
{
@@ -254,4 +239,18 @@ internal static OrderedDictionary GetEnumValues(Type enumType, out bool isFlags)
254239

255240
#endregion
256241
}
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+
}
257256
}

0 commit comments

Comments
 (0)