|
| 1 | +class_name NodeUtils |
| 2 | + |
| 3 | + |
| 4 | +## remove child from node |
| 5 | +static func remove_child(node: Node, child: Node) -> void: |
| 6 | + node.remove_child(child) |
| 7 | + child.queue_free() |
| 8 | + |
| 9 | + |
| 10 | +## remove all children of given node |
| 11 | +static func remove_children(node: Node) -> void: |
| 12 | + for child: Node in node.get_children(): |
| 13 | + remove_child(node, child) |
| 14 | + |
| 15 | + |
| 16 | +## remove all children of given node with given type |
| 17 | +static func remove_children_of(node: Node, type: Variant) -> void: |
| 18 | + for child: Node in node.get_children(): |
| 19 | + if is_instance_of(child, type): |
| 20 | + remove_child(node, child) |
| 21 | + |
| 22 | + |
| 23 | +## remove child of given node at given index (allows large and negative index, looping around) |
| 24 | +static func remove_child_at(parent: Node, index: int) -> void: |
| 25 | + index = index % parent.get_child_count() |
| 26 | + var child: Node = parent.get_child(index) |
| 27 | + parent.remove_child(child) |
| 28 | + child.queue_free() |
| 29 | + |
| 30 | + |
| 31 | +## remove last child of given node |
| 32 | +static func remove_child_back(parent: Node) -> void: |
| 33 | + remove_child_at(parent, -1) |
| 34 | + |
| 35 | + |
| 36 | +## remove first child of given node |
| 37 | +static func remove_child_front(parent: Node) -> void: |
| 38 | + remove_child_at(parent, 0) |
| 39 | + |
| 40 | + |
| 41 | +## add child to given node to last spot |
| 42 | +static func add_child_back(child: Node, parent: Node) -> void: |
| 43 | + parent.add_child(child) |
| 44 | + |
| 45 | + |
| 46 | +## add child to given node to first spot |
| 47 | +static func add_child_front(child: Node, parent: Node) -> void: |
| 48 | + parent.add_child(child) |
| 49 | + parent.move_child(child, 0) |
| 50 | + |
| 51 | + |
| 52 | +## add child to given node such that children remain sorted (assumes children are sorted before add) |
| 53 | +static func add_child_sorted(child: Node, parent: Node, compare_func: Callable) -> void: |
| 54 | + parent.add_child(child) |
| 55 | + var children: Array[Node] = parent.get_children() |
| 56 | + if children.size() == 0: |
| 57 | + return |
| 58 | + var position: int = children.bsearch_custom(child, compare_func) |
| 59 | + parent.move_child(child, position) |
| 60 | + |
| 61 | + |
| 62 | +## returns active theme of control node (control nodes inheirt parent theme if theirs is null) |
| 63 | +static func get_inherited_theme(control: Node) -> Resource: |
| 64 | + var theme: Resource = null |
| 65 | + while (control != null) and ("theme" in control): |
| 66 | + theme = control.theme |
| 67 | + if theme != null: |
| 68 | + break |
| 69 | + control = control.get_parent() |
| 70 | + return theme |
0 commit comments