test with ecs source and target drag drops
This commit is contained in:
		@@ -2,3 +2,5 @@
 | 
			
		||||
 | 
			
		||||
public readonly record struct SelectedInHierachy;
 | 
			
		||||
public readonly record struct ClickedInHierachy;
 | 
			
		||||
public readonly record struct PayloadSourceInHierachy;
 | 
			
		||||
public readonly record struct PayloadTargetInHierachy;
 | 
			
		||||
@@ -17,6 +17,7 @@ namespace Nerfed.Editor.Systems
 | 
			
		||||
        private readonly Filter rootEntitiesFilter;
 | 
			
		||||
 | 
			
		||||
        private readonly EditorHierachySelectionSystem hierachySelectionSystem;
 | 
			
		||||
        private readonly EditorHierachyDragAndDropSystem hierachyDragAndDropSystem;
 | 
			
		||||
 | 
			
		||||
        public EditorHierarchyWindow(World world) : base(world)
 | 
			
		||||
        {
 | 
			
		||||
@@ -33,6 +34,7 @@ namespace Nerfed.Editor.Systems
 | 
			
		||||
            // 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)
 | 
			
		||||
@@ -44,23 +46,23 @@ namespace Nerfed.Editor.Systems
 | 
			
		||||
 | 
			
		||||
            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)
 | 
			
		||||
                //{
 | 
			
		||||
@@ -78,6 +80,7 @@ namespace Nerfed.Editor.Systems
 | 
			
		||||
            ImGui.End();
 | 
			
		||||
 | 
			
		||||
            hierachySelectionSystem.Update(delta);
 | 
			
		||||
            hierachyDragAndDropSystem.Update(delta);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void DrawEntityAndChildren(in Entity entity)
 | 
			
		||||
@@ -105,31 +108,44 @@ namespace Nerfed.Editor.Systems
 | 
			
		||||
                // Drag and drop.
 | 
			
		||||
                if (ImGui.BeginDragDropSource())
 | 
			
		||||
                {
 | 
			
		||||
                    unsafe
 | 
			
		||||
                    {
 | 
			
		||||
                        fixed (Entity* payload = &entity)
 | 
			
		||||
                        {
 | 
			
		||||
                            ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", (IntPtr)payload, (uint)sizeof(Entity));
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    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));
 | 
			
		||||
                    //    }
 | 
			
		||||
                    //}
 | 
			
		||||
 | 
			
		||||
                    ImGui.EndDragDropSource();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (ImGui.BeginDragDropTarget())
 | 
			
		||||
                {
 | 
			
		||||
                    ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
 | 
			
		||||
                    unsafe
 | 
			
		||||
                    {
 | 
			
		||||
                        ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
 | 
			
		||||
                        if (payload.NativePtr != null)
 | 
			
		||||
                        {
 | 
			
		||||
                            Entity* data = (Entity*)payload.Data;
 | 
			
		||||
                            Entity ent = data[0];
 | 
			
		||||
 | 
			
		||||
                            Log.Info($"Dropped {ent.ID}");
 | 
			
		||||
 | 
			
		||||
                            Transform.SetParent(World, ent, entity);
 | 
			
		||||
                            Log.Info($"SetTarget {entity.ID}");
 | 
			
		||||
                            Set(entity, new PayloadTargetInHierachy());
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    //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();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@@ -187,13 +203,49 @@ namespace Nerfed.Editor.Systems
 | 
			
		||||
 | 
			
		||||
        private class EditorHierachyDragAndDropSystem : MoonTools.ECS.System
 | 
			
		||||
        {
 | 
			
		||||
            private readonly Filter sourceEntities;
 | 
			
		||||
            private readonly Filter targetEntities;
 | 
			
		||||
 | 
			
		||||
            public EditorHierachyDragAndDropSystem(World world) : base(world)
 | 
			
		||||
            {
 | 
			
		||||
                sourceEntities = FilterBuilder.Include<PayloadSourceInHierachy>().Build();
 | 
			
		||||
                targetEntities = FilterBuilder.Include<PayloadTargetInHierachy>().Build();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            public override void Update(TimeSpan delta)
 | 
			
		||||
            {
 | 
			
		||||
                if (!targetEntities.Empty)
 | 
			
		||||
                {
 | 
			
		||||
                    Entity target = GetSingletonEntity<PayloadTargetInHierachy>();
 | 
			
		||||
 | 
			
		||||
                    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<PayloadTargetInHierachy>(source);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    foreach (Entity source in sourceEntities.Entities)
 | 
			
		||||
                    {
 | 
			
		||||
                        Remove<PayloadSourceInHierachy>(source);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user