-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlsTest.cs
More file actions
428 lines (363 loc) · 16.8 KB
/
ControlsTest.cs
File metadata and controls
428 lines (363 loc) · 16.8 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using Cameras;
using Data;
using InputDevices;
using System;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.InteropServices;
using Transforms;
using Transforms.Components;
using UI;
using UI.Components;
using UI.ControlEditors;
using UI.Functions;
using Unmanaged;
using Windows;
using Worlds;
namespace Abacus
{
public class ControlsTest : Program
{
private readonly Window window;
private readonly Menu rightClickMenu;
private readonly Dropdown enumDropdown;
private readonly Settings settings;
public unsafe ControlsTest(Application application) : base(application)
{
window = new(world, "Editor", new(200, 200), new(900, 720), "vulkan", new(&OnWindowClosed));
window.ClearColor = new(0.5f, 0.5f, 0.5f, 1);
window.IsResizable = true;
settings = new(world);
Camera camera = Camera.CreateOrthographic(world, window, 1f);
Canvas canvas = new(settings, camera);
VirtualWindow box = VirtualWindow.Create<ControlsDemoWindow>(world, canvas);
box.Size = new(300, 300);
box.Position = new(40, 40);
box.Z += Settings.ZScale * 2f;
//box.Anchor = new(new(2f, true), new(2f, true), new(0f, false), new(2f, true), new(2f, true), new(0f, false));
Image image = new(canvas);
image.Size = new(200, 200);
image.Position = new(100, 100);
Resizable resizable = image.Become<Resizable>();
resizable.Boundary = IsResizable.EdgeMask.All;
DropShadow dropShadow = new(canvas, image);
Label anotherLabel = new(canvas, "Hello there\nWith another line\nNice");
anotherLabel.Position = new(105, 150);
anotherLabel.Color = new(0, 0, 0, 1);
anotherLabel.Z = Settings.ZScale * 2;
enumDropdown = new Dropdown<DropdownOptions>(canvas, new(180f, settings.SingleLineHeight));
enumDropdown.Position = new(5, -5);
enumDropdown.Size = new(180f, settings.SingleLineHeight);
enumDropdown.Anchor = Anchor.TopLeft;
enumDropdown.Pivot = new(0f, 1f, 0f);
enumDropdown.BackgroundColor = new(0.2f, 0.2f, 0.2f, 1);
enumDropdown.LabelColor = new(1, 1, 1, 1);
enumDropdown.TriangleColor = new(1, 1, 1, 1);
enumDropdown.Z = Settings.ZScale * 5f;
Vector2 optionSize = new(100, settings.SingleLineHeight);
rightClickMenu = new(canvas, optionSize, new(&ChoseMenuOption));
rightClickMenu.AddOption("Stop");
rightClickMenu.AddOption("And Listen");
rightClickMenu.AddOption("OoOoh");
rightClickMenu.AddOption("Deep.../First");
rightClickMenu.AddOption("Deep.../Second");
rightClickMenu.AddOption("Deep.../Third");
rightClickMenu.AddOption("Deep.../Apple");
rightClickMenu.AddOption("Deep.../Banana");
rightClickMenu.AddOption("Deep.../Car");
rightClickMenu.Position = new(200, 300);
rightClickMenu.Z = Settings.ZScale * 20f;
rightClickMenu.Pivot = new(0, 1f, 0f);
rightClickMenu.IsExpanded = false;
}
[UnmanagedCallersOnly]
private static void ChoseMenuOption(MenuOption option)
{
Trace.WriteLine($"Chose option: {option}");
option.rootMenu.IsExpanded = false;
}
public override bool Update(double deltaTime)
{
if (!IsAnyVirtualWindowOpen(world))
{
return false;
}
if (!IsAnyWindowOpen(world))
{
return false;
}
ToggleRightClickMenu(world);
SharedFunctions.UpdateUISettings(world);
return true;
}
private void ToggleRightClickMenu(World world)
{
if (world.TryGetFirst(out Mouse mouse))
{
if (mouse.WasPressed(Mouse.Button.RightButton))
{
rightClickMenu.Position = mouse.Position;
rightClickMenu.IsExpanded = true;
}
}
}
public override void Dispose()
{
settings.Dispose();
if (!window.IsDestroyed)
{
window.Dispose();
}
}
private static bool IsAnyVirtualWindowOpen(World world)
{
return world.TryGetFirst(out VirtualWindow _);
}
private static bool IsAnyWindowOpen(World world)
{
return world.TryGetFirst(out Window _);
}
[UnmanagedCallersOnly]
private static void OnWindowClosed(Window window)
{
window.Dispose();
}
public readonly struct ControlsDemoWindow : IVirtualWindow
{
readonly ASCIIText256 IVirtualWindow.Title => "Controls Demo";
readonly unsafe VirtualWindowClose IVirtualWindow.CloseCallback => new(&Closed);
readonly unsafe void IVirtualWindow.OnCreated(Transform container, Canvas canvas)
{
float singleLineHeight = canvas.Settings.SingleLineHeight;
float gap = 4f;
float y = -gap;
float indent = 0f;
Label testLabel = new(canvas, "Hello, World!");
testLabel.SetParent(container);
testLabel.Anchor = Anchor.TopLeft;
testLabel.Color = new(0, 0, 0, 1);
testLabel.Position = new(gap, y);
testLabel.Pivot = new(0f, 1f, 0f);
y -= singleLineHeight + gap;
Button testButton = new(new(&PressedTestButton), canvas);
testButton.SetParent(container);
testButton.Color = new(0.2f, 0.2f, 0.2f, 1);
testButton.Anchor = Anchor.TopLeft;
testButton.Pivot = new(0f, 1f, 0f);
testButton.Size = new(180f, singleLineHeight);
testButton.Position = new(gap, y);
Label testButtonLabel = new(canvas, "Press count: 0");
testButtonLabel.SetParent(testButton);
testButtonLabel.Anchor = Anchor.TopLeft;
testButtonLabel.Position = new(gap, -4f);
testButtonLabel.Pivot = new(0f, 1f, 0f);
y -= singleLineHeight + gap;
Toggle testToggle = new(canvas);
testToggle.SetParent(container);
testToggle.Position = new(gap, y);
testToggle.Size = new(24, singleLineHeight);
testToggle.Anchor = Anchor.TopLeft;
testToggle.Pivot = new(0f, 1f, 0f);
testToggle.BackgroundColor = new(0.2f, 0.2f, 0.2f, 1);
testToggle.CheckmarkColor = new(1, 1, 1, 1);
y -= singleLineHeight + gap;
ScrollBar horizontalScrollBar = new(canvas, Vector2.UnitX, 0.25f);
horizontalScrollBar.SetParent(container);
horizontalScrollBar.Position = new(gap, y);
horizontalScrollBar.Size = new(180f, singleLineHeight);
horizontalScrollBar.Anchor = Anchor.TopLeft;
horizontalScrollBar.Pivot = new(0f, 1f, 0f);
horizontalScrollBar.BackgroundColor = new(0.2f, 0.2f, 0.2f, 1);
horizontalScrollBar.ScrollHandleColor = new(1, 1, 1, 1);
y -= singleLineHeight + gap;
Dropdown testDropdown = new(canvas, new(180f, singleLineHeight));
testDropdown.SetParent(container);
testDropdown.Position = new(gap, y);
testDropdown.Size = new(180f, singleLineHeight);
testDropdown.Anchor = Anchor.TopLeft;
testDropdown.Pivot = new(0f, 1f, 0f);
testDropdown.BackgroundColor = new(0.2f, 0.2f, 0.2f, 1);
testDropdown.LabelColor = new(1, 1, 1, 1);
testDropdown.TriangleColor = new(1, 1, 1, 1);
Menu testDropdownMenu = testDropdown.Menu;
testDropdownMenu.AddOption("Option A");
testDropdownMenu.AddOption("Option B");
OptionPath lastOption = testDropdownMenu.AddOption("Option C");
testDropdownMenu.AddOption("Option D/Apple");
testDropdownMenu.AddOption("Option D/Banana");
testDropdownMenu.AddOption("Option D/Cherry");
testDropdownMenu.AddOption("Option D/More.../Toyota");
testDropdownMenu.AddOption("Option D/More.../Honda");
testDropdownMenu.AddOption("Option D/More.../Hyndai");
testDropdownMenu.AddOption("Option D/More.../Mitsubishi");
testDropdown.SelectedOption = lastOption;
testDropdown.Callback = new(&DropdownOptionChanged);
[UnmanagedCallersOnly]
static void DropdownOptionChanged(DropdownCallback.Input input)
{
IsMenuOption option = input.dropdown.Options[input.currentOption];
Trace.WriteLine($"Selected option: {option.text}");
}
y -= singleLineHeight + gap;
TextField testTextField = new(canvas);
testTextField.SetParent(container);
testTextField.Position = new(gap, y);
testTextField.Size = new(180f, singleLineHeight);
testTextField.Anchor = Anchor.TopLeft;
testTextField.Pivot = new(0f, 1f, 0f);
testTextField.BackgroundColor = new(0.2f, 0.2f, 0.2f, 1f);
testTextField.TextColor = new(1, 1, 1, 1);
y -= singleLineHeight + gap;
Label headerLabel = new(canvas, "Fields");
headerLabel.SetParent(container);
headerLabel.Anchor = Anchor.TopLeft;
headerLabel.Color = new(0, 0, 0, 1);
headerLabel.Position = new(gap, y);
headerLabel.Pivot = new(0f, 1f, 0f);
y -= singleLineHeight + gap;
indent += 20f;
{
World world = canvas.world;
world.Schema.RegisterComponent<bool>();
world.Schema.RegisterComponent<float>();
world.Schema.RegisterComponent<ASCIIText256>();
Entity dataEntity = new(world);
dataEntity.AddComponent(true);
dataEntity.AddComponent(0f);
dataEntity.AddComponent(new ASCIIText256("babash"));
ControlField numberField = ControlField.Create<float, NumberTextEditor>(canvas, "Number", dataEntity);
numberField.LabelColor = new(0, 0, 0, 1);
numberField.SetParent(container);
numberField.Anchor = Anchor.TopLeft;
numberField.Pivot = new(0f, 1f, 0f);
numberField.Position = new(gap + indent, y);
numberField.Size = new(180f, singleLineHeight);
y -= singleLineHeight + gap;
ControlField textField = ControlField.Create<ASCIIText256, TextEditor>(canvas, "Text", dataEntity);
textField.LabelColor = new(0, 0, 0, 1);
textField.SetParent(container);
textField.Anchor = Anchor.TopLeft;
textField.Pivot = new(0f, 1f, 0f);
textField.Position = new(gap + indent, y);
textField.Size = new(180f, singleLineHeight);
y -= singleLineHeight + gap;
ControlField toggleField = ControlField.Create<bool, BooleanEditor>(canvas, "Toggle", dataEntity);
toggleField.LabelColor = new(0, 0, 0, 1);
toggleField.SetParent(container);
toggleField.Anchor = Anchor.TopLeft;
toggleField.Pivot = new(0f, 1f, 0f);
toggleField.Position = new(gap + indent, y);
toggleField.Size = new(180f, singleLineHeight);
y -= singleLineHeight + gap;
}
indent -= 20f;
Label treeLabel = new(canvas, "Tree of objects");
treeLabel.SetParent(container);
treeLabel.Anchor = Anchor.TopLeft;
treeLabel.Color = new(0, 0, 0, 1);
treeLabel.Position = new(gap, y);
treeLabel.Pivot = new(0f, 1f, 0f);
y -= singleLineHeight + gap;
Tree testTree = new(canvas);
testTree.SetParent(container);
testTree.Position = new(gap, y);
testTree.Size = new(180f, singleLineHeight);
testTree.Anchor = Anchor.TopLeft;
testTree.Pivot = new(0f, 1f, 0f);
testTree.AddLeaf("Game Object");
y -= singleLineHeight;
TreeNode canvasLeaf = testTree.AddLeaf("Canvas");
canvasLeaf.AddLeaf("Button");
canvasLeaf.AddLeaf("Toggle");
y -= singleLineHeight;
TreeNode playerLeaf = canvasLeaf.AddLeaf("Player");
playerLeaf.AddLeaf("Health");
playerLeaf.AddLeaf("Score");
testTree.AddLeaf("Game Object (1)");
testTree.AddLeaf("Game Object (2)");
y -= singleLineHeight;
y -= singleLineHeight + gap;
for (int i = 0; i < 20; i++)
{
Image box = new(canvas);
box.SetParent(container);
box.Size = new(60f, 20f);
box.Position = new(200f, i * 26f);
Color color = box.Color;
color.Hue = (i * 0.1f) % 1;
box.Color = color;
}
TextField multiLineTextField = new(canvas);
multiLineTextField.SetParent(container);
multiLineTextField.Position = new(gap, y);
multiLineTextField.Size = new(180f, singleLineHeight * 3);
multiLineTextField.Anchor = Anchor.TopLeft;
multiLineTextField.Pivot = new(0f, 1f, 0f);
multiLineTextField.BackgroundColor = new(0.2f, 0.2f, 0.2f, 1f);
multiLineTextField.TextColor = new(1, 1, 1, 1);
}
[UnmanagedCallersOnly]
private static void PressedTestButton(Entity buttonEntity)
{
World world = buttonEntity.world;
Entity containerEntity = buttonEntity.Parent;
Span<uint> children = stackalloc uint[containerEntity.ChildCount];
containerEntity.CopyChildrenTo(children);
bool toggleValue = default;
for (int i = 0; i < children.Length; i++)
{
uint child = children[i];
if (world.ContainsComponent<IsToggle>(child))
{
Toggle toggle = Entity.Get<Toggle>(world, child);
toggleValue = toggle.Value;
break;
}
}
children = stackalloc uint[buttonEntity.ChildCount];
buttonEntity.CopyChildrenTo(children);
for (int i = 0; i < children.Length; i++)
{
uint child = children[i];
if (world.ContainsTag<IsLabel>(child))
{
Label label = Entity.Get<Label>(world, child);
ReadOnlySpan<char> text = label.ProcessedText;
int startIndex = text.IndexOf(':') + 1;
int countValue = int.Parse(text.Slice(startIndex));
Span<char> countText = stackalloc char[10];
if (!toggleValue)
{
countValue++;
}
else
{
countValue--;
}
int countTextLength = countValue.ToString(countText);
ASCIIText256 textValue = new(text);
textValue.Length = (byte)(startIndex + 1);
textValue.Append(countText.Slice(0, countTextLength));
label.SetText(textValue);
break;
}
}
}
[UnmanagedCallersOnly]
private static void Closed(VirtualWindow virtualWindow)
{
virtualWindow.Dispose();
}
}
public enum DropdownOptions
{
First,
Second,
Third,
Yo,
What,
Is,
Up
}
}
}