-
Notifications
You must be signed in to change notification settings - Fork 5
/
CherryPicker.cs
244 lines (177 loc) · 7.71 KB
/
CherryPicker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System.Reflection;
using FrooxEngine;
using Elements.Core;
using FrooxEngine.UIX;
using static CherryPick.CherryPick;
namespace CherryPick;
public class CherryPicker(string? scope = null)
{
public string? Scope
{
get => scope;
set
{
scope = value;
WarmScope(value);
}
}
public static bool IsReady { get; private set; }
public List<WorkerDetails> Workers => scope != null ? _pathCache[scope] : _allWorkers;
private static readonly Dictionary<string, List<WorkerDetails>> _pathCache = [];
private static readonly List<WorkerDetails> _allWorkers = [];
private readonly List<WorkerDetails> _results = [];
static CherryPicker()
{
foreach (var worker in WorkerInitializer.Workers)
{
if (!typeof(Component).IsAssignableFrom(worker))
continue;
WorkerInitInfo info = worker.GetInitInfo();
string? path = info.CategoryPath;
var detail = new WorkerDetails(worker.GetNiceName(), path, worker);
_allWorkers.Add(detail);
}
}
public static void SetReady()
{
IsReady = true;
}
// Makes a new pre-filtered list that is scoped to whatever the string is. This is somewhat heavy, so it's done when the mod initializes.
public static void WarmScope(string? scope = null)
{
if (!string.IsNullOrEmpty(scope) && !_pathCache.ContainsKey(scope!))
{
var filteredDict = _allWorkers
.Where(w => w.Path.StartsWith(scope))
.ToList();
_pathCache.Add(scope!, filteredDict);
}
}
public void PerformMatch(string query, int resultCount = 10)
{
_results.Clear();
// Occam's razor on this fellas. Sometimes the simplest solution is the right one. This takes like 3-7ms for the first sweep, then 0ms on subsequent queries. Wacky.
var results = Workers
.Select(w => new { worker = w, ratio = MatchRatioInsensitive(w.LowerName, query) })
.Where(x => x.ratio > 0f)
.OrderByDescending(x => x.ratio)
.Take(resultCount)
.Select(x => x.worker);
foreach (var w in results)
{
_results.Add(w);
}
}
// Out of the total string length, how many characters actually match the query. Gives decent results.
static float MatchRatioInsensitive(string? result, string match)
{
if (result == null)
return 0f;
var contains = match.Split(' ').Select(item => (item, score: result.IndexOf(item, StringComparison.OrdinalIgnoreCase)));
var score = contains
.Where(v => v.score >= 0)
.Select((v, i) => (v.item, v.score, i))
.Sum((v) => v.item.Length / (result.Length + v.i + 1f));
return contains.All(v => v.score >= 0) ? score : 0f;
}
public void EditStart(Slot searchRoot, Slot defaultRoot, Sync<string> scope)
{
Scope = scope;
if (defaultRoot != null && searchRoot != null)
{
defaultRoot.ActiveSelf = false;
searchRoot.ActiveSelf = true;
}
}
public void EditFinished(TextEditor editor, Slot searchRoot, Slot defaultRoot, bool forceFinish = false)
{
if (forceFinish)
editor.Text.Target.Text = null;
if (editor != null &&
editor.Text.Target != null &&
string.IsNullOrEmpty(editor.Text.Target.Text) &&
defaultRoot != null &&
searchRoot != null)
{
defaultRoot.ActiveSelf = true;
searchRoot.ActiveSelf = false;
}
}
public void EditChanged(TextEditor editor, Slot searchRoot, Slot defaultRoot, UIBuilder searchBuilder, ButtonEventHandler<string> onGenericPressed, ButtonEventHandler<string> onAddPressed)
{
if (searchRoot == null ||
defaultRoot == null ||
editor == null ||
onGenericPressed == null ||
onAddPressed == null ||
searchBuilder == null ||
!IsReady) // You can't search until the cache is built! This is fine in most cases, but if you end up searching before then, too bad!
return;
string txt = editor.Text.Target.Text;
if (txt == null)
return;
int genericStart = txt.IndexOf('<');
string? matchTxt = null;
string? genericType = null;
if (genericStart > 0)
{
matchTxt = txt.Substring(0, genericStart);
genericType = txt.Substring(genericStart);
}
else
{
matchTxt = txt;
}
searchRoot.DestroyChildren();
int resultCount = Config!.GetValue(ResultCount);
resultCount = MathX.Min(resultCount, MAX_RESULT_COUNT);
PerformMatch(matchTxt, resultCount);
foreach (var result in _results)
{
string arg = result.Type.IsGenericTypeDefinition ? Path.Combine(result.Path, result.Type.FullName) : result.Type.FullName;
var pressed = result.Type.IsGenericTypeDefinition ? onGenericPressed : onAddPressed;
CreateButton(result, pressed, arg, searchBuilder, editor, searchRoot, defaultRoot, RadiantUI_Constants.Sub.CYAN);
}
WorkerDetails firstGeneric = _results.FirstOrDefault(w => w.Type.IsGenericTypeDefinition);
if (genericType != null)
{
string typeName = firstGeneric.Type.Name;
typeName = typeName.Substring(0, typeName.IndexOf("`")) + genericType;
Type? constructed = WorkerManager.ParseNiceType(typeName);
if (constructed != null)
{
WorkerDetails detail = new(constructed.GetNiceName(), firstGeneric.Path, constructed);
Button typeButton = CreateButton(detail, onAddPressed, typeName, searchBuilder, editor, searchRoot, defaultRoot, RadiantUI_Constants.Sub.ORANGE);
typeButton.Slot.OrderOffset = -1024;
}
}
}
// One day we will have better UI construction... one day :')
private Button CreateButton(WorkerDetails detail, ButtonEventHandler<string> pressed, string arg, UIBuilder builder, TextEditor editor, Slot searchRoot, Slot defaultRoot, colorX col)
{
string path = Scope != null ? detail.Path.Replace(Scope, null) : detail.Path;
var button = builder.Button($"<noparse={detail.Name.Length}>{detail.Name}<br><size=61.803%><line-height=133%>{path}", col, pressed, arg, CherryPick.PressDelay);
ValueField<double> lastPressed = button.Slot.AddSlot("LastPressed").AttachComponent<ValueField<double>>();
button.ClearFocusOnPress.Value = CherryPick.Config!.GetValue(CherryPick.ClearFocus);
if (detail.Type.IsGenericTypeDefinition)
{
button.LocalPressed += (b, d) =>
{
double now = searchRoot.World.Time.WorldTime;
if (now - lastPressed.Value < CherryPick.PressDelay || CherryPick.SingleClick)
EditFinished(editor, searchRoot, defaultRoot, true);
else
lastPressed.Value.Value = now;
};
}
var text = (Text)button.LabelTextField.Parent;
text.Size.Value = 24.44582f;
var smooth = button.Slot.AttachComponent<SmoothValue<colorX>>();
IField<colorX> target = button.ColorDrivers.First().ColorDrive.Target;
smooth.TargetValue.Value = target.Value;
button.ColorDrivers.First().ColorDrive.Target = smooth.TargetValue;
smooth.Value.Target = target;
smooth.Speed.Value = 12f;
return button;
}
}