Skip to content

Commit

Permalink
Updated addEntityChild to change position based on parent
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Oct 23, 2024
1 parent 8377efc commit 3167f41
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 51 deletions.
96 changes: 57 additions & 39 deletions engine/core/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,59 +287,77 @@ int32_t Scene::findBranchLastIndex(Entity entity){
return currentIndex;
}

void Scene::addEntityChild(Entity parent, Entity child){
Signature parentSignature = entityManager.getSignature(parent);
Signature childSignature = entityManager.getSignature(child);

void Scene::addEntityChild(Entity parent, Entity child, bool changeTransform){
Signature signature;
signature.set(componentManager.getComponentId<Transform>(), true);

if ( ((parentSignature & signature) == signature) && ((childSignature & signature) == signature) ){
Signature childSignature = entityManager.getSignature(child);
if ((childSignature & signature) == signature){
Transform& transformChild = componentManager.getComponent<Transform>(child);
Transform& transformParent = componentManager.getComponent<Transform>(parent);

auto transforms = componentManager.getComponentArray<Transform>();
transformChild.needUpdate = true;

if (transformChild.parent != parent) {
transformChild.parent = parent;
if (parent == NULL_ENTITY){
transformChild.parent = NULL_ENTITY;
if (changeTransform){
// set local position same of world position
transformChild.modelMatrix.decompose(transformChild.position, transformChild.scale, transformChild.rotation);
}
return;
}

//----------DEBUG
//Log::debug("Add child - BEFORE");
//for (int i = 0; i < transforms->size(); i++){
// auto transform = transforms->getComponentFromIndex(i);
// Log::debug("Transform %i - Entity: %i - Parent: %i: %s", i, transforms->getEntity(i), transform.parent, transform.name.c_str());
//}
//----------DEBUG
Signature parentSignature = entityManager.getSignature(parent);
if ((parentSignature & signature) == signature){
Transform& transformParent = componentManager.getComponent<Transform>(parent);

//size_t parentIndex = transforms->getIndex(parent);
size_t childIndex = transforms->getIndex(child);
auto transforms = componentManager.getComponentArray<Transform>();

//find children of parent and child family
size_t newIndex = findBranchLastIndex(parent) + 1;
size_t lastChild = findBranchLastIndex(child);
if (transformChild.parent != parent) {
transformChild.parent = parent;

int length = lastChild - childIndex + 1;
//----------DEBUG
//Log::debug("Add child - BEFORE");
//for (int i = 0; i < transforms->size(); i++){
// auto transform = transforms->getComponentFromIndex(i);
// Log::debug("Transform %i - Entity: %i - Parent: %i: %s", i, transforms->getEntity(i), transform.parent, transform.name.c_str());
//}
//----------DEBUG

if (newIndex > childIndex) {
newIndex = newIndex - length;
}
//size_t parentIndex = transforms->getIndex(parent);
size_t childIndex = transforms->getIndex(child);

//find children of parent and child family
size_t newIndex = findBranchLastIndex(parent) + 1;
size_t lastChild = findBranchLastIndex(child);

if (childIndex != newIndex) {
if (length == 1) {
transforms->moveEntityToIndex(child, newIndex);
} else {
transforms->moveEntityRangeToIndex(child, transforms->getEntity(lastChild), newIndex);
int length = lastChild - childIndex + 1;

if (newIndex > childIndex) {
newIndex = newIndex - length;
}

if (childIndex != newIndex) {
if (length == 1) {
transforms->moveEntityToIndex(child, newIndex);
} else {
transforms->moveEntityRangeToIndex(child, transforms->getEntity(lastChild), newIndex);
}
}
}

//----------DEBUG
//Log::debug("Add child - AFTER");
//for (int i = 0; i < transforms->size(); i++){
// auto transform = transforms->getComponentFromIndex(i);
// Log::debug("Transform %i - Entity: %i - Parent: %i: %s", i, transforms->getEntity(i), transform.parent, transform.name.c_str());
//}
//Log::debug("\n");
//----------DEBUG
if (changeTransform){
Matrix4 localMatrix = transformParent.modelMatrix.inverse() * transformChild.modelMatrix;
localMatrix.decompose(transformChild.position, transformChild.scale, transformChild.rotation);
}

//----------DEBUG
//Log::debug("Add child - AFTER");
//for (int i = 0; i < transforms->size(); i++){
// auto transform = transforms->getComponentFromIndex(i);
// Log::debug("Transform %i - Entity: %i - Parent: %i: %s", i, transforms->getEntity(i), transform.parent, transform.name.c_str());
//}
//Log::debug("\n");
//----------DEBUG
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion engine/core/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ namespace Supernova{

void destroyEntity(Entity entity);

void addEntityChild(Entity parent, Entity child);
void addEntityChild(Entity parent, Entity child, bool changeTransform);

void moveChildToTop(Entity entity);
void moveChildUp(Entity entity);
Expand Down
4 changes: 2 additions & 2 deletions engine/core/object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ Matrix4 Object::getModelViewProjectionMatrix() const{
}

void Object::addChild(Object* child){
scene->addEntityChild(this->entity, child->entity);
scene->addEntityChild(this->entity, child->entity, false);
}

void Object::addChild(Entity child){
scene->addEntityChild(this->entity, child);
scene->addEntityChild(this->entity, child, false);
}

void Object::moveToTop(){
Expand Down
4 changes: 2 additions & 2 deletions engine/core/subsystem/MeshSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ Entity MeshSystem::generateSketetalStructure(Entity entity, ModelComponent& mode

for (size_t i = 0; i < node.children.size(); i++){
// here bonetransform and bonecomp losts references
scene->addEntityChild(bone, generateSketetalStructure(entity, model, node.children[i], skinIndex));
scene->addEntityChild(bone, generateSketetalStructure(entity, model, node.children[i], skinIndex), false);
}

return bone;
Expand Down Expand Up @@ -2060,7 +2060,7 @@ bool MeshSystem::loadGLTF(Entity entity, std::string filename){
Log::error("Cannot create skinning bigger than %i", MAX_BONES);
return false;
}
scene->addEntityChild(entity, model.skeleton);
scene->addEntityChild(entity, model.skeleton, false);
}
}

Expand Down
14 changes: 7 additions & 7 deletions engine/core/subsystem/UISystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void UISystem::createButtonObjects(Entity entity, ButtonComponent& button){
scene->addComponent<UIComponent>(button.label, {});
scene->addComponent<TextComponent>(button.label, {});

scene->addEntityChild(entity, button.label);
scene->addEntityChild(entity, button.label, false);

UIComponent& labelui = scene->getComponent<UIComponent>(button.label);
UILayoutComponent& labellayout = scene->getComponent<UILayoutComponent>(button.label);
Expand All @@ -287,7 +287,7 @@ void UISystem::createPanelObjects(Entity entity, PanelComponent& panel){
scene->addComponent<UIComponent>(panel.headerimage, {});
scene->addComponent<ImageComponent>(panel.headerimage, {});

scene->addEntityChild(entity, panel.headerimage);
scene->addEntityChild(entity, panel.headerimage, false);

UIComponent& headerui = scene->getComponent<UIComponent>(panel.headerimage);
UILayoutComponent& headerimagelayout = scene->getComponent<UILayoutComponent>(panel.headerimage);
Expand All @@ -309,7 +309,7 @@ void UISystem::createPanelObjects(Entity entity, PanelComponent& panel){
scene->addComponent<UILayoutComponent>(panel.headercontainer, {});
scene->addComponent<UIContainerComponent>(panel.headercontainer, {});

scene->addEntityChild(panel.headerimage, panel.headercontainer);
scene->addEntityChild(panel.headerimage, panel.headercontainer, false);

UIContainerComponent& containerui = scene->getComponent<UIContainerComponent>(panel.headercontainer);
UILayoutComponent& containerlayout = scene->getComponent<UILayoutComponent>(panel.headercontainer);
Expand All @@ -328,7 +328,7 @@ void UISystem::createPanelObjects(Entity entity, PanelComponent& panel){
scene->addComponent<UIComponent>(panel.headertext, {});
scene->addComponent<TextComponent>(panel.headertext, {});

scene->addEntityChild(panel.headercontainer, panel.headertext);
scene->addEntityChild(panel.headercontainer, panel.headertext, false);

UIComponent& titleui = scene->getComponent<UIComponent>(panel.headertext);
UILayoutComponent& titlelayout = scene->getComponent<UILayoutComponent>(panel.headertext);
Expand All @@ -350,7 +350,7 @@ void UISystem::createScrollbarObjects(Entity entity, ScrollbarComponent& scrollb
scene->addComponent<UIComponent>(scrollbar.bar, {});
scene->addComponent<ImageComponent>(scrollbar.bar, {});

scene->addEntityChild(entity, scrollbar.bar);
scene->addEntityChild(entity, scrollbar.bar, false);

UILayoutComponent& barlayout = scene->getComponent<UILayoutComponent>(scrollbar.bar);

Expand All @@ -367,7 +367,7 @@ void UISystem::createTextEditObjects(Entity entity, TextEditComponent& textedit)
scene->addComponent<UIComponent>(textedit.text, {});
scene->addComponent<TextComponent>(textedit.text, {});

scene->addEntityChild(entity, textedit.text);
scene->addEntityChild(entity, textedit.text, false);

UILayoutComponent& textlayout = scene->getComponent<UILayoutComponent>(textedit.text);
UIComponent& textui = scene->getComponent<UIComponent>(textedit.text);
Expand All @@ -383,7 +383,7 @@ void UISystem::createTextEditObjects(Entity entity, TextEditComponent& textedit)
scene->addComponent<UIComponent>(textedit.cursor, {});
scene->addComponent<PolygonComponent>(textedit.cursor, {});

scene->addEntityChild(entity, textedit.cursor);
scene->addEntityChild(entity, textedit.cursor, false);

UILayoutComponent& cursorlayout = scene->getComponent<UILayoutComponent>(textedit.cursor);

Expand Down

0 comments on commit 3167f41

Please sign in to comment.