From d23fe77a5f394f07ea8bb12cb49bb2f9aee971a7 Mon Sep 17 00:00:00 2001 From: ducphamhong Date: Fri, 13 May 2022 17:58:38 +0700 Subject: [PATCH] #123 Display tree nodes of RenderMesh Entity --- .../Space/Hierarchy/CHierarchyController.cpp | 5 +++- .../Source/Editor/Space/Scene/CSpaceScene.cpp | 14 +++++++---- .../SpaceController/CPropertyController.cpp | 9 ++++++- .../SpaceController/CSceneController.cpp | 15 ++++++++--- .../Engine/Source/Entity/CEntityHandler.cpp | 25 +++++++++++++++++++ .../Engine/Source/Entity/CEntityHandler.h | 5 ++++ .../Engine/Source/RenderMesh/CRenderMesh.cpp | 12 ++++++--- .../Engine/Source/RenderMesh/CRenderMesh.h | 8 +++--- 8 files changed, 75 insertions(+), 18 deletions(-) diff --git a/Projects/Editor/Source/Editor/Space/Hierarchy/CHierarchyController.cpp b/Projects/Editor/Source/Editor/Space/Hierarchy/CHierarchyController.cpp index 17ef55d13..6be554081 100644 --- a/Projects/Editor/Source/Editor/Space/Hierarchy/CHierarchyController.cpp +++ b/Projects/Editor/Source/Editor/Space/Hierarchy/CHierarchyController.cpp @@ -477,7 +477,10 @@ namespace Skylicht GUI::CListRowItem* rowItem = (GUI::CListRowItem*)data->UserData; std::string path = rowItem->getTagString(); - CSceneController::getInstance()->createResourceComponent(path, targetObject); + + CSceneController* sceneController = CSceneController::getInstance(); + sceneController->createResourceComponent(path, targetObject); + sceneController->updateTreeNode(targetObject); } } diff --git a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp index 7b9faa530..85dbf17a7 100644 --- a/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp +++ b/Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp @@ -236,6 +236,7 @@ namespace Skylicht sceneController->createResourceComponent(path, targetObject); CHierachyNode* node = sceneController->selectOnHierachy(targetObject); sceneController->onSelectNode(node, true); + sceneController->updateTreeNode(targetObject); } } } @@ -579,8 +580,11 @@ namespace Skylicht // end toolbar - void CSpaceScene::initRenderPipeline(float w, float h) + void CSpaceScene::initRenderPipeline(float fw, float fh) { + int w = (int)fw; + int h = (int)fh; + if (w >= 32.0f && h >= 32.0f) { if (m_renderRP == NULL) @@ -620,7 +624,7 @@ namespace Skylicht { // resize if (m_shadowMapRendering != NULL) - m_shadowMapRendering->resize(w, h); + m_shadowMapRendering->resize((int)w, h); if (m_rendering != NULL) m_rendering->resize(w, h); @@ -635,15 +639,15 @@ namespace Skylicht if (m_viewpointRP == NULL) { m_viewpointRP = new CForwardRP(false); - m_viewpointRP->initRender((int)w, (int)h); + m_viewpointRP->initRender(w, h); } else { - m_viewpointRP->resize((int)w, (int)h); + m_viewpointRP->resize(w, h); } // update camera aspect - m_editorCamera->setAspect(w / h); + m_editorCamera->setAspect(fw / fh); } } diff --git a/Projects/Editor/Source/Editor/SpaceController/CPropertyController.cpp b/Projects/Editor/Source/Editor/SpaceController/CPropertyController.cpp index 53f451254..e4a16aa46 100644 --- a/Projects/Editor/Source/Editor/SpaceController/CPropertyController.cpp +++ b/Projects/Editor/Source/Editor/SpaceController/CPropertyController.cpp @@ -130,7 +130,9 @@ namespace Skylicht { // Name and icon spaceProperty->setIcon(GUI::ESystemIcon::Poly); - spaceProperty->setLabel(L"Entity"); + + // Label on Propety + std::wstring label = L"Entity: "; // Clear old ui spaceProperty->clearAllGroup(); @@ -141,6 +143,9 @@ namespace Skylicht CEntity* entity = scene->getEntityManager()->getEntityByID(object->getID().c_str()); if (entity != NULL) { + CWorldTransformData* worldTransform = entity->getData(); + label += CStringImp::convertUTF8ToUnicode(worldTransform->Name.c_str()); + // Activator CEditorActivator* activator = CEditorActivator::getInstance(); @@ -157,6 +162,8 @@ namespace Skylicht } } } + + spaceProperty->setLabel(label.c_str()); } } else diff --git a/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp b/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp index 8e2703a54..0755d04c4 100644 --- a/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp +++ b/Projects/Editor/Source/Editor/SpaceController/CSceneController.cpp @@ -360,24 +360,33 @@ namespace Skylicht } } - void CSceneController::rebuildHierarchyEntityData(CGameObject* object, CHierachyNode* parentNode) + void CSceneController::rebuildHierarchyEntityData(CGameObject* object, CHierachyNode* rootNode) { - parentNode->removeAll(CHierachyNode::Entity); + rootNode->removeAll(CHierachyNode::Entity); CEntityHandler* entityHandler = object->getComponent(); if (entityHandler == NULL) return; + CEntity* root = object->getEntity(); + + std::map treeNodes; + treeNodes[root->getIndex()] = rootNode; + std::vector& entities = entityHandler->getEntities(); for (CEntity* entity : entities) { - CHierachyNode* node = parentNode->addChild(); CWorldTransformData* worldData = entity->getData(); + CHierachyNode* parentNode = treeNodes[worldData->ParentIndex]; + + CHierachyNode* node = parentNode->addChild(); node->setName(CStringImp::convertUTF8ToUnicode(worldData->Name.c_str()).c_str()); node->setIcon(GUI::ESystemIcon::Poly); node->setTagData(entity, CHierachyNode::Entity); + treeNodes[entity->getIndex()] = node; + setNodeEvent(node); } } diff --git a/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.cpp b/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.cpp index ece8a87c2..9e503daf0 100644 --- a/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.cpp +++ b/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.cpp @@ -127,4 +127,29 @@ namespace Skylicht } } } + + void CEntityHandler::setEntities(CEntity** entities, int count) + { + m_entities.clear(); + for (int i = 0; i < count; i++) + { + CEntity* entity = entities[i]; + CWorldTransformData* transformData = entity->addData(); + + // assign name + if (transformData != NULL && transformData->Name.empty()) + { + char name[512]; + sprintf(name, "#%d", entity->getIndex()); + transformData->Name = name; + } + + // generate id for entity + std::string id = m_gameObject->getZone()->generateRandomID(); + entity->setID(id.c_str()); + + // add to handler + m_entities.push_back(entity); + } + } } \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.h b/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.h index a9489f47e..4ab0c72e8 100644 --- a/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.h +++ b/Projects/Skylicht/Engine/Source/Entity/CEntityHandler.h @@ -53,5 +53,10 @@ namespace Skylicht { return m_entities; } + + protected: + + void setEntities(CEntity** entities, int count); + }; } \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.cpp b/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.cpp index 97d178fa5..d27bd4e55 100644 --- a/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.cpp +++ b/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.cpp @@ -73,12 +73,13 @@ namespace Skylicht if (m_gameObject != NULL) { CEntityManager* entityManager = m_gameObject->getEntityManager(); - for (u32 i = 0, n = m_entities.size(); i < n; i++) - entityManager->removeEntity(m_entities[i]); + for (u32 i = 0, n = m_allEntities.size(); i < n; i++) + entityManager->removeEntity(m_allEntities[i]); } - m_entities.clear(); + m_allEntities.clear(); m_renderers.clear(); + m_entities.clear(); } void CRenderMesh::initComponent() @@ -189,7 +190,7 @@ namespace Skylicht // spawn childs entity int numEntities = prefab->getNumEntities(); - CEntity** entities = entityManager->createEntity(numEntities, m_entities); + CEntity** entities = entityManager->createEntity(numEntities, m_allEntities); // map new entity index from src prefab std::map entityIndex; @@ -270,6 +271,9 @@ namespace Skylicht bool addInvData = false; + // for handler on Editor UI + setEntities(entities, numEntities); + // re-map joint with new entity in CEntityManager for (CRenderMeshData*& r : m_renderers) { diff --git a/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.h b/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.h index 7474c7fe4..31d3be3b2 100644 --- a/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.h +++ b/Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.h @@ -25,7 +25,7 @@ This file is part of the "Skylicht Engine". #pragma once #include "Entity/CEntityPrefab.h" -#include "Components/CComponentSystem.h" +#include "Entity/CEntityHandler.h" #include "Material/CMaterial.h" #include "Material/CMaterialManager.h" #include "RenderMesh/CRenderMeshData.h" @@ -33,11 +33,11 @@ This file is part of the "Skylicht Engine". namespace Skylicht { - class CRenderMesh : public CComponentSystem + class CRenderMesh : public CEntityHandler { protected: CEntity* m_root; - core::array m_entities; + core::array m_allEntities; std::vector m_renderTransforms; std::vector m_transforms; @@ -89,7 +89,7 @@ namespace Skylicht core::array& getEntities() { - return m_entities; + return m_allEntities; } std::vector& getRenderers()