From a8026e535a80a5e3d7effd8d2109a051043c3676 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 16:41:02 -0800 Subject: [PATCH 1/8] Add Window SetTitle --- Hazel/src/Hazel/Core/Window.h | 2 ++ Hazel/src/Platform/Windows/WindowsWindow.cpp | 5 +++++ Hazel/src/Platform/Windows/WindowsWindow.h | 4 +++- Hazelnut/src/EditorLayer.cpp | 2 ++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Hazel/src/Hazel/Core/Window.h b/Hazel/src/Hazel/Core/Window.h index baff4ce16..a969ccde6 100644 --- a/Hazel/src/Hazel/Core/Window.h +++ b/Hazel/src/Hazel/Core/Window.h @@ -39,6 +39,8 @@ namespace Hazel { virtual void SetVSync(bool enabled) = 0; virtual bool IsVSync() const = 0; + virtual void SetTitle(const std::string& title) = 0; + virtual void* GetNativeWindow() const = 0; static Scope Create(const WindowProps& props = WindowProps()); diff --git a/Hazel/src/Platform/Windows/WindowsWindow.cpp b/Hazel/src/Platform/Windows/WindowsWindow.cpp index be7684efb..db169bb0c 100644 --- a/Hazel/src/Platform/Windows/WindowsWindow.cpp +++ b/Hazel/src/Platform/Windows/WindowsWindow.cpp @@ -197,4 +197,9 @@ namespace Hazel { return m_Data.VSync; } + void WindowsWindow::SetTitle(const std::string& title) + { + glfwSetWindowTitle(m_Window, title.c_str()); + } + } diff --git a/Hazel/src/Platform/Windows/WindowsWindow.h b/Hazel/src/Platform/Windows/WindowsWindow.h index cb1d19cb2..06147c468 100644 --- a/Hazel/src/Platform/Windows/WindowsWindow.h +++ b/Hazel/src/Platform/Windows/WindowsWindow.h @@ -23,6 +23,8 @@ namespace Hazel { void SetVSync(bool enabled) override; bool IsVSync() const override; + void SetTitle(const std::string& title) override; + virtual void* GetNativeWindow() const { return m_Window; } private: virtual void Init(const WindowProps& props); @@ -43,4 +45,4 @@ namespace Hazel { WindowData m_Data; }; -} \ No newline at end of file +} diff --git a/Hazelnut/src/EditorLayer.cpp b/Hazelnut/src/EditorLayer.cpp index 454a5d4c6..11bfc435f 100644 --- a/Hazelnut/src/EditorLayer.cpp +++ b/Hazelnut/src/EditorLayer.cpp @@ -25,6 +25,8 @@ namespace Hazel { { HZ_PROFILE_FUNCTION(); + Application::Get().GetWindow().SetTitle("Test"); + m_CheckerboardTexture = Texture2D::Create("assets/textures/Checkerboard.png"); m_IconPlay = Texture2D::Create("Resources/Icons/PlayButton.png"); m_IconStop = Texture2D::Create("Resources/Icons/StopButton.png"); From 7c4187a561580c03be86b376bcfe20162aff14af Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 16:52:09 -0800 Subject: [PATCH 2/8] Add names to scenes --- Hazel/src/Hazel/Scene/Scene.cpp | 5 +++++ Hazel/src/Hazel/Scene/Scene.h | 5 +++++ Hazel/src/Hazel/Scene/SceneSerializer.cpp | 2 ++ Hazelnut/src/EditorLayer.cpp | 1 + 4 files changed, 13 insertions(+) diff --git a/Hazel/src/Hazel/Scene/Scene.cpp b/Hazel/src/Hazel/Scene/Scene.cpp index e946d4b59..a8507ac42 100644 --- a/Hazel/src/Hazel/Scene/Scene.cpp +++ b/Hazel/src/Hazel/Scene/Scene.cpp @@ -128,6 +128,11 @@ namespace Hazel { return {}; } + void Scene::SetName(const std::string& name) + { + m_Name = name; + } + template void Scene::OnComponentAdded(Entity entity, T& component) { diff --git a/Hazel/src/Hazel/Scene/Scene.h b/Hazel/src/Hazel/Scene/Scene.h index a6374660e..e4e489854 100644 --- a/Hazel/src/Hazel/Scene/Scene.h +++ b/Hazel/src/Hazel/Scene/Scene.h @@ -23,6 +23,9 @@ namespace Hazel { void OnViewportResize(uint32_t width, uint32_t height); Entity GetPrimaryCameraEntity(); + + void SetName(const std::string& name); + const std::string& GetName() const { return m_Name; } private: template void OnComponentAdded(Entity entity, T& component); @@ -30,6 +33,8 @@ namespace Hazel { entt::registry m_Registry; uint32_t m_ViewportWidth = 0, m_ViewportHeight = 0; + std::string m_Name = "Untitled"; + friend class Entity; friend class SceneSerializer; friend class SceneHierarchyPanel; diff --git a/Hazel/src/Hazel/Scene/SceneSerializer.cpp b/Hazel/src/Hazel/Scene/SceneSerializer.cpp index e30f5967d..7e82393ea 100644 --- a/Hazel/src/Hazel/Scene/SceneSerializer.cpp +++ b/Hazel/src/Hazel/Scene/SceneSerializer.cpp @@ -252,6 +252,8 @@ namespace Hazel { } } + m_Scene->SetName(std::filesystem::path(filepath).filename().string()); + return true; } diff --git a/Hazelnut/src/EditorLayer.cpp b/Hazelnut/src/EditorLayer.cpp index 11bfc435f..b1ca50d9b 100644 --- a/Hazelnut/src/EditorLayer.cpp +++ b/Hazelnut/src/EditorLayer.cpp @@ -499,6 +499,7 @@ namespace Hazel { { SceneSerializer serializer(m_ActiveScene); serializer.Serialize(filepath); + m_ActiveScene->SetName(std::filesystem::path(filepath).filename().string()); } } From e7bad3bce5064499d687b964c0800ba11ff13738 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 16:54:19 -0800 Subject: [PATCH 3/8] Consolidate changing active scene and update title --- Hazelnut/src/EditorLayer.cpp | 42 +++++++++++++++++++++++++----------- Hazelnut/src/EditorLayer.h | 3 +++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Hazelnut/src/EditorLayer.cpp b/Hazelnut/src/EditorLayer.cpp index b1ca50d9b..bad7ca8ca 100644 --- a/Hazelnut/src/EditorLayer.cpp +++ b/Hazelnut/src/EditorLayer.cpp @@ -25,8 +25,6 @@ namespace Hazel { { HZ_PROFILE_FUNCTION(); - Application::Get().GetWindow().SetTitle("Test"); - m_CheckerboardTexture = Texture2D::Create("assets/textures/Checkerboard.png"); m_IconPlay = Texture2D::Create("Resources/Icons/PlayButton.png"); m_IconStop = Texture2D::Create("Resources/Icons/StopButton.png"); @@ -37,14 +35,20 @@ namespace Hazel { fbSpec.Height = 720; m_Framebuffer = Framebuffer::Create(fbSpec); - m_ActiveScene = CreateRef(); - auto commandLineArgs = Application::Get().GetCommandLineArgs(); if (commandLineArgs.Count > 1) { + Ref startScene = CreateRef(); + auto sceneFilePath = commandLineArgs[1]; - SceneSerializer serializer(m_ActiveScene); + SceneSerializer serializer(startScene); serializer.Deserialize(sceneFilePath); + + SetActiveScene(startScene); + } + else + { + NewScene(); } m_EditorCamera = EditorCamera(30.0f, 1.778f, 0.1f, 1000.0f); @@ -99,8 +103,6 @@ namespace Hazel { m_CameraEntity.AddComponent().Bind(); m_SecondCamera.AddComponent().Bind(); #endif - - m_SceneHierarchyPanel.SetContext(m_ActiveScene); } void EditorLayer::OnDetach() @@ -462,9 +464,8 @@ namespace Hazel { void EditorLayer::NewScene() { - m_ActiveScene = CreateRef(); - m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y); - m_SceneHierarchyPanel.SetContext(m_ActiveScene); + Ref newScene = CreateRef(); + SetActiveScene(newScene); } void EditorLayer::OpenScene() @@ -486,9 +487,7 @@ namespace Hazel { SceneSerializer serializer(newScene); if (serializer.Deserialize(path.string())) { - m_ActiveScene = newScene; - m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y); - m_SceneHierarchyPanel.SetContext(m_ActiveScene); + SetActiveScene(newScene); } } @@ -500,6 +499,7 @@ namespace Hazel { SceneSerializer serializer(m_ActiveScene); serializer.Serialize(filepath); m_ActiveScene->SetName(std::filesystem::path(filepath).filename().string()); + SetWindowTitleFromActiveScene(); } } @@ -513,5 +513,21 @@ namespace Hazel { m_SceneState = SceneState::Edit; } + + void EditorLayer::SetActiveScene(const Ref& activeScene) + { + HZ_ASSERT(activeScene, "EditorLayer ActiveScene cannot be null"); + m_ActiveScene = activeScene; + m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y); + + m_SceneHierarchyPanel.SetContext(m_ActiveScene); + + SetWindowTitleFromActiveScene(); + } + + void EditorLayer::SetWindowTitleFromActiveScene() + { + Application::Get().GetWindow().SetTitle("Hazelnut - " + m_ActiveScene->GetName()); + } } diff --git a/Hazelnut/src/EditorLayer.h b/Hazelnut/src/EditorLayer.h index 35bdd9cb8..aaa8dd740 100644 --- a/Hazelnut/src/EditorLayer.h +++ b/Hazelnut/src/EditorLayer.h @@ -32,6 +32,9 @@ namespace Hazel { void OnScenePlay(); void OnSceneStop(); + void SetActiveScene(const Ref& activeScene); + void SetWindowTitleFromActiveScene(); + // UI Panels void UI_Toolbar(); private: From 0ddba76709b6fc86af5e84e7974a321611ca7cd2 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 16:56:09 -0800 Subject: [PATCH 4/8] Use filename stem --- Hazel/src/Hazel/Scene/Scene.cpp | 5 +++++ Hazel/src/Hazel/Scene/Scene.h | 1 + Hazel/src/Hazel/Scene/SceneSerializer.cpp | 2 +- Hazelnut/src/EditorLayer.cpp | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Hazel/src/Hazel/Scene/Scene.cpp b/Hazel/src/Hazel/Scene/Scene.cpp index a8507ac42..afb60748e 100644 --- a/Hazel/src/Hazel/Scene/Scene.cpp +++ b/Hazel/src/Hazel/Scene/Scene.cpp @@ -133,6 +133,11 @@ namespace Hazel { m_Name = name; } + void Scene::SetName(const std::filesystem::path& filepath) + { + m_Name = filepath.stem().string(); + } + template void Scene::OnComponentAdded(Entity entity, T& component) { diff --git a/Hazel/src/Hazel/Scene/Scene.h b/Hazel/src/Hazel/Scene/Scene.h index e4e489854..b2f5e44b4 100644 --- a/Hazel/src/Hazel/Scene/Scene.h +++ b/Hazel/src/Hazel/Scene/Scene.h @@ -25,6 +25,7 @@ namespace Hazel { Entity GetPrimaryCameraEntity(); void SetName(const std::string& name); + void SetName(const std::filesystem::path& filepath); const std::string& GetName() const { return m_Name; } private: template diff --git a/Hazel/src/Hazel/Scene/SceneSerializer.cpp b/Hazel/src/Hazel/Scene/SceneSerializer.cpp index 7e82393ea..baa7c3949 100644 --- a/Hazel/src/Hazel/Scene/SceneSerializer.cpp +++ b/Hazel/src/Hazel/Scene/SceneSerializer.cpp @@ -252,7 +252,7 @@ namespace Hazel { } } - m_Scene->SetName(std::filesystem::path(filepath).filename().string()); + m_Scene->SetName(std::filesystem::path(filepath)); return true; } diff --git a/Hazelnut/src/EditorLayer.cpp b/Hazelnut/src/EditorLayer.cpp index bad7ca8ca..e58956cf3 100644 --- a/Hazelnut/src/EditorLayer.cpp +++ b/Hazelnut/src/EditorLayer.cpp @@ -498,7 +498,7 @@ namespace Hazel { { SceneSerializer serializer(m_ActiveScene); serializer.Serialize(filepath); - m_ActiveScene->SetName(std::filesystem::path(filepath).filename().string()); + m_ActiveScene->SetName(std::filesystem::path(filepath)); SetWindowTitleFromActiveScene(); } } From 1778125d68e0263243ff9487b1b1e8f7d55ce467 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 17:01:11 -0800 Subject: [PATCH 5/8] Fix name in scene files --- Hazelnut/assets/scenes/3DExample.hazel | 2 +- Hazelnut/assets/scenes/Example.hazel | 2 +- Hazelnut/assets/scenes/PinkCube.hazel | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Hazelnut/assets/scenes/3DExample.hazel b/Hazelnut/assets/scenes/3DExample.hazel index 6d2ff15b2..464ed0ebc 100644 --- a/Hazelnut/assets/scenes/3DExample.hazel +++ b/Hazelnut/assets/scenes/3DExample.hazel @@ -1,4 +1,4 @@ -Scene: Untitled +Scene: 3DExample Entities: - Entity: 12837192831273 TagComponent: diff --git a/Hazelnut/assets/scenes/Example.hazel b/Hazelnut/assets/scenes/Example.hazel index 9d177bd46..1535f9e76 100644 --- a/Hazelnut/assets/scenes/Example.hazel +++ b/Hazelnut/assets/scenes/Example.hazel @@ -1,4 +1,4 @@ -Scene: Untitled +Scene: Example Entities: - Entity: 12837192831273 TagComponent: diff --git a/Hazelnut/assets/scenes/PinkCube.hazel b/Hazelnut/assets/scenes/PinkCube.hazel index 2142971ef..3cff7ef20 100644 --- a/Hazelnut/assets/scenes/PinkCube.hazel +++ b/Hazelnut/assets/scenes/PinkCube.hazel @@ -1,4 +1,4 @@ -Scene: Untitled +Scene: PinkCube Entities: - Entity: 12837192831273 TagComponent: From 2e5bc9c011dc077138b8786a3baa9e0a977a5739 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 17:02:19 -0800 Subject: [PATCH 6/8] Use scene name in file --- Hazel/src/Hazel/Scene/Scene.cpp | 5 ----- Hazel/src/Hazel/Scene/Scene.h | 1 - Hazel/src/Hazel/Scene/SceneSerializer.cpp | 5 ++--- Hazelnut/src/EditorLayer.cpp | 2 -- 4 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Hazel/src/Hazel/Scene/Scene.cpp b/Hazel/src/Hazel/Scene/Scene.cpp index afb60748e..a8507ac42 100644 --- a/Hazel/src/Hazel/Scene/Scene.cpp +++ b/Hazel/src/Hazel/Scene/Scene.cpp @@ -133,11 +133,6 @@ namespace Hazel { m_Name = name; } - void Scene::SetName(const std::filesystem::path& filepath) - { - m_Name = filepath.stem().string(); - } - template void Scene::OnComponentAdded(Entity entity, T& component) { diff --git a/Hazel/src/Hazel/Scene/Scene.h b/Hazel/src/Hazel/Scene/Scene.h index b2f5e44b4..e4e489854 100644 --- a/Hazel/src/Hazel/Scene/Scene.h +++ b/Hazel/src/Hazel/Scene/Scene.h @@ -25,7 +25,6 @@ namespace Hazel { Entity GetPrimaryCameraEntity(); void SetName(const std::string& name); - void SetName(const std::filesystem::path& filepath); const std::string& GetName() const { return m_Name; } private: template diff --git a/Hazel/src/Hazel/Scene/SceneSerializer.cpp b/Hazel/src/Hazel/Scene/SceneSerializer.cpp index baa7c3949..d54eef411 100644 --- a/Hazel/src/Hazel/Scene/SceneSerializer.cpp +++ b/Hazel/src/Hazel/Scene/SceneSerializer.cpp @@ -156,7 +156,7 @@ namespace Hazel { { YAML::Emitter out; out << YAML::BeginMap; - out << YAML::Key << "Scene" << YAML::Value << "Untitled"; + out << YAML::Key << "Scene" << YAML::Value << m_Scene->GetName(); out << YAML::Key << "Entities" << YAML::Value << YAML::BeginSeq; m_Scene->m_Registry.each([&](auto entityID) { @@ -196,6 +196,7 @@ namespace Hazel { std::string sceneName = data["Scene"].as(); HZ_CORE_TRACE("Deserializing scene '{0}'", sceneName); + m_Scene->SetName(sceneName); auto entities = data["Entities"]; if (entities) @@ -252,8 +253,6 @@ namespace Hazel { } } - m_Scene->SetName(std::filesystem::path(filepath)); - return true; } diff --git a/Hazelnut/src/EditorLayer.cpp b/Hazelnut/src/EditorLayer.cpp index e58956cf3..9666db78a 100644 --- a/Hazelnut/src/EditorLayer.cpp +++ b/Hazelnut/src/EditorLayer.cpp @@ -498,8 +498,6 @@ namespace Hazel { { SceneSerializer serializer(m_ActiveScene); serializer.Serialize(filepath); - m_ActiveScene->SetName(std::filesystem::path(filepath)); - SetWindowTitleFromActiveScene(); } } From 36d30106f1aa2c4356b8853d74a7b448050748d1 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 17:17:35 -0800 Subject: [PATCH 7/8] Track last loaded scene path --- Hazelnut/src/EditorLayer.cpp | 51 +++++++++++++++++++++++++----------- Hazelnut/src/EditorLayer.h | 7 +++-- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/Hazelnut/src/EditorLayer.cpp b/Hazelnut/src/EditorLayer.cpp index 9666db78a..3a1ecf80c 100644 --- a/Hazelnut/src/EditorLayer.cpp +++ b/Hazelnut/src/EditorLayer.cpp @@ -35,18 +35,23 @@ namespace Hazel { fbSpec.Height = 720; m_Framebuffer = Framebuffer::Create(fbSpec); + // When setting the active scene later in this method, we try to resize its viewport + // If viewport size is still 0, we'll assert + // So set it to some valid value now + // Then later in OnUpdate()/OnImGuiRender() fix it to the correct value + m_ViewportSize = { fbSpec.Width, fbSpec.Height }; + + // Try to load a scene from command line args + // If it fails (no args, invalid scene, etc.) then fallback to default new untitled scene + bool loadedScene = false; + auto commandLineArgs = Application::Get().GetCommandLineArgs(); if (commandLineArgs.Count > 1) { - Ref startScene = CreateRef(); - - auto sceneFilePath = commandLineArgs[1]; - SceneSerializer serializer(startScene); - serializer.Deserialize(sceneFilePath); - - SetActiveScene(startScene); + loadedScene = OpenScene(std::filesystem::path(commandLineArgs[1])); } - else + + if (!loadedScene) { NewScene(); } @@ -466,6 +471,7 @@ namespace Hazel { { Ref newScene = CreateRef(); SetActiveScene(newScene); + SetEditorScenePath(std::filesystem::path()); } void EditorLayer::OpenScene() @@ -475,20 +481,24 @@ namespace Hazel { OpenScene(filepath); } - void EditorLayer::OpenScene(const std::filesystem::path& path) + bool EditorLayer::OpenScene(const std::filesystem::path& path) { if (path.extension().string() != ".hazel") { HZ_WARN("Could not load {0} - not a scene file", path.filename().string()); - return; + return false; } Ref newScene = CreateRef(); SceneSerializer serializer(newScene); - if (serializer.Deserialize(path.string())) + if (!serializer.Deserialize(path.string())) { - SetActiveScene(newScene); + return false; } + + SetActiveScene(newScene); + SetEditorScenePath(path); + return true; } void EditorLayer::SaveSceneAs() @@ -498,6 +508,7 @@ namespace Hazel { { SceneSerializer serializer(m_ActiveScene); serializer.Serialize(filepath); + SetEditorScenePath(std::filesystem::path(filepath)); } } @@ -521,11 +532,21 @@ namespace Hazel { m_SceneHierarchyPanel.SetContext(m_ActiveScene); - SetWindowTitleFromActiveScene(); + SyncWindowTitle(); + } + + void EditorLayer::SetEditorScenePath(const std::filesystem::path& path) + { + m_EditorScenePath = path; + SyncWindowTitle(); } - void EditorLayer::SetWindowTitleFromActiveScene() + void EditorLayer::SyncWindowTitle() { - Application::Get().GetWindow().SetTitle("Hazelnut - " + m_ActiveScene->GetName()); + std::string title = "Hazelnut"; + title += " - " + m_ActiveScene->GetName(); + title += " (" + (m_EditorScenePath.empty() ? "unsaved" : m_EditorScenePath.string()) + ")"; + + Application::Get().GetWindow().SetTitle(title); } } diff --git a/Hazelnut/src/EditorLayer.h b/Hazelnut/src/EditorLayer.h index aaa8dd740..67142c534 100644 --- a/Hazelnut/src/EditorLayer.h +++ b/Hazelnut/src/EditorLayer.h @@ -26,14 +26,15 @@ namespace Hazel { void NewScene(); void OpenScene(); - void OpenScene(const std::filesystem::path& path); + bool OpenScene(const std::filesystem::path& path); void SaveSceneAs(); void OnScenePlay(); void OnSceneStop(); void SetActiveScene(const Ref& activeScene); - void SetWindowTitleFromActiveScene(); + void SetEditorScenePath(const std::filesystem::path& path); + void SyncWindowTitle(); // UI Panels void UI_Toolbar(); @@ -46,6 +47,8 @@ namespace Hazel { Ref m_Framebuffer; Ref m_ActiveScene; + std::filesystem::path m_EditorScenePath; + Entity m_SquareEntity; Entity m_CameraEntity; Entity m_SecondCamera; From 1871d71881d69490412d37059d40b3653bed1896 Mon Sep 17 00:00:00 2001 From: Ben Cooper Date: Mon, 27 Dec 2021 17:29:35 -0800 Subject: [PATCH 8/8] Update WindowsWindow.cpp --- Hazel/src/Platform/Windows/WindowsWindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hazel/src/Platform/Windows/WindowsWindow.cpp b/Hazel/src/Platform/Windows/WindowsWindow.cpp index db169bb0c..032f1f185 100644 --- a/Hazel/src/Platform/Windows/WindowsWindow.cpp +++ b/Hazel/src/Platform/Windows/WindowsWindow.cpp @@ -199,7 +199,7 @@ namespace Hazel { void WindowsWindow::SetTitle(const std::string& title) { + m_Data.Title = title; glfwSetWindowTitle(m_Window, title.c_str()); } - }