From c6eb7059774176e7c91a641e173295ec679fb596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 23 Jun 2024 18:31:53 +0200 Subject: [PATCH] Drag component window --- .../src/EditWindows/ComponentEditWindow.bf | 89 +++++++++++++++---- GlitchyEditor/src/EditWindows/EditorWindow.bf | 2 + .../src/EditWindows/InspectorWindow.bf | 9 +- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index b0e7aed..df1c51a 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -19,7 +19,7 @@ namespace GlitchyEditor.EditWindows { private static uint32 TableId; - public static void ShowComponents(Entity entity) + public static void ShowComponents(Entity entity, Type componentType = null) { float cellPaddingY = ImGui.GetTextLineHeight() / 3.0f; @@ -27,28 +27,28 @@ namespace GlitchyEditor.EditWindows TableId = ImGui.GetID("properties"); - if (ImGui.BeginTableEx("properties", TableId, 2, .SizingStretchSame | .BordersInner | .Resizable)) + if (componentType == null && ImGui.BeginTableEx("properties", TableId, 2, .SizingStretchSame | .BordersInner | .Resizable)) { ShowNameComponentEditor(entity); ImGui.EndTable(); } - ShowComponentEditor("Transform", entity, => ShowTransformComponentEditor); - ShowComponentEditor("Camera", entity, => ShowCameraComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Transform", entity, => ShowTransformComponentEditor); + ShowComponentEditor(componentType, "Camera", entity, => ShowCameraComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Circle Renderer", entity, => ShowCircleRendererComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Text Renderer", entity, => ShowTextRendererComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Circle Renderer", entity, => ShowCircleRendererComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Text Renderer", entity, => ShowTextRendererComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Light", entity, => ShowLightComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Mesh", entity, => ShowMeshComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Light", entity, => ShowLightComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Mesh", entity, => ShowMeshComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Rigidbody 2D", entity, => ShowRigidBody2DComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Box collider 2D", entity, => ShowBoxCollider2DComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Circle collider 2D", entity, => ShowCircleCollider2DComponentEditor, => ShowComponentContextMenu); - ShowComponentEditor("Polygon collider 2D", entity, => ShowPolygonCollider2DComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Rigidbody 2D", entity, => ShowRigidBody2DComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Box collider 2D", entity, => ShowBoxCollider2DComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Circle collider 2D", entity, => ShowCircleCollider2DComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, "Polygon collider 2D", entity, => ShowPolygonCollider2DComponentEditor, => ShowComponentContextMenu); ScriptComponent: { @@ -59,12 +59,15 @@ namespace GlitchyEditor.EditWindows scriptComponentName = scope:ScriptComponent $"Script ({scriptComponent.ScriptClassName})"; } - ShowComponentEditor(scriptComponentName, entity, => ShowScriptComponentEditor, => ShowComponentContextMenu); + ShowComponentEditor(componentType, scriptComponentName, entity, => ShowScriptComponentEditor, => ShowComponentContextMenu); } ImGui.PopStyleVar(); - ShowAddComponentButton(entity); + if (componentType == null) + { + ShowAddComponentButton(entity); + } } public static void DrawSceneGUI(Entity entity) @@ -107,18 +110,66 @@ namespace GlitchyEditor.EditWindows entity.RemoveComponent(); } - private static void ShowComponentEditor(String header, Entity entity, function void(Entity, TComponent*) showComponentEditor, function void(Entity, TComponent*) showComponentContextMenu = null) where TComponent: struct, new + private static void ShowOnlyComponent(UUID entityId, function void(Entity, TComponent*) showComponentEditor) where TComponent: struct, new { - if (!entity.HasComponent()) + if (Entity entity = Editor.Instance.CurrentScene.GetEntityByID(entityId)) + { + if (entity.TryGetComponent(let component)) + { + if (ImGui.BeginPropertyTable("properties", TableId)) + { + if (entity.TryGetComponent(let actualComponent)) + showComponentEditor(entity, actualComponent); + + ImGui.EndTable(); + } + } + else + { + ImGui.TextWrapped($"ERROR!\n\nEntity {entity.Name} doesn't have a component of type {nameof(TComponent)}."); + } + } + else + { + ImGui.TextWrapped($"ERROR!\n\nEntity {entityId} doesn't exist."); + } + } + + static PropertiesWindow window = null; + + private static void ShowComponentEditor(Type soloType, String header, Entity entity, function void(Entity, TComponent*) showComponentEditor, function void(Entity, TComponent*) showComponentContextMenu = null) where TComponent: struct, new + { + if ((soloType != null && soloType != typeof(TComponent)) || !entity.HasComponent()) return; TComponent* component = entity.GetComponent(); - bool nodeOpen = ImGui.CollapsingHeader(header.CStr(), .DefaultOpen | .AllowOverlap | .Framed | .SpanFullWidth); + bool nodeOpen = (soloType != null) || ImGui.CollapsingHeader(header.CStr(), .DefaultOpen | .AllowOverlap | .Framed | .SpanFullWidth); ImGui.PushID(header.CStr()); defer { ImGui.PopID(); } + if (ImGui.IsItemActive() && ImGui.IsMouseDragging(.Left) && ImGui.GetIO().KeyAlt && window == null) + { + window = new PropertiesWindow(Editor.Instance, .Component(entity.UUID, typeof(TComponent))); + window.WindowTitle = header; + } + else if (!ImGui.GetIO().KeyAlt) + { + window = null; + } + + if (window != null) + { + ImGui.SetWindowFocus(window.WindowTitleWithId.ToScopeCStr!()); + + ImGui.Vec2 position = ImGui.GetMousePos(); + position.y -= ImGui.GetTextLineHeight() / 2.0f; + position.x -= 100.0f; + + ImGui.SetWindowPos(window.WindowTitleWithId.ToScopeCStr!(), position); + } + if (showComponentContextMenu != null && ImGui.IsItemClicked(.Right)) { ImGui.OpenPopup("component_popup"); diff --git a/GlitchyEditor/src/EditWindows/EditorWindow.bf b/GlitchyEditor/src/EditWindows/EditorWindow.bf index c1437c0..3175eb4 100644 --- a/GlitchyEditor/src/EditWindows/EditorWindow.bf +++ b/GlitchyEditor/src/EditWindows/EditorWindow.bf @@ -52,6 +52,8 @@ namespace GlitchyEditor.EditWindows } } + public StringView WindowTitleWithId => _windowTitleId; + public this(Editor editor, StringView windowTitle = "Window") { _windowId = ++_nextWindowId; diff --git a/GlitchyEditor/src/EditWindows/InspectorWindow.bf b/GlitchyEditor/src/EditWindows/InspectorWindow.bf index 45c372b..3c5ec27 100644 --- a/GlitchyEditor/src/EditWindows/InspectorWindow.bf +++ b/GlitchyEditor/src/EditWindows/InspectorWindow.bf @@ -16,6 +16,7 @@ enum SelectedObject case None; case Asset(AssetHandle AssetHandle); case Entity(UUID entityId); + case Component(UUID entityId, Type componentType); } class InspectorWindow : EditorWindow @@ -120,6 +121,10 @@ class InspectorWindow : EditorWindow { ShowEntityProperties(entityId, editor); } + else if (object case .Component(let entityId, let componentType)) + { + ShowEntityProperties(entityId, editor, componentType); + } } private static void ShowAssetProperties(AssetHandle assetHandle, Editor editor) @@ -160,13 +165,13 @@ class InspectorWindow : EditorWindow ImGui.EndDisabled(); } - private static void ShowEntityProperties(UUID entityId, Editor editor) + private static void ShowEntityProperties(UUID entityId, Editor editor, Type componentType = null) { Result entityResult = editor.CurrentScene.GetEntityByID(entityId); if (entityResult case .Ok(let entity)) { - ComponentEditWindow.ShowComponents(entity); + ComponentEditWindow.ShowComponents(entity, componentType); } else {