Exemple #1
0
        public static void TabLabels(List <DockTab> tabs, ref DockTab selected)
        {
            if (tabs.Count > 0)
            {
                var flags = ImGuiTabBarFlags.Reorderable | ImGuiTabBarFlags.FittingPolicyScroll |
                            ImGuiTabBarFlags.AutoSelectNewTabs | ImGuiTabBarFlags.TabListPopupButton;
                ImGui.BeginTabBar("##tabbar", flags);
                for (int i = 0; i < tabs.Count; i++)
                {
                    bool isTabOpen = true;
                    if (ImGui.BeginTabItem(tabs[i].RenderTitle, ref isTabOpen, ImGuiTabItemFlags.None))
                    {
                        ImGui.EndTabItem();
                    }
                    if (!isTabOpen)
                    {
                        tabs[i].Dispose();
                        tabs.RemoveAt(i);
                        selected = null;
                    }
                    else
                    {
                        selected = tabs[i];
                    }
                }

                ImGui.EndTabBar();
            }
        }
Exemple #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(ImGuiCol.FrameBg, 1);
            var color_active        = ImGuiNative.igGetColorU32(ImGuiCol.FrameBgActive, 1);
            var color_hovered       = ImGuiNative.igGetColorU32(ImGuiCol.FrameBgHovered, 1);
            var text_color          = ImGuiNative.igGetColorU32(ImGuiCol.Text, 1);
            var text_color_disabled = ImGuiNative.igGetColorU32(ImGuiCol.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.CalcTextSize(sztab.Title).X;
                totalWidth += 28 + style.ItemSpacing.X;
            }
            var winSize = new Vector2(windowWidth, lineheight);
            var cflags  = ImGuiWindowFlags.None;

            if (totalWidth > windowWidth)
            {
                cflags = ImGuiWindowFlags.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.CalcTextSize(title).X;
                var size   = new Vector2(textSz, lineheight);
                //Selection and hover
                if (ImGui.InvisibleButton(title, size))
                {
                    selected = tabs[i];
                }
                var  itemRectSize = ImGuiNative.igGetItemRectSize();
                bool hovered      = ImGui.IsItemHovered(ImGuiHoveredFlags.RectOnly);
                if (hovered)
                {
                    // tab reordering
                    if (isMouseDragging)
                    {
                        if (draggingTabIndex == -1)
                        {
                            draggingTabIndex = i;
                            draggingtabSize  = size;
                            Vector2 mp = ImGui.GetIO().MousePos;
                            var     draggingTabCursorPos = ImGui.GetCursorPos();
                            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.GetItemRectMin();
                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.GetItemRectMin() +
                             (Vector2)ImGui.GetItemRectMax()) * 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(new Vector2(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);
        }