summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkobewi <kobewi4e@gmail.com>2022-09-10 03:54:04 +0200
committerkobewi <kobewi4e@gmail.com>2022-09-10 03:54:04 +0200
commit31e62ca827e179e3803772afb279ed995ab3019e (patch)
tree86fc0fea72f31eaee1ffac4e2af342035fa379e1
parentce6f284e5ff16794658db4ccd265b9c71f6b62b2 (diff)
Allow negative indices in move_child()
-rw-r--r--doc/classes/Node.xml2
-rw-r--r--scene/main/canvas_item.cpp2
-rw-r--r--scene/main/node.cpp9
3 files changed, 11 insertions, 2 deletions
diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml
index 92beefa5fc..fe29825a70 100644
--- a/doc/classes/Node.xml
+++ b/doc/classes/Node.xml
@@ -514,7 +514,7 @@
<param index="0" name="child_node" type="Node" />
<param index="1" name="to_position" type="int" />
<description>
- Moves a child node to a different position (order) among the other children. Since calls, signals, etc are performed by tree order, changing the order of children nodes may be useful.
+ Moves a child node to a different position (order) among the other children. Since calls, signals, etc are performed by tree order, changing the order of children nodes may be useful. If [param to_position] is negative, the index will be counted from the end.
[b]Note:[/b] Internal children can only be moved within their expected "internal range" (see [code]internal[/code] parameter in [method add_child]).
</description>
</method>
diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp
index 093e4a8cd3..05d86f77f2 100644
--- a/scene/main/canvas_item.cpp
+++ b/scene/main/canvas_item.cpp
@@ -373,7 +373,7 @@ void CanvasItem::move_to_front() {
if (!get_parent()) {
return;
}
- get_parent()->move_child(this, get_parent()->get_child_count() - 1);
+ get_parent()->move_child(this, -1);
}
void CanvasItem::set_modulate(const Color &p_modulate) {
diff --git a/scene/main/node.cpp b/scene/main/node.cpp
index f01ec99205..4de483a074 100644
--- a/scene/main/node.cpp
+++ b/scene/main/node.cpp
@@ -328,12 +328,21 @@ void Node::move_child(Node *p_child, int p_pos) {
// We need to check whether node is internal and move it only in the relevant node range.
if (p_child->_is_internal_front()) {
+ if (p_pos < 0) {
+ p_pos += data.internal_children_front;
+ }
ERR_FAIL_INDEX_MSG(p_pos, data.internal_children_front, vformat("Invalid new child position: %d. Child is internal.", p_pos));
_move_child(p_child, p_pos);
} else if (p_child->_is_internal_back()) {
+ if (p_pos < 0) {
+ p_pos += data.internal_children_back;
+ }
ERR_FAIL_INDEX_MSG(p_pos, data.internal_children_back, vformat("Invalid new child position: %d. Child is internal.", p_pos));
_move_child(p_child, data.children.size() - data.internal_children_back + p_pos);
} else {
+ if (p_pos < 0) {
+ p_pos += get_child_count(false);
+ }
ERR_FAIL_INDEX_MSG(p_pos, data.children.size() + 1 - data.internal_children_front - data.internal_children_back, vformat("Invalid new child position: %d.", p_pos));
_move_child(p_child, p_pos + data.internal_children_front);
}