From 01652792200fb377caf5685e68dd3dc5d302b581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Mon, 21 Aug 2023 14:11:51 +0200 Subject: [PATCH] Implemented "Open recent Project" menu entry + Clean up handle -> asset map when chaning Asset or Resource directory --- GlitchyEditor/src/AssetFile.bf | 4 +- GlitchyEditor/src/Assets/AssetHierarchy.bf | 11 ++++++ GlitchyEditor/src/EditorLayer.bf | 33 ++++++++++++++++ GlitchyEditor/src/EditorSettings.bf | 46 +++++++++++++++++++++- 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/GlitchyEditor/src/AssetFile.bf b/GlitchyEditor/src/AssetFile.bf index 37b4104..f4f9f67 100644 --- a/GlitchyEditor/src/AssetFile.bf +++ b/GlitchyEditor/src/AssetFile.bf @@ -28,7 +28,7 @@ class AssetFile private String _path; private String _identifier; - private String _assetConfigPath; + private String _assetConfigPath ~ delete _; private AssetConfig _assetConfig ~ delete _; @@ -53,7 +53,7 @@ class AssetFile { String identifierBuffer = append String(identifier); String pathBuffer = append String(path); - String configPathBuffer = append String(path.Length + ConfigFileExtension.Length); + String configPathBuffer = new String(path.Length + ConfigFileExtension.Length); _identifier = identifierBuffer; _path = pathBuffer; diff --git a/GlitchyEditor/src/Assets/AssetHierarchy.bf b/GlitchyEditor/src/Assets/AssetHierarchy.bf index 7f3fc1b..3e2d8a5 100644 --- a/GlitchyEditor/src/Assets/AssetHierarchy.bf +++ b/GlitchyEditor/src/Assets/AssetHierarchy.bf @@ -250,6 +250,10 @@ class AssetHierarchy _resourcesDirectoryNode?.ForEach(scope (node) => { _pathToAssetNode.Remove(node->Path); _identifierToAssetNode.Remove(node->Identifier); + + let handle = node->AssetFile?.AssetConfig?.AssetHandle; + if (handle != null) + _handleToAssetNode.Remove(handle.Value); }); _assetRootNode.RemoveChild(_resourcesDirectoryNode); DeleteTreeAndChildren!(_resourcesDirectoryNode); @@ -283,11 +287,18 @@ class AssetHierarchy /// Sets path to the directory that contains the game assets. public void SetAssetsDirectory(StringView fileName) { + if (AssetsDirectory == fileName) + return; + AssetsDirectory = fileName; _assetsDirectoryNode?.ForEach(scope (node) => { _pathToAssetNode.Remove(node->Path); _identifierToAssetNode.Remove(node->Identifier); + + let handle = node->AssetFile?.AssetConfig?.AssetHandle; + if (handle != null) + _handleToAssetNode.Remove(handle.Value); }); _assetRootNode.RemoveChild(_assetsDirectoryNode); DeleteTreeAndChildren!(_assetsDirectoryNode); diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index 779a8fc..bde8a8a 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -705,6 +705,12 @@ namespace GlitchyEditor UpdateWindowTitle(); + if (Directory.Exists(project.WorkspacePath)) + { + Application.Instance.Settings.EditorSettings.LastOpenedProject = project.WorkspacePath; + Application.Instance.Settings.Save(); + } + return .Ok; } @@ -1174,6 +1180,32 @@ namespace GlitchyEditor } } + private void ShowOpenRecentProjectMenu() + { + let recentProjects = Application.Instance.Settings.EditorSettings.RecentProjects; + + if (recentProjects == null) + return; + + int i = 0; + + for (let workspacePath in recentProjects) + { + i++; + + if (!Directory.Exists(workspacePath)) + { + delete workspacePath; + @workspacePath.Remove(); + } + + if (ImGui.MenuItem(scope $"{i}: {workspacePath}")) + { + LoadAndOpenProject(workspacePath); + } + } + } + private void DrawMainMenuBar() { ImGui.BeginMainMenuBar(); @@ -1219,6 +1251,7 @@ namespace GlitchyEditor if (ImGui.BeginMenu("Open recent project")) { + ShowOpenRecentProjectMenu(); ImGui.EndMenu(); } diff --git a/GlitchyEditor/src/EditorSettings.bf b/GlitchyEditor/src/EditorSettings.bf index cbd894b..f6960f3 100644 --- a/GlitchyEditor/src/EditorSettings.bf +++ b/GlitchyEditor/src/EditorSettings.bf @@ -37,9 +37,51 @@ class EditorSettings [Setting("Editor", "Switch to Editor on pause", "If checked the editor will automatically switch to the \"Editor\" window when the game is being paused."), BonInclude] public bool SwitchToEditorOnPause = false; + + [BonInclude] + private List _recentProjects ~ DeleteContainerAndItems!(_); - // - //public readonly List RecentProjects = new .() ~ delete _; + /// Gets or sets the path of the Project that was last open. + public StringView LastOpenedProject + { + get + { + if (_recentProjects == null || _recentProjects.Count == 0) + return ""; + + return _recentProjects[0]; + } + set + { + if (_recentProjects == null) + _recentProjects = new List(); + + // Remove duplicates + for (var entry in _recentProjects) + { + if (entry == value) + { + delete entry; + @entry.Remove(); + } + } + + _recentProjects.Insert(0, new String(value)); + + if (_recentProjects.Count > 10) + { + // We only store 10 entries, delete the rest + for (int i = 10; i < _recentProjects.Count; i++) + { + delete _recentProjects[i]; + } + + _recentProjects.Count = 10; + } + } + } + + public List RecentProjects => _recentProjects; public void Apply() {