From bcb32b5885c07ec3cebea98a27a78364df52a17e Mon Sep 17 00:00:00 2001 From: kaokei Date: Sat, 31 Aug 2024 15:41:31 +0800 Subject: [PATCH 1/2] Update node.ts setSiblingIndex support negative index eg. -1 equals siblings.length -1. -2 equals siblings.length -2. -3 equals siblings.length -3. --- cocos/scene-graph/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 421e1a3f827..eebc530744b 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -630,7 +630,7 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { return; } const siblings = this._parent._children; - index = index !== -1 ? index : siblings.length - 1; + index = index >= 0 ? index : siblings.length + index; const oldIndex = siblings.indexOf(this); if (index !== oldIndex) { siblings.splice(oldIndex, 1); From f3164f9c81c5a30b2868a0cef636906b84ea8e76 Mon Sep 17 00:00:00 2001 From: kaokei Date: Sat, 7 Sep 2024 13:18:22 +0800 Subject: [PATCH 2/2] Update Node.cpp setSiblingIndex support negative index Force index=0 when index smaller than -siblings.length. --- native/cocos/core/scene-graph/Node.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/native/cocos/core/scene-graph/Node.cpp b/native/cocos/core/scene-graph/Node.cpp index bc84ecf0d26..5ec8550dd2d 100644 --- a/native/cocos/core/scene-graph/Node.cpp +++ b/native/cocos/core/scene-graph/Node.cpp @@ -353,7 +353,8 @@ void Node::setSiblingIndex(index_t index) { return; } ccstd::vector> &siblings = _parent->_children; - index = index != -1 ? index : static_cast(siblings.size()) - 1; + index = index >= 0 ? index : static_cast(siblings.size()) + index; + index = index >= 0 ? index : 0; index_t oldIdx = getIdxOfChild(siblings, this); if (index != oldIdx) { if (oldIdx != CC_INVALID_INDEX) {