From 9387bfa59c9d246dd93101b85caab069a989ef89 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 15 Oct 2024 23:17:58 +0200 Subject: [PATCH] switched back to imgui dragdrop --- .../Components/HierachyComponents.cs | 4 +- .../Systems/EditorHierarchyWindow.cs | 122 +++++------------- 2 files changed, 30 insertions(+), 96 deletions(-) diff --git a/Nerfed.Editor/Components/HierachyComponents.cs b/Nerfed.Editor/Components/HierachyComponents.cs index 83b53c6..e858d3e 100644 --- a/Nerfed.Editor/Components/HierachyComponents.cs +++ b/Nerfed.Editor/Components/HierachyComponents.cs @@ -1,6 +1,4 @@ namespace Nerfed.Editor.Components; public readonly record struct SelectedInHierachy; -public readonly record struct ClickedInHierachy; -public readonly record struct PayloadSourceInHierachy; -public readonly record struct PayloadTargetInHierachy; \ No newline at end of file +public readonly record struct ClickedInHierachy; \ No newline at end of file diff --git a/Nerfed.Editor/Systems/EditorHierarchyWindow.cs b/Nerfed.Editor/Systems/EditorHierarchyWindow.cs index 2500bc3..6f80462 100644 --- a/Nerfed.Editor/Systems/EditorHierarchyWindow.cs +++ b/Nerfed.Editor/Systems/EditorHierarchyWindow.cs @@ -17,7 +17,6 @@ internal class EditorHierarchyWindow : MoonTools.ECS.DebugSystem private readonly Filter rootEntitiesFilter; private readonly EditorHierachySelectionSystem hierachySelectionSystem; - private readonly EditorHierachyDragAndDropSystem hierachyDragAndDropSystem; public EditorHierarchyWindow(World world) : base(world) { @@ -34,7 +33,6 @@ public EditorHierarchyWindow(World world) : base(world) // Or a EditorComponent, just a component that always gets added when in editor mode. hierachySelectionSystem = new EditorHierachySelectionSystem(world); - hierachyDragAndDropSystem = new EditorHierachyDragAndDropSystem(world); } public override void Update(TimeSpan delta) @@ -46,23 +44,23 @@ public override void Update(TimeSpan delta) if (ImGui.TreeNodeEx("World", flags)) { - //if (ImGui.BeginDragDropTarget()) - //{ - // unsafe - // { - // ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}"); - // if (payload.NativePtr != null) - // { - // Entity* data = (Entity*)payload.Data; - // Entity child = data[0]; + if (ImGui.BeginDragDropTarget()) + { + unsafe + { + ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}"); + if (payload.NativePtr != null) + { + Entity* data = (Entity*)payload.Data; + Entity child = data[0]; - // Log.Info($"Dropped {child.ID}"); + Log.Info($"Dropped {child.ID}"); - // Transform.RemoveParent(World, child); - // } - // } - // ImGui.EndDragDropTarget(); - //} + Transform.RemoveParent(World, child); + } + } + ImGui.EndDragDropTarget(); + } //foreach (Entity entity in rootEntitiesWithTransformFilter.Entities) //{ @@ -80,7 +78,6 @@ public override void Update(TimeSpan delta) ImGui.End(); hierachySelectionSystem.Update(delta); - hierachyDragAndDropSystem.Update(delta); } private void DrawEntityAndChildren(in Entity entity) @@ -108,44 +105,31 @@ private void DrawEntityAndChildren(in Entity entity) // Drag and drop. if (ImGui.BeginDragDropSource()) { - ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", nint.Zero, 0); - Set(entity, new PayloadSourceInHierachy()); - Log.Info($"SetSource {entity.ID}"); - - //unsafe - //{ - // fixed (Entity* payload = &entity) - // { - // ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", (IntPtr)payload, (uint)sizeof(Entity)); - // } - //} + unsafe + { + fixed (Entity* payload = &entity) + { + ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", (IntPtr)payload, (uint)sizeof(Entity)); + } + } ImGui.EndDragDropSource(); } if (ImGui.BeginDragDropTarget()) { - ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}"); unsafe { + ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}"); if (payload.NativePtr != null) { - Log.Info($"SetTarget {entity.ID}"); - Set(entity, new PayloadTargetInHierachy()); + Entity ent = *(Entity*)payload.Data; + + Log.Info($"Dropped {ent.ID}"); + + Transform.SetParent(World, ent, entity); } } - //unsafe - //{ - // ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}"); - // if (payload.NativePtr != null) - // { - // Entity ent = *(Entity*)payload.Data; - - // Log.Info($"Dropped {ent.ID}"); - - // Transform.SetParent(World, ent, entity); - // } - //} ImGui.EndDragDropTarget(); } @@ -200,53 +184,5 @@ public override void Update(TimeSpan delta) } } } - - private class EditorHierachyDragAndDropSystem : MoonTools.ECS.System - { - private readonly Filter sourceEntities; - private readonly Filter targetEntities; - - public EditorHierachyDragAndDropSystem(World world) : base(world) - { - sourceEntities = FilterBuilder.Include().Build(); - targetEntities = FilterBuilder.Include().Build(); - } - - public override void Update(TimeSpan delta) - { - if (!targetEntities.Empty) - { - Entity target = GetSingletonEntity(); - - foreach (Entity source in sourceEntities.Entities) - { - Transform.SetParent(World, source, target); - } - } - - bool clear = false; - unsafe - { - ImGuiPayloadPtr payload = ImGui.GetDragDropPayload(); - if (payload.NativePtr == null) - { - clear = true; - } - } - - if (clear) - { - foreach (Entity source in targetEntities.Entities) - { - Remove(source); - } - - foreach (Entity source in sourceEntities.Entities) - { - Remove(source); - } - } - } - } } -} +} \ No newline at end of file