From 422331dec23e1765d17accc609d7232411a00013 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Thu, 5 Sep 2024 19:44:27 -0400 Subject: [PATCH] Automatically refresh filebrowser on window focus, and preserve path after scanning --- Editor/src/EditorApplication.cpp | 26 ++++++++++++++++++ Editor/src/EditorLayer.cpp | 5 ++++ Editor/src/EditorLayer.h | 2 ++ Editor/src/Windows/EditorInterface.cpp | 8 ++++++ Editor/src/Windows/EditorInterface.h | 3 ++ Editor/src/Windows/FileSystemUI.cpp | 22 ++++++++++++++- Editor/src/Windows/FileSystemUI.h | 2 ++ Nuake/src/Application/Layer.h | 1 + Nuake/src/FileSystem/Directory.h | 3 ++ Nuake/src/FileSystem/FileSystem.cpp | 38 ++++++++++++++++++++++++++ Nuake/src/FileSystem/FileSystem.h | 2 +- 11 files changed, 110 insertions(+), 2 deletions(-) diff --git a/Editor/src/EditorApplication.cpp b/Editor/src/EditorApplication.cpp index eb8252f1..fd998a70 100644 --- a/Editor/src/EditorApplication.cpp +++ b/Editor/src/EditorApplication.cpp @@ -45,6 +45,32 @@ void EditorApplication::OnInit() } } + m_Window->SetOnWindowFocusedCallback([&](Window& window, bool focused) + { + if (!focused) + { + return; + } + + for (auto& layer : m_LayerStack) + { + layer->OnWindowFocused(); + } + }); + + m_Window->SetOnWindowClosedCallback([](Window& window) + { + if (Engine::GetProject()) + { + Engine::GetProject()->Save(); + } + + if (Engine::GetCurrentScene()) + { + Engine::GetCurrentScene()->Save(); + } + }); + PushLayer(CreateScope()); } diff --git a/Editor/src/EditorLayer.cpp b/Editor/src/EditorLayer.cpp index f986319e..b65318d6 100644 --- a/Editor/src/EditorLayer.cpp +++ b/Editor/src/EditorLayer.cpp @@ -75,4 +75,9 @@ void EditorLayer::OnUpdate() void EditorLayer::OnDetach() { delete m_EditorInterface; +} + +void EditorLayer::OnWindowFocused() +{ + m_EditorInterface->OnWindowFocused(); } \ No newline at end of file diff --git a/Editor/src/EditorLayer.h b/Editor/src/EditorLayer.h index 46802596..77759544 100644 --- a/Editor/src/EditorLayer.h +++ b/Editor/src/EditorLayer.h @@ -22,6 +22,8 @@ public: virtual void OnUpdate() override; virtual void OnDetach() override; + virtual void OnWindowFocused() override; + private: CommandBuffer mCommandBuffer; Nuake::EditorInterface* m_EditorInterface; diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 9dd0d15d..f083179d 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -34,6 +34,8 @@ #include "../Actions/EditorSelection.h" #include "FileSystemUI.h" +#include + #include "../Misc/InterfaceFonts.h" #include "WelcomeWindow.h" @@ -3177,6 +3179,12 @@ namespace Nuake { mCommandBuffer->PushCommand(command); } + void EditorInterface::OnWindowFocused() + { + filesystem->Scan(); + + } + bool EditorInterface::LoadProject(const std::string& projectPath) { FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); diff --git a/Editor/src/Windows/EditorInterface.h b/Editor/src/Windows/EditorInterface.h index e8745690..5b2ca572 100644 --- a/Editor/src/Windows/EditorInterface.h +++ b/Editor/src/Windows/EditorInterface.h @@ -102,5 +102,8 @@ namespace Nuake public: std::string GetEntityTypeName(const Entity& entity) const; static void PushCommand(ICommand&& command); + + public: + void OnWindowFocused(); }; } diff --git a/Editor/src/Windows/FileSystemUI.cpp b/Editor/src/Windows/FileSystemUI.cpp index 21f066bf..bbe0bbd1 100644 --- a/Editor/src/Windows/FileSystemUI.cpp +++ b/Editor/src/Windows/FileSystemUI.cpp @@ -694,8 +694,28 @@ namespace Nuake void FileSystemUI::RefreshFileBrowser() { + Scan(); + } + + void FileSystemUI::Scan() + { + if (!m_CurrentDirectory) + { + return; + } + + std::string previousPath = m_CurrentDirectory->FullPath; + FileSystem::Scan(); - m_CurrentDirectory = FileSystem::RootDirectory; + + if (FileSystem::DirectoryExists(previousPath, true)) + { + m_CurrentDirectory = FileSystem::GetDirectory(FileSystem::AbsoluteToRelative(previousPath)); + } + else + { + m_CurrentDirectory = FileSystem::RootDirectory; + } } float h = 200; diff --git a/Editor/src/Windows/FileSystemUI.h b/Editor/src/Windows/FileSystemUI.h index d6c9c4d8..d4c7f8a6 100644 --- a/Editor/src/Windows/FileSystemUI.h +++ b/Editor/src/Windows/FileSystemUI.h @@ -32,5 +32,7 @@ namespace Nuake { bool DeletePopup(); void DrawContextMenu(); void RefreshFileBrowser(); + + void Scan(); }; } diff --git a/Nuake/src/Application/Layer.h b/Nuake/src/Application/Layer.h index 3cd68f04..89fb1c1b 100644 --- a/Nuake/src/Application/Layer.h +++ b/Nuake/src/Application/Layer.h @@ -17,6 +17,7 @@ namespace Nuake { virtual void OnDraw() {}; + virtual void OnWindowFocused() {}; // TODO: OnEvent inline const std::string& GetName() const { return m_Name; } diff --git a/Nuake/src/FileSystem/Directory.h b/Nuake/src/FileSystem/Directory.h index 49b9f668..8e29c158 100644 --- a/Nuake/src/FileSystem/Directory.h +++ b/Nuake/src/FileSystem/Directory.h @@ -19,5 +19,8 @@ namespace Nuake Directory() = default; ~Directory() = default; + + std::string GetName() const; + std::string GetFullPath() const; }; } \ No newline at end of file diff --git a/Nuake/src/FileSystem/FileSystem.cpp b/Nuake/src/FileSystem/FileSystem.cpp index 1220067a..57856d0b 100644 --- a/Nuake/src/FileSystem/FileSystem.cpp +++ b/Nuake/src/FileSystem/FileSystem.cpp @@ -237,6 +237,39 @@ Ref FileSystem::GetFile(const std::string& path) return nullptr; } +Ref FileSystem::GetDirectory(const std::string& path) +{ + // Note, Might be broken on other platforms. + auto splits = String::Split(path, '/'); + + int currentDepth = -1; + std::string currentDirName = "."; + Ref currentDirComparator = RootDirectory; + while (currentDirName == currentDirComparator->Name) + { + currentDepth++; + + if (currentDepth >= splits.size()) + { + return currentDirComparator; + } + + currentDirName = splits[currentDepth]; + + // Find next directory + for (auto& d : currentDirComparator->Directories) + { + if (d->Name == currentDirName) + { + currentDirComparator = d; + continue; + } + } + } + + return currentDirComparator; +} + std::string FileSystem::GetFileNameFromPath(const std::string& path) { const auto& split = String::Split(path, '/'); @@ -251,3 +284,8 @@ Directory::Directory(const std::string& path) Name = FileSystem::AbsoluteToRelative(path); FullPath = path; } + +std::string Directory::GetName() const +{ + return Name; +} diff --git a/Nuake/src/FileSystem/FileSystem.h b/Nuake/src/FileSystem/FileSystem.h index d527dfa1..f7813639 100644 --- a/Nuake/src/FileSystem/FileSystem.h +++ b/Nuake/src/FileSystem/FileSystem.h @@ -16,7 +16,6 @@ namespace Nuake { public: static std::string Root; - static Ref RootDirectory; static Ref> RootFileWatch; @@ -28,6 +27,7 @@ namespace Nuake static std::string GetParentPath(const std::string& fullPath); static Ref GetFileTree(); static Ref GetFile(const std::string& path); + static Ref GetDirectory(const std::string& path); static std::string GetFileNameFromPath(const std::string& path); static void ScanDirectory(Ref directory); static void GetDirectories();