Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update node.ts setSiblingIndex support negative index #17592

Merged
merged 2 commits into from
Sep 12, 2024

Conversation

kaokei
Copy link
Contributor

@kaokei kaokei commented Aug 31, 2024

eg.
-1 equals siblings.length -1.
-2 equals siblings.length -2.
-3 equals siblings.length -3.

Re: #

Changelog

  • Support negative index in setSiblingIndex method

Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

Greptile Summary

This PR updates the setSiblingIndex method in the Node class to support negative indices for more convenient node positioning within the scene graph.

  • Modifies setSiblingIndex in cocos/scene-graph/node.ts to handle negative indices
  • Allows positioning nodes relative to the end of the sibling list (e.g. -1 for last position)
  • Affects core functionality of node positioning and hierarchy management
  • Requires careful review to ensure consistency with existing behavior
  • May impact other engine components that rely on sibling indexing

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings

cocos/scene-graph/node.ts Show resolved Hide resolved
Copy link

Interface Check Report

This pull request does not change any public interfaces !

Copy link

@kaokei, Please check the result of run test cases:

Task Details

Copy link

@kaokei, Please check the result of run test cases:

Task Details

@minggo
Copy link
Contributor

minggo commented Sep 5, 2024

It will break compatibility.

@kaokei
Copy link
Contributor Author

kaokei commented Sep 5, 2024

It will break compatibility.

@minggo
Is there something I haven't considered?
From my understanding, the original code index = index !== -1 ? index : siblings.length - 1; only accounts for scenarios where the index is a positive number or -1.
However, the new code index = index >= 0 ? index : siblings.length + index; not only supports positive numbers but also supports a wider range of negative numbers.
The new code should encompass the behavior of the old one. Could you provide some cases to illustrate where there are breaking changes?

@minggo
Copy link
Contributor

minggo commented Sep 6, 2024

My fault. I doesn't break compatibility.

And this PR just make code more clear, it doesn't support more index range. As the current implementation also supports -2 and -3.

@@ -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);
Copy link
Contributor

@dumganhar dumganhar Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it run well if passing a negative value that smaller than -siblings.length?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kaokei
Could you take a look at this question?
If index < -siblings.length, for example, it's -siblings.length - 10,
After this logic index = index >= 0 ? index : siblings.length + index;
The index will be siblings.length + (-siblings.length - 10) = -10, -10 will be a invalid index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems splice will check whether the index is in the range and do a clamp operation.

@dumganhar
Copy link
Contributor

https://github.com/cocos/cocos-engine/blob/v3.8.5/native/cocos/core/scene-graph/Node.cpp#L347

Also need to modify the cpp code for native platforms.

@kaokei
Copy link
Contributor Author

kaokei commented Sep 7, 2024

https://github.com/cocos/cocos-engine/blob/v3.8.5/native/cocos/core/scene-graph/Node.cpp#L347

Also need to modify the cpp code for native platforms.

@dumganhar
Actually, I'm not familiar with C++ code. Could you help me review if this implementation is feasible? The main change is adding logic to force the index to be greater than or equal to 0.

Copy link

github-actions bot commented Sep 7, 2024

@kaokei, Please check the result of run test cases:

Task Details

Copy link

github-actions bot commented Sep 7, 2024

@kaokei, Please check the result of run test cases:

Task Details

@dumganhar
Copy link
Contributor

@dumganhar
Actually, I'm not familiar with C++ code. Could you help me review if this implementation is feasible? The main change is adding logic to force the index to be greater than or equal to 0.

OK, I will be glad to update the cpp code later.

@dumganhar
Copy link
Contributor

@kaokei I found you have already updated the cpp code. It looks good.

@minggo minggo changed the base branch from v3.8.4 to v3.8.5 September 11, 2024 10:11
@minggo minggo changed the base branch from v3.8.5 to v3.8.4 September 11, 2024 10:12
@minggo
Copy link
Contributor

minggo commented Sep 11, 2024

@kaokei could you please send it to v3.8.5? Thanks.

@kaokei kaokei changed the base branch from v3.8.4 to v3.8.5 September 12, 2024 02:12
eg.
-1 equals siblings.length -1.
-2 equals siblings.length -2.
-3 equals siblings.length -3.
Force index=0 when index smaller than -siblings.length.
@kaokei
Copy link
Contributor Author

kaokei commented Sep 12, 2024

@kaokei could you please send it to v3.8.5? Thanks.

done. @dumganhar

@minggo
Copy link
Contributor

minggo commented Sep 12, 2024

@cocos-robot run test cases

@minggo minggo merged commit f7aa23f into cocos:v3.8.5 Sep 12, 2024
24 of 25 checks passed
Copy link

@kaokei, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL Hexa,puzzle,shield-node
ios PASS PASS FAIL Hexa,puzzle,shield-node
mac PASS PASS FAIL Hexa,puzzle,shield-node

Copy link

@kaokei, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
windows PASS PASS FAIL Hexa,puzzle,shield-node
android PASS PASS FAIL Hexa,puzzle,shield-node
wechatgame PASS PASS FAIL Hexa,puzzle,shield-node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants