Request to close window

This commit is contained in:
antopilo
2025-01-26 21:23:44 -05:00
parent c4e79c61ed
commit faf86c05a7
6 changed files with 46 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
#include <src/Core/Core.h>
#include <src/Core/MulticastDelegate.h>
#include <string>
namespace Nuake
{
class Scene;
@@ -12,6 +14,7 @@ class EditorRequests
{
private:
MulticastDelegate<Ref<Nuake::File>> requestLoadScene;
MulticastDelegate<std::string> requestCloseEditorWindow;
EditorRequests() = default;
~EditorRequests() = default;
@@ -24,10 +27,18 @@ public:
}
public:
// Broadcast requests
void RequestLoadScene(Ref<Nuake::File> 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; }
};

View File

@@ -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();
}

View File

@@ -125,7 +125,10 @@ namespace Nuake
void DrawProjectSettings();
void Overlay();
// Signals
void OnRequestLoadScene(Ref<File> file);
void OnRequestCloseEditorWindow(std::string windowName);
void OpenPrefabWindow(const std::string& prefabPath);
void OpenSceneWindow(const std::string& scenePath);

View File

@@ -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> 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;
}

View File

@@ -36,6 +36,8 @@ public:
void Update(float ts);
void Draw();
std::string GetWindowName() const;
private:
template<DerivedFromEditorWidget T>
inline void RegisterWidget()

View File

@@ -498,9 +498,7 @@ void FileBrowserWidget::DrawFile(Ref<Nuake::File> 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());