Files
XCEngine/editor/src/panels/MenuBar.cpp

143 lines
4.9 KiB
C++
Raw Normal View History

2026-03-26 22:10:43 +08:00
#include "Actions/ActionRouting.h"
2026-03-26 22:31:22 +08:00
#include "Actions/EditActionRouter.h"
2026-03-26 21:18:33 +08:00
#include "Actions/EditorActions.h"
#include "Commands/SceneCommands.h"
#include "MenuBar.h"
2026-03-26 21:18:33 +08:00
#include "Core/EditorEvents.h"
#include "Core/EventBus.h"
#include "Core/IEditorContext.h"
#include "Core/ISceneManager.h"
2026-03-26 21:18:33 +08:00
#include "UI/UI.h"
#include <filesystem>
#include <imgui.h>
namespace XCEngine {
namespace Editor {
MenuBar::MenuBar() : Panel("MenuBar") {}
void MenuBar::Render() {
HandleShortcuts();
if (ImGui::BeginMainMenuBar()) {
ShowFileMenu();
ShowEditMenu();
ShowViewMenu();
ShowHelpMenu();
RenderSceneStatus();
ImGui::EndMainMenuBar();
}
2026-03-26 21:18:33 +08:00
RenderAboutPopup();
}
2026-03-26 21:18:33 +08:00
void MenuBar::HandleShortcuts() {
if (!m_context) {
return;
}
2026-03-26 22:10:43 +08:00
const Actions::ShortcutContext shortcutContext = Actions::GlobalShortcutContext();
Actions::HandleShortcut(Actions::MakeNewSceneAction(), shortcutContext, [&]() { Commands::NewScene(*m_context); });
Actions::HandleShortcut(Actions::MakeOpenSceneAction(), shortcutContext, [&]() { Commands::OpenSceneWithDialog(*m_context); });
Actions::HandleShortcut(Actions::MakeSaveSceneAction(), shortcutContext, [&]() { Commands::SaveCurrentScene(*m_context); });
Actions::HandleShortcut(Actions::MakeSaveSceneAsAction(), shortcutContext, [&]() { Commands::SaveSceneAsWithDialog(*m_context); });
Actions::HandleShortcut(Actions::MakeUndoAction(*m_context), shortcutContext, [&]() { m_context->GetUndoManager().Undo(); });
Actions::HandleShortcut(Actions::MakeRedoAction(*m_context), shortcutContext, [&]() { m_context->GetUndoManager().Redo(); });
2026-03-26 22:31:22 +08:00
Actions::HandleEditShortcuts(*m_context, shortcutContext);
}
2026-03-26 21:18:33 +08:00
void MenuBar::ShowFileMenu() {
UI::DrawMenuScope("File", [&]() {
Actions::DrawMenuAction(Actions::MakeNewSceneAction(), [&]() { Commands::NewScene(*m_context); });
Actions::DrawMenuAction(Actions::MakeOpenSceneAction(), [&]() { Commands::OpenSceneWithDialog(*m_context); });
Actions::DrawMenuAction(Actions::MakeSaveSceneAction(), [&]() { Commands::SaveCurrentScene(*m_context); });
Actions::DrawMenuAction(Actions::MakeSaveSceneAsAction(), [&]() { Commands::SaveSceneAsWithDialog(*m_context); });
Actions::DrawMenuSeparator();
Actions::DrawMenuAction(Actions::MakeExitAction(), [&]() {
m_context->GetEventBus().Publish(EditorExitRequestedEvent{});
});
});
}
2026-03-26 21:18:33 +08:00
void MenuBar::ShowEditMenu() {
UI::DrawMenuScope("Edit", [&]() {
2026-03-26 22:31:22 +08:00
Actions::DrawEditActions(*m_context);
2026-03-26 21:18:33 +08:00
});
}
2026-03-26 21:18:33 +08:00
void MenuBar::ShowViewMenu() {
UI::DrawMenuScope("View", [&]() {
Actions::DrawMenuAction(Actions::MakeResetLayoutAction(), [&]() {
m_context->GetEventBus().Publish(DockLayoutResetRequestedEvent{});
});
});
}
2026-03-26 21:18:33 +08:00
void MenuBar::ShowHelpMenu() {
UI::DrawMenuScope("Help", [&]() {
Actions::DrawMenuAction(Actions::MakeAboutAction(), [&]() {
m_aboutPopup.RequestOpen();
});
});
}
2026-03-26 21:18:33 +08:00
void MenuBar::RenderAboutPopup() {
m_aboutPopup.ConsumeOpenRequest("About XCEngine Editor");
2026-03-26 21:18:33 +08:00
if (!UI::BeginModalPopup("About XCEngine Editor")) {
return;
}
2026-03-26 21:18:33 +08:00
ImGui::TextUnformatted("XCEngine Editor");
ImGui::Separator();
UI::DrawHintText("Unity-like editor shell built on Dear ImGui.");
ImGui::Spacing();
ImGui::Text("Date: 2026-03-26");
ImGui::Text("UI Refactor: Actions / Commands / Layout in progress");
if (m_context) {
ImGui::Text("Project: %s", m_context->GetProjectPath().c_str());
}
2026-03-26 21:18:33 +08:00
ImGui::Spacing();
2026-03-26 21:18:33 +08:00
if (Actions::DrawButtonAction(Actions::MakeAction("Close"), UI::DialogActionButtonSize())) {
ImGui::CloseCurrentPopup();
}
2026-03-26 21:18:33 +08:00
UI::EndPopup();
}
void MenuBar::RenderSceneStatus() {
if (!m_context) {
return;
}
auto& sceneManager = m_context->GetSceneManager();
std::string sceneLabel = sceneManager.HasActiveScene() ? sceneManager.GetCurrentSceneName() : "No Scene";
if (sceneLabel.empty()) {
sceneLabel = "Untitled Scene";
}
std::string fileLabel = sceneManager.GetCurrentScenePath().empty()
? "Unsaved.xc"
: std::filesystem::path(sceneManager.GetCurrentScenePath()).filename().string();
const bool dirty = sceneManager.IsSceneDirty();
2026-03-26 21:18:33 +08:00
const std::string statusText = dirty ? (std::string("* ") + fileLabel) : fileLabel;
UI::DrawRightAlignedText(
statusText.c_str(),
dirty ? UI::MenuBarStatusDirtyColor() : UI::MenuBarStatusIdleColor());
if (ImGui::IsItemHovered()) {
2026-03-26 21:18:33 +08:00
UI::BeginTitledTooltip("Scene");
ImGui::Text("Name: %s", sceneLabel.c_str());
ImGui::Text("File: %s", fileLabel.c_str());
ImGui::Text("State: %s", dirty ? "Modified" : "Saved");
ImGui::Text("Path: %s", sceneManager.GetCurrentScenePath().empty() ? "(not saved yet)" : sceneManager.GetCurrentScenePath().c_str());
2026-03-26 21:18:33 +08:00
UI::EndTitledTooltip();
}
}
}
}