Files
XCEngine/editor/src/Layers/EditorLayer.cpp

85 lines
2.1 KiB
C++
Raw Normal View History

2026-03-26 21:18:33 +08:00
#include "Commands/SceneCommands.h"
#include "EditorLayer.h"
2026-03-26 21:18:33 +08:00
#include "Layout/DockLayoutController.h"
#include "panels/MenuBar.h"
#include "panels/HierarchyPanel.h"
#include "panels/SceneViewPanel.h"
#include "panels/GameViewPanel.h"
#include "panels/InspectorPanel.h"
#include "panels/ConsolePanel.h"
#include "panels/ProjectPanel.h"
#include "Core/IEditorContext.h"
#include "Core/EditorContext.h"
#include "Core/IUndoManager.h"
#include <filesystem>
#include <imgui.h>
namespace XCEngine {
namespace Editor {
2026-03-26 21:18:33 +08:00
namespace {
std::string BuildFallbackScenePath(const IEditorContext& context) {
return (std::filesystem::path(context.GetProjectPath()) / "Assets" / "Scenes" / "Main.xc").string();
}
} // namespace
EditorLayer::EditorLayer() : Layer("Editor") {}
void EditorLayer::SetContext(std::shared_ptr<IEditorContext> context) {
m_context = context;
}
void EditorLayer::onAttach() {
if (!m_context) {
m_context = std::make_shared<EditorContext>();
}
2026-03-26 21:18:33 +08:00
m_panels.Clear();
m_panels.SetContext(m_context.get());
m_panels.Emplace<MenuBar>();
m_panels.Emplace<HierarchyPanel>();
m_panels.Emplace<SceneViewPanel>();
m_panels.Emplace<GameViewPanel>();
m_panels.Emplace<InspectorPanel>();
m_panels.Emplace<ConsolePanel>();
m_projectPanel = &m_panels.Emplace<ProjectPanel>();
m_dockLayoutController = std::make_unique<DockLayoutController>();
m_projectPanel->Initialize(m_context->GetProjectPath());
2026-03-26 21:18:33 +08:00
Commands::LoadStartupScene(*m_context);
m_dockLayoutController->Attach(*m_context);
m_panels.AttachAll();
}
void EditorLayer::onDetach() {
2026-03-26 21:18:33 +08:00
if (m_context) {
Commands::SaveDirtySceneWithFallback(*m_context, BuildFallbackScenePath(*m_context));
}
if (m_dockLayoutController) {
m_dockLayoutController->Detach();
}
2026-03-26 21:18:33 +08:00
m_panels.DetachAll();
m_panels.Clear();
m_projectPanel = nullptr;
}
void EditorLayer::onUpdate(float dt) {
2026-03-26 21:18:33 +08:00
m_panels.UpdateAll(dt);
}
void EditorLayer::onEvent(void* event) {
2026-03-26 21:18:33 +08:00
m_panels.DispatchEvent(event);
}
void EditorLayer::onImGuiRender() {
2026-03-26 21:18:33 +08:00
m_dockLayoutController->RenderDockspace();
m_panels.RenderAll();
}
}
}