From f61d1bc6443dfb2876d402171fb5d17ef3433b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 9 Jun 2024 01:16:21 +0200 Subject: [PATCH] Inspector window to combine asset properties and components editor --- .../src/Assets/Importers/TextureImporter.bf | 2 - .../src/EditWindows/ComponentEditWindow.bf | 51 +------ .../src/EditWindows/ContentBrowserWindow.bf | 20 ++- .../src/EditWindows/EditorViewportWindow.bf | 2 +- .../src/EditWindows/EntityHierarchyWindow.bf | 13 +- .../src/EditWindows/InspectorWindow.bf | 141 ++++++++++++++++++ .../src/EditWindows/PropertiesWindow.bf | 72 +-------- GlitchyEditor/src/Editor.bf | 12 +- GlitchyEditor/src/EditorLayer.bf | 9 +- 9 files changed, 185 insertions(+), 137 deletions(-) create mode 100644 GlitchyEditor/src/EditWindows/InspectorWindow.bf diff --git a/GlitchyEditor/src/Assets/Importers/TextureImporter.bf b/GlitchyEditor/src/Assets/Importers/TextureImporter.bf index f107d3d..0d38fd4 100644 --- a/GlitchyEditor/src/Assets/Importers/TextureImporter.bf +++ b/GlitchyEditor/src/Assets/Importers/TextureImporter.bf @@ -6,7 +6,6 @@ using GlitchyEngine.Content; using GlitchyEngine; using GlitchyEngine.Renderer; using GlitchyEngine.Math; -using System.Threading; using GlitchyEditor.Assets.Processors; using ImGui; @@ -68,7 +67,6 @@ class TextureImporter : IAssetImporter public Result Import(StringView fullFileName, AssetIdentifier assetIdentifier, AssetImporterConfig config) { - Thread.Sleep(1000); Log.EngineLogger.AssertDebug(config is TextureImporterConfig); diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index 7f7bf78..2dcd423 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -15,56 +15,11 @@ namespace GlitchyEditor.EditWindows { using internal GlitchyEngine.World.TransformComponent; - class ComponentEditWindow : EditorWindow + class ComponentEditWindow { - public const String s_WindowTitle = "Components"; - - private EntityHierarchyWindow _entityHierarchyWindow; - - private List<(Type, ComponentAttribute attribute)> _componentTypes = new .() ~ delete _; - - public this(EntityHierarchyWindow entityHierarchyWindow) - { - _entityHierarchyWindow = entityHierarchyWindow; - - for (let type in Type.Types) - { - if (let componentAttribute = type.GetCustomAttribute()) - { - _componentTypes.Add((type, componentAttribute)); - } - } - } - - protected override void InternalShow() - { - ImGui.SetNextWindowSizeConstraints(.(200, 200), .(-1, -1)); - - if(!ImGui.Begin(s_WindowTitle, &_open, .None)) - { - ImGui.PopStyleVar(); - ImGui.End(); - return; - } - - if (_entityHierarchyWindow.SelectionSize == 1) - { - Result entityResult = _entityHierarchyWindow.GetSelectedEntity(0); - - if (entityResult case .Ok(let selectedEntity)) - ShowComponents(selectedEntity); - } - else - { - _editVerticesPolygonCollider2D = false; - } - - ImGui.End(); - } - private static uint32 TableId; - private void ShowComponents(Entity entity) + public static void ShowComponents(Entity entity) { float cellPaddingY = ImGui.GetTextLineHeight() / 3.0f; @@ -112,7 +67,7 @@ namespace GlitchyEditor.EditWindows ShowAddComponentButton(entity); } - public void DrawSceneGUI(Entity entity) + public static void DrawSceneGUI(Entity entity) { DrawComponenSceneGUI(entity, => ShowPolygonColliderSceneGUI); } diff --git a/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf b/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf index 7053460..e14a321 100644 --- a/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf +++ b/GlitchyEditor/src/EditWindows/ContentBrowserWindow.bf @@ -79,6 +79,11 @@ namespace GlitchyEditor.EditWindows bool _showNewFile = false; AssetCreator _newFileCreator = null; + char8[256] _filesFilter = .(); + bool _searchEverywhere; + + public Event> OnFileSelected ~ _.Dispose(); + public this(EditorContentManager contentManager, AssetThumbnailManager thumbnailManager) { _manager = contentManager; @@ -188,8 +193,15 @@ namespace GlitchyEditor.EditWindows ImGui.End(); } - char8[256] _filesFilter = .(); - bool _searchEverywhere; + private void SelectFile(StringView fullFileName) + { + if (_selectedFile == fullFileName) + return; + + _selectedFile.Set(fullFileName); + + OnFileSelected(this, _selectedFile); + } private void DrawSearchBar() { @@ -517,7 +529,7 @@ namespace GlitchyEditor.EditWindows { if (_selectedFile != entry->Path) { - _selectedFile.Set(entry->Path); + SelectFile(entry->Path); _assetToRename.Clear(); } } @@ -617,7 +629,7 @@ namespace GlitchyEditor.EditWindows { if (_selectedFile != entry->Path) { - _selectedFile.Set(entry->Path); + SelectFile(entry->Path); _assetToRename.Clear(); } } diff --git a/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf b/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf index fe5cbc0..90a29c5 100644 --- a/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf +++ b/GlitchyEditor/src/EditWindows/EditorViewportWindow.bf @@ -339,7 +339,7 @@ namespace GlitchyEditor.EditWindows parentView = parentTransformCmp.WorldTransform.Invert(); } - _editor.ComponentEditWindow.DrawSceneGUI(entity); + ComponentEditWindow.DrawSceneGUI(entity); float3 snap = (float3)_snap; if (_gizmoType.HasFlag(.ROTATE)) diff --git a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf index 4914a1b..ffafd4a 100644 --- a/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf +++ b/GlitchyEditor/src/EditWindows/EntityHierarchyWindow.bf @@ -35,6 +35,8 @@ namespace GlitchyEditor.EditWindows /// Gets the number of entities that are currently selected. public int SelectionSize => _selectedEntityIds.Count; + + public Event>> SelectionChanged ~ _.Dispose(); /// Returns the Entity at the given index or null if it doesn't exist. public Result GetSelectedEntity(int index) @@ -102,6 +104,8 @@ namespace GlitchyEditor.EditWindows public void ClearEntitySelection() { _selectedEntityIds.Clear(); + + SelectionChanged(this, _selectedEntityIds); } /// Selects the given entity. @@ -113,13 +117,20 @@ namespace GlitchyEditor.EditWindows ClearEntitySelection(); _selectedEntityIds.Add(entity.UUID); + + SelectionChanged(this, _selectedEntityIds); } /// Deselects the given entity. /// @param entity The entity to deselect. public bool DeselectEntity(Entity entity) { - return _selectedEntityIds.Remove(entity.UUID); + bool removed = _selectedEntityIds.Remove(entity.UUID); + + if (removed) + SelectionChanged(this, _selectedEntityIds); + + return removed; } /// Returns whether or not the given entity is currently selected. diff --git a/GlitchyEditor/src/EditWindows/InspectorWindow.bf b/GlitchyEditor/src/EditWindows/InspectorWindow.bf new file mode 100644 index 0000000..9b6a7fd --- /dev/null +++ b/GlitchyEditor/src/EditWindows/InspectorWindow.bf @@ -0,0 +1,141 @@ +using ImGui; +using System; +using GlitchyEngine.Collections; +using GlitchyEngine.Content; +using System.Reflection; +using GlitchyEngine; +using GlitchyEditor.Assets; +using GlitchyEngine.Core; +using System.Collections; +using GlitchyEngine.World; + +namespace GlitchyEditor.EditWindows; + +enum SelectedObject +{ + case None; + case Asset(AssetHandle AssetHandle); + case Entity(Entity entity); +} + +class InspectorWindow : EditorWindow +{ + public const String s_WindowTitle = "Inspector"; + + private bool _lockCurrentSelection; + + private append String _selectedFileName = .(); + + private SelectedObject _selectedObject = .None; + + public this(Editor editor) + { + _editor = editor; + + _editor.ContentBrowserWindow.OnFileSelected.Add(new => SetSelectedAsset); + _editor.EntityHierarchyWindow.SelectionChanged.Add(new => SetSelectedEntities); + } + + private void SetSelectedAsset(Object sender, StringView fullFileName) + { + if (_lockCurrentSelection) + return; + + Result> treeNode = _editor.ContentManager.AssetHierarchy.GetNodeFromPath(fullFileName); + + if (treeNode case .Ok(let assetNode)) + { + AssetHandle? assetHandle = assetNode->AssetFile?.AssetConfig?.AssetHandle; + + if (assetHandle != null) + { + _selectedObject = .Asset(assetHandle.Value); + return; + } + } + + _selectedObject = .None; + } + + private void SetSelectedEntities(Object sender, List selectedEntities) + { + if (_lockCurrentSelection) + return; + + if (selectedEntities.Count > 0) + { + Result entityResult = _editor.EntityHierarchyWindow.GetSelectedEntity(0); + + if (entityResult case .Ok(let selectedEntity)) + { + _selectedObject = .Entity(selectedEntity); + return; + } + } + + _selectedObject = .None; + } + + protected override void InternalShow() + { + //ImGui.SetNextWindowSizeConstraints(.(200, 200), .(-1, -1)); + + defer + { + //ImGui.PopStyleVar(); + ImGui.End(); + } + + if(!ImGui.Begin(s_WindowTitle, &_open, .None)) + return; + + // TODO: make a little button in title bar? + ImGui.Checkbox("Lock", &_lockCurrentSelection); + ImGui.Separator(); + + if (_selectedObject case .Asset(let assetHandle)) + { + ShowAssetProperties(assetHandle); + } + else if (_selectedObject case .Entity(let entity)) + { + ShowEntityProperties(entity); + } + } + + private void ShowAssetProperties(AssetHandle assetHandle) + { + TreeNode assetNode = TrySilent!(_editor.ContentManager.AssetHierarchy.GetNodeFromAssetHandle(assetHandle)); + + AssetFile assetFile = assetNode->AssetFile; + + if (ImGui.BeginPropertyTable("asset_properties", ImGui.GetID("asset_properties"))) + { + assetFile.AssetConfig?.ImporterConfig?.ShowEditor(); + assetFile.AssetConfig?.ProcessorConfig?.ShowEditor(); + assetFile.AssetConfig?.ExporterConfig?.ShowEditor(); + + ImGui.EndTable(); + + ImGui.Separator(); + } + + bool hasChanges = (assetFile.AssetConfig?.ImporterConfig?.Changed ?? false) || (assetFile.AssetConfig?.ProcessorConfig?.Changed ?? false) || (assetFile.AssetConfig?.ExporterConfig?.Changed ?? false); + + if (!hasChanges) + ImGui.BeginDisabled(); + + if (ImGui.Button("Apply")) + { + assetFile.SaveAssetConfig(); + } + + if (!hasChanges) + ImGui.EndDisabled(); + } + + private void ShowEntityProperties(Entity entity) + { + ComponentEditWindow.ShowComponents(entity); + } +} diff --git a/GlitchyEditor/src/EditWindows/PropertiesWindow.bf b/GlitchyEditor/src/EditWindows/PropertiesWindow.bf index b6d7b45..a330edc 100644 --- a/GlitchyEditor/src/EditWindows/PropertiesWindow.bf +++ b/GlitchyEditor/src/EditWindows/PropertiesWindow.bf @@ -5,6 +5,7 @@ using GlitchyEngine.Content; using System.Reflection; using GlitchyEngine; using GlitchyEditor.Assets; +using GlitchyEngine.Core; namespace GlitchyEditor.EditWindows; @@ -22,6 +23,8 @@ class PropertiesWindow : EditorWindow private AssetHandle _currentAssetHandle; + private SelectedObject _selectedObject; + public this(Editor editor) { _editor = editor; @@ -93,74 +96,5 @@ class PropertiesWindow : EditorWindow if (!hasChanges) ImGui.EndDisabled(); - - /*if (_currentPropertiesEditor?.Asset != assetFile) - { - delete _currentPropertiesEditor; - _currentPropertiesEditor = _editor.ContentManager.GetNewPropertiesEditor(assetFile); - } - - if (assetFile == null) - return; - - Asset asset = _editor.ContentManager.GetAsset(null, _currentAssetHandle); - - // We need the actual asset for preview and sometimes for editing - if (asset?.Identifier != assetFile.AssetFile.Identifier) - { - //_currentAssetHandle = _editor.ContentManager.LoadAsset(assetFile.AssetFile.Identifier); - } - - // TODO: allow changing AssetLoader - // assetFile.AssetConfig.AssetLoade - - // TODO: ignore file - /*ImGui.Checkbox("Ignore", &assetFile.AssetConfig.IgnoreFile); - - if (ImGui.IsItemHovered()) - ImGui.SetTooltip("If checked this file will be ignored and not treated as an asset.");*/ - - ShowPropertiesEditor(assetFile); - - ImGui.Separator(); - */ - // TODO: preview asset - } - - private void ShowPropertiesEditor(AssetFile assetFile) - { - return; - - if (_currentPropertiesEditor == null) - return; - - _currentPropertiesEditor.ShowEditor(); - - bool assetConfigChanged = assetFile.AssetConfig.Config.Changed; - bool hasAssetSaver = (_editor.ContentManager.[Friend]GetAssetLoader(assetFile) is IAssetSaver); - - // If the asset can't be saved and it's config didn't change disable save button - if (!hasAssetSaver && !assetConfigChanged) - { - ImGui.BeginDisabled(); - defer:: { ImGui.EndDisabled(); } - } - - if (ImGui.Button("Save")) - { - // Only save config if it changed - if (assetConfigChanged) - { - assetFile.SaveAssetConfig(); - } - - // If the asset type has an asset saver, also save the asset - // TODO: Check whether or not the asset was changed? - if (hasAssetSaver) - { - Asset asset = _editor.ContentManager.GetAsset(null, _currentAssetHandle); - _editor.ContentManager.SaveAsset(asset); - } - } } } diff --git a/GlitchyEditor/src/Editor.bf b/GlitchyEditor/src/Editor.bf index 11877d6..5428054 100644 --- a/GlitchyEditor/src/Editor.bf +++ b/GlitchyEditor/src/Editor.bf @@ -23,7 +23,7 @@ namespace GlitchyEditor private EditorViewportWindow _sceneViewportWindow ~ delete _; private GameViewportWindow _gameViewportWindow ~ delete _; private ContentBrowserWindow _contentBrowserWindow ~ delete _; - private PropertiesWindow _propertiesWindow ~ delete _; + private InspectorWindow _inspectorWindow ~ delete _; private AssetViewer _assetViewer ~ delete _; private LogWindow _logWindow ~ delete _; @@ -55,7 +55,7 @@ namespace GlitchyEditor public EditorViewportWindow SceneViewportWindow => _sceneViewportWindow; public GameViewportWindow GameViewportWindow => _gameViewportWindow; public ContentBrowserWindow ContentBrowserWindow => _contentBrowserWindow; - public PropertiesWindow PropertiesWindow => _propertiesWindow; + public InspectorWindow InspectorWindow => _inspectorWindow; public AssetViewer AssetViewer => _assetViewer; public LogWindow LogWindow => _logWindow; @@ -93,9 +93,9 @@ namespace GlitchyEditor _sceneViewportWindow = new EditorViewportWindow(this); _gameViewportWindow = new GameViewportWindow(this); _entityHierarchyWindow = new EntityHierarchyWindow(this, _scene); - _componentEditWindow = new ComponentEditWindow(_entityHierarchyWindow); + _componentEditWindow = new ComponentEditWindow(); _contentBrowserWindow = new ContentBrowserWindow((.)Application.Get().ContentManager, _thumbnailManager); - _propertiesWindow = new PropertiesWindow(this); + _inspectorWindow = new InspectorWindow(this); _assetViewer = new AssetViewer((.)Application.Get().ContentManager); _logWindow = new LogWindow(); } @@ -105,9 +105,9 @@ namespace GlitchyEditor _sceneViewportWindow.Show(); _gameViewportWindow.Show(); _entityHierarchyWindow.Show(); - _componentEditWindow.Show(); + //_componentEditWindow.Show(); _contentBrowserWindow.Show(); - _propertiesWindow.Show(); + _inspectorWindow.Show(); _assetViewer.Show(); _logWindow.Show(); } diff --git a/GlitchyEditor/src/EditorLayer.bf b/GlitchyEditor/src/EditorLayer.bf index 8ff1e72..b16bb2d 100644 --- a/GlitchyEditor/src/EditorLayer.bf +++ b/GlitchyEditor/src/EditorLayer.bf @@ -1444,11 +1444,8 @@ namespace GlitchyEditor if(ImGui.BeginMenu("View", true)) { - if(ImGui.MenuItem(ComponentEditWindow.s_WindowTitle)) - _editor.ComponentEditWindow.Open = true; - if(ImGui.MenuItem(ContentBrowserWindow.s_WindowTitle)) - _editor.ComponentEditWindow.Open = true; + _editor.ContentBrowserWindow.Open = true; if(ImGui.MenuItem(EditorViewportWindow.s_WindowTitle)) _editor.SceneViewportWindow.Open = true; @@ -1459,8 +1456,8 @@ namespace GlitchyEditor if(ImGui.MenuItem(GameViewportWindow.s_WindowTitle)) _editor.GameViewportWindow.Open = true; - if(ImGui.MenuItem(PropertiesWindow.s_WindowTitle)) - _editor.PropertiesWindow.Open = true; + if(ImGui.MenuItem(InspectorWindow.s_WindowTitle)) + _editor.InspectorWindow.Open = true; if(ImGui.MenuItem(AssetViewer.s_WindowTitle)) _editor.AssetViewer.Open = true;