Exemplo n.º 1
0
        void Traverse(GameObject gameObject)
        {
            bool leaf = gameObject.Transform.Children.Count <= 0;

            if (gameObject == selectedObj)
            {
                ImGui.PushStyleColor(ImGuiCol.Text, new System.Numerics.Vector4(0.25882352941f, 0.5294117647f, 0.96078431372f, 1));
            }
            var x = ImGui.GetCursorPosX();

            ImGui.Dummy(new System.Numerics.Vector2(ImGui.GetContentRegionAvail().X, ImGui.GetFontSize()));
            ImGui.SameLine();
            ImGui.SetCursorPosX(x);
            bool open = ImGui.TreeNodeEx(gameObject.UIDText, (!ImGui.IsMouseClicked(ImGuiMouseButton.Left) && leaf ? ImGuiTreeNodeFlags.Leaf : 0) | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth | ImGuiTreeNodeFlags.OpenOnArrow, gameObject.Name);


            if (ImGui.BeginDragDropSource())
            {
                if (ImGui.SetDragDropPayload("Transform", IntPtr.Zero, 0))
                {
                    scr = gameObject.Transform;
                }
                ImGui.EndDragDropSource();
            }

            if (ImGui.BeginDragDropTarget())
            {
                var payload = ImGui.AcceptDragDropPayload("Transform");
                if (payload.NativePtr != null && scr != null)
                {
                    scr.Parent = gameObject.Transform;
                }
                ImGui.EndDragDropTarget();
            }

            if (gameObject == selectedObj)
            {
                ImGui.PopStyleColor();
            }

            if (ImGui.IsItemClicked())
            {
                if (selectedObj != gameObject)
                {
                    SelectionChanged(gameObject);
                }
                selectedObj = gameObject;
            }

            if (open)
            {
                for (int i = 0; i < gameObject.Transform.Children.Count; i++)
                {
                    Traverse(gameObject.Transform.Children[i].GameObject);
                }
                ImGui.TreePop();
            }
        }
Exemplo n.º 2
0
 protected virtual void DragDropProc()
 {
     if (ImGui.BeginDragDropTarget())
     {
         if (m_NodeGraph != null)
         {
             OnDropEvent?.Invoke(m_NodeGraph);
         }
     }
 }
Exemplo n.º 3
0
        private static bool DrawDragDropTargetSources(CooldownTrigger trigger, IList <int> toSwap)
        {
            const string payloadIdentifier = "PRIORITY_PAYLOAD";

            if (ImGui.BeginDragDropSource(ImGuiDragDropFlags.SourceNoHoldToOpenOthers |
                                          ImGuiDragDropFlags.SourceNoPreviewTooltip))
            {
                unsafe
                {
                    var prio = trigger.Priority;
                    var ptr  = new IntPtr(&prio);
                    ImGui.SetDragDropPayload(payloadIdentifier, ptr, sizeof(int));
                }

                ImGui.SameLine();
                ImGui.AlignTextToFramePadding();
#if DEBUG
                ImGui.Text($"{trigger.Priority + 1} Dragging {trigger.ActionName}.");
#else
                ImGui.Text($"Dragging {trigger.ActionName}.");
#endif
                ImGui.EndDragDropSource();
                return(true);
            }

            if (!ImGui.BeginDragDropTarget())
            {
                return(false);
            }
            var imGuiPayloadPtr = ImGui.AcceptDragDropPayload(payloadIdentifier,
                                                              ImGuiDragDropFlags.AcceptNoPreviewTooltip |
                                                              ImGuiDragDropFlags.AcceptNoDrawDefaultRect);
            unsafe
            {
                if (imGuiPayloadPtr.NativePtr is not null)
                {
                    var prio = *(int *)imGuiPayloadPtr.Data;
                    toSwap[0] = prio;
                    toSwap[1] = trigger.Priority;
                }
            }

            ImGui.SameLine();
            ImGui.AlignTextToFramePadding();
#if DEBUG
            ImGui.Text($"{trigger.Priority + 1} Swap with {trigger.ActionName}");
#else
            ImGui.Text($"Swap with {trigger.ActionName}");
#endif
            ImGui.EndDragDropTarget();
            return(true);
        }
Exemplo n.º 4
0
        public static bool DragDropTarget(ushort handleIdx,
                                          DragDropWindow window, DragDropTree tree,
                                          string payloadType, ImGuiDragDropFlags flags,
                                          Action <DragDropWindow, DragDropTree, ushort,
                                                  DragDropWindow, DragDropTree, ushort> action)
        {
            bool ret = false;

            if (ImGui.BeginDragDropTarget())
            {
                var payload = ImGui.AcceptDragDropPayload(payloadType, flags);
                unsafe
                {
                    if (payload.NativePtr != null)
                    {
                        IntPtr dataSource = payload.Data;
                        System.Runtime.InteropServices.Marshal.Copy(dataSource, s_dragdropDataE, 0, s_dragdrop_size);
                        DragDropWindow windowSource    = (DragDropWindow)s_dragdropDataE[0];
                        DragDropTree   treeSource      = (DragDropTree)s_dragdropDataE[1];
                        ushort         handleIdxSource = 0;
                        unsafe
                        {
                            fixed(byte *pbyte = &s_dragdropDataE[2])
                            {
                                handleIdxSource = *((ushort *)pbyte);
                            }
                        }

                        if (action != null)
                        {
                            action(windowSource, treeSource, handleIdxSource,
                                   window, tree, handleIdx);
                        }

                        ret = true;
                    }
                }

                ImGui.EndDragDropTarget();
            }

            return(ret);
        }
Exemplo n.º 5
0
        public override void DrawUI()
        {
            if (ImGui.Begin(UIName, ref IsActive, ImGuiWindowFlags.NoCollapse))
            {
                ImGui.Dummy(ImGui.GetContentRegionAvail());
                ImGui.SetCursorPosY(0);

                ImGui.PushStyleColor(ImGuiCol.DragDropTarget, new System.Numerics.Vector4(0.219f, 0.223f, 0.623f, 1));
                if (ImGui.BeginDragDropTarget())
                {
                    var payload = ImGui.AcceptDragDropPayload("Transform");
                    if (payload.NativePtr != null && scr != null)
                    {
                        scr.Parent = null;
                    }
                    ImGui.EndDragDropTarget();
                }
                //for (int i = 0; i < EditorManager.Instance.LÖÖPS.Scenes.Count; i++)
                {
                    Scene scene = Manager.GameGameLoop.MasterScene;
                    if (ImGui.TreeNodeEx(scene.UIDText, ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.Framed | ImGuiTreeNodeFlags.DefaultOpen | ImGuiTreeNodeFlags.FramePadding, "Root Scene"))
                    {
                        var gos = scene.RootObjects;
                        for (int j = 0; j < gos.Count; j++)
                        {
                            if (gos[j].Transform.Parent == null)
                            {
                                Traverse(gos[j]);
                            }
                        }
                        ImGui.TreePop();
                    }
                }
                ImGui.PopStyleColor();
            }
            ImGui.End();
        }
Exemplo n.º 6
0
        private void TextNodeDragDrop(ITextNode node)
        {
            if (node != plugin.Configuration.RootFolder && ImGui.BeginDragDropSource())
            {
                DraggedNode = node;

                ImGui.Text(node.Name);
                ImGui.SetDragDropPayload("TextNodePayload", IntPtr.Zero, 0);
                ImGui.EndDragDropSource();
            }

            if (ImGui.BeginDragDropTarget())
            {
                var payload = ImGui.AcceptDragDropPayload("TextNodePayload");

                bool nullPtr;
                unsafe { nullPtr = payload.NativePtr == null; }

                var targetNode = node;
                if (!nullPtr && payload.IsDelivery() && DraggedNode != null)
                {
                    if (plugin.Configuration.TryFindParent(DraggedNode, out var draggedNodeParent))
                    {
                        if (targetNode is TextFolderNode targetFolderNode)
                        {
                            AddTextNode.Add(new AddTextNodeOperation {
                                Node = DraggedNode, ParentNode = targetFolderNode, Index = -1
                            });
                            RemoveTextNode.Add(new RemoveTextNodeOperation {
                                Node = DraggedNode, ParentNode = draggedNodeParent
                            });
                        }
                        else
                        {
                            if (plugin.Configuration.TryFindParent(targetNode, out var targetNodeParent))
                            {
                                var targetNodeIndex = targetNodeParent.Children.IndexOf(targetNode);
                                if (targetNodeParent == draggedNodeParent)
                                {
                                    var draggedNodeIndex = targetNodeParent.Children.IndexOf(DraggedNode);
                                    if (draggedNodeIndex < targetNodeIndex)
                                    {
                                        targetNodeIndex -= 1;
                                    }
                                }
                                AddTextNode.Add(new AddTextNodeOperation {
                                    Node = DraggedNode, ParentNode = targetNodeParent, Index = targetNodeIndex
                                });
                                RemoveTextNode.Add(new RemoveTextNodeOperation {
                                    Node = DraggedNode, ParentNode = draggedNodeParent
                                });
                            }
                            else
                            {
                                throw new Exception($"Could not find parent of node \"{targetNode.Name}\"");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception($"Could not find parent of node \"{DraggedNode.Name}\"");
                    }

                    DraggedNode = null;
                }

                ImGui.EndDragDropTarget();
            }
        }
Exemplo n.º 7
0
        unsafe private void MapObjectSelectable(Entity e, bool visicon, bool hierarchial = false)
        {
            // Main selectable
            if (e is MapEntity me)
            {
                ImGui.PushID(me.Type.ToString() + e.Name);
            }
            else
            {
                ImGui.PushID(e.Name);
            }
            bool doSelect = false;

            if (_setNextFocus)
            {
                ImGui.SetItemDefaultFocus();
                _setNextFocus = false;
                doSelect      = true;
            }
            bool   nodeopen = false;
            string padding  = hierarchial ? "   " : "    ";

            if (hierarchial && e.Children.Count > 0)
            {
                var treeflags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.SpanAvailWidth;
                if (_selection.GetSelection().Contains(e))
                {
                    treeflags |= ImGuiTreeNodeFlags.Selected;
                }
                nodeopen = ImGui.TreeNodeEx(e.PrettyName, treeflags);
                if (ImGui.IsItemHovered() && ImGui.IsMouseDoubleClicked(0))
                {
                    if (e.RenderSceneMesh != null)
                    {
                        _viewport.FrameBox(e.RenderSceneMesh.GetBounds());
                    }
                }
            }
            else
            {
                if (ImGui.Selectable(padding + e.PrettyName, _selection.GetSelection().Contains(e), ImGuiSelectableFlags.AllowDoubleClick | ImGuiSelectableFlags.AllowItemOverlap))
                {
                    // If double clicked frame the selection in the viewport
                    if (ImGui.IsMouseDoubleClicked(0))
                    {
                        if (e.RenderSceneMesh != null)
                        {
                            _viewport.FrameBox(e.RenderSceneMesh.GetBounds());
                        }
                    }
                }
            }
            if (ImGui.IsItemClicked(0))
            {
                _pendingClick = e;
            }

            if (_pendingClick == e && ImGui.IsMouseReleased(ImGuiMouseButton.Left))
            {
                if (ImGui.IsItemHovered())
                {
                    doSelect = true;
                }
                _pendingClick = null;
            }

            if (ImGui.IsItemFocused() && !_selection.IsSelected(e))
            {
                doSelect = true;
            }

            if (hierarchial && doSelect)
            {
                if ((nodeopen && !_treeOpenEntities.Contains(e)) ||
                    (!nodeopen && _treeOpenEntities.Contains(e)))
                {
                    doSelect = false;
                }

                if (nodeopen && !_treeOpenEntities.Contains(e))
                {
                    _treeOpenEntities.Add(e);
                }
                else if (!nodeopen && _treeOpenEntities.Contains(e))
                {
                    _treeOpenEntities.Remove(e);
                }
            }

            if (ImGui.BeginPopupContextItem())
            {
                _handler.OnEntityContextMenu(e);
                ImGui.EndPopup();
            }

            if (ImGui.BeginDragDropSource())
            {
                ImGui.Text(e.PrettyName);
                // Kinda meme
                DragDropPayload p = new DragDropPayload();
                p.Entity = e;
                _dragDropPayloads.Add(_dragDropPayloadCounter, p);
                DragDropPayloadReference r = new DragDropPayloadReference();
                r.Index = _dragDropPayloadCounter;
                _dragDropPayloadCounter++;
                GCHandle handle = GCHandle.Alloc(r, GCHandleType.Pinned);
                ImGui.SetDragDropPayload("entity", handle.AddrOfPinnedObject(), (uint)sizeof(DragDropPayloadReference));
                ImGui.EndDragDropSource();
                handle.Free();
                _initiatedDragDrop = true;
            }
            if (hierarchial && ImGui.BeginDragDropTarget())
            {
                var payload = ImGui.AcceptDragDropPayload("entity");
                if (payload.NativePtr != null)
                {
                    DragDropPayloadReference *h = (DragDropPayloadReference *)payload.Data;
                    var pload = _dragDropPayloads[h->Index];
                    _dragDropPayloads.Remove(h->Index);
                    _dragDropSources.Add(pload.Entity);
                    _dragDropDestObjects.Add(e);
                    _dragDropDests.Add(e.Children.Count);
                }
                ImGui.EndDragDropTarget();
            }

            // Visibility icon
            if (visicon)
            {
                ImGui.SetItemAllowOverlap();
                bool visible = e.EditorVisible;
                ImGui.SameLine(ImGui.GetWindowContentRegionWidth() - 18.0f);
                ImGui.PushStyleColor(ImGuiCol.Text, visible ? new Vector4(1.0f, 1.0f, 1.0f, 1.0f)
                    : new Vector4(0.6f, 0.6f, 0.6f, 1.0f));
                ImGui.TextWrapped(visible ? ForkAwesome.Eye : ForkAwesome.EyeSlash);
                ImGui.PopStyleColor();
                if (ImGui.IsItemClicked(0))
                {
                    e.EditorVisible = !e.EditorVisible;
                    doSelect        = false;
                }
            }

            // If the visibility icon wasn't clicked actually perform the selection
            if (doSelect)
            {
                if (InputTracker.GetKey(Key.ControlLeft) || InputTracker.GetKey(Key.ControlRight))
                {
                    _selection.AddSelection(e);
                }
                else
                {
                    _selection.ClearSelection();
                    _selection.AddSelection(e);
                }
            }

            ImGui.PopID();

            // Invisible item to be a drag drop target between nodes
            if (_pendingDragDrop)
            {
                if (e is MapEntity me2)
                {
                    ImGui.SetItemAllowOverlap();
                    ImGui.InvisibleButton(me2.Type.ToString() + e.Name, new Vector2(-1, 3.0f));
                }
                else
                {
                    ImGui.SetItemAllowOverlap();
                    ImGui.InvisibleButton(e.Name, new Vector2(-1, 3.0f));
                }
                if (ImGui.IsItemFocused())
                {
                    _setNextFocus = true;
                }
                if (ImGui.BeginDragDropTarget())
                {
                    var payload = ImGui.AcceptDragDropPayload("entity");
                    if (payload.NativePtr != null)
                    {
                        DragDropPayloadReference *h = (DragDropPayloadReference *)payload.Data;
                        var pload = _dragDropPayloads[h->Index];
                        _dragDropPayloads.Remove(h->Index);
                        if (hierarchial)
                        {
                            _dragDropSources.Add(pload.Entity);
                            _dragDropDestObjects.Add(e.Parent);
                            _dragDropDests.Add(e.Parent.ChildIndex(e) + 1);
                        }
                        else
                        {
                            _dragDropSources.Add(pload.Entity);
                            _dragDropDests.Add(pload.Entity.Container.Objects.IndexOf(e) + 1);
                        }
                    }
                    ImGui.EndDragDropTarget();
                }
            }

            // If there's children then draw them
            if (nodeopen)
            {
                HierarchyView(e);
                ImGui.TreePop();
            }
        }
Exemplo n.º 8
0
        public override void DrawUI()
        {
            if (IsWindowOpen)
            {
                ImGui.Begin("Tilemap Editor");

                if (Input.GetKeyDown(Microsoft.Xna.Framework.Input.Keys.D))
                {
                    ActiveTool = Tools.Delete;
                }
                else if (Input.GetKeyDown(Microsoft.Xna.Framework.Input.Keys.B))
                {
                    ActiveTool = Tools.Brush;
                }
                else if (Input.GetKeyDown(Microsoft.Xna.Framework.Input.Keys.G))
                {
                    ActiveTool = Tools.Bucket;
                }
                else if (ImGui.IsMouseClicked(ImGuiMouseButton.Middle))
                {
                    ActiveTool = Tools.None;
                }
                //else if (Input.GetKeyDown(Microsoft.Xna.Framework.Input.Keys.S))
                //    ActiveTool = Tools.Select;
                //else if (Input.GetKeyDown(Microsoft.Xna.Framework.Input.Keys.C))
                //    ActiveTool = Tools.CustomBrush;

                Vector2 ThisWindowPos  = ImGui.GetWindowPos();
                Vector2 ThisWindowSize = ImGui.GetWindowSize();

                MultiSelectRect.X += (int)(ThisWindowPos.X - LastWindPos.X);
                MultiSelectRect.Y += (int)(ThisWindowPos.Y - LastWindPos.Y);
                LastWindPos        = ThisWindowPos;

                if (ImGui.IsKeyPressed(ImGui.GetKeyIndex(ImGuiKey.Escape)))
                {
                    IsWindowOpen = false;
                }

                if (ImGui.BeginCombo("TileSets", ChosenTileSet))
                {
                    for (int i = 0; i < TileSets.Count; i++)
                    {
                        if (ImGui.Selectable(TileSets[i].Name))
                        {
                            ChosenTileSet = TileSets[i].Name;
                            ActiveTileSet = i;
                        }
                    }

                    ImGui.EndCombo();
                }

                ImGui.SameLine();
                ImGui.Checkbox("Show Utils", ref ShowUtils);

                ImGui.Separator();

                ImGui.PushStyleColor(ImGuiCol.Button, new Vector4(0.4f, 0.4f, 0.4f, 1));

                if (ImGui.Button("New Tileset"))
                {
                    ActiveTileSet = TileSets.Count;

                    string[] TilesetNames = new string[TileSets.Count];
                    for (int i = 0; i < TilesetNames.Length; i++)
                    {
                        TilesetNames[i] = TileSets[i].Name;
                    }

                    ChosenTileSet = Utility.UniqueName(ChosenTileSet, TilesetNames);
                    TileSets.Add(new TileSetClass()
                    {
                        Name = ChosenTileSet
                    });
                }

                ImGui.SameLine();

                if (ImGui.Button("Delete Tileset") && ActiveTileSet != -1)
                {
                    ImGui.OpenPopup("Are You Sure?" + "##2");
                    EnsureClipDeletion = true;
                }

                string activeTilemap = ActiveTilemap != null ? ActiveTilemap.Name : "Drag a tilemap gameobject here!";
                ImGui.InputText("Active Tilemap", ref activeTilemap, 50, ImGuiInputTextFlags.ReadOnly);

                if (ImGui.BeginDragDropTarget() && ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                {
                    if (GameObjects_Tab.DraggedGO != null && GameObjects_Tab.DraggedGO.GetComponent <Tilemap>() != null)
                    {
                        ActiveTilemap = GameObjects_Tab.DraggedGO;
                    }

                    ImGui.EndDragDropTarget();
                }

                ImGui.SameLine();

                ContentWindow.HelpMarker("Drag a gameobject having a Tilemap component here!", true);

                if (ActiveTilemap != null && (ActiveTilemap.ShouldBeDeleted || ActiveTilemap.ShouldBeRemoved))
                {
                    ActiveTilemap = null;
                }

                if (ActiveTileSet != -1)
                {
                    ImGui.InputText("Name", ref ChosenTileSet, 50);

                    if (ImGui.IsItemDeactivatedAfterEdit())
                    {
                        string[] TilesetNames = new string[TileSets.Count];
                        for (int i = 0; i < TilesetNames.Length; i++)
                        {
                            TilesetNames[i] = TileSets[i].Name;
                        }

                        ChosenTileSet = TileSets[ActiveTileSet].Name.Equals(ChosenTileSet)? ChosenTileSet : Utility.UniqueName(ChosenTileSet, TilesetNames);
                        TileSets[ActiveTileSet].Name = ChosenTileSet;
                    }

                    ImGui.InputText("Tileset", ref TilesetName, 50, ImGuiInputTextFlags.ReadOnly);

                    if (ImGui.BeginDragDropTarget() && ImGui.IsMouseReleased(ImGuiMouseButton.Left) && ContentWindow.DraggedAsset != null && ActiveTileSet != -1)
                    {
                        if (ContentWindow.DraggedAsset is KeyValuePair <string, Vector4> )
                        {
                            KeyValuePair <string, Vector4> KVP = (KeyValuePair <string, Vector4>)ContentWindow.DraggedAsset;
                            TileSetClass tileset = new TileSetClass();
                            tileset.Tex    = Setup.Content.Load <Texture2D>(KVP.Key);
                            tileset.Name   = "TileSet";
                            tileset.TexPtr = Scene.GuiRenderer.BindTexture(tileset.Tex);

                            int TileSetColumnCount = (int)Math.Round(10000.0f / KVP.Value.Z);
                            int TileSetRowCount    = (int)Math.Round(10000.0f / KVP.Value.W);

                            tileset.Rects = new Vector4[TileSetRowCount * TileSetColumnCount];

                            for (int i = 0; i < TileSetRowCount; i++)
                            {
                                for (int j = 0; j < TileSetColumnCount; j++)
                                {
                                    tileset.Rects[i * TileSetColumnCount + j] = new Vector4(j * KVP.Value.Z, i * KVP.Value.W, KVP.Value.Z, KVP.Value.W) / 10000.0f;
                                }
                            }

                            TileSets[ActiveTileSet] = tileset;
                            TilesetName             = tileset.Name;

                            ContentWindow.DraggedAsset = null;
                        }

                        ImGui.EndDragDropTarget();
                    }

                    float UpperGroupCursPos = ImGui.GetCursorPosY() + ImGui.GetWindowPos().Y;

                    ImGui.BeginChild("SpriteSheet", new Vector2(ImGui.GetWindowSize().X * 0.95f, 64 * 5.1f), false, ImGuiWindowFlags.AlwaysHorizontalScrollbar);
                    if (ActiveTileSet != -1 && TileSets[ActiveTileSet].Rects != null)
                    {
                        ImGui.Separator();

                        TileSets[ActiveTileSet].TexPtr = TileSets[ActiveTileSet].TexPtr == IntPtr.Zero ? Scene.GuiRenderer.BindTexture(TileSets[ActiveTileSet].Tex) : TileSets[ActiveTileSet].TexPtr;

                        //MultiSelect
                        Vector2 MousePos = ImGui.GetMousePos();
                        Microsoft.Xna.Framework.Point ScrollPos = new Microsoft.Xna.Framework.Point((int)ImGui.GetScrollX(), (int)ImGui.GetScrollY());
                        if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
                        {
                            WindowHoveredLastClick = ImGui.IsWindowHovered() && MousePos.Y > UpperGroupCursPos;

                            if (WindowHoveredLastClick)
                            {
                                VisualizeMultiSelect = false;
                                MultiSelectRect      = new Microsoft.Xna.Framework.Rectangle((int)MousePos.X + ScrollPos.X, (int)MousePos.Y + ScrollPos.Y, 0, 0);
                            }
                        }

                        if (ImGui.IsMouseDown(ImGuiMouseButton.Left) && WindowHoveredLastClick)
                        {
                            VisualizeMultiSelect = true;
                            MultiSelectRect      = new Microsoft.Xna.Framework.Rectangle(MultiSelectRect.X, MultiSelectRect.Y, (int)MousePos.X - MultiSelectRect.X + ScrollPos.X, (int)MousePos.Y - MultiSelectRect.Y + ScrollPos.Y);

                            Microsoft.Xna.Framework.Rectangle AdjustedRect = new Microsoft.Xna.Framework.Rectangle(Math.Min(MultiSelectRect.X, MultiSelectRect.X + MultiSelectRect.Width), Math.Min(MultiSelectRect.Y, MultiSelectRect.Y + MultiSelectRect.Height), Math.Abs(MultiSelectRect.Width), Math.Abs(MultiSelectRect.Height));
                            MultiSelectRect = AdjustedRect;
                        }

                        int     ID      = 0;
                        Vector2 WindPos = ImGui.GetWindowPos();
                        Vector2 Min     = new Vector2(int.MaxValue, int.MaxValue);
                        Vector2 Max     = new Vector2(int.MinValue, int.MinValue);
                        Vector2 Size    = new Vector2(TileSets[ActiveTileSet].Rects[0].Z * TileSets[ActiveTileSet].Tex.Width, TileSets[ActiveTileSet].Rects[0].W * TileSets[ActiveTileSet].Tex.Height);
                        Size = new Vector2((float)Math.Round(Size.X), (float)Math.Round(Size.Y));

                        Vector2 InnserSpacingOld = ImGui.GetStyle().ItemSpacing;

                        ImGui.GetStyle().ItemSpacing = Vector2.Zero;
                        Vector4 SelectedRect         = Vector4.Zero;

                        for (int j = 0; j < TileSets[ActiveTileSet].Rects.Length; j++)
                        {
                            Vector2 CursPos  = ImGui.GetCursorPos();
                            Vector4 Rect     = TileSets[ActiveTileSet].Rects[j];
                            Vector2 RealSize = new Vector2(64, Size.X / Size.Y * 64);

                            bool MultiSelected = VisualizeMultiSelect && MultiSelectRect.Intersects(new Microsoft.Xna.Framework.Rectangle((int)(CursPos.X + WindPos.X), (int)(CursPos.Y + WindPos.Y), (int)RealSize.X, (int)RealSize.Y));

                            if (MultiSelected)
                            {
                                if (CursPos.X < Min.X)
                                {
                                    SelectedRect.X = Rect.X;
                                    Min.X          = CursPos.X;
                                }
                                if (CursPos.Y < Min.Y)
                                {
                                    SelectedRect.Y = Rect.Y;
                                    Min.Y          = CursPos.Y;
                                }
                                if (CursPos.X + RealSize.X > Max.X)
                                {
                                    SelectedRect.Z = Rect.X + Rect.Z;
                                    Max.X          = CursPos.X + RealSize.X;
                                }
                                if (CursPos.Y + RealSize.Y > Max.Y)
                                {
                                    SelectedRect.W = Rect.Y + Rect.W;
                                    Max.Y          = CursPos.Y + RealSize.Y;
                                }
                            }

                            ImGui.PushID(ID++);
                            ImGui.Image(TileSets[ActiveTileSet].TexPtr, RealSize, new Vector2(Rect.X, Rect.Y), new Vector2(Rect.Z + Rect.X, Rect.W + Rect.Y), (SelectedTile == j || MultiSelected) ? new Vector4(1f, 1f, 1f, 0.5f) : Vector4.One);
                            ImGui.PopID();

                            if (ImGui.IsItemClicked(ImGuiMouseButton.Left))
                            {
                                SelectedTile = j;
                            }

                            if (ImGui.IsItemHovered())
                            {
                                HitBoxDebuger.DrawNonFilledRectangle_Effect(new Microsoft.Xna.Framework.Rectangle((int)(CursPos.X + WindPos.X - ImGui.GetScrollX()), (int)(CursPos.Y + WindPos.Y - ImGui.GetScrollY()), (int)RealSize.X, (int)RealSize.Y));
                            }

                            ImGui.SameLine();

                            if ((j + 1) * Size.X % TileSets[ActiveTileSet].Tex.Width == 0)
                            {
                                ImGui.NewLine();
                            }
                        }

                        ImGui.GetStyle().ItemSpacing = InnserSpacingOld;

                        ImGui.EndChild();

                        if (ImGui.BeginPopupModal("Are You Sure?" + "##2", ref EnsureClipDeletion, ImGuiWindowFlags.AlwaysAutoResize))
                        {
                            if (ImGui.Button("Yes, delete this"))
                            {
                                TileSets.RemoveAt(ActiveTileSet);
                                ActiveTileSet = -1;
                                ChosenTileSet = "";

                                ImGui.CloseCurrentPopup();
                            }

                            if (ImGui.Button("No"))
                            {
                                ImGui.CloseCurrentPopup();
                            }

                            ImGui.EndPopup();
                        }

                        if (ActiveTilemap != null) //Edit mode
                        {
                            if (GizmosVisualizer.IsThisWindowHovered && !ImGui.IsAnyItemHovered())
                            {
                                Tilemap tilemap = ActiveTilemap.GetComponent <Tilemap>();

                                if (tilemap != null)
                                {
                                    var Bias     = Setup.Camera.Position - ResolutionIndependentRenderer.GetVirtualRes() * 0.5f - new Microsoft.Xna.Framework.Vector2(GizmosVisualizer.SceneWindow.X + 7, GizmosVisualizer.SceneWindow.Y + 28);
                                    var GridSize = tilemap.TileSize.ToVector2() * tilemap.GridSize.ToVector2() * tilemap.gameObject.Transform.Scale;
                                    var MapRect  = new Microsoft.Xna.Framework.Rectangle((tilemap.gameObject.Transform.Position - GridSize * 0.5f).ToPoint(), GridSize.ToPoint());

                                    bool InsideBoundaries = MapRect.Contains(Bias + Input.GetMousePosition());

                                    ImGui.Text(InsideBoundaries.ToString());

                                    if (InsideBoundaries)
                                    {
                                        switch (ActiveTool)
                                        {
                                        case Tools.Brush:
                                            if (Min.X == int.MaxValue)
                                            {
                                                break;
                                            }

                                            if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
                                            {
                                                HandyList.Clear();
                                                HandyList2.Clear();
                                            }
                                            else if (ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                                            {
                                                //Undo action
                                                if (HandyList2.Count != 0)
                                                {
                                                    GameObjects_Tab.AddToACircularBuffer(GameObjects_Tab.Undo_Buffer, new KeyValuePair <object, Operation>(HandyList2.ToArray(), Operation.Delete));
                                                }
                                                if (HandyList.Count != 0)
                                                {
                                                    GameObjects_Tab.AddToACircularBuffer(GameObjects_Tab.Undo_Buffer, new KeyValuePair <object, Operation>(HandyList.ToArray(), Operation.Create));
                                                }
                                                GameObjects_Tab.Redo_Buffer.Clear();
                                            }
                                            else if (!ImGui.IsMouseDown(ImGuiMouseButton.Left))
                                            {
                                                break;
                                            }

                                            Microsoft.Xna.Framework.Point tileSize = (tilemap.TileSize.ToVector2() * tilemap.gameObject.Transform.Scale).ToPoint();

                                            GameObject NewTile = new GameObject();
                                            NewTile.Name  = Utility.UniqueGameObjectName("Tile");
                                            NewTile.Layer = ActiveTilemap.Layer;
                                            NewTile.AddComponent(new Transform());
                                            NewTile.AddComponent(new SpriteRenderer());
                                            NewTile.Start();

                                            NewTile.GetComponent <SpriteRenderer>().Sprite.Texture  = TileSets[ActiveTileSet].Tex;
                                            NewTile.GetComponent <SpriteRenderer>().SourceRectangle = new Microsoft.Xna.Framework.Rectangle((int)Math.Round(SelectedRect.X * TileSets[ActiveTileSet].Tex.Width), (int)Math.Round(SelectedRect.Y * TileSets[ActiveTileSet].Tex.Height), (int)Math.Round((SelectedRect.Z - SelectedRect.X) * TileSets[ActiveTileSet].Tex.Width), (int)Math.Round((SelectedRect.W - SelectedRect.Y) * TileSets[ActiveTileSet].Tex.Height));
                                            var RealTileSize = NewTile.GetComponent <SpriteRenderer>().SourceRectangle.Size.ToVector2() * tilemap.gameObject.Transform.Scale;
                                            var NewPos       = (Input.GetMousePosition() + Bias - MapRect.Location.ToVector2()) / MapRect.Size.ToVector2() / (RealTileSize / MapRect.Size.ToVector2());
                                            NewPos  *= RealTileSize / tileSize.ToVector2();
                                            NewPos.X = (int)NewPos.X;
                                            NewPos.Y = (int)NewPos.Y;

                                            NewTile.Transform.Scale    = tilemap.gameObject.Transform.Scale;
                                            NewTile.Transform.Position = MapRect.Location.ToVector2() + tileSize.ToVector2() * NewPos + RealTileSize * 0.5f * Microsoft.Xna.Framework.Vector2.One;

                                            bool ShouldBreak = false;
                                            foreach (GameObject Child in ActiveTilemap.Children)
                                            {
                                                if (Utility.Vector2Int(Child.Transform.Position) == NewTile.Transform.Position)
                                                {
                                                    SpriteRenderer DeletedOBJ_SR = Child.GetComponent <SpriteRenderer>();
                                                    SpriteRenderer NewOBJ_SR     = NewTile.GetComponent <SpriteRenderer>();

                                                    if (DeletedOBJ_SR.TextureName == NewOBJ_SR.TextureName && DeletedOBJ_SR.SourceRectangle == NewOBJ_SR.SourceRectangle)
                                                    {
                                                        ShouldBreak = true;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        HandyList2.Add(Child);
                                                    }

                                                    Child.ShouldBeRemoved = true;

                                                    break;
                                                }
                                            }

                                            if (ShouldBreak)
                                            {
                                                break;
                                            }

                                            HandyList.Add(NewTile);

                                            SceneManager.ActiveScene.AddGameObject_Recursive(NewTile);
                                            ActiveTilemap.AddChild(NewTile);

                                            break;

                                        case Tools.Delete:

                                            if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
                                            {
                                                HandyList2.Clear();
                                            }
                                            else if (ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                                            {
                                                if (HandyList2.Count != 0)
                                                {
                                                    GameObjects_Tab.AddToACircularBuffer(GameObjects_Tab.Undo_Buffer, new KeyValuePair <object, Operation>(HandyList2.ToArray(), Operation.Delete));
                                                    GameObjects_Tab.Redo_Buffer.Clear();
                                                }
                                            }

                                            if (ImGui.IsMouseDown(ImGuiMouseButton.Left))
                                            {
                                                tileSize = (tilemap.TileSize.ToVector2() * tilemap.gameObject.Transform.Scale).ToPoint();

                                                foreach (GameObject Child in ActiveTilemap.Children)
                                                {
                                                    if (new Microsoft.Xna.Framework.Rectangle(Child.Transform.Position.ToPoint() - (tileSize.ToVector2() * 0.5f).ToPoint(), tileSize).Contains(Input.GetMousePosition() + Bias))
                                                    {
                                                        Child.ShouldBeRemoved = true;
                                                        HandyList2.Add(Child);
                                                        break;
                                                    }
                                                }
                                            }

                                            break;

                                        case Tools.Bucket:     //remember to take another look at the logic (Solved!)

                                            if (Min.X == int.MaxValue)
                                            {
                                                break;
                                            }

                                            if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
                                            {
                                                HandyList.Clear();
                                                HandyList2.Clear();

                                                foreach (GameObject Child in ActiveTilemap.Children)
                                                {
                                                    Child.ShouldBeRemoved = true;
                                                    HandyList2.Add(Child);
                                                }

                                                tileSize = (tilemap.TileSize.ToVector2() * tilemap.gameObject.Transform.Scale).ToPoint();
                                                var SrcRect = new Microsoft.Xna.Framework.Rectangle((int)Math.Round(SelectedRect.X * TileSets[ActiveTileSet].Tex.Width), (int)Math.Round(SelectedRect.Y * TileSets[ActiveTileSet].Tex.Height), (int)Math.Round((SelectedRect.Z - SelectedRect.X) * TileSets[ActiveTileSet].Tex.Width), (int)Math.Round((SelectedRect.W - SelectedRect.Y) * TileSets[ActiveTileSet].Tex.Height));
                                                RealTileSize = SrcRect.Size.ToVector2() * tilemap.gameObject.Transform.Scale;
                                                var Ratio = new Microsoft.Xna.Framework.Point((int)Math.Round(RealTileSize.X / tileSize.X), (int)Math.Round(RealTileSize.Y / tileSize.Y));

                                                for (int i = 0; i < tilemap.GridSize.X / Ratio.Y; i++)
                                                {
                                                    for (int j = 0; j < tilemap.GridSize.Y / Ratio.X; j++)
                                                    {
                                                        NewTile       = new GameObject();
                                                        NewTile.Name  = Utility.UniqueGameObjectName("Tile");
                                                        NewTile.Layer = ActiveTilemap.Layer;
                                                        NewTile.AddComponent(new Transform());
                                                        NewTile.AddComponent(new SpriteRenderer());
                                                        NewTile.Start();

                                                        NewTile.GetComponent <SpriteRenderer>().Sprite.Texture  = TileSets[ActiveTileSet].Tex;
                                                        NewTile.GetComponent <SpriteRenderer>().SourceRectangle = SrcRect;
                                                        NewPos   = new Microsoft.Xna.Framework.Vector2(j * RealTileSize.X, i * RealTileSize.Y);
                                                        NewPos.X = (int)NewPos.X;
                                                        NewPos.Y = (int)NewPos.Y;

                                                        NewTile.Transform.Scale    = tilemap.gameObject.Transform.Scale;
                                                        NewTile.Transform.Position = MapRect.Location.ToVector2() + NewPos + RealTileSize * 0.5f;

                                                        SceneManager.ActiveScene.AddGameObject_Recursive(NewTile);
                                                        ActiveTilemap.AddChild(NewTile);

                                                        HandyList.Add(NewTile);
                                                    }
                                                }

                                                //Undo action
                                                if (HandyList2.Count != 0)
                                                {
                                                    GameObjects_Tab.AddToACircularBuffer(GameObjects_Tab.Undo_Buffer, new KeyValuePair <object, Operation>(HandyList2.ToArray(), Operation.Delete));
                                                }
                                                if (HandyList.Count != 0)
                                                {
                                                    GameObjects_Tab.AddToACircularBuffer(GameObjects_Tab.Undo_Buffer, new KeyValuePair <object, Operation>(HandyList.ToArray(), Operation.Create));
                                                }
                                                GameObjects_Tab.Redo_Buffer.Clear();
                                            }

                                            break;
                                            //case Tools.Select:

                                            //    tileSize = (tilemap.TileSize.ToVector2() * tilemap.gameObject.Transform.Scale).ToPoint();
                                            //    NewPos = (Input.GetMousePosition() + Bias - MapRect.Location.ToVector2()) / MapRect.Size.ToVector2() / (tileSize.ToVector2() / MapRect.Size.ToVector2());
                                            //    NewPos.X = (int)NewPos.X;
                                            //    NewPos.Y = (int)NewPos.Y;

                                            //    var TPos = MapRect.Location.ToVector2() + tileSize.ToVector2() * NewPos;

                                            //    if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
                                            //        TileMapSelectionArea = new Microsoft.Xna.Framework.Rectangle(TPos.ToPoint(), Microsoft.Xna.Framework.Point.Zero);
                                            //    else if (ImGui.IsMouseDown(ImGuiMouseButton.Left))
                                            //    {
                                            //        TileMapSelectionArea.Size = (TPos + tileSize.ToVector2() - TileMapSelectionArea.Location.ToVector2()).ToPoint();
                                            //        TileMapSelectionArea.Size = MathCompanion.Abs(TileMapSelectionArea.Size);
                                            //        TileMapSelectionArea.Location = new Microsoft.Xna.Framework.Point(Math.Min(TileMapSelectionArea.X, TileMapSelectionArea.X + TileMapSelectionArea.Width), Math.Min(TileMapSelectionArea.Y, TileMapSelectionArea.Y + TileMapSelectionArea.Height));
                                            //    }

                                            //    break;
                                        }
                                    }
                                    //else if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
                                    //    TileMapSelectionArea = Microsoft.Xna.Framework.Rectangle.Empty;
                                }
                                else if (tilemap == null)
                                {
                                    ActiveTilemap = null;
                                }
                            }
                        }
                    }

                    ImGui.EndChild();
                }

                ImGui.PopStyleColor();

                ImGui.End();

                if (ShowUtils)
                {
                    ImGui.Begin("Utils", ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoMove);

                    ContentWindow.HelpMarker("Handy Shortcuts: \nBrush(B)\nBucket(G)\nDelete(D)", true);

                    ImGui.SetWindowPos(new Vector2(ThisWindowPos.X + ThisWindowSize.X, ThisWindowPos.Y));

                    ImGui.PushStyleColor(ImGuiCol.Button, 0);

                    if (ImGui.ImageButton(TexPtrs[0], new Vector2(32, 32))) //Brush
                    {
                        ActiveTool = Tools.Brush;
                    }

                    if (ImGui.ImageButton(TexPtrs[1], new Vector2(32, 32))) //Bucket
                    {
                        ActiveTool = Tools.Bucket;
                    }

                    if (ImGui.ImageButton(TexPtrs[2], new Vector2(32, 32))) //Delete
                    {
                        ActiveTool = Tools.Delete;
                    }

                    ImGui.PopStyleColor();

                    ImGui.End();
                }
            }
        }
Exemplo n.º 9
0
        private void DrawComponents(Entity context)
        {
            // TODO: fix
            bool entityEnabled = context.Enabled;

            if (ImGui.Checkbox("Enabled", ref entityEnabled))
            {
                context.Enabled = entityEnabled;
            }

            var components = context.Components;

            for (int compi = 0; compi < components.Count; compi++)
            {
                Component component = components[compi];

                Type componentType = component.GetType();

                bool stay = true;

                ImGui.PushID(componentType.Name + compi);

                bool valcol = !component.Valid;
                if (valcol)
                {
                    ImGui.PushStyleColor(ImGuiCol.Header, new Vector4(0.412f, 0.118f, 0.118f, 1.0f));
                    ImGui.PushStyleColor(ImGuiCol.HeaderHovered, new Vector4(0.729f, 0.208f, 0.208f, 1.0f));
                }

                // item shifting drop
                unsafe
                {
                    // smoll fix
                    bool resetTarget = false;
                    if (targetedComponent == component)
                    {
                        ImGui.Button("##target", new Vector2(ImGui.GetColumnWidth(), 2.5f));
                        if (ImGui.IsItemHovered())
                        {
                            resetTarget = true;
                        }
                    }

                    if (ImGui.BeginDragDropTarget())
                    {
                        var payload = ImGui.AcceptDragDropPayload("_COMPONENT");
                        if (payload.NativePtr != null && selectedDragNDropComponent != null)
                        {
                            int tci = Context.ActiveEntity.GetComponentIndex(targetedComponent);
                            if (tci > Context.ActiveEntity.GetComponentIndex(selectedDragNDropComponent))
                            {
                                tci--;
                            }
                            Context.ActiveEntity.ShiftComponent(selectedDragNDropComponent, tci);

                            selectedDragNDropComponent = null;
                            targetedComponent          = null;
                        }
                        ImGui.EndDragDropTarget();
                    }

                    if (resetTarget)
                    {
                        targetedComponent = null;
                    }
                }

                bool collapsingHeader = ImGui.CollapsingHeader(componentType.Name, ref stay);

                // item shifting drag
                {
                    if (ImGui.BeginDragDropSource())
                    {
                        ImGui.Text("Component: " + componentType.Name);
                        selectedDragNDropComponent = component;
                        ImGui.SetDragDropPayload("_COMPONENT", IntPtr.Zero, 0);
                        ImGui.EndDragDropSource();
                    }

                    if (ImGui.BeginDragDropTarget())
                    {
                        targetedComponent = component;
                        ImGui.EndDragDropTarget();
                    }
                }

                // contex menu
                {
                    if (ImGui.BeginPopupContextItem())
                    {
                        if (ImGui.MenuItem("Move Up"))
                        {
                            context.ShiftComponent(component, Math.Max(0, compi - 1));
                        }
                        if (ImGui.MenuItem("Move Down"))
                        {
                            context.ShiftComponent(component, Math.Min(components.Count - 1, compi + 1));
                        }
                        ImGui.Separator();
                        if (ImGui.MenuItem("Remove"))
                        {
                            Context.ActiveEntity.RemoveComponent(component);
                        }

                        ImGui.EndPopup();
                    }
                }

                var   style = ImGui.GetStyle();
                float collapsingHeaderButtonOffset = ((ImGui.GetTextLineHeight() + style.FramePadding.Y * 2) + 1);
                ImGui.SameLine(ImGui.GetContentRegionAvail().X - (collapsingHeaderButtonOffset * 1 + style.FramePadding.X + style.FramePadding.Y));
                bool enabled = component.Enabled;
                if (ImGui.Checkbox("##enabled", ref enabled))
                {
                    component.Enabled = enabled;
                }
                ImGui.SameLine(ImGui.GetContentRegionAvail().X - (collapsingHeaderButtonOffset * 2 + style.FramePadding.X + style.FramePadding.Y));
                if (ImGui.ArrowButton("up", ImGuiDir.Up))
                {
                    context.ShiftComponent(component, Math.Max(0, compi - 1));
                }
                ImGui.SameLine(ImGui.GetContentRegionAvail().X - (collapsingHeaderButtonOffset * 3 + style.FramePadding.X + style.FramePadding.Y));
                if (ImGui.ArrowButton("down", ImGuiDir.Down))
                {
                    context.ShiftComponent(component, Math.Min(components.Count - 1, compi + 1));
                }


                if (collapsingHeader)
                {
                    ImGuiUtils.BeginGroupFrame();

                    MemberInfo[] membs = componentType.GetMembers();

                    for (int mi = 0; mi < membs.Length; mi++)
                    {
                        var mem = membs[mi];
                        if (mem.MemberType == MemberTypes.Field || mem.MemberType == MemberTypes.Property)
                        {
                            PropertyDrawer.DrawEditorValue(mem, component);
                        }
                    }

                    //ImGui.Separator();

                    ImGuiUtils.EndGroupFrame();
                }

                if (valcol)
                {
                    ImGui.PopStyleColor(2);
                }

                ImGui.PopID();

                if (!stay)
                {
                    Context.ActiveEntity.RemoveComponent(component);
                }
            }
        }
Exemplo n.º 10
0
        static unsafe void SubmitFieldPropertyInspector(FieldPropertyListInfo info, object entityComponent, bool showMidi = true)
        {
            ImGui.PushID(GetIdString(info, entityComponent));

            EditorHelper.RangeAttribute rangeAttribute = null;
            if (info.MemberInfo != null)
            {
                rangeAttribute = CustomAttributeExtensions.GetCustomAttribute <EditorHelper.RangeAttribute>(info.MemberInfo, true);
            }

            var infoType = info.FieldPropertyType;

            if (infoType == typeof(string))
            {
                string val = (string)info.GetValue();
                if (val == null)
                {
                    val = string.Empty;
                }
                if (ImGui.InputText(info.Name, ref val, 1000))
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(bool))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Button);
                }

                bool val = (bool)info.GetValue();
                if (ImGui.Checkbox(info.Name, ref val))
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(float))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                float val = (float)info.GetValue();
                bool  result;
                if (rangeAttribute != null &&
                    (rangeAttribute.RangeType == EditorHelper.RangeAttribute.RangeTypeEnum.Float ||
                     rangeAttribute.RangeType == EditorHelper.RangeAttribute.RangeTypeEnum.Int))
                {
                    if (rangeAttribute.RangeType == EditorHelper.RangeAttribute.RangeTypeEnum.Float)
                    {
                        result = ImGui.SliderFloat(info.Name, ref val, rangeAttribute.MinFloat, rangeAttribute.MaxFloat);
                    }
                    else
                    {
                        result = ImGui.SliderFloat(info.Name, ref val, rangeAttribute.MinInt, rangeAttribute.MaxInt);
                    }
                }
                else
                {
                    result = ImGui.DragFloat(info.Name, ref val, 0.1f);
                }
                if (result)
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(Vector2))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                Vector2 val = (Vector2)info.GetValue();
                if (ImGui.DragFloat2(info.Name, ref val))
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(Vector3))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                Vector3 val = (Vector3)info.GetValue();
                if (ImGui.DragFloat3(info.Name, ref val))
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(Vector4))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                Vector4 val = (Vector4)info.GetValue();
                if (ImGui.DragFloat4(info.Name, ref val))
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(Xna.Vector2))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                Xna.Vector2 xnaVal = (Xna.Vector2)info.GetValue();
                Vector2     val    = new Vector2(xnaVal.X, xnaVal.Y);
                if (ImGui.DragFloat2(info.Name, ref val))
                {
                    xnaVal.X = val.X;
                    xnaVal.Y = val.Y;
                    info.SetValue(xnaVal);
                }
            }
            else if (infoType == typeof(Xna.Vector3))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                Xna.Vector3 xnaVal = (Xna.Vector3)info.GetValue();
                Vector3     val    = new Vector3(xnaVal.X, xnaVal.Y, xnaVal.Z);
                if (ImGui.DragFloat3(info.Name, ref val))
                {
                    xnaVal.X = val.X;
                    xnaVal.Y = val.Y;
                    xnaVal.Z = val.Z;
                    info.SetValue(xnaVal);
                }
            }
            else if (infoType == typeof(Xna.Vector4))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                Xna.Vector4 xnaVal = (Xna.Vector4)info.GetValue();
                Vector4     val    = new Vector4(xnaVal.X, xnaVal.Y, xnaVal.Z, xnaVal.W);
                if (ImGui.DragFloat4(info.Name, ref val))
                {
                    xnaVal.X = val.X;
                    xnaVal.Y = val.Y;
                    xnaVal.Z = val.Z;
                    xnaVal.W = val.W;
                    info.SetValue(xnaVal);
                }
            }
            else if (infoType == typeof(int))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                int  val = (int)info.GetValue();
                bool result;
                if (rangeAttribute != null && rangeAttribute.RangeType == EditorHelper.RangeAttribute.RangeTypeEnum.Int)
                {
                    result = ImGui.SliderInt(info.Name, ref val, rangeAttribute.MinInt, rangeAttribute.MaxInt);
                }
                else
                {
                    result = ImGui.InputInt(info.Name, ref val);
                }
                if (result)
                {
                    info.SetValue(val);
                }
            }
            else if (infoType == typeof(uint))
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Knob);
                }

                int  val = (int)((uint)info.GetValue());
                bool result;
                if (rangeAttribute != null && rangeAttribute.RangeType == EditorHelper.RangeAttribute.RangeTypeEnum.Int)
                {
                    result = ImGui.SliderInt(info.Name, ref val, rangeAttribute.MinInt, rangeAttribute.MaxInt);
                }
                else
                {
                    result = ImGui.InputInt(info.Name, ref val);
                }
                if (result)
                {
                    if (val < 0)
                    {
                        val = 0;
                    }
                    info.SetValue((uint)val);
                }
            }
            else if (infoType.IsEnum)
            {
                if (showMidi)
                {
                    SubmitMidiAssignment(entityComponent, info, MidiState.MidiControlDescriptionType.Button);
                }

                var val          = info.GetValue();
                var enumNames    = infoType.GetEnumNames();
                int currentIndex = 0;
                for (int i = 0; i < enumNames.Length; i++)
                {
                    if (enumNames[i] == val.ToString())
                    {
                        currentIndex = i;
                    }
                }
                if (ImGui.Combo(info.Name, ref currentIndex, enumNames, enumNames.Length))
                {
                    info.SetValue(infoType.GetEnumValues().GetValue(currentIndex));
                }
            }
            else if (typeof(IList).IsAssignableFrom(infoType))
            {
                var   listthing = info.GetValue();
                IList list      = listthing as IList;
                ImGui.Text($"{info.Name} List ({list.Count} items)");
                ImGui.SameLine();
                if (ImGui.Button("-"))
                {
                    if (list.Count > 0)
                    {
                        list.RemoveAt(list.Count - 1);
                    }
                }
                ImGui.SameLine();
                if (ImGui.Button("+"))
                {
                    Type listItemType = list.GetType().GetGenericArguments().First();
                    if (listItemType.IsValueType)
                    {
                        list.Add(Activator.CreateInstance(listItemType));
                    }
                    else
                    {
                        list.Add(null);
                    }
                }

                ImGui.Indent();
                for (int i = 0; i < list.Count; i++)
                {
                    FieldPropertyListInfo itemInfo = new FieldPropertyListInfo(list, i);
                    SubmitFieldPropertyInspector(itemInfo, list);
                }
                ImGui.Unindent();
            }
            else if (!infoType.IsValueType)
            {
                string valText;
                var    value = info.GetValue();
                if (value != null)
                {
                    valText = value.ToString();
                }
                else
                {
                    valText = "null";
                }
                string label = $"{info.Name}: {valText}";
                if (typeof(Component).IsAssignableFrom(infoType) || typeof(Entity).IsAssignableFrom(infoType))
                {
                    if (ImGui.Selectable(label, false))
                    {
                        SelectedEntityComponent = value;
                        scrollEntitiesView      = true;
                        scrollSceneGraphView    = true;
                    }
                }
                else
                {
                    ImGui.Text(label);
                }

                if (draggedObject != null && infoType.IsAssignableFrom(draggedObject.GetType()))
                {
                    if (ImGui.BeginDragDropTarget())
                    {
                        var payload = ImGui.AcceptDragDropPayload(PAYLOAD_STRING);
                        if (payload.NativePtr != null) // Only when this is non-null does it mean that we've released the drag
                        {
                            info.SetValue(draggedObject);
                            draggedObject = null;
                        }
                        ImGui.EndDragDropTarget();
                    }
                }
            }
            else
            {
                SubmitReadonlyFieldPropertyInspector(info);
            }
            ImGui.PopID();

            SubmitHelpMarker(info);
        }
Exemplo n.º 11
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();
            }
        }
Exemplo n.º 12
0
        void DrawEntityNode(TreeNode <Entity> node, string id, int i = 0)
        {
            Entity nodeEntity = node.Value;


            // drag drop
            unsafe
            {
                // smoll fix
                bool resetTarget = false;
                if (targetedNode == node && targetedNode.Value != null)
                {
                    ImGui.Button("##target", new Vector2(ImGui.GetColumnWidth(), 2.5f));

                    if (ImGui.IsItemHovered())
                    {
                        resetTarget = true;
                    }
                }

                // dropped next to
                if (ImGui.BeginDragDropTarget())
                {
                    var payload = ImGui.AcceptDragDropPayload("_TREENODE<ENTITY>");
                    if (payload.NativePtr != null && selectedDragNDropNode != null)
                    {
                        EditorApplication.Log.Trace("dropped " +
                                                    $"{((selectedDragNDropNode != null && selectedDragNDropNode.Value != null) ? selectedDragNDropNode.Value.UID : "null")} before " +
                                                    $"{((targetedNode != null && nodeEntity != null) ? nodeEntity.UID : "null")}");

                        if (!targetedNode.IsParentedBy(selectedDragNDropNode))
                        {
                            if (selectedDragNDropNode.Parent != targetedNode.Parent)
                            {
                                selectedDragNDropNode.Value.Parent = targetedNode.Value.Parent;
                            }

                            int tnei = Context.Scene.GetEntityIndex(targetedNode.Value);
                            if (tnei > Context.Scene.GetEntityIndex(selectedDragNDropNode.Value))
                            {
                                tnei--;
                            }
                            Context.Scene.ShiftEntity(selectedDragNDropNode.Value, tnei);

                            int tnci = targetedNode.Parent.GetChildIndex(targetedNode);
                            if (tnci > targetedNode.Parent.GetChildIndex(selectedDragNDropNode))
                            {
                                tnci--;
                            }
                            targetedNode.Parent.ShiftChild(selectedDragNDropNode, tnci);
                        }
                        else
                        {
                            EditorApplication.Log.Trace($"drop failed (cyclic tree prevented)");
                        }

                        selectedDragNDropNode = null;
                        targetedNode          = null;
                    }
                    ImGui.EndDragDropTarget();
                }

                if (resetTarget)
                {
                    targetedNode = null;
                }
            }

            ImGuiTreeNodeFlags flags = ((nodeEntity == Context.ActiveEntity) ? ImGuiTreeNodeFlags.Selected : ImGuiTreeNodeFlags.None) |
                                       ImGuiTreeNodeFlags.OpenOnArrow |
                                       ImGuiTreeNodeFlags.OpenOnDoubleClick |
                                       ImGuiTreeNodeFlags.DefaultOpen |
                                       ((node.Children.Count <= 0) ? ImGuiTreeNodeFlags.Leaf : ImGuiTreeNodeFlags.None);

            if (nodeEntity == Context.ActiveEntity)
            {
                ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.259f, 0.588f, 0.98f, 1.0f));
            }

            string label = (Context.Scene.HierarchyRoot == node) ? "Scene" : (nodeEntity != null) ? (
                nodeEntity.TryGetComponent(out TagComponent tagcomp) ?
                tagcomp.Tag :
                "uid: " + nodeEntity.UID.ToString())
                : "";

            bool opened = ImGui.TreeNodeEx(id + i.ToString(), flags, label);

            if (nodeEntity == Context.ActiveEntity)
            {
                ImGui.PopStyleColor();
            }

            if (ImGui.IsItemClicked())
            {
                Context.ActiveEntity = nodeEntity;
            }

            // drag drop
            unsafe
            {
                if (ImGui.BeginDragDropSource())
                {
                    ImGui.Text("Entity: " + label);
                    selectedDragNDropNode = node;
                    ImGui.SetDragDropPayload("_TREENODE<ENTITY>", IntPtr.Zero, 0);
                    ImGui.EndDragDropSource();
                }

                // dropped on to
                if (ImGui.BeginDragDropTarget())
                {
                    targetedNode = node;

                    var payload = ImGui.AcceptDragDropPayload("_TREENODE<ENTITY>");
                    if (payload.NativePtr != null)
                    {
                        EditorApplication.Log.Trace("dropped " +
                                                    $"{((selectedDragNDropNode != null && selectedDragNDropNode.Value != null) ? selectedDragNDropNode.Value.UID : "null")} onto " +
                                                    $"{((node != null && nodeEntity != null) ? nodeEntity.UID : "null")}");

                        if (!node.IsParentedBy(selectedDragNDropNode))
                        {
                            selectedDragNDropNode.Value.Parent = nodeEntity;
                        }
                        else
                        {
                            EditorApplication.Log.Trace($"drop failed (cyclic tree prevented)");
                        }

                        selectedDragNDropNode = null;
                        targetedNode          = null;
                    }
                    ImGui.EndDragDropTarget();
                }
            }

            // context menu
            {
                if (ImGui.BeginPopupContextItem())
                {
                    if (ImGui.MenuItem("Add child"))
                    {
                        var newboi = Context.Scene.CreateEntity();
                        newboi.Parent = nodeEntity;
                    }
                    if (ImGui.MenuItem("Add empty child"))
                    {
                        var newboi = Context.Scene.CreateEmptyEntity();
                        newboi.Parent = nodeEntity;
                    }
                    ImGui.Separator();
                    if (ImGui.MenuItem("Remove entity", nodeEntity != null))
                    {
                        Context.Scene.DestroyEntity(nodeEntity);
                        Context.ActiveEntity = null;
                    }

                    ImGui.EndPopup();
                }
            }

            if (opened)
            {
                for (int cni = 0; cni < node.Children.Count; cni++)
                {
                    DrawEntityNode(node.Children[cni], id + "|" + i++.ToString());
                }
                ImGui.TreePop();
            }
        }
Exemplo n.º 13
0
Arquivo: GUI.cs Projeto: randomcrab/SE
 public static bool BeginDragDropTarget() => ImGui.BeginDragDropTarget();
Exemplo n.º 14
0
        public void Render()
        {
            //Menu
            if (ImGui.BeginMenuBar())
            {
                if (ImGui.BeginMenu("View Setting"))
                {
                    if (ImGui.BeginMenu("Background"))
                    {
                        ImGui.Checkbox("Display", ref DrawableBackground.Display);
                        ImGui.ColorEdit3("Color Top", ref DrawableBackground.BackgroundTop);
                        ImGui.ColorEdit3("Color Bottom", ref DrawableBackground.BackgroundBottom);
                        ImGui.EndMenu();
                    }
                    if (ImGui.BeginMenu("Grid"))
                    {
                        ImGui.Checkbox("Display", ref DrawableFloor.Display);
                        ImGui.ColorEdit4("Grid Color", ref DrawableFloor.GridColor);
                        ImGui.InputInt("Grid Cell Count", ref Toolbox.Core.Runtime.GridSettings.CellAmount);
                        ImGui.InputFloat("Grid Cell Size", ref Toolbox.Core.Runtime.GridSettings.CellSize);
                        ImGui.EndMenu();
                    }

                    // ImGui.Checkbox("VSync", ref Toolbox.Core.Runtime.EnableVSync);
                    ImGui.Checkbox("Wireframe", ref Toolbox.Core.Runtime.RenderSettings.Wireframe);
                    ImGui.Checkbox("WireframeOverlay", ref Toolbox.Core.Runtime.RenderSettings.WireframeOverlay);
                    ImGui.EndMenu();
                }
                if (ImGui.BeginMenu($"Shading [{Runtime.DebugRendering}]"))
                {
                    foreach (var mode in Enum.GetValues(typeof(Runtime.DebugRender)))
                    {
                        bool isSelected = (Runtime.DebugRender)mode == Runtime.DebugRendering;
                        if (ImGui.Selectable(mode.ToString(), isSelected))
                        {
                            Runtime.DebugRendering = (Runtime.DebugRender)mode;
                        }
                        if (isSelected)
                        {
                            ImGui.SetItemDefaultFocus();
                        }
                    }
                    ImGui.EndMenu();
                }

                if (ImGui.BeginMenu("Camera"))
                {
                    if (ImGui.Button("Reset Transform"))
                    {
                        Pipeline._context.Camera.TargetPosition = new OpenTK.Vector3(0, 1, -5);
                        Pipeline._context.Camera.RotationX      = 0;
                        Pipeline._context.Camera.RotationY      = 0;
                        Pipeline._context.Camera.UpdateMatrices();
                    }
                    ImGuiHelper.InputFromBoolean("Orthographic", Pipeline._context.Camera, "IsOrthographic");

                    ImGuiHelper.InputFromFloat("Fov (Degrees)", Pipeline._context.Camera, "FovDegrees", true, 1f);
                    if (Pipeline._context.Camera.FovDegrees != 45)
                    {
                        ImGui.SameLine(); if (ImGui.Button("Reset"))
                        {
                            Pipeline._context.Camera.FovDegrees = 45;
                        }
                    }

                    ImGuiHelper.InputFromFloat("ZFar", Pipeline._context.Camera, "ZFar", true, 1f);
                    if (Pipeline._context.Camera.ZFar != 100000.0f)
                    {
                        ImGui.SameLine(); if (ImGui.Button("Reset"))
                        {
                            Pipeline._context.Camera.ZFar = 100000.0f;
                        }
                    }

                    ImGuiHelper.InputFromFloat("ZNear", Pipeline._context.Camera, "ZNear", true, 0.1f);
                    if (Pipeline._context.Camera.ZNear != 0.1f)
                    {
                        ImGui.SameLine(); if (ImGui.Button("Reset"))
                        {
                            Pipeline._context.Camera.ZNear = 0.1f;
                        }
                    }

                    ImGuiHelper.InputFromFloat("Zoom Speed", Pipeline._context.Camera, "ZoomSpeed", true, 0.1f);
                    if (Pipeline._context.Camera.ZoomSpeed != 1.0f)
                    {
                        ImGui.SameLine(); if (ImGui.Button("Reset"))
                        {
                            Pipeline._context.Camera.ZoomSpeed = 1.0f;
                        }
                    }

                    ImGuiHelper.InputFromFloat("Pan Speed", Pipeline._context.Camera, "PanSpeed", true, 0.1f);
                    if (Pipeline._context.Camera.PanSpeed != 1.0f)
                    {
                        ImGui.SameLine(); if (ImGui.Button("Reset"))
                        {
                            Pipeline._context.Camera.PanSpeed = 1.0f;
                        }
                    }

                    ImGui.EndMenu();
                }
                if (ImGui.BeginMenu("Reset Animations"))
                {
                    parentWindow.Reset();
                    ImGui.EndMenu();
                }

                ImGui.EndMenuBar();

                var menuBG = ImGui.GetStyle().Colors[(int)ImGuiCol.MenuBarBg];
                ImGui.PushStyleColor(ImGuiCol.WindowBg, menuBG);
                ImGui.PushStyleColor(ImGuiCol.ChildBg, menuBG);
                if (ImGui.BeginChild("viewport_menu2", new System.Numerics.Vector2(350, 22)))
                {
                    ImGui.AlignTextToFramePadding();
                    ImGui.Text("Active Model(s)");
                    ImGui.SameLine();
                    if (ImGui.BeginCombo("##model_select", selectedModel))
                    {
                        bool isSelected = "All Models" == selectedModel;
                        if (ImGui.Selectable("All Models", isSelected))
                        {
                            selectedModel = "All Models";
                        }
                        if (isSelected)
                        {
                            ImGui.SetItemDefaultFocus();
                        }

                        foreach (var file in Pipeline.Files)
                        {
                            string name = file.Renderer.Name;
                            isSelected = name == selectedModel;

                            if (ImGui.Selectable(name, isSelected))
                            {
                                selectedModel = name;
                            }
                            if (isSelected)
                            {
                                ImGui.SetItemDefaultFocus();
                            }
                        }
                        ImGui.EndCombo();
                    }
                }
                ImGui.EndChild();
                ImGui.PopStyleColor(2);
            }

            //Make sure the entire viewport is within a child window to have accurate mouse and window sizing relative to the space it uses.
            if (ImGui.BeginChild("viewport_child1"))
            {
                var size = ImGui.GetWindowSize();
                if (Pipeline.Width != (int)size.X || Pipeline.Height != (int)size.Y)
                {
                    Pipeline.Width  = (int)size.X;
                    Pipeline.Height = (int)size.Y;
                    Pipeline.OnResize();
                }

                Pipeline.RenderScene();

                if (ImGui.IsWindowFocused() && ImGui.IsWindowHovered() || ForceUpdate || _mouseDown)
                {
                    ForceUpdate = false;

                    if (!onEnter)
                    {
                        Pipeline.ResetPrevious();
                        onEnter = true;
                    }

                    //Only update scene when necessary
                    UpdateCamera();
                }
                else
                {
                    onEnter = false;

                    //Reset drag/dropped model data if mouse leaves the viewport during a drag event
                    if (DragDroppedModel != null)
                    {
                        DragDroppedModel.DragDroppedOnLeave();
                        DragDroppedModel = null;
                    }
                }

                var id = Pipeline.GetViewportTexture();
                ImGui.Image((IntPtr)id, size, new System.Numerics.Vector2(0, 1), new System.Numerics.Vector2(1, 0));

                if (ImGui.BeginDragDropTarget())
                {
                    ImGuiPayloadPtr outlinerDrop = ImGui.AcceptDragDropPayload("OUTLINER_ITEM",
                                                                               ImGuiDragDropFlags.AcceptNoDrawDefaultRect | ImGuiDragDropFlags.AcceptBeforeDelivery);

                    if (outlinerDrop.IsValid())
                    {
                        //Drag/drop things onto meshes
                        var mouseInfo = CreateMouseState();
                        var picked    = Pipeline.GetPickedObject(mouseInfo);
                        //Picking object changed.
                        if (DragDroppedModel != picked)
                        {
                            //Set exit drop event for previous model
                            if (DragDroppedModel != null)
                            {
                                DragDroppedModel.DragDroppedOnLeave();
                            }

                            DragDroppedModel = picked;

                            //Model has changed so call the enter event
                            if (picked != null)
                            {
                                picked.DragDroppedOnEnter();
                            }
                        }

                        if (picked != null)
                        {
                            //Set the drag/drop event
                            var node = Outliner.GetDragDropNode();
                            picked.DragDropped(node.Tag);
                        }
                    }
                    ImGui.EndDragDropTarget();
                }
            }
            ImGui.EndChild();
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        private void TreeRecursive(GameObject GO, bool Root)
        {
            if (GO.ShouldBeDeleted || GO.IsEditor)
            {
                return;
            }

            int ChildrenCount = GO.Children.Count;

            if (ChildrenCount == 0)
            {
                if (!Root)
                {
                    ImGui.Indent();
                }

                if (!GO.IsActive())
                {
                    ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.5f, 0.5f, 0.5f, 1));
                    ImGui.PushStyleColor(ImGuiCol.HeaderHovered, new Vector4(0.8f, 0.8f, 0.8f, 1));
                }

                ImGui.Selectable(GO.Name, SelectedGOs.Contains(GO));

                if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
                {
                    if (ImGui.IsItemClicked())
                    {
                        Setup.Camera.Position = GO.Transform.Position + new Microsoft.Xna.Framework.Vector2(-GizmosVisualizer.SceneWindow.Z * 0.5f + Setup.graphics.PreferredBackBufferWidth * 0.5f, -GizmosVisualizer.SceneWindow.W * 0.5f + Setup.graphics.PreferredBackBufferHeight * 0.5f);
                    }
                }

                //Accept Drag and Drop
                if (ImGui.BeginDragDropSource(ImGuiDragDropFlags.SourceNoDisableHover))
                {
                    ImGui.SetDragDropPayload("GameObject", IntPtr.Zero, 0);
                    DraggedGO = GO;
                    InspectorWindow.DraggedObject = GO;

                    ImGui.Text(GO.Name);

                    ImGui.EndDragDropSource();
                }

                // The GameObject is a drag source and drop target at the same time
                if (ImGui.BeginDragDropTarget())
                {
                    if (ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                    {
                        if (DraggedGO != null && GO.Parent != DraggedGO)
                        {
                            if (DraggedGO.Parent != null)
                            {
                                DraggedGO.Parent.RemoveChild(DraggedGO);
                            }
                            GO.AddChild(DraggedGO);

                            // Undo and Redo Buffering
                            KeyValuePair <object, GameObject> KVP_GO = new KeyValuePair <object, GameObject>(new KeyValuePair <GameObject, string>(DraggedGO, (DraggedGO.Parent == null) ? null : DraggedGO.Parent.Name), GO);
                            AddToACircularBuffer(Undo_Buffer, new KeyValuePair <object, Operation>(KVP_GO, Operation.GO_DragAndDrop));
                            Redo_Buffer.Clear();
                        }

                        DraggedGO = null;
                    }

                    ImGui.EndDragDropTarget();
                }

                if (!GO.IsActive())
                {
                    ImGui.PopStyleColor();
                    ImGui.PopStyleColor();
                }

                if (!Root)
                {
                    ImGui.Unindent();
                }

                if (ImGui.IsMouseReleased(ImGuiMouseButton.Left) && ImGui.IsItemHovered())
                {
                    if (ImGui.GetIO().KeyCtrl)
                    {
                        if (!SelectedGOs.Add(GO))
                        {
                            SelectedGOs.Remove(GO);
                        }
                        else
                        {
                            WhoIsSelected = GO;
                        }
                    }
                    else if (ImGui.GetIO().KeyShift)
                    {
                        if (SelectedGOs.Count == 0)
                        {
                            SelectedGOs.Add(GO);
                            WhoIsSelected = GO;
                        }
                        else
                        {
                            SelectedGOs.Clear();
                            int CurrentGoIndex = -1;
                            int LastOneIndex   = -1;

                            for (int i = 0; i < SceneManager.ActiveScene.GameObjects.Count; i++)
                            {
                                if (SceneManager.ActiveScene.GameObjects[i] == GO)
                                {
                                    CurrentGoIndex = i;
                                }

                                if (SceneManager.ActiveScene.GameObjects[i] == WhoIsSelected)
                                {
                                    LastOneIndex = i;
                                }
                            }

                            WhoIsSelected = GO;

                            if (CurrentGoIndex > LastOneIndex)
                            {
                                for (int i = LastOneIndex; i <= CurrentGoIndex; i++)
                                {
                                    SelectedGOs.Add(SceneManager.ActiveScene.GameObjects[i]);
                                }
                            }
                            else
                            {
                                for (int i = CurrentGoIndex; i <= LastOneIndex; i++)
                                {
                                    SelectedGOs.Add(SceneManager.ActiveScene.GameObjects[i]);
                                }
                            }
                        }
                    }
                    else
                    {
                        SelectedGOs.Clear();
                        if (!SelectedGOs.Add(GO))
                        {
                            SelectedGOs.Remove(GO);
                        }
                        else
                        {
                            WhoIsSelected = GO;
                        }
                    }
                }

                return;
            }

            if (Root)
            {
                ImGui.Unindent();
            }

            bool Open = ImGui.TreeNodeEx(GO.Name, SelectedGOs.Contains(GO) ? ImGuiTreeNodeFlags.Selected : ImGuiTreeNodeFlags.None);

            if (!GO.IsActive())
            {
                ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.5f, 0.5f, 0.5f, 1));
                ImGui.PushStyleColor(ImGuiCol.HeaderHovered, new Vector4(0.8f, 0.8f, 0.8f, 1));
            }

            if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left))
            {
                if (ImGui.IsItemClicked())
                {
                    Setup.Camera.Position = GO.Transform.Position + (MyRegion[1].X + MyRegion[0].X) * Microsoft.Xna.Framework.Vector2.UnitX;
                }
            }

            //Accept Drag and Drop
            if (ImGui.BeginDragDropSource(ImGuiDragDropFlags.SourceNoDisableHover))
            {
                ImGui.SetDragDropPayload("GameObject", IntPtr.Zero, 0);
                DraggedGO = GO;
                InspectorWindow.DraggedObject = GO;

                ImGui.Text(GO.Name);

                ImGui.EndDragDropSource();
            }

            // The GameObject is a drag source and drop targer at the same time
            if (ImGui.BeginDragDropTarget())
            {
                if (ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                {
                    if (DraggedGO != null && GO.Parent != DraggedGO)
                    {
                        if (DraggedGO.Parent != null)
                        {
                            DraggedGO.Parent.RemoveChild(DraggedGO);
                        }
                        GO.AddChild(DraggedGO);

                        // Undo and Redo Buffering
                        KeyValuePair <object, GameObject> KVP_GO = new KeyValuePair <object, GameObject>(new KeyValuePair <GameObject, string>(DraggedGO, (DraggedGO.Parent == null) ? null : DraggedGO.Parent.Name), GO);
                        AddToACircularBuffer(Undo_Buffer, new KeyValuePair <object, Operation>(KVP_GO, Operation.GO_DragAndDrop));
                        Redo_Buffer.Clear();
                    }

                    DraggedGO = null;
                }

                ImGui.EndDragDropTarget();
            }

            if (!GO.IsActive())
            {
                ImGui.PopStyleColor();
                ImGui.PopStyleColor();
            }

            if (ImGui.IsMouseReleased(ImGuiMouseButton.Left) && ImGui.IsItemHovered())
            {
                if (ImGui.GetIO().KeyCtrl)
                {
                    if (!SelectedGOs.Add(GO))
                    {
                        SelectedGOs.Remove(GO);
                    }

                    if (SelectedGOs.Count != 0)
                    {
                        WhoIsSelected = GO;
                    }
                }
                else if (ImGui.GetIO().KeyShift)
                {
                    if (SelectedGOs.Count == 0)
                    {
                        SelectedGOs.Add(GO);
                        WhoIsSelected = GO;
                    }
                    else
                    {
                        SelectedGOs.Clear();
                        int CurrentGoIndex = -1;
                        int LastOneIndex   = -1;

                        for (int i = 0; i < SceneManager.ActiveScene.GameObjects.Count; i++)
                        {
                            if (SceneManager.ActiveScene.GameObjects[i] == GO)
                            {
                                CurrentGoIndex = i;
                            }

                            if (SceneManager.ActiveScene.GameObjects[i] == WhoIsSelected)
                            {
                                LastOneIndex = i;
                            }
                        }

                        WhoIsSelected = GO;

                        if (CurrentGoIndex > LastOneIndex)
                        {
                            for (int i = LastOneIndex; i <= CurrentGoIndex; i++)
                            {
                                SelectedGOs.Add(SceneManager.ActiveScene.GameObjects[i]);
                            }
                        }
                        else
                        {
                            for (int i = CurrentGoIndex; i <= LastOneIndex; i++)
                            {
                                SelectedGOs.Add(SceneManager.ActiveScene.GameObjects[i]);
                            }
                        }
                    }
                }
                else
                {
                    SelectedGOs.Clear();
                    if (!SelectedGOs.Add(GO))
                    {
                        SelectedGOs.Remove(GO);
                    }
                    else
                    {
                        WhoIsSelected = GO;
                    }
                }
            }

            for (int i = 0; i < ChildrenCount; i++)
            {
                if (ChildrenCount != GO.Children.Count)
                {
                    break;
                }

                if (Open)
                {
                    TreeRecursive(GO.Children[i], false);
                }
            }

            if (Open)
            {
                ImGui.TreePop();
            }

            if (Root)
            {
                ImGui.Indent();
            }
        }
Exemplo n.º 17
0
        private static unsafe void AddSceneGraphTransforms(EntityAdmin admin, List <Transform> list)
        {
            // The selectedTransform is either the selectedEntityComponent (if it's a Transform) or the Transform associated with the selected Entity/Component.
            Transform selectedTransform = SelectedEntityComponent as Transform;

            if (selectedTransform == null)
            {
                Entity entity = SelectedEntityComponent as Entity;
                if (entity == null)
                {
                    Component component = SelectedEntityComponent as Component;
                    if (component != null)
                    {
                        entity = component.Entity;
                    }
                }

                if (entity != null)
                {
                    selectedTransform = entity.GetComponent <Transform>(true);
                }
            }

            foreach (var transform in list)
            {
                ImGuiTreeNodeFlags nodeFlags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth; // OpenOnDoubleClick doesn't seem to work. Not sure why.
                if (transform.Children.Count == 0)
                {
                    nodeFlags |= ImGuiTreeNodeFlags.Leaf;
                }
                if (transform == selectedTransform)
                {
                    nodeFlags |= ImGuiTreeNodeFlags.Selected;
                    if (scrollSceneGraphView)
                    {
                        ImGui.SetScrollHereY();
                    }
                }

                bool      parentOfSelected = false;
                Transform parent           = selectedTransform as Transform;
                while (parent != null)
                {
                    parent = parent.Parent;
                    if (parent == transform)
                    {
                        parentOfSelected = true;
                        break;
                    }
                }
                if (parentOfSelected)
                {
                    ImGui.SetNextItemOpen(true);
                }
                if (!transform.IsActive)
                {
                    ImGui.PushStyleColor(ImGuiCol.Text, new Vector4(0.5f, 0.5f, 0.5f, 1f));
                }
                bool expanded = ImGui.TreeNodeEx(transform.Guid.ToString(), nodeFlags, transform.EntityName);
                if (!transform.IsActive)
                {
                    ImGui.PopStyleColor();
                }

                if (ImGui.IsItemClicked())
                {
                    SelectedEntityComponent = transform;
                    scrollEntitiesView      = true;
                }
                if (ImGui.BeginDragDropSource())
                {
                    ImGui.SetDragDropPayload(PAYLOAD_STRING, IntPtr.Zero, 0); // Payload is needed to trigger BeginDragDropTarget()
                    draggedObject = transform;
                    ImGui.Text(transform.EntityName);
                    ImGui.EndDragDropSource();
                }
                if (draggedObject != null && draggedObject is Transform)
                {
                    if (ImGui.BeginDragDropTarget())
                    {
                        var payload = ImGui.AcceptDragDropPayload(PAYLOAD_STRING);
                        if (payload.NativePtr != null) // Only when this is non-null does it mean that we've released the drag
                        {
                            Transform draggedTransform = draggedObject as Transform;
                            Transform newParent;
                            if (draggedTransform.Parent == transform)
                            {
                                newParent = null;
                            }
                            else
                            {
                                newParent = transform;
                            }
                            Transform.AssignParent(draggedTransform, newParent, admin, true);

                            draggedObject = null;
                        }
                        ImGui.EndDragDropTarget();
                    }
                }
                if (expanded)
                {
                    AddSceneGraphTransforms(admin, transform.Children.ToList());
                    ImGui.TreePop();
                }
            }
        }
Exemplo n.º 18
0
        public override void DrawUI()
        {
            if (IsWindowOpen)
            {
                if (ImGui.IsKeyPressed(ImGui.GetKeyIndex(ImGuiKey.Escape)))
                {
                    IsWindowOpen = false;
                }

                ImGui.PushStyleVar(ImGuiStyleVar.Alpha, 2);
                ImGui.PushStyleColor(ImGuiCol.Button, new Vector4(0.4f, 0.4f, 0.4f, 1));
                ImGui.Begin("Animation Editor");

                if (ImGui.Button("New Animation"))
                {
                    string[] ClipNames = new string[AnimationClips.Count];
                    for (int i = 0; i < AnimationClips.Count; i++)
                    {
                        ClipNames[i] = AnimationClips[i].Name;
                    }

                    AnimationClips.Add(new AnimationInfo()
                    {
                        Frames = new List <Frame>(), Name = Utility.UniqueName("Default Animation", ClipNames)
                    });
                    SelectedClip = AnimationClips.Count - 1;
                    SearchText   = AnimationClips[SelectedClip].Name;
                }

                ImGui.SameLine();

                if (ImGui.Button("Delete Clip"))
                {
                    if (SelectedClip != -1)
                    {
                        ImGui.OpenPopup("Are You Sure?");
                        EnsureClipDeletionBool = true;
                    }
                }

                if (ImGui.BeginPopupModal("Are You Sure?", ref EnsureClipDeletionBool, ImGuiWindowFlags.AlwaysAutoResize))
                {
                    if (ImGui.Button("Yes, delete this clip"))
                    {
                        AnimationClips.RemoveAt(SelectedClip);
                        ActiveFrame  = 0;
                        SelectedClip = -1;

                        ImGui.CloseCurrentPopup();
                    }

                    ImGui.SameLine();

                    if (ImGui.Button("No"))
                    {
                        ImGui.CloseCurrentPopup();
                    }

                    ImGui.EndPopup();
                }

                ImGui.SameLine();

                ImGui.InputText("", ref DraggedGO, 50, ImGuiInputTextFlags.ReadOnly);

                if (ImGui.BeginDragDropTarget() && GameObjects_Tab.DraggedGO != null && ImGui.IsMouseReleased(ImGuiMouseButton.Left) && GameObjects_Tab.DraggedGO.GetComponent <Animator>() != null)
                {
                    DraggedGO                 = GameObjects_Tab.DraggedGO.Name;
                    DraggedGO_AnimClips       = GameObjects_Tab.DraggedGO.GetComponent <Animator>().AnimationClips;
                    GameObjects_Tab.DraggedGO = null;
                }

                bool AnimatorTree = ImGui.TreeNode("Animator");

                if (ImGui.BeginDragDropTarget() && DraggedClip != -1 && ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                {
                    bool SafeToAdd = true;
                    foreach (Animation AI in DraggedGO_AnimClips)
                    {
                        if (AI.Name == AnimationClips[DraggedClip].Name)
                        {
                            SafeToAdd = false;
                            break;
                        }
                    }

                    if (SafeToAdd)
                    {
                        DraggedGO_AnimClips.Add(CopyAnimation(AnimationClips[DraggedClip]));
                    }

                    DraggedClip = -1;
                    ImGui.EndDragDropTarget();
                }

                ImGui.SameLine();
                ContentWindow.HelpMarker("- To add an animation to a gameobject, \njust drag the animation player on the right \nto this drop down menu called \"Animator\". \n- To delete an existing animation from the \nanimator gameobject, just right click on an entry!");

                if (AnimatorTree)
                {
                    if (DraggedGO_AnimClips != null)
                    {
                        for (int i = 0; i < DraggedGO_AnimClips.Count; i++)
                        {
                            ImGui.Selectable(DraggedGO_AnimClips[i].Name);

                            if (ImGui.IsItemClicked(ImGuiMouseButton.Right))
                            {
                                DraggedGO_AnimClips.RemoveAt(i--);
                            }

                            if (ImGui.IsMouseDoubleClicked(ImGuiMouseButton.Left) && ImGui.IsItemHovered())
                            {
                                for (int j = 0; j < AnimationClips.Count; j++)
                                {
                                    if (AnimationClips[j].Name == DraggedGO_AnimClips[i].Name)
                                    {
                                        SelectedClip                             = j;
                                        AnimationClips[j].Loop                   = DraggedGO_AnimClips[i].Loop;
                                        AnimationClips[j].FixedTimeAmount        = DraggedGO_AnimClips[i].FixedTime;
                                        AnimationClips[j].FixedTimeBewteenFrames = DraggedGO_AnimClips[i].FixedTimeBetweenFrames;
                                        AnimationClips[j].PlayReverse            = DraggedGO_AnimClips[i].Reverse;
                                        AnimationClips[j].Speed                  = DraggedGO_AnimClips[i].Speed;

                                        ActiveFrame = 0;
                                        SearchText  = DraggedGO_AnimClips[i].Name;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    ImGui.TreePop();
                }

                ImGui.Separator();

                if (ImGui.BeginCombo("Clips", SearchText))
                {
                    for (int i = 0; i < AnimationClips.Count; i++)
                    {
                        if (ImGui.Selectable(AnimationClips[i].Name))
                        {
                            SelectedClip = i;
                            SearchText   = AnimationClips[i].Name;
                            ActiveFrame  = 0;
                        }
                    }

                    ImGui.EndCombo();
                }

                //var CursPos = ImGui.GetCursorPos();
                //ImGui.Combo("Selected Clip", ref SelectedClip, ClipsNames, AnimationClips.Count);
                //ImGui.SetCursorPos(CursPos);
                //ImGui.SetItemAllowOverlap();
                //ImGui.InputText("", ref SearchText, 50);


                if (SelectedClip >= 0)
                {
                    //Animation Player
                    var CursPos1 = ImGui.GetCursorPos();
                    ImGui.SameLine();
                    ImGui.SetCursorPosX(ImGui.GetCursorPosX() + 90);

                    ImGui.BeginChild("Animation Player", new Vector2(128, 128), false, ImGuiWindowFlags.NoScrollbar);

                    if (ImGui.BeginDragDropSource())
                    {
                        DraggedClip = SelectedClip;
                        ImGui.SetDragDropPayload("Animation Clip", IntPtr.Zero, 0);

                        ImGui.EndDragDropSource();
                    }

                    if (AnimationClips[SelectedClip].Frames.Count == 0)
                    {
                        ImGui.Image(DragAndDropTex, new Vector2(128, 128));
                    }
                    else
                    {
                        Frame F = AnimationClips[SelectedClip].Frames[ActiveFrame];
                        if (ChangeFrameTimer < (AnimationClips[SelectedClip].FixedTimeBewteenFrames ? AnimationClips[SelectedClip].FixedTimeAmount : F.Time) / AnimationClips[SelectedClip].Speed)
                        {
                            ChangeFrameTimer += ImGui.GetIO().DeltaTime;
                        }
                        else
                        {
                            if (AnimationClips[SelectedClip].PlayReverse)
                            {
                                ActiveFrame = (ActiveFrame > 0) ? ActiveFrame - 1 : AnimationClips[SelectedClip].Frames.Count - 1;
                            }
                            else
                            {
                                ActiveFrame = (ActiveFrame < AnimationClips[SelectedClip].Frames.Count - 1) ? ActiveFrame + 1 : 0;
                            }

                            ChangeFrameTimer = 0;
                        }

                        ImGui.Image(F.TexPtr, new Vector2(128, 128), new Vector2((float)F.SourceRectangle.X / F.Tex.Width, (float)F.SourceRectangle.Y / F.Tex.Height), new Vector2((float)F.SourceRectangle.Right / F.Tex.Width, (float)F.SourceRectangle.Bottom / F.Tex.Height));
                    }

                    ImGui.EndChild();

                    ImGui.SetCursorPos(CursPos1);
                    ImGui.InputText("Name", ref AnimationClips[SelectedClip].Name, 50);
                    if (ImGui.IsItemDeactivatedAfterEdit())
                    {
                        string[] ClipNames = new string[AnimationClips.Count];
                        for (int i = 0; i < AnimationClips.Count; i++)
                        {
                            ClipNames[i] = AnimationClips[i].Name;
                        }


                        AnimationClips[SelectedClip].Name = AnimationClips[SelectedClip].Name.Equals(AnimationClips[SelectedClip].Name) ? AnimationClips[SelectedClip].Name : Utility.UniqueName(AnimationClips[SelectedClip].Name, ClipNames);
                        SearchText = AnimationClips[SelectedClip].Name;
                    }

                    ImGui.InputText("Tag", ref AnimationClips[SelectedClip].Tag, 50);
                    ImGui.InputFloat("Speed", ref AnimationClips[SelectedClip].Speed);
                    ImGui.Checkbox("Reversed", ref AnimationClips[SelectedClip].PlayReverse);
                    ImGui.Checkbox("Loop", ref AnimationClips[SelectedClip].Loop);

                    ImGui.Checkbox("Fixed Time Bewteen Frames", ref AnimationClips[SelectedClip].FixedTimeBewteenFrames);

                    if (AnimationClips[SelectedClip].FixedTimeBewteenFrames)
                    {
                        ImGui.InputFloat("Time", ref AnimationClips[SelectedClip].FixedTimeAmount);
                    }

                    //Frames
                    ImGui.Separator();

                    if (AnimationClips[SelectedClip].Frames.Count != 0)
                    {
                        ImGui.BeginChild("Frames", Vector2.Zero, false, ImGuiWindowFlags.HorizontalScrollbar);

                        for (int i = 0; i < AnimationClips[SelectedClip].Frames.Count; i++)
                        {
                            Frame F = AnimationClips[SelectedClip].Frames[i];
                            if (F.TexPtr == default(IntPtr))
                            {
                                F.TexPtr = Scene.GuiRenderer.BindTexture(F.Tex);
                            }

                            ImGui.BeginGroup();
                            ImGui.ImageButton(F.TexPtr, new Vector2(64, 64), new Vector2((float)F.SourceRectangle.X / F.Tex.Width, (float)F.SourceRectangle.Y / F.Tex.Height), new Vector2((float)F.SourceRectangle.Right / F.Tex.Width, (float)F.SourceRectangle.Bottom / F.Tex.Height));

                            if (ImGui.IsItemClicked(ImGuiMouseButton.Right))
                            {
                                AnimationClips[SelectedClip].Frames.RemoveAt(i--);
                                ActiveFrame = 0;
                            }

                            if (ImGui.BeginDragDropSource())
                            {
                                DraggedFrame = i;
                                ImGui.SetDragDropPayload("Dragged Frame", IntPtr.Zero, 0);

                                ImGui.EndDragDropSource();
                            }

                            if (ImGui.BeginDragDropTarget() && DraggedFrame != -1 && ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                            {
                                Frame Source = AnimationClips[SelectedClip].Frames[DraggedFrame];
                                Frame Target = AnimationClips[SelectedClip].Frames[i];
                                AnimationClips[SelectedClip].Frames.RemoveAt(DraggedFrame);
                                AnimationClips[SelectedClip].Frames.Insert(DraggedFrame, Target);
                                AnimationClips[SelectedClip].Frames.RemoveAt(i);
                                AnimationClips[SelectedClip].Frames.Insert(i, Source);

                                DraggedFrame = -1;
                                ImGui.EndDragDropTarget();
                            }

                            if (!AnimationClips[SelectedClip].FixedTimeBewteenFrames)
                            {
                                ImGui.PushItemWidth(64);
                                ImGui.PushID(i);
                                ImGui.SliderFloat("", ref F.Time, 0, 10);
                                ImGui.PopID();
                                ImGui.PopItemWidth();
                            }

                            ImGui.EndGroup();

                            ImGui.SameLine();
                        }

                        ImGui.EndChild();

                        ImGui.SameLine();
                    }

                    ImGui.ImageButton(DragAndDropTex, new Vector2(64, 64));

                    //Drag and Drop
                    if (ImGui.BeginDragDropTarget() && ImGui.IsMouseReleased(ImGuiMouseButton.Left))
                    {
                        if (ContentWindow.DraggedAsset != null)
                        {
                            if (ContentWindow.DraggedAsset is string) //Whole texture
                            {
                                Frame NewFrame = new Frame();
                                bool  Safe     = true;
                                try { NewFrame.Tex = Setup.Content.Load <Texture2D>((string)ContentWindow.DraggedAsset); }
                                catch (Exception E) { Safe = false; Utility.Log(E.Message); }

                                if (Safe)
                                {
                                    NewFrame.SourceRectangle = NewFrame.Tex.Bounds;
                                    NewFrame.Time            = 0.25f;
                                    NewFrame.TexPtr          = Scene.GuiRenderer.BindTexture(NewFrame.Tex);

                                    AnimationClips[SelectedClip].Frames.Add(NewFrame);
                                    ActiveFrame = 0;
                                }
                            }
                            else if (ContentWindow.DraggedAsset is KeyValuePair <string, Vector4> ) //Sliced Texture
                            {
                                Frame NewFrame = new Frame();
                                bool  Safe     = true;
                                try { NewFrame.Tex = Setup.Content.Load <Texture2D>(((KeyValuePair <string, Vector4>)ContentWindow.DraggedAsset).Key); }
                                catch (Exception E) { Safe = false; Utility.Log(E.Message); }

                                if (Safe)
                                {
                                    Vector4 SrcRect = ((KeyValuePair <string, Vector4>)ContentWindow.DraggedAsset).Value;
                                    NewFrame.SourceRectangle = new Microsoft.Xna.Framework.Rectangle((int)Math.Round(SrcRect.X * (NewFrame.Tex.Width / 10000.0f)), (int)Math.Round(SrcRect.Y * (NewFrame.Tex.Height / 10000.0f)), (int)Math.Round(SrcRect.Z * (NewFrame.Tex.Width / 10000.0f)), (int)Math.Round(SrcRect.W * (NewFrame.Tex.Height / 10000.0f)));
                                    NewFrame.Time            = 0.25f;
                                    NewFrame.TexPtr          = Scene.GuiRenderer.BindTexture(NewFrame.Tex);

                                    AnimationClips[SelectedClip].Frames.Add(NewFrame);
                                    ActiveFrame = 0;
                                }
                            }

                            ContentWindow.DraggedAsset = null;
                        }
                        else if (slicedTexs.Key != null)
                        {
                            bool      Safe = true;
                            Texture2D T2D  = null;
                            try { T2D = Setup.Content.Load <Texture2D>(slicedTexs.Key); }
                            catch (Exception E) { Safe = false; Utility.Log(E.Message); }

                            if (Safe)
                            {
                                foreach (Microsoft.Xna.Framework.Rectangle Rect in slicedTexs.Value)
                                {
                                    Frame NewFrame = new Frame();
                                    NewFrame.Tex = T2D;
                                    Vector4 SrcRect = new Vector4(Rect.X, Rect.Y, Rect.Width, Rect.Height);
                                    NewFrame.SourceRectangle = new Microsoft.Xna.Framework.Rectangle((int)Math.Round(SrcRect.X * (NewFrame.Tex.Width / 10000.0f)), (int)Math.Round(SrcRect.Y * (NewFrame.Tex.Height / 10000.0f)), (int)Math.Round(SrcRect.Z * (NewFrame.Tex.Width / 10000.0f)), (int)Math.Round(SrcRect.W * (NewFrame.Tex.Height / 10000.0f)));
                                    NewFrame.Time            = 0.25f;
                                    NewFrame.TexPtr          = Scene.GuiRenderer.BindTexture(NewFrame.Tex);

                                    AnimationClips[SelectedClip].Frames.Add(NewFrame);
                                }

                                ActiveFrame = 0;
                            }

                            slicedTexs = new KeyValuePair <string, List <Microsoft.Xna.Framework.Rectangle> >(null, null);
                        }

                        ImGui.EndDragDropTarget();
                    }
                }

                ImGui.End();
                ImGui.PopStyleColor();
                ImGui.PopStyleVar();
            }
        }
Exemplo n.º 19
0
        private void DrawViewport()
        {
            var size = ImGui.GetWindowSize();

            if (Pipeline.Width != (int)size.X || Pipeline.Height != (int)size.Y)
            {
                Pipeline.Width  = (int)size.X;
                Pipeline.Height = (int)size.Y;
                Pipeline.OnResize();
            }

            Pipeline.RenderScene();

            if ((ImGui.IsWindowFocused() && _mouseDown) ||
                ImGui.IsWindowFocused() && ImGui.IsWindowHovered() || _mouseDown)
            {
                if (!onEnter)
                {
                    Pipeline.ResetPrevious();
                    onEnter = true;
                }

                //Only update scene when necessary
                if (ImGuiController.ApplicationHasFocus)
                {
                    UpdateCamera();
                }
            }
            else
            {
                onEnter = false;

                //Reset drag/dropped model data if mouse leaves the viewport during a drag event
                if (DragDroppedModel != null)
                {
                    DragDroppedModel.DragDroppedOnLeave();
                    DragDroppedModel = null;
                }
            }

            var id = Pipeline.GetViewportTexture();

            ImGui.Image((IntPtr)id, size, new System.Numerics.Vector2(0, 1), new System.Numerics.Vector2(1, 0));

            if (ImGui.BeginDragDropTarget())
            {
                ImGuiPayloadPtr outlinerDrop = ImGui.AcceptDragDropPayload("OUTLINER_ITEM",
                                                                           ImGuiDragDropFlags.AcceptNoDrawDefaultRect | ImGuiDragDropFlags.AcceptBeforeDelivery);

                if (outlinerDrop.IsValid())
                {
                    //Drag/drop things onto meshes
                    var mouseInfo = CreateMouseState();
                    var picked    = Pipeline.GetPickedObject(mouseInfo);
                    //Picking object changed.
                    if (DragDroppedModel != picked)
                    {
                        //Set exit drop event for previous model
                        if (DragDroppedModel != null)
                        {
                            DragDroppedModel.DragDroppedOnLeave();
                        }

                        DragDroppedModel = picked;

                        //Model has changed so call the enter event
                        if (picked != null)
                        {
                            picked.DragDroppedOnEnter();
                        }
                    }

                    if (picked != null)
                    {
                        //Set the drag/drop event
                        var node = Outliner.GetDragDropNode();
                        picked.DragDropped(node.Tag);
                    }
                    if (mouseInfo.LeftButton == ButtonState.Released)
                    {
                        DragDroppedModel = null;
                    }
                }
                ImGui.EndDragDropTarget();
            }
        }
Exemplo n.º 20
0
 public static bool BeginDragDropTarget()
 {
     return(ImGui.BeginDragDropTarget());
 }