-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScriptEditor.cs
376 lines (348 loc) · 11 KB
/
ScriptEditor.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Microsoft.Maui.Controls;
using StereoKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using VisualScript;
namespace VisualScript;
public sealed class ScriptEditor
{
public struct UIElement
{
public string name;
public string spawnNodeType;
}
private readonly Player _player;
public ScriptEditor(Player player) {
_player = player;
UIPos = new Pose(new Vec3(0, 0.25f, -0.5f), Quat.LookAt(Vec3.Forward, Vec3.Zero));
_targetReadType = typeof(string);
SpawnNodeType = BuildSpawnNodeType(typeof(Variable<string>));
ReBuildUI();
}
private static readonly Dictionary<Color, TextStyle> _anyColor = new();
public static TextStyle GetTextStyle(Color color) {
if (_anyColor.ContainsKey(color)) {
return _anyColor[color];
}
_anyColor.Add(color, Text.MakeStyle(Font.Default, TextStyle.Default.CharHeight, color));
return _anyColor[color];
}
private TextStyle _targetReadTypeStyle;
private Type _targetReadType;
private readonly List<UIElement> _elements = new();
public Type TypeTargetReadType
{
get => _targetReadType; set {
_targetReadType = value;
ReBuildUI();
}
}
public bool IsInput { get; set; }
public string SpawnNodeType;
public int CurrentPage { get; set; } = 0;
private NodeBase.NodeButton _nodeOutput;
private void ClearNodeOutput() {
_nodeOutput = null;
}
public NodeBase.NodeButton NodeButton
{
get => _nodeOutput;
set {
if (_nodeOutput is not null) {
_nodeOutput.OnDispos -= ClearNodeOutput;
}
if (value is not null) {
value.OnDispos += ClearNodeOutput;
}
_nodeOutput = value;
}
}
private void ReBuildUI() {
_targetReadTypeStyle = GetTextStyle(TypeTargetReadType.GetTypeColor());
_elements.Clear();
CurrentPage = 0;
_elements.Add(new UIElement {
name = "TriggerButton",
spawnNodeType = BuildSpawnNodeType(typeof(TriggerButton))
});
_elements.Add(new UIElement {
name = "Update",
spawnNodeType = BuildSpawnNodeType(typeof(UpdateNode))
});
_elements.Add(new UIElement {
name = "Display",
spawnNodeType = BuildSpawnNodeType(typeof(DisplayNode<string>))
});
if (!IsInput) {
var methods = TypeTargetReadType?.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public) ?? Array.Empty<MethodInfo>();
foreach (var item in methods) {
if (item.Attributes.HasFlag(MethodAttributes.SpecialName)) {
continue;
}
if (item.Attributes.HasFlag(MethodAttributes.HasSecurity)) {
continue;
}
if (item.IsGenericMethod) {
}
else {
if (item.ReturnParameter.ParameterType == typeof(void)) {
if (item.GetParameters().Length == 0) {
try {
_elements.Add(new UIElement {
name = "void " + item.Name + "()",
spawnNodeType = BuildSpawnNodeType((typeof(CallMethodVoid<>).MakeGenericType(item.DeclaringType)), item.Name)
});
}
catch { }
}
else if (item.GetParameters().Length == 1) {
try {
_elements.Add(new UIElement {
name = "void " + item.Name + "(" + item.GetParameters()[0].ParameterType.GetFormattedName() + " " + item.GetParameters()[0].Name + ")",
spawnNodeType = BuildSpawnNodeType((typeof(CallMethodVoidARG<,>).MakeGenericType(item.DeclaringType, item.GetParameters()[0].ParameterType)), item.Name)
});
}
catch { }
}
else if (item.GetParameters().Length == 2) {
try {
_elements.Add(new UIElement {
name = "void " + item.Name + "(" + item.GetParameters()[0].ParameterType.GetFormattedName() + " " + item.GetParameters()[0].Name + "," + item.GetParameters()[1].ParameterType.GetFormattedName() + " " + item.GetParameters()[1].Name + ")",
spawnNodeType = BuildSpawnNodeType((typeof(CallMethodVoidARG<,,>).MakeGenericType(item.DeclaringType, item.GetParameters()[0].ParameterType, item.GetParameters()[1].ParameterType)), item.Name)
});
}
catch { }
}
else {
}
}
else {
if (item.GetParameters().Length == 0) {
try {
_elements.Add(new UIElement {
name = item.ReturnParameter.ParameterType.GetFormattedName() + " " + item.Name + "()",
spawnNodeType = BuildSpawnNodeType((typeof(CallMethodOut<,>).MakeGenericType(item.DeclaringType, item.ReturnParameter.ParameterType)), item.Name)
});
}
catch { }
}
else if (item.GetParameters().Length == 1) {
try {
_elements.Add(new UIElement {
name = item.ReturnParameter.ParameterType.GetFormattedName() + " " + item.Name + "(" + item.GetParameters()[0].ParameterType.GetFormattedName() + " " + item.GetParameters()[0].Name + ")",
spawnNodeType = BuildSpawnNodeType((typeof(CallMethodOutARG<,,>).MakeGenericType(item.DeclaringType, item.ReturnParameter.ParameterType, item.GetParameters()[0].ParameterType)), item.Name)
});
}
catch { }
}
else if (item.GetParameters().Length == 2) {
try {
_elements.Add(new UIElement {
name = item.ReturnParameter.ParameterType.GetFormattedName() + " " + item.Name + "(" + item.GetParameters()[0].ParameterType.GetFormattedName() + " " + item.GetParameters()[0].Name + "," + item.GetParameters()[1].ParameterType.GetFormattedName() + " " + item.GetParameters()[1].Name + ")",
spawnNodeType = BuildSpawnNodeType((typeof(CallMethodOutARG<,,,>).MakeGenericType(item.DeclaringType, item.ReturnParameter.ParameterType, item.GetParameters()[0].ParameterType, item.GetParameters()[1].ParameterType)), item.Name)
});
}
catch { }
}
else {
}
}
}
}
var props = TypeTargetReadType?.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public) ?? Array.Empty<PropertyInfo>();
foreach (var prop in props) {
if (prop.CanRead) {
try {
_elements.Add(new UIElement {
name = "Get " + prop.Name,
spawnNodeType = BuildSpawnNodeType((typeof(GetProperty<,>).MakeGenericType(prop.DeclaringType, prop.PropertyType)), prop.Name)
});
}
catch { }
}
if (prop.CanWrite) {
try {
_elements.Add(new UIElement {
name = "Set " + prop.Name,
spawnNodeType = BuildSpawnNodeType((typeof(SetProperty<,>).MakeGenericType(prop.DeclaringType, prop.PropertyType)), prop.Name)
});
}
catch { }
}
}
}
var asemb = new Assembly[2] {
typeof(UIElement).Assembly,
typeof(Color).Assembly,
};
var systemTypes = new Type[] { typeof(string), typeof(Type), typeof(bool), typeof(float), typeof(Half), typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong) };
foreach (var item in systemTypes) {
_elements.Add(new UIElement {
name = "Variable " + item.GetFormattedName(),
spawnNodeType = BuildSpawnNodeType(typeof(Variable<>).MakeGenericType(item))
});
}
var staticTypes = from asm in asemb
from t in asm.GetTypes()
where t.IsPublic
where !t.IsGenericType
select t;
foreach (var item in staticTypes) {
try {
_elements.Add(new UIElement {
name = "Variable " + item.GetFormattedName(),
spawnNodeType = BuildSpawnNodeType(typeof(Variable<>).MakeGenericType(item))
});
}
catch {
_elements.Add(new UIElement {
name = "Static " + item.GetFormattedName(),
spawnNodeType = BuildSpawnNodeType(typeof(StaticVariable), item.FullName)
});
}
}
}
public Pose UIPos;
private bool _spawnLastFrame = false;
public void Step() {
var spawnThisFrame = false;
var currentSide = Handed.Right;
var (typeData, ArgData) = GetSpawnNodeTypeData(SpawnNodeType);
if (Input.Controller(Handed.Right).trigger >= 0.5f) {
spawnThisFrame = true;
currentSide = Handed.Right;
}
if (Input.Controller(Handed.Left).trigger >= 0.5f) {
spawnThisFrame = true;
currentSide = Handed.Left;
}
if (Input.Key(Key.MouseCenter).IsActive()) {
spawnThisFrame = true;
}
if (Input.Key(Key.Tab).IsActive()) {
spawnThisFrame = true;
}
if (spawnThisFrame & !_spawnLastFrame) {
var spawnPos = Pose.Identity;
switch (currentSide) {
case Handed.Left:
spawnPos = Input.Hand(Handed.Left).palm;
break;
case Handed.Right:
spawnPos = Input.Hand(Handed.Right).palm;
break;
default:
break;
}
var node = _player.App.AddWorldObject<NodeBase>(typeData);
node.LoadArgs(ArgData);
if (node is not null) {
node.Pos = spawnPos;
}
}
_spawnLastFrame = spawnThisFrame;
UI.WindowBegin("Script Editor", ref UIPos, new Vec2(0.3f, 0.5f));
var startIndex = CurrentPage * 9;
var xPos = 0;
var yPos = 0;
void AddButton() {
if (startIndex < _elements.Count) {
var element = _elements[startIndex];
UI.PushId(startIndex);
var hasBeenEnabled = false;
if (SpawnNodeType == element.spawnNodeType) {
UI.PushEnabled(false);
hasBeenEnabled = true;
}
if (UI.ButtonAt(element.name, new Vec3(0.13f - (xPos * 0.09f), -0.02f - (yPos * 0.09f), 0), new Vec2(0.075f))) {
SpawnNodeType = element.spawnNodeType;
}
if (hasBeenEnabled) {
UI.PopEnabled();
}
UI.PopId();
}
startIndex++;
}
AddButton();
xPos++;
AddButton();
xPos++;
AddButton();
yPos++;
xPos = 0;
AddButton();
xPos++;
AddButton();
xPos++;
AddButton();
yPos++;
xPos = 0;
AddButton();
xPos++;
AddButton();
xPos++;
AddButton();
if (CurrentPage == 0) {
UI.PushEnabled(false);
}
UI.PushId("BackPage");
if (UI.ButtonAt("<", new Vec3(0.13f, -0.3f, 0), new Vec2(0.06f))) {
CurrentPage--;
}
UI.PopId();
if (CurrentPage == 0) {
UI.PopEnabled();
}
var maxPage = _elements.Count / 9;
var enabledActive = false;
if (CurrentPage >= maxPage) {
UI.PushEnabled(false);
enabledActive = true;
}
UI.PushId("ForwaredPage");
if (UI.ButtonAt(">", new Vec3(-0.06f, -0.3f, 0), new Vec2(0.06f))) {
CurrentPage++;
}
UI.PopId();
if (enabledActive) {
UI.PopEnabled();
}
UI.NextLine();
UI.Space(0.4f);
if (typeData is not null) {
var argString = "";
if (ArgData.Length > 0) {
argString = "(";
foreach (var item in ArgData) {
argString += item + " ,";
}
argString = argString.Remove(argString.Length - 2);
argString += ")";
}
UI.Text(typeData.GetFormattedName() + argString, TextAlign.Center);
}
UI.PushTextStyle(_targetReadTypeStyle);
UI.Text((IsInput ? "Input: " : "Output: ") + TypeTargetReadType.GetFormattedName(), TextAlign.Center);
UI.PopTextStyle();
UI.WindowEnd();
}
public string BuildSpawnNodeType(Type targetComp, params string[] args) {
return args.Length <= 0 ? (targetComp?.FullName) : targetComp?.FullName + "`~`" + string.Join("`~`", args);
}
public (Type, string[]) GetSpawnNodeTypeData(string type) {
if (string.IsNullOrEmpty(type)) {
return (null, null);
}
var data = type.Split("`~`");
if (data.Length <= 0) {
return (null, null);
}
var typeInfo = Type.GetType(data[0]);
var returenArray = new string[data.Length - 1];
Array.Copy(data, 1, returenArray, 0, returenArray.Length);
return (typeInfo, returenArray);
}
}