Unify inspector and console panel actions

This commit is contained in:
2026-03-27 00:08:46 +08:00
parent 31675e00c8
commit 3ebad63874
15 changed files with 296 additions and 142 deletions

View File

@@ -1,6 +1,6 @@
#include "Actions/InspectorActionRouter.h"
#include "Actions/ActionRouting.h"
#include "Actions/EditorActions.h"
#include "Commands/ComponentCommands.h"
#include "InspectorPanel.h"
#include "Core/IEditorContext.h"
#include "Core/ISceneManager.h"
@@ -19,9 +19,28 @@ namespace Editor {
InspectorPanel::InspectorPanel() : Panel("Inspector") {}
InspectorPanel::~InspectorPanel() {
if (m_context) {
m_context->GetEventBus().Unsubscribe<SelectionChangedEvent>(m_selectionHandlerId);
}
void InspectorPanel::OnAttach() {
if (!m_context || m_selectionHandlerId) {
return;
}
m_selectionHandlerId = m_context->GetEventBus().Subscribe<SelectionChangedEvent>(
[this](const SelectionChangedEvent& event) {
OnSelectionChanged(event);
}
);
m_selectedEntityId = m_context->GetSelectionManager().GetSelectedEntity();
}
void InspectorPanel::OnDetach() {
if (!m_context || !m_selectionHandlerId) {
return;
}
m_context->GetEventBus().Unsubscribe<SelectionChangedEvent>(m_selectionHandlerId);
m_selectionHandlerId = 0;
}
void InspectorPanel::OnSelectionChanged(const SelectionChangedEvent& event) {
@@ -39,17 +58,7 @@ void InspectorPanel::Render() {
}
Actions::ObserveInactiveActionRoute(*m_context);
if (!m_selectionHandlerId && m_context) {
m_selectionHandlerId = m_context->GetEventBus().Subscribe<SelectionChangedEvent>(
[this](const SelectionChangedEvent& event) {
OnSelectionChanged(event);
}
);
}
m_selectedEntityId = m_context->GetSelectionManager().GetSelectedEntity();
if (m_selectedEntityId) {
auto& sceneManager = m_context->GetSceneManager();
auto* gameObject = sceneManager.GetEntity(m_selectedEntityId);
@@ -107,32 +116,7 @@ void InspectorPanel::RenderAddComponentPopup(::XCEngine::Components::GameObject*
return;
}
bool drewAnyEntry = false;
for (const auto& editor : ComponentEditorRegistry::Get().GetEditors()) {
if (!editor || !editor->ShowInAddComponentMenu()) {
continue;
}
drewAnyEntry = true;
const bool canAdd = Commands::CanAddComponent(*editor, gameObject);
std::string label = editor->GetDisplayName();
if (!canAdd) {
const char* reason = editor->GetAddDisabledReason(gameObject);
if (reason && reason[0] != '\0') {
label += " (";
label += reason;
label += ")";
}
}
Actions::DrawMenuAction(Actions::MakeAddComponentMenuAction(label, canAdd), [&]() {
if (Commands::AddComponent(*m_context, *editor, gameObject)) {
ImGui::CloseCurrentPopup();
}
});
}
if (!drewAnyEntry) {
if (!Actions::DrawInspectorAddComponentMenu(*m_context, gameObject)) {
UI::DrawHintText("No registered component editors");
}
@@ -145,14 +129,15 @@ void InspectorPanel::RenderComponent(::XCEngine::Components::Component* componen
IComponentEditor* editor = ComponentEditorRegistry::Get().FindEditor(component);
const std::string name = component->GetName();
const UI::ComponentSectionResult section =
UI::BeginComponentSection(
(void*)typeid(*component).hash_code(),
name.c_str(),
Commands::CanRemoveComponent(component, editor));
bool removed = false;
const UI::ComponentSectionResult section = UI::BeginComponentSection(
(void*)typeid(*component).hash_code(),
name.c_str(),
[&]() {
removed = Actions::DrawInspectorComponentMenu(*m_context, component, gameObject, editor);
});
if (section.removeRequested) {
Commands::RemoveComponent(*m_context, component, gameObject, editor);
if (removed) {
return;
}