Skip to content

Commit 036db92

Browse files
committed
Improvements at moveChildToIndex
1 parent ad0073c commit 036db92

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

engine/core/Scene.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,14 +529,20 @@ void Scene::moveChildToIndex(Entity entity, size_t index, bool adjustFinalPositi
529529
size_t length = lastChildIndex - entityIndex + 1;
530530

531531
if (adjustFinalPosition && (index > entityIndex)){
532-
index--;
532+
if (index > 0){ // Prevent underflow
533+
index--;
534+
}
533535
}
534536

535537
if (length == 1){
536538
transforms->moveEntityToIndex(entity, index);
537539
}else{
538-
if (index > entityIndex){
539-
index = index - length + 1;
540+
if (adjustFinalPosition && (index > entityIndex)){
541+
if (index >= length - 1) { // Prevent underflow
542+
index = index - length + 1;
543+
} else {
544+
index = 0;
545+
}
540546
}
541547
transforms->moveEntityRangeToIndex(entity, transforms->getEntity(lastChildIndex), index);
542548
}

0 commit comments

Comments
 (0)