Extract shared editor tree view

This commit is contained in:
2026-03-27 22:05:05 +08:00
parent a51e0f6f88
commit 37a90b39e2
6 changed files with 120 additions and 169 deletions

View File

@@ -65,71 +65,41 @@ void HierarchyPanel::OnRenameRequested(const EntityRenameRequestedEvent& event)
}
void HierarchyPanel::Render() {
UI::PanelWindowScope panel(m_name.c_str());
if (!panel.IsOpen()) {
return;
ImGui::PushStyleColor(ImGuiCol_WindowBg, UI::HierarchyInspectorPanelBackgroundColor());
ImGui::PushStyleColor(ImGuiCol_ChildBg, UI::HierarchyInspectorPanelBackgroundColor());
{
UI::PanelWindowScope panel(m_name.c_str());
if (!panel.IsOpen()) {
ImGui::PopStyleColor(2);
return;
}
Actions::ObserveFocusedActionRoute(*m_context, EditorActionRoute::Hierarchy);
UI::PanelContentScope content("EntityList");
if (!content.IsOpen()) {
ImGui::PopStyleColor(2);
return;
}
auto& sceneManager = m_context->GetSceneManager();
auto rootEntities = sceneManager.GetRootEntities();
for (auto* gameObject : rootEntities) {
RenderEntity(gameObject);
}
Actions::HandleHierarchyBackgroundPrimaryClick(*m_context, m_renameState);
Actions::DrawHierarchyBackgroundContextPopup(*m_context);
Actions::DrawHierarchyRootDropTarget(*m_context);
}
Actions::ObserveFocusedActionRoute(*m_context, EditorActionRoute::Hierarchy);
RenderSearchBar();
std::string filter = m_searchBuffer;
UI::PanelContentScope content("EntityList");
if (!content.IsOpen()) {
return;
}
auto& sceneManager = m_context->GetSceneManager();
auto rootEntities = sceneManager.GetRootEntities();
SortEntities(const_cast<std::vector<::XCEngine::Components::GameObject*>&>(rootEntities));
for (auto* gameObject : rootEntities) {
RenderEntity(gameObject, filter);
}
Actions::HandleHierarchyBackgroundPrimaryClick(*m_context, m_renameState);
Actions::DrawHierarchyBackgroundContextPopup(*m_context);
Actions::DrawHierarchyRootDropTarget(*m_context);
ImGui::PopStyleColor(2);
}
void HierarchyPanel::RenderSearchBar() {
UI::PanelToolbarScope toolbar("HierarchyToolbar", UI::StandardPanelToolbarHeight());
if (!toolbar.IsOpen()) {
return;
}
const float buttonWidth = UI::HierarchyOverflowButtonWidth();
UI::ToolbarSearchField(
"##Search",
"Search hierarchy",
m_searchBuffer,
sizeof(m_searchBuffer),
buttonWidth + UI::ToolbarSearchTrailingSpacing());
ImGui::SameLine();
if (UI::ToolbarButton("...", false, ImVec2(buttonWidth, 0.0f))) {
Actions::RequestHierarchyOptionsPopup(m_optionsPopup);
}
Actions::DrawHierarchySortOptionsPopup(
m_optionsPopup,
m_sortMode,
SortMode::Name,
SortMode::ComponentCount,
SortMode::TransformFirst,
[this](SortMode mode) {
m_sortMode = mode;
});
}
void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject, const std::string& filter) {
void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject) {
if (!gameObject) return;
if (!filter.empty() && !PassesFilter(gameObject, filter)) {
return;
}
ImGui::PushID(static_cast<int>(gameObject->GetID()));
if (m_renameState.IsEditing(gameObject->GetID())) {
@@ -152,11 +122,14 @@ void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject
CommitRename();
}
} else {
const UI::HierarchyNodeResult node = UI::DrawHierarchyNode(
UI::TreeNodeOptions nodeOptions;
nodeOptions.selected = m_context->GetSelectionManager().IsSelected(gameObject->GetID());
nodeOptions.leaf = gameObject->GetChildCount() == 0;
const UI::TreeNodeResult node = UI::DrawTreeNode(
(void*)gameObject->GetUUID(),
gameObject->GetName().c_str(),
m_context->GetSelectionManager().IsSelected(gameObject->GetID()),
gameObject->GetChildCount() == 0);
nodeOptions);
if (node.clicked) {
Actions::HandleHierarchySelectionClick(*m_context, gameObject->GetID(), ImGui::GetIO().KeyCtrl);
@@ -173,9 +146,9 @@ void HierarchyPanel::RenderEntity(::XCEngine::Components::GameObject* gameObject
if (node.open) {
for (size_t i = 0; i < gameObject->GetChildCount(); i++) {
RenderEntity(gameObject->GetChild(i), filter);
RenderEntity(gameObject->GetChild(i));
}
UI::EndHierarchyNode();
UI::EndTreeNode();
}
}
@@ -205,46 +178,5 @@ void HierarchyPanel::CancelRename() {
m_renameState.Cancel();
}
bool HierarchyPanel::PassesFilter(::XCEngine::Components::GameObject* gameObject, const std::string& filter) {
if (!gameObject) return false;
if (gameObject->GetName().find(filter) != std::string::npos) {
return true;
}
for (size_t i = 0; i < gameObject->GetChildCount(); i++) {
if (PassesFilter(gameObject->GetChild(i), filter)) {
return true;
}
}
return false;
}
void HierarchyPanel::SortEntities(std::vector<::XCEngine::Components::GameObject*>& entities) {
switch (m_sortMode) {
case SortMode::Name:
std::sort(entities.begin(), entities.end(), [](::XCEngine::Components::GameObject* a, ::XCEngine::Components::GameObject* b) {
return a->GetName() < b->GetName();
});
break;
case SortMode::ComponentCount:
std::sort(entities.begin(), entities.end(), [](::XCEngine::Components::GameObject* a, ::XCEngine::Components::GameObject* b) {
return a->GetComponents<::XCEngine::Components::Component>().size() > b->GetComponents<::XCEngine::Components::Component>().size();
});
break;
case SortMode::TransformFirst:
std::sort(entities.begin(), entities.end(), [](::XCEngine::Components::GameObject* a, ::XCEngine::Components::GameObject* b) {
bool aHasTransform = a->GetComponent<::XCEngine::Components::TransformComponent>() != nullptr;
bool bHasTransform = b->GetComponent<::XCEngine::Components::TransformComponent>() != nullptr;
if (aHasTransform != bHasTransform) {
return aHasTransform;
}
return a->GetName() < b->GetName();
});
break;
}
}
}
}