public static void DrawTabDrag(List <DockTab> tabs) { var style = ImGui.GetStyle(); var color_hovered = ImGuiNative.igGetColorU32(ColorTarget.FrameBgHovered, 1); var drawList = ImGuiNative.igGetOverlayDrawList(); if (draggingTabIndex >= 0 && draggingTabIndex < tabs.Count) { var mp = ImGui.GetIO().MousePosition; var wp = ImGui.GetWindowPosition(); var start = new Vector2( wp.X + mp.X - draggingTabOffset.X - draggingtabSize.X * 0.5f, wp.Y + mp.Y - draggingTabOffset.Y - draggingtabSize.Y * 0.5f ); var end = new Vector2( start.X + draggingtabSize.X + 8, start.Y + draggingtabSize.Y ); ImGuiNative.ImDrawList_AddRectFilled( drawList, start, end, color_hovered, 0, 0 ); start.X += style.FramePadding.X; start.Y += style.FramePadding.Y; var title = tabs[draggingTabIndex].Title.Split(new string[] { "##" }, StringSplitOptions.None)[0]; var textbytes = System.Text.Encoding.UTF8.GetBytes(title); fixed(byte *ptr = textbytes) { ImGuiNative.ImDrawList_AddText( drawList, start, uint.MaxValue, ptr, (byte *)0 ); } ImGuiNative.igSetMouseCursor(MouseCursorKind.Move); } }
public unsafe void AddText(Vector2 position, string text, uint color) { // Consider using stack allocation if a newer version of Encoding is used (with byte* overloads). int bytes = Encoding.UTF8.GetByteCount(text); byte[] tempBytes = ArrayPool <byte> .Shared.Rent(bytes); Encoding.UTF8.GetBytes(text, 0, text.Length, tempBytes, 0); fixed(byte *bytePtr = &tempBytes[0]) { ImGuiNative.ImDrawList_AddText(_nativeDrawList, position, color, bytePtr, bytePtr + bytes); } ArrayPool <byte> .Shared.Return(tempBytes); }
public void AddText(Vector2 pos, uint col, string text_begin) { int text_begin_byteCount = Encoding.UTF8.GetByteCount(text_begin); byte *native_text_begin = stackalloc byte[text_begin_byteCount + 1]; fixed(char *text_begin_ptr = text_begin) { int native_text_begin_offset = Encoding.UTF8.GetBytes(text_begin_ptr, text_begin.Length, native_text_begin, text_begin_byteCount); native_text_begin[native_text_begin_offset] = 0; } byte *native_text_end = null; ImGuiNative.ImDrawList_AddText(NativePtr, pos, col, native_text_begin, native_text_end); }
public static unsafe void ToastText(string text, Color4 background, Color4 foreground) { var displaySize = (Vector2)(ImGui.GetIO().DisplaySize); var textSize = (Vector2)ImGui.CalcTextSize(text); var drawlist = ImGuiNative.igGetOverlayDrawList(); var textbytes = System.Text.Encoding.UTF8.GetBytes(text); ImGuiNative.ImDrawList_AddRectFilled( drawlist, new Vector2(displaySize.X - textSize.X - 9, 2), new Vector2(displaySize.X, textSize.Y + 9), GetUint(background), 2, ImDrawCornerFlags_All ); fixed(byte *ptr = textbytes) { ImGuiNative.ImDrawList_AddText( drawlist, new Vector2(displaySize.X - textSize.X - 3, 2), GetUint(foreground), ptr, (byte *)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); }