Update utility functions to use ImU8String

This commit is contained in:
Soreepeong 2025-07-27 00:53:01 +09:00
parent b3e4505237
commit 91ab875dc2
5 changed files with 49 additions and 241 deletions

View file

@ -57,11 +57,11 @@ public static class Events
ImGui.TableNextColumn();
ImGui.TextUnformatted($"{evt->State.StateFlags}");
ImGui.TableNextColumn();
ImGui.TextUnformatted($"{evt->State.UnkFlags1}");
ImGui.TextUnformatted($"{evt->State.ReturnFlags}");
ImGui.TableNextColumn();
ImGuiHelpers.ClickToCopyText($"{(nint)evt->Target:X}", null, new Vector4(0.6f, 0.6f, 0.6f, 1));
ImGuiHelpers.ClickToCopyText($"{(nint)evt->Target:X}", default, new Vector4(0.6f, 0.6f, 0.6f, 1));
ImGui.TableNextColumn();
ImGuiHelpers.ClickToCopyText($"{(nint)evt->Listener:X}", null, new Vector4(0.6f, 0.6f, 0.6f, 1));
ImGuiHelpers.ClickToCopyText($"{(nint)evt->Listener:X}", default, new Vector4(0.6f, 0.6f, 0.6f, 1));
evt = evt->NextEvent;
}
}

View file

@ -28,7 +28,7 @@ internal static class Gui
var grey60 = new Vector4(0.6f, 0.6f, 0.6f, 1);
if (copy)
{
ImGuiHelpers.ClickToCopyText(value, null, grey60);
ImGuiHelpers.ClickToCopyText(value, default, grey60);
}
else
{

View file

@ -136,11 +136,7 @@ public static partial class ImGuiHelpers
/// <param name="name">The name/ID of the window.</param>
/// <param name="position">The position of the window.</param>
/// <param name="condition">When to set the position.</param>
public static void SetWindowPosRelativeMainViewport(string name, Vector2 position, ImGuiCond condition = ImGuiCond.None)
=> ImGui.SetWindowPos(name, position + MainViewport.Pos, condition);
/// <inheritdoc cref="SetWindowPosRelativeMainViewport(string, Vector2, ImGuiCond)"/>
public static void SetWindowPosRelativeMainViewport(ReadOnlySpan<byte> name, Vector2 position, ImGuiCond condition = ImGuiCond.None)
public static void SetWindowPosRelativeMainViewport(ImU8String name, Vector2 position, ImGuiCond condition = ImGuiCond.None)
=> ImGui.SetWindowPos(name, position + MainViewport.Pos, condition);
/// <summary>
@ -166,11 +162,7 @@ public static partial class ImGuiHelpers
/// </summary>
/// <param name="text">Text in the button.</param>
/// <returns><see cref="Vector2"/> with the size of the button.</returns>
public static Vector2 GetButtonSize(string text)
=> ImGui.CalcTextSize(text) + (ImGui.GetStyle().FramePadding * 2);
/// <inheritdoc cref="GetButtonSize(string)"/>
public static Vector2 GetButtonSize(ReadOnlySpan<byte> text)
public static Vector2 GetButtonSize(ImU8String text)
=> ImGui.CalcTextSize(text) + (ImGui.GetStyle().FramePadding * 2);
/// <summary>
@ -179,11 +171,8 @@ public static partial class ImGuiHelpers
/// <param name="text">The text to show.</param>
/// <param name="textCopy">The text to copy when clicked.</param>
/// <param name="color">The color of the text.</param>
public static void ClickToCopyText(string text, string? textCopy = null, Vector4? color = null)
public static void ClickToCopyText(ImU8String text, ImU8String textCopy = default, Vector4? color = null)
{
text ??= string.Empty;
textCopy ??= text;
using (var col = new ImRaii.Color())
{
if (color.HasValue)
@ -191,7 +180,7 @@ public static partial class ImGuiHelpers
col.Push(ImGuiCol.Text, color.Value);
}
ImGui.TextUnformatted(text);
ImGui.TextUnformatted(text.Span);
}
if (ImGui.IsItemHovered())
@ -206,52 +195,17 @@ public static partial class ImGuiHelpers
}
ImGui.SameLine();
ImGui.TextUnformatted(textCopy);
ImGui.TextUnformatted(textCopy.IsNull ? text.Span : textCopy.Span);
}
}
if (ImGui.IsItemClicked())
{
ImGui.SetClipboardText(textCopy);
}
}
/// <inheritdoc cref="ClickToCopyText(string, string?, Vector4?)"/>
public static void ClickToCopyText(ReadOnlySpan<byte> text, ReadOnlySpan<byte> textCopy = default, Vector4? color = null)
{
if (textCopy.IsEmpty)
textCopy = text;
using (var col = new ImRaii.Color())
{
if (color.HasValue)
{
col.Push(ImGuiCol.Text, color.Value);
}
ImGui.TextUnformatted(text);
ImGui.SetClipboardText(textCopy.IsNull ? text.Span : textCopy.Span);
}
if (ImGui.IsItemHovered())
{
ImGui.SetMouseCursor(ImGuiMouseCursor.Hand);
using (ImRaii.Tooltip())
{
using (ImRaii.PushFont(UiBuilder.IconFont))
{
ImGui.TextUnformatted(FontAwesomeIcon.Copy.ToIconString());
}
ImGui.SameLine();
ImGui.TextUnformatted(textCopy);
}
}
if (ImGui.IsItemClicked())
{
ImGui.SetClipboardText(textCopy);
}
text.Dispose();
textCopy.Dispose();
}
/// <summary>Draws a SeString.</summary>
@ -285,32 +239,14 @@ public static partial class ImGuiHelpers
/// Write unformatted text wrapped.
/// </summary>
/// <param name="text">The text to write.</param>
public static void SafeTextWrapped(string text) => ImGui.TextWrapped(text.Replace("%", "%%"));
/// <inheritdoc cref="SafeTextWrapped(string)"/>
public static void SafeTextWrapped(ReadOnlySpan<byte> text)
{
if (text.Contains((byte)'%'))
ImGui.TextWrapped(Encoding.UTF8.GetString(text).Replace("%", "%%"));
else
ImGui.TextWrapped(text);
}
public static void SafeTextWrapped(ImU8String text) => ImGui.TextWrapped(text);
/// <summary>
/// Write unformatted text wrapped.
/// </summary>
/// <param name="color">The color of the text.</param>
/// <param name="text">The text to write.</param>
public static void SafeTextColoredWrapped(Vector4 color, string text)
{
using (ImRaii.PushColor(ImGuiCol.Text, color))
{
SafeTextWrapped(text);
}
}
/// <inheritdoc cref="SafeTextColoredWrapped(Vector4, string)"/>
public static void SafeTextColoredWrapped(Vector4 color, ReadOnlySpan<byte> text)
public static void SafeTextColoredWrapped(Vector4 color, ImU8String text)
{
using (ImRaii.PushColor(ImGuiCol.Text, color))
{
@ -510,16 +446,9 @@ public static partial class ImGuiHelpers
/// Show centered text.
/// </summary>
/// <param name="text">Text to show.</param>
public static void CenteredText(string text)
public static void CenteredText(ImU8String text)
{
CenterCursorForText(text);
ImGui.TextUnformatted(text);
}
/// <inheritdoc cref="CenteredText(string)"/>
public static void CenteredText(ReadOnlySpan<byte> text)
{
CenterCursorForText(text);
CenterCursorForText(text.Span);
ImGui.TextUnformatted(text);
}
@ -527,11 +456,7 @@ public static partial class ImGuiHelpers
/// Center the ImGui cursor for a certain text.
/// </summary>
/// <param name="text">The text to center for.</param>
public static void CenterCursorForText(string text)
=> CenterCursorFor(ImGui.CalcTextSize(text).X);
/// <inheritdoc cref="CenterCursorForText(string)"/>
public static void CenterCursorForText(ReadOnlySpan<byte> text)
public static void CenterCursorForText(ImU8String text)
=> CenterCursorFor(ImGui.CalcTextSize(text).X);
/// <summary>

View file

@ -1,5 +1,4 @@
using System.Numerics;
using System.Text;
using Dalamud.Bindings.ImGui;
@ -11,28 +10,16 @@ public static partial class ImRaii
{
private static int disabledCount = 0;
public static IEndObject Child(string strId)
public static IEndObject Child(ImU8String strId)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId));
public static IEndObject Child(ReadOnlySpan<byte> strId)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId));
public static IEndObject Child(string strId, Vector2 size)
public static IEndObject Child(ImU8String strId, Vector2 size)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId, size));
public static IEndObject Child(ReadOnlySpan<byte> strId, Vector2 size)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId, size));
public static IEndObject Child(string strId, Vector2 size, bool border)
public static IEndObject Child(ImU8String strId, Vector2 size, bool border)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId, size, border));
public static IEndObject Child(ReadOnlySpan<byte> strId, Vector2 size, bool border)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId, size, border));
public static IEndObject Child(string strId, Vector2 size, bool border, ImGuiWindowFlags flags)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId, size, border, flags));
public static IEndObject Child(ReadOnlySpan<byte> strId, Vector2 size, bool border, ImGuiWindowFlags flags)
public static IEndObject Child(ImU8String strId, Vector2 size, bool border, ImGuiWindowFlags flags)
=> new EndUnconditionally(ImGui.EndChild, ImGui.BeginChild(strId, size, border, flags));
public static IEndObject DragDropTarget()
@ -44,88 +31,40 @@ public static partial class ImRaii
public static IEndObject DragDropSource(ImGuiDragDropFlags flags)
=> new EndConditionally(ImGui.EndDragDropSource, ImGui.BeginDragDropSource(flags));
public static IEndObject Popup(string id)
public static IEndObject Popup(ImU8String id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopup(id));
public static IEndObject Popup(ReadOnlySpan<byte> id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopup(id));
public static IEndObject Popup(string id, ImGuiWindowFlags flags)
public static IEndObject Popup(ImU8String id, ImGuiWindowFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopup(id, flags));
public static IEndObject Popup(ReadOnlySpan<byte> id, ImGuiWindowFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopup(id, flags));
public static IEndObject PopupModal(string id)
public static IEndObject PopupModal(ImU8String id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupModal(id));
public static IEndObject PopupModal(ReadOnlySpan<byte> id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupModal(id));
public static IEndObject PopupModal(string id, ref bool open)
public static IEndObject PopupModal(ImU8String id, ref bool open)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupModal(id, ref open));
public static IEndObject PopupModal(ReadOnlySpan<byte> id, ref bool open)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupModal(id, ref open));
public static IEndObject PopupModal(string id, ref bool open, ImGuiWindowFlags flags)
public static IEndObject PopupModal(ImU8String id, ref bool open, ImGuiWindowFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupModal(id, ref open, flags));
public static IEndObject PopupModal(ReadOnlySpan<byte> id, ref bool open, ImGuiWindowFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupModal(id, ref open, flags));
public static IEndObject ContextPopup(string id)
public static IEndObject ContextPopup(ImU8String id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextWindow(id));
public static IEndObject ContextPopup(ReadOnlySpan<byte> id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextWindow(id));
public static IEndObject ContextPopup(string id, ImGuiPopupFlags flags)
public static IEndObject ContextPopup(ImU8String id, ImGuiPopupFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextWindow(id, flags));
public static IEndObject ContextPopup(ReadOnlySpan<byte> id, ImGuiPopupFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextWindow(id, flags));
public static IEndObject ContextPopupItem(string id)
public static IEndObject ContextPopupItem(ImU8String id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextItem(id));
public static IEndObject ContextPopupItem(ReadOnlySpan<byte> id)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextItem(id));
public static IEndObject ContextPopupItem(string id, ImGuiPopupFlags flags)
public static IEndObject ContextPopupItem(ImU8String id, ImGuiPopupFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextItem(id, flags));
public static IEndObject ContextPopupItem(ReadOnlySpan<byte> id, ImGuiPopupFlags flags)
=> new EndConditionally(ImGui.EndPopup, ImGui.BeginPopupContextItem(id, flags));
public static IEndObject Combo(string label, string previewValue)
public static IEndObject Combo(ImU8String label, ImU8String previewValue)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue));
public static IEndObject Combo(ReadOnlySpan<byte> label, string previewValue)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue));
public static IEndObject Combo(string label, ReadOnlySpan<byte> previewValue)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue));
public static IEndObject Combo(ReadOnlySpan<byte> label, ReadOnlySpan<byte> previewValue)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue));
public static IEndObject Combo(string label, string previewValue, ImGuiComboFlags flags)
public static IEndObject Combo(ImU8String label, ImU8String previewValue, ImGuiComboFlags flags)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue, flags));
public static IEndObject Combo(ReadOnlySpan<byte> label, string previewValue, ImGuiComboFlags flags)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue, flags));
public static IEndObject Combo(string label, ReadOnlySpan<byte> previewValue, ImGuiComboFlags flags)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue, flags));
public static IEndObject Combo(ReadOnlySpan<byte> label, ReadOnlySpan<byte> previewValue, ImGuiComboFlags flags)
=> new EndConditionally(ImGui.EndCombo, ImGui.BeginCombo(label, previewValue, flags));
public static IEndObject Menu(string label)
=> new EndConditionally(ImGui.EndMenu, ImGui.BeginMenu(label));
public static IEndObject Menu(ReadOnlySpan<byte> label)
public static IEndObject Menu(ImU8String label)
=> new EndConditionally(ImGui.EndMenu, ImGui.BeginMenu(label));
public static IEndObject MenuBar()
@ -170,91 +109,49 @@ public static partial class ImRaii
return new EndUnconditionally(ImGui.PopTextWrapPos, true);
}
public static IEndObject ListBox(string label)
public static IEndObject ListBox(ImU8String label)
=> new EndConditionally(ImGui.EndListBox, ImGui.BeginListBox(label));
public static IEndObject ListBox(ReadOnlySpan<byte> label)
=> new EndConditionally(ImGui.EndListBox, ImGui.BeginListBox(label));
public static IEndObject ListBox(string label, Vector2 size)
public static IEndObject ListBox(ImU8String label, Vector2 size)
=> new EndConditionally(ImGui.EndListBox, ImGui.BeginListBox(label, size));
public static IEndObject ListBox(ReadOnlySpan<byte> label, Vector2 size)
=> new EndConditionally(ImGui.EndListBox, ImGui.BeginListBox(label, size));
public static IEndObject Table(string table, int numColumns)
public static IEndObject Table(ImU8String table, int numColumns)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns));
public static IEndObject Table(ReadOnlySpan<byte> table, int numColumns)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns));
public static IEndObject Table(string table, int numColumns, ImGuiTableFlags flags)
public static IEndObject Table(ImU8String table, int numColumns, ImGuiTableFlags flags)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns, flags));
public static IEndObject Table(ReadOnlySpan<byte> table, int numColumns, ImGuiTableFlags flags)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns, flags));
public static IEndObject Table(string table, int numColumns, ImGuiTableFlags flags, Vector2 outerSize)
public static IEndObject Table(ImU8String table, int numColumns, ImGuiTableFlags flags, Vector2 outerSize)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns, flags, outerSize));
public static IEndObject Table(ReadOnlySpan<byte> table, int numColumns, ImGuiTableFlags flags, Vector2 outerSize)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns, flags, outerSize));
public static IEndObject Table(string table, int numColumns, ImGuiTableFlags flags, Vector2 outerSize, float innerWidth)
public static IEndObject Table(ImU8String table, int numColumns, ImGuiTableFlags flags, Vector2 outerSize, float innerWidth)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns, flags, outerSize, innerWidth));
public static IEndObject Table(ReadOnlySpan<byte> table, int numColumns, ImGuiTableFlags flags, Vector2 outerSize, float innerWidth)
=> new EndConditionally(ImGui.EndTable, ImGui.BeginTable(table, numColumns, flags, outerSize, innerWidth));
public static IEndObject TabBar(string label)
public static IEndObject TabBar(ImU8String label)
=> new EndConditionally(ImGui.EndTabBar, ImGui.BeginTabBar(label));
public static IEndObject TabBar(ReadOnlySpan<byte> label)
=> new EndConditionally(ImGui.EndTabBar, ImGui.BeginTabBar(label));
public static IEndObject TabBar(string label, ImGuiTabBarFlags flags)
public static IEndObject TabBar(ImU8String label, ImGuiTabBarFlags flags)
=> new EndConditionally(ImGui.EndTabBar, ImGui.BeginTabBar(label, flags));
public static IEndObject TabBar(ReadOnlySpan<byte> label, ImGuiTabBarFlags flags)
=> new EndConditionally(ImGui.EndTabBar, ImGui.BeginTabBar(label, flags));
public static IEndObject TabItem(string label)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label));
public static IEndObject TabItem(ReadOnlySpan<byte> label)
public static IEndObject TabItem(ImU8String label)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label));
public static unsafe IEndObject TabItem(byte* label, ImGuiTabItemFlags flags)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, flags));
public static unsafe IEndObject TabItem(string label, ImGuiTabItemFlags flags)
public static unsafe IEndObject TabItem(ImU8String label, ImGuiTabItemFlags flags)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, flags));
public static unsafe IEndObject TabItem(ReadOnlySpan<byte> label, ImGuiTabItemFlags flags)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, flags));
public static IEndObject TabItem(string label, ref bool open)
public static IEndObject TabItem(ImU8String label, ref bool open)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, ref open));
public static IEndObject TabItem(ReadOnlySpan<byte> label, ref bool open)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, ref open));
public static IEndObject TabItem(string label, ref bool open, ImGuiTabItemFlags flags)
public static IEndObject TabItem(ImU8String label, ref bool open, ImGuiTabItemFlags flags)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, ref open, flags));
public static IEndObject TabItem(ReadOnlySpan<byte> label, ref bool open, ImGuiTabItemFlags flags)
=> new EndConditionally(ImGui.EndTabItem, ImGui.BeginTabItem(label, ref open, flags));
public static IEndObject TreeNode(string label)
public static IEndObject TreeNode(ImU8String label)
=> new EndConditionally(ImGui.TreePop, ImGui.TreeNodeEx(label));
public static IEndObject TreeNode(ReadOnlySpan<byte> label)
=> new EndConditionally(ImGui.TreePop, ImGui.TreeNodeEx(label));
public static IEndObject TreeNode(string label, ImGuiTreeNodeFlags flags)
=> new EndConditionally(flags.HasFlag(ImGuiTreeNodeFlags.NoTreePushOnOpen) ? Nop : ImGui.TreePop, ImGui.TreeNodeEx(label, flags));
public static IEndObject TreeNode(ReadOnlySpan<byte> label, ImGuiTreeNodeFlags flags)
public static IEndObject TreeNode(ImU8String label, ImGuiTreeNodeFlags flags)
=> new EndConditionally(flags.HasFlag(ImGuiTreeNodeFlags.NoTreePushOnOpen) ? Nop : ImGui.TreePop, ImGui.TreeNodeEx(label, flags));
public static IEndObject Disabled()

View file

@ -6,10 +6,7 @@ namespace Dalamud.Interface.Utility.Raii;
// If condition is false, no id is pushed.
public static partial class ImRaii
{
public static Id PushId(string id, bool enabled = true)
=> enabled ? new Id().Push(id) : new Id();
public static Id PushId(ReadOnlySpan<byte> id, bool enabled = true)
public static Id PushId(ImU8String id, bool enabled = true)
=> enabled ? new Id().Push(id) : new Id();
public static Id PushId(int id, bool enabled = true)
@ -22,18 +19,7 @@ public static partial class ImRaii
{
private int count;
public Id Push(string id, bool condition = true)
{
if (condition)
{
ImGui.PushID(id);
++this.count;
}
return this;
}
public Id Push(ReadOnlySpan<byte> id, bool condition = true)
public Id Push(ImU8String id, bool condition = true)
{
if (condition)
{