-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.lua
1092 lines (855 loc) · 33 KB
/
Node.lua
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
local class = require "com.class"
---@class Node
---@overload fun(data, parent):Node
local Node = class:derive("Node")
local Vec2 = require("Vector2")
local PropertyList = require("PropertyList")
local Box = require("Widgets.Box")
local Button = require("Widgets.Button")
local Canvas = require("Widgets.Canvas")
local InputText = require("Widgets.InputText")
local NineSprite = require("Widgets.NineSprite")
local Text = require("Widgets.Text")
local TitleDigit = require("Widgets.TitleDigit")
---Creates a new UI Node.
---@param data table The node data.
---@param parent Node? The parent node.
function Node:new(data, parent)
self.parent = parent
self.PROPERTY_LIST = {
{name = "Name", key = "name", type = "string", defaultValue = "ERROR", disabledIfControlled = true},
{name = "Position", key = "pos", type = "Vector2", defaultValue = Vec2()},
{name = "Align", key = "align", type = "align", defaultValue = _ALIGNMENTS.topLeft},
{name = "Parent Align", key = "parentAlign", type = "align", defaultValue = _ALIGNMENTS.topLeft},
{name = "Visible", key = "visible", type = "boolean", defaultValue = true},
{name = "Canvas Input Mode", key = "canvasInputMode", type = "boolean", defaultValue = false},
{name = "Shortcut", key = "shortcut", type = "shortcut", nullable = true},
{name = "Signal On Click", key = "signalOnClick", type = "string", nullable = true}
}
self.properties = PropertyList(self.PROPERTY_LIST, data)
self.WIDGET_TYPES = {
none = {constructor = nil, defaultName = "Node", icon = _IMAGES.widget_none},
box = {constructor = Box, defaultName = "Box", icon = _IMAGES.widget_box},
button = {constructor = Button, defaultName = "Button", icon = _IMAGES.widget_button},
canvas = {constructor = Canvas, defaultName = "Canvas", icon = _IMAGES.widget_canvas},
input_text = {constructor = InputText, defaultName = "InputText", icon = _IMAGES.widget_none},
["9sprite"] = {constructor = NineSprite, defaultName = "NineSprite", icon = _IMAGES.widget_ninesprite},
text = {constructor = Text, defaultName = "Text", icon = _IMAGES.widget_text},
["@titleDigit"] = {constructor = TitleDigit, defaultName = "TitleDigit", icon = _IMAGES.widget_titledigit}
}
self.type = "none"
self.clicked = false
self.onClick = nil
self.disabled = false
self.children = {}
self.deleteIndex = nil
self:deserialize(data)
end
---Returns the given property of this Node.
---@param key string The property key.
---@return any?
function Node:getProp(key)
return self.properties:getValue(key)
end
---Sets the given property of this Node to a given value.
---@param key string The property key.
---@param value any? The property value.
function Node:setProp(key, value)
self.properties:setValue(key, value)
end
---Returns the given property base of this Node.
---@param key string The property key.
---@return any?
function Node:getPropBase(key)
return self.properties:getBaseValue(key)
end
---Sets the given property base of this Node to a given value.
---@param key string The property key.
---@param value any? The property value.
function Node:setPropBase(key, value)
self.properties:setBaseValue(key, value)
end
---Resets all of the properties of this Node, its Widget, and all its children to the base values.
function Node:resetProperties()
self.properties:reset()
if self.widget and self.widget.properties then
self.widget.properties:reset()
end
for i, child in ipairs(self.children) do
child:resetProperties()
end
end
---Returns the name of this Node.
function Node:getName()
return self:getProp("name")
end
---Renames this Node. Returns `true` on success.
---@param name string The new name.
---@return boolean
function Node:setName(name)
-- Controlled Nodes cannot be renamed.
if self:isControlled() then
return false
end
self:setPropBase("name", name)
return true
end
---If another sibling of this Node has the same name, this function will change the name of this Node by adding a suffix of the form `#2`, `#3`, `#4`, etc.
---Otherwise, this Node's name will remain unchanged.
function Node:ensureUniqueName()
if not self.parent then
return
end
local prop = self.properties:getValues()
-- Check if any other Node with this name exists.
local found = false
for i, child in ipairs(self.parent.children) do
if child ~= self and child.name == prop.name then
-- We've found a sibling that has the same name as us!
found = true
break
end
end
-- If this Node's name is unique, we don't need to do anything.
if not found then
return
end
local nameBase = prop.name
local suffixNumber = 2
-- Check if we've got a suffix already.
local split = _Utils.strSplit(prop.name, "#")
if #split > 1 then
local n = tonumber(split[#split])
if n then
-- We've got a proper suffix.
split[#split] = nil
nameBase = table.concat(split, "#")
suffixNumber = n + 1
end
end
-- Now, try our new name and increment each time we find a sibling.
local newName
repeat
newName = nameBase .. "#" .. tostring(suffixNumber)
suffixNumber = suffixNumber + 1
until not self.parent:findChildByName(newName)
-- When we finally find it, set it as our new name.
prop.name = newName
end
---Returns the local position of this Node, which is relative to its parent's position.
---@return Vector2
function Node:getPos()
return self:getProp("pos")
end
---Sets the local position of this Node, which is relative to its parent's position.
---@param pos Vector2 The new local position to be set.
function Node:setPos(pos)
self:setPropBase("pos", pos)
end
---Returns the global position of this Node, i.e. the actual position (top left corner) after factoring in all parents' modifiers.
---@return Vector2
function Node:getGlobalPos()
local prop = self.properties:getValues()
return prop.pos - ((self:getSize() - 1) * prop.align):ceil() + self:getParentAlignPos()
end
---Returns the global position of this Node, which has not been adjusted for the local widget alignment.
---@return Vector2
function Node:getGlobalPosWithoutLocalAlign()
return self:getProp("pos") + self:getParentAlignPos()
end
---Returns the Node's parent anchor. If the Node does not have a parent, this function will return `(0, 0)`.
---@return Vector2
function Node:getParentAlignPos()
if self.parent then
return self.parent:getGlobalPos() + ((self.parent:getSize() - 1) * self:getProp("parentAlign")):ceil()
end
return Vec2()
end
---Returns the size of this Node's widget, or `(1, 1)` if it contains no widget.
---@return Vector2
function Node:getSize()
if self.widget then
return self.widget:getSize()
end
return Vec2(1)
end
---Sets the size of this Node's widget. Not all widgets support resizing. Check `:isResizable()` to see if you can.
---@param size Vector2 The new size of this Node's widget.
function Node:setSize(size)
if self.widget then
self.widget:setSize(size)
end
end
---Returns whether this Node's widget is resizable. If so, squares will appear around it when selected.
---@return boolean
function Node:isResizable()
if self.widget then
return self:hasProperty("size")
end
return false
end
---Returns the position of the resize handle for this Node's widget.
---@param id integer 1 to 8: top left, top middle, top right, middle left, middle right, bottom left, bottom middle, and you guessed it - bottom right.
---@return Vector2
function Node:getResizeHandlePos(id)
local pos = self:getGlobalPos()
local size = self:getSize()
if id == 1 then
return Vec2(pos.x - 4, pos.y - 4)
elseif id == 2 then
return Vec2(pos.x + size.x / 2 - 1, pos.y - 4)
elseif id == 3 then
return Vec2(pos.x + size.x + 1, pos.y - 4)
elseif id == 4 then
return Vec2(pos.x - 4, pos.y + size.y / 2 - 1)
elseif id == 5 then
return Vec2(pos.x + size.x + 1, pos.y + size.y / 2 - 1)
elseif id == 6 then
return Vec2(pos.x - 4, pos.y + size.y + 1)
elseif id == 7 then
return Vec2(pos.x + size.x / 2 - 1, pos.y + size.y + 1)
elseif id == 8 then
return Vec2(pos.x + size.x + 1, pos.y + size.y + 1)
end
error(string.format("Invalid resize handle ID: %s (expected 1..8)", id))
end
---Returns this Node's icon based on its Widget's type, for use in editors.
---@return Image
function Node:getIcon()
return self.WIDGET_TYPES[self.type].icon
end
---Returns whether the canvas input mode is enabled on this Node or its hierarchy.
---@return boolean
function Node:isCanvasInputModeEnabled()
if self.parent then
return self.parent:isCanvasInputModeEnabled()
end
return self:getProp("canvasInputMode")
end
---Returns the current alignment of the Node.
---@return Vector2
function Node:getAlign()
return self:getProp("align")
end
---Sets the alignment of this Node.
---@param align Vector2 The new Node alignment.
--- - `(0, 0)` aligns to top left.
--- - `(1, 1)` aligns to bottom right.
--- - `(0.5, 0.5)` aligns to the center.
--- - Any combination is available, including going out of bounds.
function Node:setAlign(align)
self:setPropBase("align", align)
end
---Returns the current parental alignment of the Node.
---@return Vector2
function Node:getParentAlign()
return self:getProp("parentAlign")
end
---Sets the parental alignment of this Node.
---@param parentAlign Vector2 The new Node alignment.
--- - `(0, 0)` aligns to top left.
--- - `(1, 1)` aligns to bottom right.
--- - `(0.5, 0.5)` aligns to the center.
--- - Any combination is available, including going out of bounds.
function Node:setParentAlign(parentAlign)
self:setPropBase("parentAlign", parentAlign)
end
---Sets an on-click function (or resets it, if no argument is provided).
---@param f function? The function to be executed if this Node is clicked.
function Node:setOnClick(f)
self.onClick = f
end
---Fires the callback specified in the `onClick` and `signalOnClick` fields if these fields have been defined.
---If `onClick` is defined, it will be executed without any parameters.
---If `signalOnClick` is defined, it will send a signal, for now hardcoded to the `_OnSignal(signalOnClick)` call.
function Node:click()
if self.onClick then
self.onClick()
end
if self:getProp("signalOnClick") then
_OnSignal(self:getProp("signalOnClick"))
end
end
---Returns the list of properties on this Node. This list is always the same.
---This function does NOT return properties belonging to its Widget. For that, call `node.widget:getPropertyList()`.
---Make sure to wrap this call as the node or the function might not exist!
---@return table
function Node:getPropertyList()
return self.PROPERTY_LIST
end
---Returns `true` if this Node has a widget and that widget has a property of the given key.
---@param key string The key to search for.
---@return boolean
function Node:hasProperty(key)
if not self.widget or not self.widget.getPropertyList then
return false
end
local properties = self.widget:getPropertyList()
for i, property in ipairs(properties) do
if property.key == key then
return true
end
end
return false
end
---Returns whether this Node is hovered.
function Node:isHovered()
return self:hasPixel(self:isCanvasInputModeEnabled() and _MouseCPos or _MousePos)
end
---Returns the ID of the hovered resize handle of this Node's widget, if any is hovered.
---If none of the resize handles are hovered, returns `nil`.
---@return integer?
function Node:getHoveredResizeHandleID()
if not self:isResizable() then
return nil
end
for i = 1, 8 do
local pos = self:getResizeHandlePos(i)
if _Utils.isPointInsideBox(self:isCanvasInputModeEnabled() and _MouseCPos or _MousePos, pos, Vec2(3)) then
return i
end
end
return nil
end
---Returns whether this Node or any of the Nodes up the tree is disabled.
---
---Disabled Nodes can still be hovered, but their `onClick` callbacks will not fire if the mouse button or a keyboard shortcut has been pressed.
---They can have their own graphics used in Widgets.
---@return boolean
function Node:isDisabled()
if self.parent then
return self.parent:isDisabled() or self.disabled
end
return self.disabled
end
---Sets whether this Node should be disabled.
---@param disabled boolean Whether this Node should be disabled.
function Node:setDisabled(disabled)
self.disabled = disabled
end
---Returns whether this Node is visible, i.e. all of the Nodes in this Node's hierarchy have the visible flag set.
---@return boolean
function Node:isVisible()
if self.parent then
return self.parent:isVisible() and self:getProp("visible")
end
return self:getProp("visible")
end
---Sets whether this Node (and all its children!) should be visible.
---@param visible boolean Whether this Node should be visible. If a Node is not visible, it cannot be seen, including all its children.
function Node:setVisible(visible)
self:setPropBase("visible", visible)
end
---Returns any Text this Node's widget contains. Works only with `text` widgets. Returns `nil` otherwise.
---@return string?
function Node:getText()
if self.type == "text" then
return self.widget:getProp("text")
end
end
---Sets the given text on this Node's widget. Works only with `text` widgets.
---@param text string The text to be set on this Node's widget.
function Node:setText(text)
assert(type(text) == "string", string.format("Cannot set the text to a non-string: %s!", text))
if self.type == "text" then
self.widget:setProp("text", text)
end
end
---Returns any color this Node's widget contains. Works only with `text` and `box` widgets. Returns `nil` otherwise.
---@return Color?
function Node:getColor()
if self.type == "text" or self.type == "box" then
return self.widget:getProp("color")
end
end
---Sets the given color on this Node's widget. Works only with `text` and `box` widgets.
---@param color Color The color to be set on this Node's widget.
function Node:setColor(color)
if self.type == "text" or self.type == "box" then
self.widget:setProp("color", color)
end
end
---Returns whether this Node or any of its children is hovered.
function Node:isHoveredWithChildren()
if self:isHovered() then
return true
end
for i, child in ipairs(self.children) do
if child:isHoveredWithChildren() then
return true
end
end
return false
end
---Returns whether this Node is controlled.
---By a Controlled Node we mean a Node of which at least one of their parents up the hierarchy is a Controller Node.
---Controlled Nodes are limited: you cannot change their name, add children to them or move them outside of their current position in hierarchy.
---These limits are not enforced by this class; instead, the Editor should provide sufficient safeguards so that this does not happen.
---Renaming or changing the hierarchy of controlled Nodes will have unexpected consequences... mostly. You can expect that the program will crash!
---@return boolean
function Node:isControlled()
if not self.parent then
return false
end
if self.parent and self.parent.isController then
return true
end
return self.parent:isControlled()
end
---Inserts the provided Node as a child of this Node.
---If the provided Node is already integrated into another UI tree (has a parent), it is removed from that parent - the Node is effectively moved.
---Returns `true` on success, `false` if the tree would form a cycle or when the widget would be parented to itself.
---@param node Node The node to be added.
---@param index number? The index specifying where in the hierarchy the Node should be located. By default, it is inserted as the last element (on the bottom).
---@return boolean
function Node:addChild(node, index)
-- We cannot parent a node to itself or its own child, or else we will get stuck in a loop!!!
if node == self or node:findChild(self) then
return false
end
-- Controller Nodes cannot have their structure changed. And we must not accept any Controlled Node that's trying to run away from their controller, either.
if self:isControlled() or self.isController or node:isControlled() then
return false
end
-- Resolve linkages.
if node.parent then
node:removeSelf()
end
node.parent = self
-- Add as a child.
if index then
table.insert(self.children, index, node)
else
table.insert(self.children, node)
end
return true
end
---Removes a child Node by its reference. Returns `true` on success.
---@param node Node The node to be removed.
---@return boolean
function Node:removeChild(node)
-- Controlled Nodes cannot be removed.
if node:isControlled() then
return false
end
local index = self:getChildIndex(node)
if index then
table.remove(self.children, index)
node.deleteIndex = index
return true
end
return false
end
---Removes itself from a parent Node. If succeeded, returns `true`.
---If this Node has no parent, this function will fail and return `false`.
---@return boolean
function Node:removeSelf()
if not self.parent then
return false
end
return self.parent:removeChild(self)
end
---Restores this Node to the tree after its deletion.
function Node:restoreSelf()
if not self.deleteIndex then
return
end
self.parent:addChild(self, self.deleteIndex)
self.deleteIndex = nil
end
---Moves a child Node up in the hierarchy (to the front), by its reference.
---Returns `true` if the node has been successfully moved, `false` if the node could not be found or is already the frontmost node.
---@param node Node The node to be moved up.
---@return boolean
function Node:moveChildUp(node)
-- Controlled Nodes cannot be moved in their hierarchy.
if node:isControlled() then
return false
end
local index = self:getChildIndex(node)
if not index then
return false
end
return self:moveChildToPosition(node, index - 1)
end
---Moves itself up in the hierarchy of the parent Node.
---If this Node has no parent, this function will fail and return `false`. If the operation succeeds, returns `true`.
---@return boolean
function Node:moveSelfUp()
if not self.parent then
return false
end
return self.parent:moveChildUp(self)
end
---Moves a child Node down in the hierarchy (to the back), by its reference.
---Returns `true` if the node has been successfully moved, `false` if the node could not be found or is already the backmost node.
---@param node Node The node to be moved down.
---@return boolean
function Node:moveChildDown(node)
-- Controlled Nodes cannot be moved in their hierarchy.
if node:isControlled() then
return false
end
local index = self:getChildIndex(node)
if not index then
return false
end
return self:moveChildToPosition(node, index + 1)
end
---Moves itself down in the hierarchy of the parent Node.
---If this Node has no parent, this function will fail and return `false`. If the operation succeeds, returns `true`.
---@return boolean
function Node:moveSelfDown()
if not self.parent then
return false
end
return self.parent:moveChildDown(self)
end
---Moves a child Node to the top of the hierarchy (to the front), by its reference.
---Returns `true` if the node has been successfully moved, `false` if the node could not be found or is already the frontmost node.
---@param node Node The node to be moved to the top.
---@return boolean
function Node:moveChildToTop(node)
return self:moveChildToPosition(node, 1)
end
---Moves itself to the top of the hierarchy of the parent Node.
---If this Node has no parent, this function will fail and return `false`. If the operation succeeds, returns `true`.
---@return boolean
function Node:moveSelfToTop()
if not self.parent then
return false
end
return self.parent:moveChildToTop(self)
end
---Moves a child Node to the bottom of the hierarchy (to the front), by its reference.
---Returns `true` if the node has been successfully moved, `false` if the node could not be found or is already the backmost node.
---@param node Node The node to be moved to the bottom.
---@return boolean
function Node:moveChildToBottom(node)
return self:moveChildToPosition(node, #self.children)
end
---Moves itself to the bottom of the hierarchy of the parent Node.
---If this Node has no parent, this function will fail and return `false`. If the operation succeeds, returns `true`.
---@return boolean
function Node:moveSelfToBottom()
if not self.parent then
return false
end
return self.parent:moveChildToBottom(self)
end
---Moves a child Node to the given position in the hierarchy by its reference.
---Returns `true` if the node has been successfully moved, `false` if the node could not be found, is already at the given position or the position is out of bounds.
---@param node Node The node to be moved.
---@param position integer The new Node position. `1` is the top, `#self.children` is the bottom.
---@return boolean
function Node:moveChildToPosition(node, position)
-- Controlled Nodes cannot be moved in their hierarchy.
if node:isControlled() then
return false
end
-- Fail if the position is illegal.
if position < 1 or position > #self.children then
return false
end
local i = self:getChildIndex(node)
-- Fail if the given node is not our child or is already at the right position.
if not i or i == position then
return false
end
table.remove(self.children, i)
table.insert(self.children, position, node)
return true
end
---Moves itself to the given position if the hierarchy of the parent Node.
---If this Node has no parent, this function will fail and return `false`. If the operation succeeds, returns `true`.
---@return boolean
function Node:moveSelfToPosition(position)
if not self.parent then
return false
end
return self.parent:moveChildToPosition(self, position)
end
---Returns the index of a child contained in this Node. If the given node is not a child of this node, returns `nil`.
---@param node Node The node to be looked for.
---@return integer?
function Node:getChildIndex(node)
return _Utils.getKeyInTable(self.children, node)
end
---Returns the index of this Node in its parent. If this Node has no parent, returns `nil`.
---@return integer?
function Node:getSelfIndex()
if not self.parent then
return nil
end
return self.parent:getChildIndex(self)
end
---Returns `true` if the given pixel position is inside of this Node's bounding box.
---@param pos Vector2 The position to be checked.
---@return boolean
function Node:hasPixel(pos)
return _Utils.isPointInsideBox(pos, self:getGlobalPos(), self:getSize())
end
---Returns `true` if this Node has at least one child.
---@return boolean
function Node:hasChildren()
return #self.children > 0
end
---Returns the first encountered child by reference (recursively), or `nil` if it is not found.
---@param node Node The instance of the child to be found.
---@return Node?
function Node:findChild(node)
for i, child in ipairs(self.children) do
if child == node then
return child
end
local potentialResult = child:findChild(node)
if potentialResult then
return potentialResult
end
end
end
---Returns the first encountered child of the provided name (recursively), or `nil` if it is not found.
---@param name string The name of the child to be found.
---@return Node?
function Node:findChildByName(name)
for i, child in ipairs(self.children) do
if child:getProp("name") == name then
return child
end
local potentialResult = child:findChildByName(name)
if potentialResult then
return potentialResult
end
end
end
---Returns the first encountered child that contains the provided position (recursively), or `nil` if it is not found.
---@param pos Vector2 The position to be checked.
---@return Node?
function Node:findChildByPixel(pos)
for i, child in ipairs(self.children) do
if child:hasPixel(pos) then
return child
end
local potentialResult = child:findChildByPixel(pos)
if potentialResult then
return potentialResult
end
end
end
---Returns the first encountered child that contains the provided position (recursively, depth first), or `nil` if it is not found.
---@param pos Vector2 The position to be checked.
---@param ignoreControlledNodes boolean? If set, controlled nodes will not be returned by this function.
---@param ignoreInvisibleNodes boolean? If set, invisible nodes will not be returned by this function.
---@return Node?
function Node:findChildByPixelDepthFirst(pos, ignoreControlledNodes, ignoreInvisibleNodes)
for i, child in ipairs(self.children) do
local potentialResult = child:findChildByPixelDepthFirst(pos, ignoreControlledNodes, ignoreInvisibleNodes)
if potentialResult then
return potentialResult
end
if child:hasPixel(pos) and (not ignoreControlledNodes or not child:isControlled()) and (not ignoreInvisibleNodes or child:isVisible()) then
return child
end
end
end
---Returns the last encountered child that contains the provided position (recursively, depth first), or `nil` if it is not found.
---@param pos Vector2 The position to be checked.
---@return Node?
function Node:findChildByPixelDepthFirstReverse(pos)
local result = nil
for i, child in ipairs(self.children) do
if child:hasPixel(pos) then
result = child
end
local potentialResult = child:findChildByPixelDepthFirst(pos)
if potentialResult then
result = potentialResult
end
end
return result
end
---Updates this Node's widget, if it exists, and all its children.
---@param dt number Time delta, in seconds.
function Node:update(dt)
self.properties:update(dt)
if self.widget then
self.widget:update(dt)
end
for i, child in ipairs(self.children) do
child:update(dt)
end
end
---Draws this Node's widget, if it exists, and all of its children.
---The draw order is as follows:
--- - First, the node itself is drawn.
--- - Then, its children are drawn in *reverse* order (from bottom to the top), so that the first entry in the hierarchy is the topmost one.
--- - If any child has its own children, draw them immediately after that child has been drawn.
---If the Node is invisible, the call immediately returns, resulting in neither this nor any children's widgets being drawn.
function Node:draw()
if not self:getProp("visible") then
return
end
if not self.isCanvas then
if self.widget then
self.widget:draw()
end
for i = #self.children, 1, -1 do
local child = self.children[i]
child:draw()
end
else
-- Canvases are treated differently.
self.widget:activate()
for i = #self.children, 1, -1 do
local child = self.children[i]
child:draw()
end
self.widget:draw()
end
end
---Draws this Node's bounding box, or a crosshair if this Node has no widget.
---The borders are all inclusive.
function Node:drawHitbox()
local pos = self:getGlobalPos()
love.graphics.setLineWidth(1)
if self.widget then
local size = self:getSize()
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle("line", pos.x + 0.5, pos.y + 0.5, size.x - 1, size.y - 1)
else
love.graphics.setColor(0.5, 0.5, 0.5)
self:drawCrosshair(pos, 2)
end
end
---Draws this Node's bounding box in a different color (or a crosshair if this Node has no widget), and some indicators around.
---The borders are all inclusive.
function Node:drawSelected()
local pos = self:getGlobalPos()
love.graphics.setLineWidth(1)
if self.widget then
local size = self:getSize()
love.graphics.setColor(0, 1, 1)
love.graphics.rectangle("line", pos.x + 0.5, pos.y + 0.5, size.x - 1, size.y - 1)
else
love.graphics.setColor(1, 1, 1)
self:drawCrosshair(pos, 2)
end
-- Draw local crosshair.
local localPos = self:getGlobalPosWithoutLocalAlign()
love.graphics.setColor(0, 0, 1)
self:drawCrosshair(localPos, 2)
-- Draw parent align crosshair.
local localPos2 = self:getParentAlignPos()
love.graphics.setColor(1, 0, 1)
self:drawCrosshair(localPos2, 2)
-- Draw a line between them.
love.graphics.setColor(0.5, 0, 1)
love.graphics.line(localPos.x, localPos.y, localPos2.x, localPos2.y)
-- Draw resizing boxes if the widget can be resized.
if self:isResizable() then
local id = self:getHoveredResizeHandleID()
for i = 1, 8 do
if i == id then
-- This handle is hovered.
love.graphics.setColor(1, 1, 1)
else
love.graphics.setColor(0, 1, 1)
end
local p = self:getResizeHandlePos(i)
love.graphics.rectangle("fill", p.x, p.y, 3, 3)
end
end
end
---Internal function which draws a crosshair.
---@param pos Vector2 The crosshair position.
---@param size number The crosshair size, in pixels.
function Node:drawCrosshair(pos, size)
pos = pos:floor() + 0.5
love.graphics.line(pos.x - size, pos.y, pos.x + size + 1, pos.y)
love.graphics.line(pos.x, pos.y - size, pos.x, pos.y + size + 1)
end
---Executed whenever a mouse button is pressed anywhere on the screen.
---@param x integer The X coordinate.
---@param y integer The Y coordinate.
---@param button integer The button that has been pressed.
---@param istouch boolean Whether the press is coming from a touch input.
---@param presses integer How many clicks have been performed in a short amount of time. Useful for double click checks.
function Node:mousepressed(x, y, button, istouch, presses)
if button == 1 and self:isHovered() and not self.disabled then
self.clicked = true
end
for i, child in ipairs(self.children) do
child:mousepressed(x, y, button, istouch, presses)
end
end