From aecaf86dc0bb30d6ccdfa42e89091dd6d67ed432 Mon Sep 17 00:00:00 2001 From: Atdunbg <979541498@qq.com> Date: Fri, 20 Jun 2025 21:07:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=80=E5=8D=95=E5=9C=BA?= =?UTF-8?q?=E6=99=AFctrl+s=E4=BF=9D=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sandbox/src/Editor/EditorLayer.cpp | 127 ++++++++++-------- Sandbox/src/Editor/EditorLayer.h | 6 +- .../src/Editor/Panels/SceneHierachyPanel.cpp | 37 ++--- 3 files changed, 96 insertions(+), 74 deletions(-) diff --git a/Sandbox/src/Editor/EditorLayer.cpp b/Sandbox/src/Editor/EditorLayer.cpp index c6f8aa9..de11441 100644 --- a/Sandbox/src/Editor/EditorLayer.cpp +++ b/Sandbox/src/Editor/EditorLayer.cpp @@ -190,7 +190,7 @@ namespace Hazel if (ImGui::MenuItem("Open...", "Ctrl+O")) OpenScene(); if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) - SaveScene(); + SaveSceneAs(); ImGui::Separator(); if (ImGui::MenuItem("Exit")) { Hazel::Application::Get().Close(); } @@ -222,11 +222,11 @@ namespace Hazel ImGui::Separator(); - std::string name = "none"; - if (m_HoveredEntity) - name = m_HoveredEntity.GetComponent().Tag; + // std::string name = "none"; + // if (m_HoveredEntity) + // name = m_HoveredEntity.GetComponent().Tag; - ImGui::Text("Hovered Entity: %s", name.c_str()); + // ImGui::Text("Hovered Entity: %s", name.c_str()); ImGui::End(); } @@ -348,13 +348,12 @@ namespace Hazel } } - void EditorLayer::SaveScene() const + void EditorLayer::SaveSceneAs() const { std::string filepath = FileDiaglogs::SaveFile("Hazel Scene (*.scene,*.yaml)\0*.scene;*.yaml\0*\0*.*\0\0"); if (!filepath.empty()) { - SceneSerializer serializer(m_ActiveScene); - serializer.Serialize(filepath); + SerializeScene(m_ActiveScene, filepath); } } @@ -380,17 +379,9 @@ namespace Hazel m_ActiveScene = m_EditorScene; m_SceneHierachyPanel.SetContext(m_ActiveScene); + + m_EditorScenePath = scenePath; } - - /* - m_ActiveScene = CreateRef(); - m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); - m_SceneHierachyPanel.SetContext(m_ActiveScene); - - SceneSerializer serializer(m_ActiveScene); - serializer.Deserialize(scenePath.string()); - */ - } void EditorLayer::NewScene() @@ -398,6 +389,22 @@ namespace Hazel m_ActiveScene = CreateRef(); m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); m_SceneHierachyPanel.SetContext(m_ActiveScene); + + m_EditorScenePath = std::filesystem::path(); + } + + void EditorLayer::SaveScene() const + { + if (!m_EditorScenePath.empty()) + { + SerializeScene(m_ActiveScene, m_EditorScenePath); + } + } + + void EditorLayer::SerializeScene(Ref scene, const std::filesystem::path& scenePath) const + { + const SceneSerializer serializer(scene); + serializer.Serialize(scenePath.string()); } void EditorLayer::OnDuplicateEntity() @@ -411,6 +418,7 @@ namespace Hazel void EditorLayer::OnScenePlay() { + SaveScene(); m_SceneState = SceneState::Play; m_ActiveScene = Scene::Copy(m_EditorScene); @@ -444,51 +452,58 @@ namespace Hazel if (e.button.clicks && m_ViewportHovered && !ImGuizmo::IsOver() && Input::IsMouseButtonPressed(SDL_BUTTON_LEFT) && !Input::IsKeyPressed(SDL_SCANCODE_LALT)) m_SceneHierachyPanel.SetSelectedEntity(m_HoveredEntity); -#define SHORTCUT_EXIT (SDL_KMOD_CTRL | SDLK_W) -#define SHORTCUT_NEW (SDL_KMOD_CTRL | SDLK_N) -#define SHORTCUT_OPEN (SDL_KMOD_CTRL | SDLK_O) -#define SHORTCUT_SAVE_ALL (SDL_KMOD_CTRL | SDL_KMOD_SHIFT | SDLK_S) -#define SHORTCUT_CTRL_D (SDL_KMOD_CTRL | SDLK_D) +#define SHORTCUT_EXIT (SDL_KMOD_CTRL + SDLK_W) +#define SHORTCUT_NEW (SDL_KMOD_CTRL + SDLK_N) +#define SHORTCUT_OPEN (SDL_KMOD_CTRL + SDLK_O) +#define SHORTCUT_SAVE (SDL_KMOD_CTRL + SDLK_S) +#define SHORTCUT_SAVE_AS (SDL_KMOD_CTRL + SDL_KMOD_SHIFT + SDLK_S) +#define SHORTCUT_CTRL_D (SDL_KMOD_CTRL + SDLK_D) + const auto mod = SDL_GetModState(); const auto ctrl = (mod & SDL_KMOD_CTRL) ? SDL_KMOD_CTRL : 0; const auto shift = (mod & SDL_KMOD_SHIFT) ? SDL_KMOD_SHIFT : 0; if (e.key.down && e.key.repeat == 0) - switch (ctrl | shift | e.key.key) - { - case SHORTCUT_EXIT: - Application::Get().Close(); - break; - case SHORTCUT_NEW: - NewScene(); - break; - case SHORTCUT_OPEN: - OpenScene(); - break; - case SHORTCUT_SAVE_ALL: - SaveScene(); - break; - case SHORTCUT_CTRL_D: - OnDuplicateEntity(); - break; + { + switch (ctrl + shift + e.key.key) + { + case SHORTCUT_EXIT: + Application::Get().Close(); + break; + case SHORTCUT_NEW: + NewScene(); + break; + case SHORTCUT_OPEN: + OpenScene(); + break; + case SHORTCUT_SAVE: + SaveScene(); + break; + case SHORTCUT_SAVE_AS: + SaveSceneAs(); + break; + case SHORTCUT_CTRL_D: + OnDuplicateEntity(); + break; - // GIZMO - case SDLK_Q: - ChangeOptMode(-1); - break; - case SDLK_W: - ChangeOptMode(ImGuizmo::OPERATION::TRANSLATE); - break; - case SDLK_E: - ChangeOptMode(ImGuizmo::OPERATION::SCALE); - break; - case SDLK_R: - ChangeOptMode(ImGuizmo::OPERATION::ROTATE); - break; + // GIZMO + case SDLK_Q: + ChangeOptMode(-1); + break; + case SDLK_W: + ChangeOptMode(ImGuizmo::OPERATION::TRANSLATE); + break; + case SDLK_E: + ChangeOptMode(ImGuizmo::OPERATION::SCALE); + break; + case SDLK_R: + ChangeOptMode(ImGuizmo::OPERATION::ROTATE); + break; - default: - break; - } + default: + break; + } + } } void EditorLayer::UI_ToolBar() diff --git a/Sandbox/src/Editor/EditorLayer.h b/Sandbox/src/Editor/EditorLayer.h index feb3383..57ab064 100644 --- a/Sandbox/src/Editor/EditorLayer.h +++ b/Sandbox/src/Editor/EditorLayer.h @@ -29,10 +29,13 @@ namespace Hazel void OnScenePlay(); void OnSceneStop(); - void SaveScene() const; + void SaveSceneAs() const; void OpenScene(); void OpenScene(const std::filesystem::path& scenePath); void NewScene(); + void SaveScene() const; + + void SerializeScene(Ref scene, const std::filesystem::path& scenePath) const; void OnDuplicateEntity(); @@ -48,6 +51,7 @@ namespace Hazel Ref m_ActiveScene; Ref m_EditorScene; EditorCamera m_EditorCamera; + std::filesystem::path m_EditorScenePath; Entity m_HoveredEntity; diff --git a/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp b/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp index e10a56a..2baa2f8 100644 --- a/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp +++ b/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp @@ -27,27 +27,30 @@ namespace Hazel void SceneHierachyPanel::OnImGuiRender() { - ImGui::Begin("Scene Hierachy"); - - m_Context->m_Registry.view().each([&](auto entityID) + if (m_Context) { - DrawEntityNode({entityID, m_Context.get()}); - }); + ImGui::Begin("Scene Hierachy"); - if (ImGui::IsMouseDown(0) && ImGui::IsWindowHovered()) - { - m_SelectionContext = { }; + m_Context->m_Registry.view().each([&](auto entityID) + { + DrawEntityNode({entityID, m_Context.get()}); + }); + + if (ImGui::IsMouseDown(0) && ImGui::IsWindowHovered()) + { + m_SelectionContext = {}; + } + + if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems)) + { + if (ImGui::MenuItem("Create Empty Entity")) + m_Context->CreateEntity("Empty Entity"); + ImGui::EndPopup(); + } + + ImGui::End(); } - if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight | ImGuiPopupFlags_NoOpenOverItems)) - { - if (ImGui::MenuItem("Create Empty Entity")) - m_Context->CreateEntity("Empty Entity"); - ImGui::EndPopup(); - } - - ImGui::End(); - ImGui::Begin("Properties"); if (m_SelectionContext) {