diff --git a/Editor/src/Events/EditorRequests.h b/Editor/src/Events/EditorRequests.h index fc567bc9..8987e908 100644 --- a/Editor/src/Events/EditorRequests.h +++ b/Editor/src/Events/EditorRequests.h @@ -1,6 +1,8 @@ #include #include +#include + namespace Nuake { class Scene; @@ -12,6 +14,7 @@ class EditorRequests { private: MulticastDelegate> requestLoadScene; + MulticastDelegate requestCloseEditorWindow; EditorRequests() = default; ~EditorRequests() = default; @@ -24,10 +27,18 @@ public: } public: + // Broadcast requests void RequestLoadScene(Ref sceneFile) { requestLoadScene.Broadcast(sceneFile); } + + void RequestCloseEditorWindow(std::string windowId) + { + requestCloseEditorWindow.Broadcast(windowId.c_str()); + } + // Subcribe to requests + auto& OnRequestCloseEditorWindow() { return requestCloseEditorWindow; } auto& OnRequestLoadScene() { return requestLoadScene; } }; \ No newline at end of file diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 56a203cf..cea20ce0 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -153,6 +153,7 @@ namespace Nuake { EditorRequests& requests = EditorRequests::Get(); requests.OnRequestLoadScene().AddRaw(this, &EditorInterface::OnRequestLoadScene); + requests.OnRequestCloseEditorWindow().AddRaw(this, &EditorInterface::OnRequestCloseEditorWindow); Engine::OnSceneLoaded.AddRaw(this, &EditorInterface::OnSceneLoaded); } @@ -2134,6 +2135,19 @@ namespace Nuake { OpenSceneWindow(file->GetRelativePath()); } + void EditorInterface::OnRequestCloseEditorWindow(std::string windowName) + { + for (size_t i = 0; i < std::size(sceneEditors); i++) + { + // Erase window from sceneEditors + if (auto window = sceneEditors[i]; window->GetWindowName() == windowName) + { + sceneEditors.erase(std::begin(sceneEditors) + i); + return; + } + } + } + void EditorInterface::OpenPrefabWindow(const std::string& prefabPath) { if (!FileSystem::FileExists(prefabPath)) @@ -2807,7 +2821,9 @@ namespace Nuake { prefabEditors->Draw(); } - for (auto& sceneEditor : sceneEditors) + // We need to cache, because we might delete one while iterating + auto cachedEditors = sceneEditors; + for (auto& sceneEditor : cachedEditors) { sceneEditor->Draw(); } diff --git a/Editor/src/Windows/EditorInterface.h b/Editor/src/Windows/EditorInterface.h index 15f5b4eb..0b26fe28 100644 --- a/Editor/src/Windows/EditorInterface.h +++ b/Editor/src/Windows/EditorInterface.h @@ -125,7 +125,10 @@ namespace Nuake void DrawProjectSettings(); void Overlay(); + // Signals void OnRequestLoadScene(Ref file); + void OnRequestCloseEditorWindow(std::string windowName); + void OpenPrefabWindow(const std::string& prefabPath); void OpenSceneWindow(const std::string& scenePath); diff --git a/Editor/src/Windows/SceneEditor/SceneEditorWindow.cpp b/Editor/src/Windows/SceneEditor/SceneEditorWindow.cpp index 5bf21f8d..7fa5b966 100644 --- a/Editor/src/Windows/SceneEditor/SceneEditorWindow.cpp +++ b/Editor/src/Windows/SceneEditor/SceneEditorWindow.cpp @@ -9,6 +9,7 @@ #include "src/Scene/Scene.h" #include "src/UI/ImUI.h" +#include "../../Events/EditorRequests.h" using namespace Nuake; @@ -33,8 +34,7 @@ void SceneEditorWindow::Update(float ts) void SceneEditorWindow::Draw() { - Ref scene = editorContext.GetScene(); - const std::string sceneName = scene->Path; + const std::string sceneName = editorContext.GetScene()->Path; // This is to prevent other windows of other scene editors to dock ImGuiWindowClass windowClass; @@ -43,7 +43,8 @@ void SceneEditorWindow::Draw() ImGui::SetNextWindowClass(&windowClass); ImGui::SetNextWindowSizeConstraints({1280, 720}, { FLT_MAX, FLT_MAX }); - if (ImGui::Begin(std::string(ICON_FA_WINDOW_MAXIMIZE + std::string(" ") + sceneName).c_str())) + bool shouldStayOpen = true; + if (ImGui::Begin(std::string(ICON_FA_WINDOW_MAXIMIZE + std::string(" ") + sceneName).c_str(), &shouldStayOpen)) { ImGuiWindowClass localSceneEditorClass; localSceneEditorClass.ClassId = ImHashStr(sceneName.c_str()); @@ -73,5 +74,14 @@ void SceneEditorWindow::Draw() } } ImGui::End(); + + if (!shouldStayOpen) + { + EditorRequests::Get().RequestCloseEditorWindow(editorContext.GetScene()->Path); + } } +std::string SceneEditorWindow::GetWindowName() const +{ + return editorContext.GetScene()->Path; +} \ No newline at end of file diff --git a/Editor/src/Windows/SceneEditor/SceneEditorWindow.h b/Editor/src/Windows/SceneEditor/SceneEditorWindow.h index 20062788..ccfbd896 100644 --- a/Editor/src/Windows/SceneEditor/SceneEditorWindow.h +++ b/Editor/src/Windows/SceneEditor/SceneEditorWindow.h @@ -36,6 +36,8 @@ public: void Update(float ts); void Draw(); + std::string GetWindowName() const; + private: template inline void RegisterWidget() diff --git a/Editor/src/Windows/SceneEditor/Widgets/FileBrowserWidget.cpp b/Editor/src/Windows/SceneEditor/Widgets/FileBrowserWidget.cpp index 99c40469..9fd2e92d 100644 --- a/Editor/src/Windows/SceneEditor/Widgets/FileBrowserWidget.cpp +++ b/Editor/src/Windows/SceneEditor/Widgets/FileBrowserWidget.cpp @@ -498,9 +498,7 @@ void FileBrowserWidget::DrawFile(Ref file, uint32_t drawId) OS::OpenIn(file->GetAbsolutePath()); break; case FileType::Scene: - //shouldOpenScene = true; EditorRequests::Get().RequestLoadScene(file); - //this->Editor->OpenSceneWindow(file->GetRelativePath()); break; case FileType::Solution: OS::OpenIn(file->GetAbsolutePath());