示例#1
0
 public void AddLine(Vector2 a, Vector2 b, uint color, float thickness)
 {
     ImGuiNative.ImDrawList_AddLine(_nativeDrawList, a, b, color, thickness);
 }
示例#2
0
        public static void TabLabels(List <DockTab> tabs, ref DockTab selected)
        {
            if (tabs.Count == 0)
            {
                return;
            }

            var style               = ImGui.GetStyle();
            var color               = ImGuiNative.igGetColorU32(ColorTarget.FrameBg, 1);
            var color_active        = ImGuiNative.igGetColorU32(ColorTarget.FrameBgActive, 1);
            var color_hovered       = ImGuiNative.igGetColorU32(ColorTarget.FrameBgHovered, 1);
            var text_color          = ImGuiNative.igGetColorU32(ColorTarget.Text, 1);
            var text_color_disabled = ImGuiNative.igGetColorU32(ColorTarget.TextDisabled, 1);

            float windowWidth = 0;

            windowWidth = ImGui.GetWindowWidth() - 2 * style.WindowPadding.X - (ImGuiNative.igGetScrollMaxY() > 0 ? style.ScrollbarSize : 0);
            float tab_base   = 0;
            var   lineheight = ImGuiNative.igGetTextLineHeightWithSpacing();

            bool isMMBreleased   = ImGui.IsMouseReleased(2);
            bool isMouseDragging = ImGui.IsMouseDragging(0, 2f);

            var drawList = ImGuiNative.igGetWindowDrawList();

            float totalWidth = 0;

            foreach (var sztab in tabs)
            {
                totalWidth += ImGui.GetTextSize(sztab.Title).X;
                totalWidth += 28 + style.ItemSpacing.X;
            }
            var         winSize = new Vector2(windowWidth, lineheight);
            WindowFlags cflags  = WindowFlags.Default;

            if (totalWidth > windowWidth)
            {
                cflags = WindowFlags.HorizontalScrollbar;
                ImGuiNative.igSetNextWindowContentSize(new Vector2(totalWidth, 0));
                winSize.Y += style.ScrollbarSize + 2;
            }
            ImGui.BeginChild("##tabbuttons", winSize, false, cflags);
            for (int i = 0; i < tabs.Count; i++)
            {
                if (i == -1)
                {
                    continue;
                }
                //do button
                if (i > 0)
                {
                    ImGui.SameLine(0, 15);
                }
                ImGui.PushID(i);
                var title  = tabs[i].Title.Split(new string[] { "##" }, StringSplitOptions.None)[0];
                var textSz = ImGui.GetTextSize(title).X;
                var size   = new Vector2(textSz, lineheight);
                //Selection and hover
                if (ImGui.InvisibleButton(title, size))
                {
                    selected = tabs[i];
                }
                System.Numerics.Vector2 itemRectSize;
                ImGuiNative.igGetItemRectSize(out itemRectSize);
                bool hovered = ImGui.IsItemHovered(HoveredFlags.RectOnly);
                if (hovered)
                {
                    // tab reordering
                    if (isMouseDragging)
                    {
                        if (draggingTabIndex == -1)
                        {
                            draggingTabIndex = i;
                            draggingtabSize  = size;
                            Vector2 mp = ImGui.GetIO().MousePosition;
                            System.Numerics.Vector2 draggingTabCursorPos;
                            ImGuiNative.igGetCursorPos(&draggingTabCursorPos);
                            draggingTabOffset = new Vector2(
                                draggingtabSize.X * 0.5f,
                                draggingtabSize.Y * 0.5f
                                );
                        }
                    }
                    else if (draggingTabIndex >= 0 && draggingTabIndex < tabs.Count && draggingTabIndex != i)
                    {
                        draggingTabTargetIndex = i; // For some odd reasons this seems to get called only when draggingTabIndex < i ! (Probably during mouse dragging ImGui owns the mouse someway and sometimes ImGui::IsItemHovered() is not getting called)
                    }
                }
                //actually draw
                var pos = (Vector2)ImGui.GetLastItemRectMin();
                tab_base = pos.Y;
                size.X  += 20 + style.ItemSpacing.X;
                ImGuiNative.ImDrawList_AddRectFilled(
                    drawList, pos + new Vector2(-8, 0),
                    pos + size,
                    hovered ? color_hovered : selected == tabs[i] ? color_active : color,
                    0,
                    0
                    );
                var bytes = System.Text.Encoding.UTF8.GetBytes(title);
                fixed(byte *ptr = bytes)
                {
                    ImGuiNative.ImDrawList_AddText(
                        drawList, pos, text_color, ptr, (byte *)0
                        );
                }

                if (tabs[i] == selected)
                {
                    ImGui.SameLine();
                    if (ImGui.InvisibleButton("close", new Vector2(16, 16)))
                    {
                        if (i > 0)
                        {
                            selected = tabs[i - 1];
                        }
                        else if (i == 0 && tabs.Count > 1)
                        {
                            selected = tabs[i + 1];
                        }
                        else
                        {
                            selected = null;
                        }
                        tabs[i].Dispose();
                        tabs.RemoveAt(i);
                    }
                    var c = ((Vector2)ImGui.GetLastItemRectMin() +
                             (Vector2)ImGui.GetLastItemRectMax()) * 0.5f;
                    ImGuiNative.ImDrawList_AddLine(
                        drawList, c - new Vector2(3.5f, 3.5f), c + new Vector2(3.5f, 3.5f), text_color, 1
                        );
                    ImGuiNative.ImDrawList_AddLine(
                        drawList, c + new Vector2(3.5f, -3.5f), c + new Vector2(-3.5f, 3.5f), text_color, 1
                        );
                }
                else
                {
                    ImGui.SameLine();
                    ImGui.Dummy(16, 16);
                }
                ImGui.PopID();
            }
            ImGui.EndChild();
            // Drop tab label
            if (draggingTabTargetIndex != -1)
            {
                // swap draggingTabIndex and draggingTabTargetIndex in tabOrder
                var tmp = tabs[draggingTabTargetIndex];
                tabs[draggingTabTargetIndex] = tabs[draggingTabIndex];
                tabs[draggingTabIndex]       = tmp;
                draggingTabTargetIndex       = draggingTabIndex = -1;
            }
            // Reset draggingTabIndex if necessary
            if (!isMouseDragging)
            {
                draggingTabIndex = -1;
            }

            var basestart = new Vector2(0, tab_base + lineheight);
            var baseEnd   = new Vector2(windowWidth, tab_base + lineheight);

            ImGuiNative.ImDrawList_AddLine(drawList, basestart, baseEnd, color, 1);
        }