Fix editor scene persistence and XC scene workflow

This commit is contained in:
2026-03-26 01:26:26 +08:00
parent 39edb0b497
commit 0651666d8c
35 changed files with 1958 additions and 256 deletions

View File

@@ -1,8 +1,13 @@
#include "InspectorPanel.h"
#include "Core/EditorContext.h"
#include "Core/IEditorContext.h"
#include "Core/ISceneManager.h"
#include "UI/UI.h"
#include <XCEngine/Debug/Logger.h>
#include "Core/ISelectionManager.h"
#include "Core/EventBus.h"
#include "Core/EditorEvents.h"
#include "ComponentEditors/CameraComponentEditor.h"
#include "ComponentEditors/IComponentEditor.h"
#include "ComponentEditors/LightComponentEditor.h"
#include "ComponentEditors/TransformComponentEditor.h"
#include <imgui.h>
#include <string>
@@ -10,7 +15,7 @@ namespace XCEngine {
namespace Editor {
InspectorPanel::InspectorPanel() : Panel("Inspector") {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "InspectorPanel constructed");
RegisterDefaultComponentEditors();
}
InspectorPanel::~InspectorPanel() {
@@ -23,8 +28,35 @@ void InspectorPanel::OnSelectionChanged(const SelectionChangedEvent& event) {
m_selectedEntityId = event.primarySelection;
}
void InspectorPanel::RegisterDefaultComponentEditors() {
RegisterComponentEditor(std::make_unique<TransformComponentEditor>());
RegisterComponentEditor(std::make_unique<CameraComponentEditor>());
RegisterComponentEditor(std::make_unique<LightComponentEditor>());
}
void InspectorPanel::RegisterComponentEditor(std::unique_ptr<IComponentEditor> editor) {
if (!editor) {
return;
}
m_componentEditors.push_back(std::move(editor));
}
IComponentEditor* InspectorPanel::GetEditorFor(::XCEngine::Components::Component* component) const {
if (!component) {
return nullptr;
}
for (const auto& editor : m_componentEditors) {
if (editor && editor->CanEdit(component)) {
return editor.get();
}
}
return nullptr;
}
void InspectorPanel::Render() {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "InspectorPanel::Render START");
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
if (!m_selectionHandlerId && m_context) {
@@ -51,21 +83,18 @@ void InspectorPanel::Render() {
}
ImGui::End();
Debug::Logger::Get().Debug(Debug::LogCategory::General, "InspectorPanel::Render END");
}
void InspectorPanel::RenderGameObject(::XCEngine::Components::GameObject* gameObject) {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "RenderGameObject START");
char nameBuffer[256];
strcpy_s(nameBuffer, gameObject->GetName().c_str());
ImGui::InputText("##Name", nameBuffer, sizeof(nameBuffer));
if (ImGui::IsItemDeactivatedAfterEdit()) {
gameObject->SetName(nameBuffer);
m_context->GetSceneManager().RenameEntity(gameObject->GetID(), nameBuffer);
}
ImGui::SameLine();
if (ImGui::Button("Add Component")) {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "Add Component BUTTON CLICKED");
ImGui::OpenPopup("AddComponent");
}
@@ -75,53 +104,54 @@ void InspectorPanel::RenderGameObject(::XCEngine::Components::GameObject* gameOb
for (auto* component : components) {
RenderComponent(component, gameObject);
}
Debug::Logger::Get().Debug(Debug::LogCategory::General, "RenderGameObject END");
}
void InspectorPanel::RenderAddComponentPopup(::XCEngine::Components::GameObject* gameObject) {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "RenderAddComponentPopup called");
if (!gameObject) {
Debug::Logger::Get().Error(Debug::LogCategory::General, "ERROR: gameObject is nullptr!");
return;
}
if (!ImGui::BeginPopup("AddComponent")) {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "BeginPopup returned false");
return;
}
Debug::Logger::Get().Debug(Debug::LogCategory::General, "BeginPopup succeeded");
ImGui::Text("Components");
ImGui::Separator();
bool hasTransform = gameObject->GetComponent<::XCEngine::Components::TransformComponent>() != nullptr;
Debug::Logger::Get().Debug(Debug::LogCategory::General, hasTransform ? "Has Transform: yes" : "Has Transform: no");
Debug::Logger::Get().Debug(Debug::LogCategory::General, "About to check MenuItem condition");
if (ImGui::MenuItem("Transform", nullptr, false, !hasTransform)) {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "MenuItem CLICKED! Before AddComponent");
auto* newComp = gameObject->AddComponent<::XCEngine::Components::TransformComponent>();
Debug::Logger::Get().Debug(Debug::LogCategory::General, newComp ? "AddComponent SUCCEEDED" : "AddComponent FAILED");
ImGui::CloseCurrentPopup();
} else {
Debug::Logger::Get().Debug(Debug::LogCategory::General, "MenuItem not clicked (disabled or condition false)");
bool drewAnyEntry = false;
for (const auto& editor : m_componentEditors) {
if (!editor || !editor->ShowInAddComponentMenu()) {
continue;
}
drewAnyEntry = true;
const bool canAdd = editor->CanAddTo(gameObject);
std::string label = editor->GetDisplayName();
if (!canAdd) {
const char* reason = editor->GetAddDisabledReason(gameObject);
if (reason && reason[0] != '\0') {
label += " (";
label += reason;
label += ")";
}
}
if (ImGui::MenuItem(label.c_str(), nullptr, false, canAdd)) {
if (editor->AddTo(gameObject)) {
m_context->GetSceneManager().MarkSceneDirty();
ImGui::CloseCurrentPopup();
}
}
}
ImGui::Separator();
ImGui::TextDisabled("No more components available");
Debug::Logger::Get().Debug(Debug::LogCategory::General, "About to EndPopup - calling EndPopup");
if (!drewAnyEntry) {
ImGui::TextDisabled("No registered component editors");
}
ImGui::EndPopup();
Debug::Logger::Get().Debug(Debug::LogCategory::General, "Popup closed");
}
void InspectorPanel::RenderComponent(::XCEngine::Components::Component* component, ::XCEngine::Components::GameObject* gameObject) {
if (!component) return;
IComponentEditor* editor = GetEditorFor(component);
const char* name = component->GetName().c_str();
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{4, 2});
@@ -138,8 +168,9 @@ void InspectorPanel::RenderComponent(::XCEngine::Components::Component* componen
ImGui::PopStyleVar();
bool removeComponent = false;
const bool canRemoveComponent = editor ? editor->CanRemove(component) : false;
if (ImGui::BeginPopupContextItem("ComponentSettings")) {
if (ImGui::MenuItem("Remove Component")) {
if (ImGui::MenuItem("Remove Component", nullptr, false, canRemoveComponent)) {
removeComponent = true;
}
ImGui::EndPopup();
@@ -151,22 +182,12 @@ void InspectorPanel::RenderComponent(::XCEngine::Components::Component* componen
}
if (open) {
if (auto* transform = dynamic_cast<::XCEngine::Components::TransformComponent*>(component)) {
::XCEngine::Math::Vector3 position = transform->GetLocalPosition();
::XCEngine::Math::Vector3 rotation = transform->GetLocalEulerAngles();
::XCEngine::Math::Vector3 scale = transform->GetLocalScale();
if (UI::DrawVec3("Position", position, 0.0f, 80.0f, 0.1f)) {
transform->SetLocalPosition(position);
}
if (UI::DrawVec3("Rotation", rotation, 0.0f, 80.0f, 1.0f)) {
transform->SetLocalEulerAngles(rotation);
}
if (UI::DrawVec3("Scale", scale, 1.0f, 80.0f, 0.1f)) {
transform->SetLocalScale(scale);
if (editor) {
if (editor->Render(component)) {
m_context->GetSceneManager().MarkSceneDirty();
}
} else {
ImGui::TextDisabled("No registered editor for this component");
}
ImGui::TreePop();
@@ -179,14 +200,9 @@ void InspectorPanel::RemoveComponentByType(::XCEngine::Components::Component* co
if (dynamic_cast<::XCEngine::Components::TransformComponent*>(component)) {
return;
}
auto components = gameObject->GetComponents<::XCEngine::Components::Component>();
for (auto* comp : components) {
if (comp == component) {
gameObject->RemoveComponent<::XCEngine::Components::Component>();
break;
}
}
gameObject->RemoveComponent(component);
m_context->GetSceneManager().MarkSceneDirty();
}
}