-
Notifications
You must be signed in to change notification settings - Fork 0
/
scr_witchcraft_ui.gml
1312 lines (1271 loc) · 44.5 KB
/
scr_witchcraft_ui.gml
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! A batteries-not-included UI system inspired loosely by the box model
//! system. Fully customisable and extendible with very few moving parts.
//# feather use syntax-errors
show_debug_message("enchanted with Witchcraft::ui by @katsaii");
/// The method of input most likely being used when navigating a menu.
enum WUIInputHint {
INDETERMINATE,
CURSOR,
KEYBOARD,
}
/// Converts keyboard and mouse events into a consumable format used by
/// `WUINavigator`.
function WUIInputManager() constructor {
/// The type of input most likely being used at the current point in time.
///
/// @return {Enum.WUIInputHint}
self.inputMethod = WUIInputHint.INDETERMINATE;
/// The position of the mouse cursor in the X direction, or `undefined`
/// if the mouse is inactive.
///
/// @return {Real}
self.cursorX = undefined;
/// The position of the mouse cursor in the Y direction, or `undefined`
/// if the mouse is inactive.
///
/// @return {Real}
self.cursorY = undefined;
/// The difference between the current and previous cursor positions, in
/// the X direction.
///
/// @return {Real}
self.cursorXDelta = 0;
/// The difference between the current and previous cursor positions, in
/// the Y direction.
///
/// @return {Real}
self.cursorYDelta = 0;
/// Whether the current mouse event has caused the cursor to move. Only
/// returns `true` if the cursor was active in the previous frame and the
/// difference between the current and previous cursor positions is
/// non-zero.
///
/// @return {Bool}
self.cursorUpdated = false;
/// The movement vector in the X direction when using keyboard navigation.
/// A positive value means movement to the right, a negative value means
/// movement to the left, and zero means no movement.
///
/// @return {Real}
self.moveX = 0;
/// The movement vector in the Y direction when using keyboard navigation.
/// A positive value means movement downards, a negative value means
/// movement upwards, and zero means no movement.
///
/// @return {Real}
self.moveY = 0;
/// The number of consecutive frames the same movement deltas have been
/// active for.
///
/// @return {Real}
self.moveDuration = 0;
/// Whether a movement event has been detected. This is likely triggered
/// when a new input delta is received, or if the same delta has been held
/// for a prolonged period of time.
///
/// @return {Bool}
self.moveUpdated = false;
/// Whether a tabbing navigation input was detected.
///
/// @return {Bool}
self.tab = false;
/// The difference between the current and previous tabbing input values.
/// A positive value means the input was just pressed, and a negative value
/// means the input was just released.
///
/// @return {Real}
self.tabDelta = 0;
/// Whether a confirmation input was detected.
///
/// @return {Bool}
self.confirm = false;
/// The difference between the current and previous confirmation input
/// values. A positive value means the input was just pressed, and a
/// negative value means the input was just released.
///
/// @return {Real}
self.confirmDelta = 0;
/// Whether a cancel input was detected.
///
/// @return {Bool}
self.cancel = false;
/// The difference between the current and previous cancel input values.
/// A positive value means the input was just pressed, and a negative
/// value means the input was just released.
///
/// @return {Real}
self.cancelDelta = 0;
/// Updates the cursor position and registers any interesting events.
///
/// If a cursor movement is detected, the `WUIInputManager::inputMethod`
/// for this manager will be changed to `WUIInputHint.CURSOR`.
///
/// @param {Real} newCursorX
/// The new cursor position in the X direction.
///
/// @param {Real} newCursorY
/// The new cursor position in the Y direction.
static cursorEvent = function (newCursorX, newCursorY) {
if (cursorX != undefined && newCursorX != undefined) {
cursorXDelta = newCursorX - cursorX;
} else {
cursorXDelta = 0;
}
cursorX = newCursorX;
if (cursorY != undefined && newCursorY != undefined) {
cursorYDelta = newCursorY - cursorY;
} else {
cursorYDelta = 0;
}
cursorY = newCursorY;
cursorUpdated = cursorXDelta != 0 || cursorYDelta != 0;
if (cursorUpdated) {
inputMethod = WUIInputHint.CURSOR;
moveDuration = 0;
moveUpdated = false;
}
};
/// Updates the keyboard movement state and registers any interesting
/// events.
///
/// If a movement is detected, the `WUIInputManager::inputMethod` for this
/// manager will be changed to `WUIInputHint.KEYBOARD`.
///
/// @param {Real} moveLeft
/// The amount of movement in the left direction.
///
/// @param {Real} moveUp
/// The amount of movement in the upwards direction.
///
/// @param {Real} moveRight
/// The amount of movement in the right direction.
///
/// @param {Real} moveDown
/// The amount of movement in the downwards direction.
static moveEvent = function (moveLeft, moveUp, moveRight, moveDown) {
var newXDelta = clamp(moveRight - moveLeft, -1, 1);
var newYDelta = clamp(moveDown - moveUp, -1, 1);
var sameState = newXDelta == moveX && newYDelta == moveY;
var nullState = newXDelta == 0 && newYDelta == 0;
moveX = newXDelta;
moveY = newYDelta;
if (!sameState) {
moveDuration = 0;
}
if (!nullState) {
moveDuration += 1;
}
// magic numbers :3c
// yummy!
if (moveDuration < 30) {
moveUpdated = moveDuration == 1;
} else {
moveUpdated = moveDuration % 4 == 0;
}
if (moveUpdated) {
inputMethod = WUIInputHint.KEYBOARD;
cursorUpdated = false;
}
};
/// Updates the tabbing navigation button state.
///
/// @param {Bool} held
/// Whether the tab button is held down.
static tabEvent = function (held) {
tabDelta = held - tab;
tab = held;
};
/// Updates the confirm button state.
///
/// @param {Bool} held
/// Whether the confirm button is held down.
static confirmEvent = function (held) {
confirmDelta = held - confirm;
confirm = held;
};
/// Updates the cancel button state.
///
/// @param {Bool} held
/// Whether the cancel button is held down.
static cancelEvent = function (held) {
cancelDelta = held - cancel;
cancel = held;
};
/// Resets all event data. Most importantly, this will also update the
/// `WUIInputManager::inputMethod` of this manager to be
/// `WUIInputHint.INDETERMINATE`.
static clearEvents = function () {
inputMethod = WUIInputHint.INDETERMINATE;
cursorX = undefined;
cursorY = undefined;
cursorXDelta = 0;
cursorYDelta = 0;
cursorUpdated = false;
moveX = 0;
moveY = 0;
moveDuration = 0;
moveUpdated = false;
tab = false;
tabDelta = 0;
confirm = false;
confirmDelta = 0;
cancel = false;
cancelDelta = 0;
};
}
/// Manages the keyboard and mouse navigation events of a menu. Does not
/// assume knowledge of a root node, and can therefore be reused between
/// menus or support cross-menu navigation.
///
/// @param {Struct.WUIInputManager} [manager]
/// The input manager to consume input events from. Defaults to the shared
/// global input manager.
function WUINavigator(manager = undefined) constructor {
/// The previous UI element being hovered or interacted with.
///
/// @return {Struct.WUIElement}
self.lastElementInFocus = undefined;
/// The current UI element being hovered or interacted with.
///
/// @return {Struct.WUIElement}
self.elementInFocus = undefined;
/// The input manager for this navigator. Stores and registers input
/// events for interactive menus.
///
/// @return {Struct.WUIInputManager}
self.input = manager ?? new WUIInputManager();
/// Sets the focused element for this navigator, invoking the enter and
/// exit events for the current focused element in the process.
///
/// @remark
/// The exit event for `WUINavigator::lastElementInFocus` is invoked
/// before the enter event for `WUINavigator::elementInFocus`.
///
/// @param {Struct.WUIElement} elem
static setFocusedElement = function (elem) {
lastElementInFocus = elementInFocus;
elementInFocus = elem;
if (elementInFocus != lastElementInFocus) {
if (lastElementInFocus != undefined) {
lastElementInFocus.invokeEventExit();
}
if (elementInFocus != undefined) {
elementInFocus.invokeEventEnter();
}
}
};
/// Uses the input events raised by `WUINavigator::input` to navigate
/// the `rootElem` menu.
///
/// @param {Struct.WUIElement} rootElem
/// The root element for the menu to navigate. Typically this is some
/// kind of view element with subelements attached to it, but it can
/// also be used on trivial elements like buttons and sliders.
///
/// @param {Struct.WUIElement} [defaultElem]
/// The default starting element to use if a keyboard event is received.
/// This element must have its `__navigable__` meta flag set to true,
/// otherwise it will be ignored.
static navigate = function (rootElem, defaultElem = undefined) {
if (defaultElem != undefined && !defaultElem.__navigable__) {
defaultElem = undefined;
}
var elementInFocus_ = elementInFocus;
var input_ = input;
// button states
var confirmPressed = input_.confirmDelta > 0;
var confirm = input_.confirm;
var confirmReleased = input_.confirmDelta < 0;
// events for the focused element
if (elementInFocus_ != undefined) {
if (confirmPressed) {
// press event
elementInFocus_.invokeEventPressed(self);
}
if (
confirm && input_.cursorUpdated ||
confirmPressed && input_.inputMethod == WUIInputHint.CURSOR
) {
// drag event
elementInFocus_.invokeEventDrag(self);
}
if (confirmReleased) {
// release event
elementInFocus_.invokeEventReleased(self);
}
elementInFocus_.invokeEventStep(self);
}
if (confirm) {
// all future events should only be triggered if the confirmation
// button is not being held
return;
}
if (input_.cursorUpdated) {
// cursor navigation
var newElementInFocus = rootElem.findElementAtPosition(
input_.cursorX, input_.cursorY
);
setFocusedElement(newElementInFocus);
}
// movement override
var moveX = input_.moveX;
var moveY = input_.moveY;
if (
elementInFocus_ != undefined &&
elementInFocus_.tryMove(moveX, moveY)
) {
return;
}
if (input_.moveUpdated) {
// keyboard navigation
if (elementInFocus_ == undefined) {
// default element
setFocusedElement(defaultElem);
} else {
// neighbour element
var newElementInFocus = undefined;
if (moveX < 0) {
newElementInFocus = elementInFocus.__navLeft__;
} else if (moveX > 0) {
newElementInFocus = elementInFocus.__navRight__;
} else if (moveY < 0) {
newElementInFocus = elementInFocus.__navUp__;
} else if (moveY > 0) {
newElementInFocus = elementInFocus.__navDown__;
}
if (newElementInFocus == undefined) {
// TODO :: could be improved!
var rootLeft = rootElem.worldX;
var rootTop = rootElem.worldY;
var rootRight = rootLeft + rootElem.worldWidth;
var rootBottom = rootTop + rootElem.worldHeight;
var focusLeft = elementInFocus.worldX;
var focusTop = elementInFocus.worldY;
var focusRight = focusLeft + elementInFocus.worldWidth;
var focusBottom = focusTop + elementInFocus.worldHeight;
var left = focusLeft + 1;
var right = focusRight - 1;
if (moveX < 0) {
left = lerp(focusLeft - 1, rootLeft, -moveX);
right = focusLeft - 1;
} else if (moveX > 0) {
left = lerp(rootRight, focusRight + 1, moveX);
right = rootRight;
}
var top = focusTop + 1;
var bottom = focusBottom - 1;
if (moveY < 0) {
top = lerp(focusTop - 1, rootTop, -moveY);
bottom = focusTop - 1;
} else if (moveY > 0) {
top = lerp(rootBottom, focusBottom + 1, moveY);
bottom = rootBottom;
}
var targetX = mean(focusLeft, focusRight);
var targetY = mean(focusTop, focusBottom);
if (lastElementInFocus != undefined) {
// slight bias towards previous element to break ties
var lastX =
lastElementInFocus.worldX +
lastElementInFocus.worldWidth / 2;
var lastY =
lastElementInFocus.worldY +
lastElementInFocus.worldHeight / 2;
var biasStrength = 3;
targetX += biasStrength * sign(lastX - targetX);
targetY += biasStrength * sign(lastY - targetY);
}
newElementInFocus = rootElem.findElementNearest(
targetX, targetY,
left, top, right, bottom
);
}
if (newElementInFocus != undefined) {
setFocusedElement(newElementInFocus);
}
}
}
};
}
/// The draw colour used for unfocused elements when rendering the debug
/// overlay.
///
/// @return {Constant.Color}
#macro WUI_ELEMENT_DEBUG_COLOUR c_red
/// The draw colour used for navigable elements when rendering the debug
/// overlay.
///
/// @return {Constant.Color}
#macro WUI_ELEMENT_DEBUG_COLOUR_NAVIGABLE c_green
/// The draw colour used for focused elements when rendering the debug overlay.
///
/// @return {Constant.Color}
#macro WUI_ELEMENT_DEBUG_COLOUR_FOCUSED c_yellow
/// The single generic base element type for menus, menu elements, and menu
/// views of the Witchcraft UI system.
///
/// @param {Struct} [schema]
/// A struct containing the following configurable properties for elements:
///
/// - `"padding"`: configures `WUIElement::__padding__`, defaults to 0.
///
/// - `"width"`: configures `WUIElement::__width__`.
///
/// - `"height"`: configures `WUIElement::__height__`.
///
/// - `"offsetX"`: configures `WUIElement::__offsetX__`, defaults to 0.
///
/// - `"offsetY"`: configures `WUIElement::__offsetY__`, defaults to 0.
///
/// - `"children"`: configures `WUIElement::__children__`.
///
/// - `"navLeft"`: the navigation override when moving the keyboard cursor
/// to the left.
///
/// - `"navUp"`: the navigation override when moving the keyboard cursor
/// upwards.
///
/// - `"navRight"`: the navigation override when moving the keyboard cursor
/// to the right.
///
/// - `"navDown"`: the navigation override when moving the keyboard cursor
/// downwards.
function WUIElement(schema = { }) constructor {
/// The number of pixels of padding between the full width of the element
/// and its inner elements.
///
/// @return {Real}
self.__padding__ = schema[$ "padding"] ?? 0;
/// The full width of the element in pixels. If set to `undefined`, the
/// width of the element will be estimated using the width of its
/// inner elements.
///
/// @return {Real}
self.__width__ = schema[$ "width"];
/// The full height of the element in pixels. If set to `undefined`, the
/// height of the element will be estimated using the height of its
/// inner elements.
///
/// @return {Real}
self.__height__ = schema[$ "height"];
/// An additional offset applied to the element in the X direction after
/// its world position has been calculated.
///
/// @return {Real}
self.__offsetX__ = schema[$ "offsetX"] ?? 0;
/// An additional offset applied to the element in the Y direction after
/// its world position has been calculated.
///
/// @return {Real}
self.__offsetY__ = schema[$ "offsetY"] ?? 0;
/// An array of inner elements, inheriting the world position of their
/// parent element.
///
/// @return {Array<Struct.WUIElement>}
self.__children__ = schema[$ "children"] ?? [];
/// The navigation override when moving the keyboard cursor to the left.
///
/// @return {Struct.WUIElement}
self.__navLeft__ = schema[$ "navLeft"];
/// The navigation override when moving the keyboard cursor upwards.
///
/// @return {Struct.WUIElement}
self.__navUp__ = schema[$ "navUp"];
/// The navigation override when moving the keyboard cursor to the right.
///
/// @return {Struct.WUIElement}
self.__navRight__ = schema[$ "navRight"];
/// The navigation override when moving the keyboard cursor downwards.
///
/// @return {Struct.WUIElement}
self.__navDown__ = schema[$ "navDown"];
/// Whether this element is navigable by an instance of `WUINavigator`.
///
/// If set to `false`, this element will ignore any input events.
///
/// @return {Bool}
self.__navigable__ = false;
/// Whether this element is visible, both when navigating and drawing.
///
/// If set to `false`, this element will not be drawn and will ignore
/// any input events.
self.__visible__ = true;
/// An event raised for this element when calling the
/// `WUIElement::recalculateWorldPosition` method. It is used to give a
/// specific layout to the inner elements.
///
/// It accepts a single parameter:
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evUpdateInnerLayout__ = function (elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evUpdateInnerLayout__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::recalculateWorldSize` method. It will be raised only when
/// `WUIElement::__width__` is `undefined`, and can be used to override the
/// inference behaviour or dynamically shrink and grow the width of the
/// element.
///
/// It accepts a single parameter:
///
/// - `"elem"`: a reference to the element itself.
///
/// It should also return a positive real number representing the inner
/// width of the element.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evGetInnerWidth__ = function (elem) {
/// return 60;
/// };
/// ```
///
/// @return {Function}
self.__evGetInnerWidth__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::recalculateWorldSize` method. It will be raised only when
/// `WUIElement::__height__` is `undefined`, and can be used to override the
/// inference behaviour or dynamically shrink and grow the height of the
/// element.
///
/// It accepts a single parameter:
///
/// - `"elem"`: a reference to the element itself.
///
/// It should also return a positive real number representing the inner
/// height of the element.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evGetInnerHeight__ = function (elem) {
/// return 30;
/// };
/// ```
///
/// @return {Function}
self.__evGetInnerHeight__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::findElementAtPosition` method. It can be used to give
/// elements with abnormal shapes a more precise collision check.
///
/// It accepts three parameters:
///
/// - `"posX"`: the position of the point to check for a collision with,
/// in the X direction.
///
/// - `"posY"`: the position of the point to check for a collision with,
/// in the Y direction.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evCheckCollision__ = function (posX, posY, elem) {
/// // ...
/// };
/// ```
///
/// @return {Function}
self.__evCheckCollision__ = undefined;
/// An event raised for this element when calling the `WUIElement::draw`
/// method. It is raised before all other draw events, and can be used to
/// set a clipping shader or some other pre-draw configuration.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::draw` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evDrawBegin__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evDrawBegin__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::drawDebug` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::drawDebug` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evDrawDebug__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evDrawDebug__ = undefined;
/// An event raised for this element when calling the `WUIElement::draw`
/// method. It is raised immediately after the
/// `WUIElement::__evDrawBegin__` event, but prior to drawing any of the
/// inner elements.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::draw` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evDraw__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evDraw__ = undefined;
/// An event raised for this element when calling the `WUIElement::draw`
/// method. It is raised after all other draw events, and can be used to
/// finalise any drawing configurations made in the
/// `WUIElement::__evDrawBegin__` event.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::draw` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evDrawEnd__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evDrawEnd__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::invokeEventPressed` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::invokeEventPressed` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evInputPressed__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evInputPressed__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::invokeEventDrag` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::invokeEventDrag` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evInputDrag__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evInputDrag__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::invokeEventPressed` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::invokeEventPressed` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evInputReleased__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evInputReleased__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::tryMove` method.
///
/// It accepts two parameters:
///
/// - `"dist"`: the non-zero distance in the X direction to move.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evMoveX__ = function (dist, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evMoveX__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::tryMove` method.
///
/// It accepts two parameters:
///
/// - `"dist"`: the non-zero distance in the Y direction to move.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evMoveY__ = function (dist, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evMoveY__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::invokeEventEnter` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::invokeEventEnter` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evEnter__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evEnter__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::invokeEventExit` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::invokeEventEnter` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evExit__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evExit__ = undefined;
/// An event raised for this element when calling the
/// `WUIElement::invokeEventStep` method.
///
/// It accepts two parameters:
///
/// - `"nav"`: a reference to the `WUINavigator` passed into the
/// `WUIElement::invokeEventStep` call.
///
/// - `"elem"`: a reference to the element itself.
///
/// @example
/// ```
/// var elem = new WUIElement();
/// elem.__evStep__ = function (nav, elem) { /* ... */ };
/// ```
///
/// @return {Function}
self.__evStep__ = undefined;
/// The world position of the top-left corner of this element in the
/// X direction.
///
/// @return {Real}
self.worldX = 0;
/// The world position of the top-left corner of this element in the
/// Y direction.
///
/// @return {Real}
self.worldY = 0;
/// The full width of this element.
///
/// Will either be `WUIElement::__width__` or estimated from the width
/// of the inner elements and `WUIElement::__padding__`.
///
/// @return {Real}
self.worldWidth = self.__width__ ?? 0;
/// The full height of this element.
///
/// Will either be `WUIElement::__height__` or estimated from the height
/// of the inner elements and `WUIElement::__padding__`.
///
/// @return {Real}
self.worldHeight = self.__height__ ?? 0;
/// The world position of the top-left corner of this element in the
/// X direction, plus padding:
/// ```
/// worldInnerX = worldX + __padding__
/// ```
///
/// @return {Real}
self.worldInnerX = self.worldX;
/// The world position of the top-left corner of this element in the
/// Y direction, plus padding:
/// ```
/// worldInnerY = worldY + __padding__
/// ```
///
/// @return {Real}
self.worldInnerY = self.worldY;
/// The inner width of this element:
/// ```
/// worldInnerWidth = worldWidth - 2 * __padding__
/// ```
///
/// @return {Real}
self.worldInnerWidth = self.worldWidth - 2 * self.__padding__;
/// The inner height of this element:
/// ```
/// worldInnerHeight = worldHeight - 2 * __padding__
/// ```
///
/// @return {Real}
self.worldInnerHeight = self.worldHeight - 2 * self.__padding__;
/// Recursively updates the `WUIElement::worldWidth`,
/// `WUIElement::worldHeight`, `WUIElement::worldInnerWidth`, and
/// `WUIElement::worldInnerHeight` properties of this element and its
/// children.
static recalculateWorldSize = function () {
var maxChildWidth = 0;
var maxChildHeight = 0;
for (var i = array_length(__children__) - 1; i >= 0; i -= 1) {
var child = __children__[i];
child.recalculateWorldSize();
maxChildWidth = max(maxChildWidth, child.worldWidth);
maxChildHeight = max(maxChildHeight, child.worldHeight);
}
worldWidth = __width__;
if (worldWidth == undefined) {
worldWidth = maxChildWidth;
if (__evGetInnerWidth__ != undefined) {
worldWidth = __evGetInnerWidth__(self);
}
worldWidth += __padding__ * 2;
}
worldWidth = floor(worldWidth);
worldInnerWidth = worldWidth - __padding__ * 2;
worldHeight = __height__;
if (worldHeight == undefined) {
worldHeight = maxChildHeight;
if (__evGetInnerHeight__ != undefined) {
worldHeight = __evGetInnerHeight__(self);
}
worldHeight += __padding__ * 2;
}
worldHeight = floor(worldHeight);
worldInnerHeight = worldHeight - __padding__ * 2;
};
/// Recursively updates the `WUIElement::worldX`, `WUIElement::worldY`,
/// `WUIElement::worldInnerX`, and `WUIElement::worldInnerY` properties of
/// this element and its children.
///
/// @param {Real} posX
/// The position, in the X direction, to anchor this element to.
///
/// @param {Real} posY
/// The position, in the Y direction, to anchor this element to.
///
/// @param {Real} [alignX]
/// The alignment, in the X direction, of this element relative to the
/// anchor position. 0 means align left, 1 means align right. Defaults
/// to 0.
///
/// @param {Real} [alignY]
/// The alignment, in the Y direction, of this element relative to the
/// anchor position. 0 means align top, 1 means align bottom. Defaults
/// to 0.
static recalculateWorldPosition = function (posX, posY, alignX = 0, alignY = 0) {
worldX = floor(posX - lerp(0, worldWidth, alignX) + __offsetX__);
worldY = floor(posY - lerp(0, worldHeight, alignY) + __offsetY__);
worldInnerX = worldX + __padding__;
worldInnerY = worldY + __padding__;
if (__evUpdateInnerLayout__ == undefined) {
for (var i = array_length(__children__) - 1; i >= 0; i -= 1) {
var child = __children__[i];
child.recalculateWorldPosition(worldInnerX, worldInnerY, 0, 0);
}
} else {
__evUpdateInnerLayout__(self);
}
};
/// Calculates the smallest distance of this element from a point.
///
/// @param {Real} posX
/// The position of the point to check in the X direction.
///
/// @param {Real} posY
/// The position of the point to check in the Y direction.
///
/// @return {Real}
static distanceToPosition = function (posX, posY) {
var cornerX = clamp(posX, worldX, worldX + worldWidth);
var cornerY = clamp(posY, worldY, worldY + worldHeight);
return point_distance(posX, posY, cornerX, cornerY);
};
/// Searches for a visible, navigable element at some precise location.
/// Returns `undefined` if no element, or inner element was found.
///
/// @param {Real} posX
/// The position of the point to check in the X direction.
///
/// @param {Real} posY
/// The position of the point to check in the Y direction.
///
/// @return {Struct.WUIElement}
static findElementAtPosition = function (posX, posY) {
if (!__visible__) {
return undefined;
}
var collision = point_in_rectangle(
posX, posY,
worldX, worldY,
worldX + worldWidth, worldY + worldHeight
);
if (collision && __evCheckCollision__ != undefined) {
collision = __evCheckCollision__(posX, posY, self);
}
if (!collision) {
return undefined;
}
var result = undefined;
// iterate forwards to avoid through-click
var n = array_length(__children__);
for (var i = 0; i < n && result == undefined; i += 1) {
var child = __children__[i];
result = child.findElementAtPosition(posX, posY);
}
result ??= self;
return result.__navigable__ ? result : undefined;
};
/// Searches for the nearest visible, navigable element to a point.
/// Returns `undefined` if no element, or inner element was found.
///
/// @param {Real} posX
/// The position of the point to check in the X direction.
///
/// @param {Real} posY
/// The position of the point to check in the Y direction.