Example #1
0
        private void AnimationFrameEditor(Dictionary <string, SpriteAnimationData> anims)
        {
            AnimatedSprite      currentFileContext = _currentAsset !.Content !;
            SpriteAnimationData selAnim            = anims[_selectedAnimation];

            ImGui.BeginChild("Animation", new Vector2(-1, -1), true);
            ImGui.Text("Frames: (Drag and Drop to rearrange)");

            SpriteAnimationFrameSource frameSource = currentFileContext.FrameSource;

            _draggedFrame = EditorHelpers.DragAndDropList(selAnim.FrameIndices, _draggedFrame, true);
            if (selAnim.FrameIndices.Length == 0)
            {
                EditorHelpers.ButtonSizedHole("");
            }

            ImGui.PushItemWidth(150);
            if (ImGui.InputInt("", ref _addFrameInput))
            {
                _addFrameInput = Maths.Clamp(_addFrameInput, 0, frameSource.GetFrameCount() - 1);
            }
            ImGui.SameLine();
            if (ImGui.Button("Add Frame"))
            {
                selAnim.FrameIndices = selAnim.FrameIndices.AddToArray(_addFrameInput);
                if (_addFrameInput < frameSource.GetFrameCount() - 2)
                {
                    _addFrameInput++;
                }
                _controller.Reset();
                UnsavedChanges();
            }

            ImGui.SameLine();
            ImGui.Button("Remove (Drop Here)");
            if (ImGui.BeginDragDropTarget())
            {
                ImGuiPayloadPtr dataPtr = ImGui.AcceptDragDropPayload("UNUSED");
                unsafe
                {
                    if ((IntPtr)dataPtr.NativePtr != IntPtr.Zero && dataPtr.IsDelivery())
                    {
                        int[] indices = selAnim.FrameIndices;
                        for (int i = _draggedFrame; i < indices.Length - 1; i++)
                        {
                            indices[i] = indices[i + 1];
                        }

                        Array.Resize(ref selAnim.FrameIndices, selAnim.FrameIndices.Length - 1);
                        _controller.Reset();
                        _draggedFrame = -1;
                        UnsavedChanges();
                    }
                }

                ImGui.EndDragDropTarget();
            }

            ImGui.PushItemWidth(150);
            ImGui.Text("Duration");
            ImGui.SameLine();
            if (ImGui.InputInt("###DurInput", ref selAnim.TimeBetweenFrames))
            {
                _controller.Reset();
                UnsavedChanges();
            }

            ImGui.SameLine();
            if (selAnim.FrameIndices.Length > 0)
            {
                ImGui.Text($"Total Duration: {selAnim.TimeBetweenFrames * selAnim.FrameIndices.Length}");
            }

            var loopType = (int)selAnim.LoopType;

            if (ImGui.Combo("Loop Type", ref loopType, string.Join('\0', Enum.GetNames(typeof(AnimationLoopType)))))
            {
                selAnim.LoopType = (AnimationLoopType)loopType;
                _controller.Reset();
                UnsavedChanges();
            }
        }
Example #2
0
        public static int DragAndDropList <T>(T[] list, int draggedState, bool horizontal = false)
        {
            //draggedState = -1;
            for (var i = 0; i < list.Length; i++)
            {
                T item = list[i];
                if (item == null)
                {
                    continue;
                }

                if (i == draggedState)
                {
                    ImGui.PushStyleColor(ImGuiCol.Button, 0);
                    ImGui.PushStyleColor(ImGuiCol.Text, 0);
                }

                ImGui.PushID($"Item {i}");
                ImGui.Button(item.ToString());
                ImGui.PopID();

                if (i == draggedState)
                {
                    ImGui.PopStyleColor();
                    ImGui.PopStyleColor();
                }

                if (ImGui.BeginDragDropSource())
                {
                    draggedState = i;
                    ImGui.PushID("carrying");
                    ImGui.Text(item.ToString());
                    ImGui.PopID();

                    ImGui.SetDragDropPayload("UNUSED", IntPtr.Zero, 0);
                    ImGui.EndDragDropSource();
                }

                if (ImGui.BeginDragDropTarget())
                {
                    ImGuiPayloadPtr dataPtr = ImGui.AcceptDragDropPayload("UNUSED");
                    unsafe
                    {
                        if ((IntPtr)dataPtr.NativePtr != IntPtr.Zero && dataPtr.IsDelivery())
                        {
                            int idxDragged = draggedState;
                            (list[idxDragged], list[i]) = (list[i], list[idxDragged]);
                            draggedState = -1;
                        }
                    }

                    ImGui.EndDragDropTarget();
                }

                // Check if drag/drop ended.
                ImGuiPayloadPtr payLoad = ImGui.GetDragDropPayload();
                unsafe
                {
                    if ((IntPtr)payLoad.NativePtr == IntPtr.Zero)
                    {
                        draggedState = -1;
                    }
                }

                if (horizontal && i != list.Length - 1)
                {
                    ImGui.SameLine();
                }
            }

            return(draggedState);
        }