A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add HierarchyPanel for entity management
esengine0189db26d54c
diff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txtindex 6cbca8364..0aede264c 100644--- a/src/esengine/editor/CMakeLists.txt+++ b/src/esengine/editor/CMakeLists.txt@@ -3,6 +3,9 @@set(EDITOR_SOURCESmain.cppEditorApplication.cpp++ # Panels+ panels/HierarchyPanel.cpp)set(EDITOR_HEADERS@@ -18,6 +21,9 @@ set(EDITOR_HEADERScommand/MacroCommand.hppcommand/PropertyCommand.hppcommand/EditorCommands.hpp++ # Panels+ panels/HierarchyPanel.hpp)# Create editor executablediff --git a/src/esengine/editor/panels/HierarchyPanel.cpp b/src/esengine/editor/panels/HierarchyPanel.cppnew file mode 100644index 000000000..65c0aac1e--- /dev/null+++ b/src/esengine/editor/panels/HierarchyPanel.cpp@@ -0,0 +1,251 @@+/**+ * @file HierarchyPanel.cpp+ * @brief Hierarchy panel implementation+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */++#include "HierarchyPanel.hpp"+#include "../../ecs/components/Common.hpp"+#include "../../ecs/components/Hierarchy.hpp"+#include "../../core/Log.hpp"+#include "../../events/Sink.hpp"++#include <sstream>++namespace esengine::editor {++// =============================================================================+// Constructor / Destructor+// =============================================================================++HierarchyPanel::HierarchyPanel(ecs::Registry& registry, EntitySelection& selection)+ : DockPanel(ui::WidgetId("hierarchy_panel"), "Hierarchy"),+ registry_(registry),+ selection_(selection) {++ setPanelType("Hierarchy");+ setClosable(false);+ setMinSize(glm::vec2(200.0f, 200.0f));++ // Create tree view+ auto treeView = makeUnique<ui::TreeView>(ui::WidgetId("hierarchy_tree"));+ treeView->setMultiSelect(false);+ treeView->setRowHeight(22.0f);+ treeView->setIndentSize(16.0f);++ treeView_ = treeView.get();+ setContent(std::move(treeView));++ // Connect tree view signals using sink helper+ nodeSelectedConnection_ = sink(treeView_->onNodeSelected).connect(+ [this](ui::TreeNodeId nodeId) { onNodeSelected(nodeId); }+ );++ nodeDoubleClickedConnection_ = sink(treeView_->onNodeDoubleClicked).connect(+ [this](ui::TreeNodeId nodeId) { onNodeDoubleClicked(nodeId); }+ );++ // Connect selection change events+ u32 listenerId = selection_.addListener(+ [this](const std::vector<Entity>& prev, const std::vector<Entity>& curr) {+ onSelectionChanged(prev, curr);+ }+ );+ selectionChangedConnection_ = Connection(listenerId, [&selection](CallbackId id) {+ selection.removeListener(static_cast<u32>(id));+ });++ // Initial tree build+ rebuildTree();+}++HierarchyPanel::~HierarchyPanel() {+ // Connections are automatically cleaned up by Connection RAII+}++// =============================================================================+// Public Methods+// =============================================================================++void HierarchyPanel::refresh() {+ needsRebuild_ = true;+}++void HierarchyPanel::setShowOrphans(bool showOrphans) {+ if (showOrphans_ != showOrphans) {+ showOrphans_ = showOrphans;+ needsRebuild_ = true;+ }+}++// =============================================================================+// Protected Methods+// =============================================================================++void HierarchyPanel::onActivated() {+ if (needsRebuild_) {+ rebuildTree();+ needsRebuild_ = false;+ }+}++void HierarchyPanel::onDeactivated() {+ // Nothing to do+}++// =============================================================================+// Tree Building+// =============================================================================++void HierarchyPanel::rebuildTree() {+ if (!treeView_) {+ return;+ }++ // Clear tree and mappings+ treeView_->clear();+ nodeToEntity_.clear();+ entityToNode_.clear();++ // Collect all entities+ std::vector<Entity> allEntities;+ registry_.each([&allEntities](Entity entity) {+ allEntities.push_back(entity);+ });++ // Build tree starting from root entities (entities without Parent component)+ for (Entity entity : allEntities) {+ // Skip if entity has a parent (will be added as child)+ if (registry_.has<ecs::Parent>(entity)) {+ continue;+ }++ // Add as root node+ addEntityToTree(entity, ui::INVALID_TREE_NODE);+ }++ // Sync with current selection+ if (!selection_.empty()) {+ Entity selectedEntity = selection_.getFirst();+ auto it = entityToNode_.find(selectedEntity);+ if (it != entityToNode_.end()) {+ treeView_->selectNode(it->second, true);+ }+ }+}++ui::TreeNodeId HierarchyPanel::addEntityToTree(Entity entity, ui::TreeNodeId parentNodeId) {+ // Get display name+ std::string displayName = getEntityDisplayName(entity);++ // Add to tree+ ui::TreeNodeId nodeId = treeView_->addNode(parentNodeId, displayName);++ // Store bidirectional mapping+ nodeToEntity_[nodeId] = entity;+ entityToNode_[entity] = nodeId;++ // Add children if entity has Children component+ if (registry_.has<ecs::Children>(entity)) {+ const auto& children = registry_.get<ecs::Children>(entity);+ for (Entity childEntity : children.entities) {+ // Verify child is valid+ if (!registry_.valid(childEntity)) {+ ES_LOG_WARN("HierarchyPanel: Invalid child entity {} for parent {}",+ childEntity, entity);+ continue;+ }++ addEntityToTree(childEntity, nodeId);+ }++ // Expand node if it has children+ if (!children.empty()) {+ treeView_->setNodeExpanded(nodeId, true);+ }+ }++ return nodeId;+}++std::string HierarchyPanel::getEntityDisplayName(Entity entity) const {+ // Try to get Name component+ if (registry_.has<ecs::Name>(entity)) {+ const auto& name = registry_.get<ecs::Name>(entity);+ if (!name.value.empty()) {+ return name.value;+ }+ }++ // Fallback to "Entity {id}"+ std::ostringstream oss;+ oss << "Entity " << entity;+ return oss.str();+}++// =============================================================================+// Event Handlers+// =============================================================================++void HierarchyPanel::onNodeSelected(ui::TreeNodeId nodeId) {+ // Get entity for node+ Entity entity = getEntityForNode(nodeId);+ if (entity == INVALID_ENTITY) {+ return;+ }++ // Update selection (this will trigger onSelectionChanged via listener)+ if (!selection_.isSelected(entity)) {+ selection_.select(entity);+ }+}++void HierarchyPanel::onNodeDoubleClicked(ui::TreeNodeId nodeId) {+ // Double-click could be used for "focus" or "rename" functionality+ // For now, just toggle expand/collapse (TreeView already handles this)+ (void)nodeId;+}++void HierarchyPanel::onSelectionChanged(const std::vector<Entity>& previous,+ const std::vector<Entity>& current) {+ if (!treeView_) {+ return;+ }++ // Deselect all previous nodes+ for (Entity entity : previous) {+ auto it = entityToNode_.find(entity);+ if (it != entityToNode_.end()) {+ treeView_->deselectNode(it->second);+ }+ }++ // Select all current nodes+ for (Entity entity : current) {+ auto it = entityToNode_.find(entity);+ if (it != entityToNode_.end()) {+ treeView_->selectNode(it->second, false); // Don't clear previous+ }+ }+}++// =============================================================================+// Mapping Helpers+// =============================================================================++Entity HierarchyPanel::getEntityForNode(ui::TreeNodeId nodeId) const {+ auto it = nodeToEntity_.find(nodeId);+ return it != nodeToEntity_.end() ? it->second : INVALID_ENTITY;+}++ui::TreeNodeId HierarchyPanel::getNodeForEntity(Entity entity) const {+ auto it = entityToNode_.find(entity);+ return it != entityToNode_.end() ? it->second : ui::INVALID_TREE_NODE;+}++} // namespace esengine::editordiff --git a/src/esengine/editor/panels/HierarchyPanel.hpp b/src/esengine/editor/panels/HierarchyPanel.hppnew file mode 100644index 000000000..abda407c2--- /dev/null+++ b/src/esengine/editor/panels/HierarchyPanel.hpp@@ -0,0 +1,174 @@+/**+ * @file HierarchyPanel.hpp+ * @brief Hierarchy panel for scene entity management+ * @details Displays all entities in the scene in a tree view, showing+ * parent-child relationships and allowing entity selection.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++// =============================================================================+// Includes+// =============================================================================++#include "../../ui/docking/DockPanel.hpp"+#include "../../ui/widgets/TreeView.hpp"+#include "../../ecs/Registry.hpp"+#include "../core/Selection.hpp"+#include "../../events/Connection.hpp"++#include <unordered_map>++namespace esengine::editor {++// =============================================================================+// HierarchyPanel Class+// =============================================================================++/**+ * @brief Hierarchy panel for displaying and managing scene entities+ *+ * @details HierarchyPanel provides a tree view of all entities in the scene.+ * It displays:+ * - Entity names (from Name component)+ * - Parent-child relationships (from Parent/Children components)+ * - Selection state (synchronized with EntitySelection)+ *+ * The panel automatically updates when entities are created, destroyed,+ * or modified.+ *+ * @code+ * auto hierarchyPanel = makeUnique<HierarchyPanel>(registry, selection);+ * hierarchyPanel->setPanelType("Hierarchy");+ * dockArea->addPanel(std::move(hierarchyPanel));+ * @endcode+ */+class HierarchyPanel : public ui::DockPanel {+public:+ /**+ * @brief Constructs a hierarchy panel+ * @param registry The ECS registry to display+ * @param selection The entity selection manager+ */+ HierarchyPanel(ecs::Registry& registry, EntitySelection& selection);++ ~HierarchyPanel() override;++ /**+ * @brief Refreshes the tree view from the registry+ *+ * @details Rebuilds the entire tree structure from scratch.+ * Call this after batch entity operations.+ */+ void refresh();++ /**+ * @brief Sets whether to show entities without Parent component as roots+ * @param showOrphans True to show orphaned entities at root level+ */+ void setShowOrphans(bool showOrphans);++ /**+ * @brief Returns true if showing orphaned entities+ */+ bool getShowOrphans() const { return showOrphans_; }++protected:+ void onActivated() override;+ void onDeactivated() override;++private:+ // =========================================================================+ // Tree Building+ // =========================================================================++ /**+ * @brief Rebuilds the tree from registry state+ */+ void rebuildTree();++ /**+ * @brief Adds an entity to the tree+ * @param entity Entity to add+ * @param parentNodeId Parent tree node (INVALID_TREE_NODE for root)+ */+ ui::TreeNodeId addEntityToTree(Entity entity, ui::TreeNodeId parentNodeId);++ /**+ * @brief Gets the display name for an entity+ * @param entity Entity to get name for+ * @return Display name (Name component or "Entity {id}")+ */+ std::string getEntityDisplayName(Entity entity) const;++ // =========================================================================+ // Event Handlers+ // =========================================================================++ /**+ * @brief Called when a tree node is selected+ * @param nodeId Selected node ID+ */+ void onNodeSelected(ui::TreeNodeId nodeId);++ /**+ * @brief Called when a tree node is double-clicked+ * @param nodeId Clicked node ID+ */+ void onNodeDoubleClicked(ui::TreeNodeId nodeId);++ /**+ * @brief Called when entity selection changes externally+ * @param previous Previous selection+ * @param current Current selection+ */+ void onSelectionChanged(const std::vector<Entity>& previous,+ const std::vector<Entity>& current);++ // =========================================================================+ // Mapping Helpers+ // =========================================================================++ /**+ * @brief Gets the entity for a tree node+ * @param nodeId Tree node ID+ * @return Entity or INVALID_ENTITY if not found+ */+ Entity getEntityForNode(ui::TreeNodeId nodeId) const;++ /**+ * @brief Gets the tree node for an entity+ * @param entity Entity+ * @return Tree node ID or INVALID_TREE_NODE if not found+ */+ ui::TreeNodeId getNodeForEntity(Entity entity) const;++ // =========================================================================+ // Member Variables+ // =========================================================================++ ecs::Registry& registry_;+ EntitySelection& selection_;++ ui::TreeView* treeView_ = nullptr;++ // Bidirectional mapping between entities and tree nodes+ std::unordered_map<ui::TreeNodeId, Entity> nodeToEntity_;+ std::unordered_map<Entity, ui::TreeNodeId> entityToNode_;++ // Settings+ bool showOrphans_ = true;+ bool needsRebuild_ = false;++ // Event connections+ Connection selectionChangedConnection_;+ Connection nodeSelectedConnection_;+ Connection nodeDoubleClickedConnection_;+};++} // namespace esengine::editor