Use ImRaii in ImGuiUtil
This commit is contained in:
@@ -36,58 +36,60 @@ public class Fonts : ISettingsTab
|
||||
|
||||
if (Mutable.FontsEnabled)
|
||||
{
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_Font_Name, Mutable.GlobalFont))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_Font_Name, Mutable.GlobalFont))
|
||||
{
|
||||
foreach (var font in Ui.Fonts.GlobalFonts)
|
||||
if (combo)
|
||||
{
|
||||
if (ImGui.Selectable(font.Name, Mutable.GlobalFont == font.Name))
|
||||
Mutable.GlobalFont = font.Name;
|
||||
foreach (var font in Ui.Fonts.GlobalFonts)
|
||||
{
|
||||
if (ImGui.Selectable(font.Name, Mutable.GlobalFont == font.Name))
|
||||
Mutable.GlobalFont = font.Name;
|
||||
|
||||
if (ImGui.IsWindowAppearing() && Mutable.GlobalFont == font.Name)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
if (ImGui.IsWindowAppearing() && Mutable.GlobalFont == font.Name)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
}
|
||||
|
||||
ImGui.Separator();
|
||||
|
||||
foreach (var name in GlobalFonts)
|
||||
{
|
||||
if (ImGui.Selectable(name, Mutable.GlobalFont == name))
|
||||
Mutable.GlobalFont = name;
|
||||
|
||||
if (ImGui.IsWindowAppearing() && Mutable.GlobalFont == name)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.Separator();
|
||||
|
||||
foreach (var name in GlobalFonts)
|
||||
{
|
||||
if (ImGui.Selectable(name, Mutable.GlobalFont == name))
|
||||
Mutable.GlobalFont = name;
|
||||
|
||||
if (ImGui.IsWindowAppearing() && Mutable.GlobalFont == name)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
}
|
||||
|
||||
ImGui.EndCombo();
|
||||
}
|
||||
|
||||
ImGuiUtil.HelpText(string.Format(Language.Options_Font_Description, Plugin.PluginName));
|
||||
ImGuiUtil.WarningText(Language.Options_Font_Warning);
|
||||
ImGui.Spacing();
|
||||
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_JapaneseFont_Name, Mutable.JapaneseFont))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_JapaneseFont_Name, Mutable.JapaneseFont))
|
||||
{
|
||||
foreach (var (name, _) in Ui.Fonts.JapaneseFonts)
|
||||
if (combo)
|
||||
{
|
||||
if (ImGui.Selectable(name, Mutable.JapaneseFont == name))
|
||||
Mutable.JapaneseFont = name;
|
||||
foreach (var (name, _) in Ui.Fonts.JapaneseFonts)
|
||||
{
|
||||
if (ImGui.Selectable(name, Mutable.JapaneseFont == name))
|
||||
Mutable.JapaneseFont = name;
|
||||
|
||||
if (ImGui.IsWindowAppearing() && Mutable.JapaneseFont == name)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
if (ImGui.IsWindowAppearing() && Mutable.JapaneseFont == name)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
}
|
||||
|
||||
ImGui.Separator();
|
||||
|
||||
foreach (var family in JpFonts)
|
||||
{
|
||||
if (ImGui.Selectable(family, Mutable.JapaneseFont == family))
|
||||
Mutable.JapaneseFont = family;
|
||||
|
||||
if (ImGui.IsWindowAppearing() && Mutable.JapaneseFont == family)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.Separator();
|
||||
|
||||
foreach (var family in JpFonts)
|
||||
{
|
||||
if (ImGui.Selectable(family, Mutable.JapaneseFont == family))
|
||||
Mutable.JapaneseFont = family;
|
||||
|
||||
if (ImGui.IsWindowAppearing() && Mutable.JapaneseFont == family)
|
||||
ImGui.SetScrollHereY(0.5f);
|
||||
}
|
||||
|
||||
ImGui.EndCombo();
|
||||
}
|
||||
|
||||
ImGuiUtil.HelpText(string.Format(Language.Options_JapaneseFont_Description, Plugin.PluginName));
|
||||
|
||||
@@ -11,42 +11,45 @@ internal sealed class Miscellaneous(Configuration mutable) : ISettingsTab
|
||||
|
||||
public void Draw(bool changed)
|
||||
{
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_Language_Name, Mutable.LanguageOverride.Name()))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_Language_Name, Mutable.LanguageOverride.Name()))
|
||||
{
|
||||
foreach (var language in Enum.GetValues<LanguageOverride>())
|
||||
if (ImGui.Selectable(language.Name()))
|
||||
Mutable.LanguageOverride = language;
|
||||
|
||||
ImGui.EndCombo();
|
||||
if (combo)
|
||||
{
|
||||
foreach (var language in Enum.GetValues<LanguageOverride>())
|
||||
if (ImGui.Selectable(language.Name()))
|
||||
Mutable.LanguageOverride = language;
|
||||
}
|
||||
}
|
||||
|
||||
ImGuiUtil.HelpText(string.Format(Language.Options_Language_Description, Plugin.PluginName));
|
||||
ImGui.Spacing();
|
||||
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_CommandHelpSide_Name, Mutable.CommandHelpSide.Name()))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_CommandHelpSide_Name, Mutable.CommandHelpSide.Name()))
|
||||
{
|
||||
foreach (var side in Enum.GetValues<CommandHelpSide>())
|
||||
if (ImGui.Selectable(side.Name(), Mutable.CommandHelpSide == side))
|
||||
Mutable.CommandHelpSide = side;
|
||||
|
||||
ImGui.EndCombo();
|
||||
if (combo)
|
||||
{
|
||||
foreach (var side in Enum.GetValues<CommandHelpSide>())
|
||||
if (ImGui.Selectable(side.Name(), Mutable.CommandHelpSide == side))
|
||||
Mutable.CommandHelpSide = side;
|
||||
}
|
||||
}
|
||||
|
||||
ImGuiUtil.HelpText(string.Format(Language.Options_CommandHelpSide_Description, Plugin.PluginName));
|
||||
ImGui.Spacing();
|
||||
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_KeybindMode_Name, Mutable.KeybindMode.Name()))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_KeybindMode_Name, Mutable.KeybindMode.Name()))
|
||||
{
|
||||
foreach (var mode in Enum.GetValues<KeybindMode>())
|
||||
if (combo)
|
||||
{
|
||||
if (ImGui.Selectable(mode.Name(), Mutable.KeybindMode == mode))
|
||||
Mutable.KeybindMode = mode;
|
||||
foreach (var mode in Enum.GetValues<KeybindMode>())
|
||||
{
|
||||
if (ImGui.Selectable(mode.Name(), Mutable.KeybindMode == mode))
|
||||
Mutable.KeybindMode = mode;
|
||||
|
||||
if (ImGui.IsItemHovered())
|
||||
ImGui.SetTooltip(mode.Tooltip());
|
||||
if (ImGui.IsItemHovered())
|
||||
ImGui.SetTooltip(mode.Tooltip());
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndCombo();
|
||||
}
|
||||
|
||||
ImGuiUtil.HelpText(string.Format(Language.Options_KeybindMode_Description, Plugin.PluginName));
|
||||
|
||||
@@ -93,31 +93,33 @@ internal sealed class Tabs : ISettingsTab
|
||||
ImGuiUtil.DragFloatVertical(Language.Options_Tabs_Opacity, ref tab.Opacity, 0.25f, 0f, 100f, $"{tab.Opacity:N2}%%", ImGuiSliderFlags.AlwaysClamp);
|
||||
}
|
||||
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_Tabs_UnreadMode, tab.UnreadMode.Name()))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_Tabs_UnreadMode, tab.UnreadMode.Name()))
|
||||
{
|
||||
foreach (var mode in Enum.GetValues<UnreadMode>())
|
||||
if (combo)
|
||||
{
|
||||
if (ImGui.Selectable(mode.Name(), tab.UnreadMode == mode))
|
||||
tab.UnreadMode = mode;
|
||||
foreach (var mode in Enum.GetValues<UnreadMode>())
|
||||
{
|
||||
if (ImGui.Selectable(mode.Name(), tab.UnreadMode == mode))
|
||||
tab.UnreadMode = mode;
|
||||
|
||||
if (mode.Tooltip() is { } tooltip && ImGui.IsItemHovered())
|
||||
ImGui.SetTooltip(tooltip);
|
||||
if (mode.Tooltip() is { } tooltip && ImGui.IsItemHovered())
|
||||
ImGui.SetTooltip(tooltip);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.EndCombo();
|
||||
}
|
||||
|
||||
var input = tab.Channel?.ToChatType().Name() ?? Language.Options_Tabs_NoInputChannel;
|
||||
if (ImGuiUtil.BeginComboVertical(Language.Options_Tabs_InputChannel, input))
|
||||
using (var combo = ImGuiUtil.BeginComboVertical(Language.Options_Tabs_InputChannel, input))
|
||||
{
|
||||
if (ImGui.Selectable(Language.Options_Tabs_NoInputChannel, tab.Channel == null))
|
||||
tab.Channel = null;
|
||||
if (combo)
|
||||
{
|
||||
if (ImGui.Selectable(Language.Options_Tabs_NoInputChannel, tab.Channel == null))
|
||||
tab.Channel = null;
|
||||
|
||||
foreach (var channel in Enum.GetValues<InputChannel>())
|
||||
if (ImGui.Selectable(channel.ToChatType().Name(), tab.Channel == channel))
|
||||
tab.Channel = channel;
|
||||
|
||||
ImGui.EndCombo();
|
||||
foreach (var channel in Enum.GetValues<InputChannel>())
|
||||
if (ImGui.Selectable(channel.ToChatType().Name(), tab.Channel == channel))
|
||||
tab.Channel = channel;
|
||||
}
|
||||
}
|
||||
|
||||
using (var channelNode = ImRaii.TreeNode(Language.Options_Tabs_Channels))
|
||||
|
||||
+30
-35
@@ -12,7 +12,7 @@ namespace ChatTwo.Util;
|
||||
|
||||
internal static class ImGuiUtil
|
||||
{
|
||||
private static Plugin Plugin;
|
||||
private static Plugin Plugin = null!;
|
||||
|
||||
public static void Initialize(Plugin plugin)
|
||||
{
|
||||
@@ -26,22 +26,22 @@ internal static class ImGuiUtil
|
||||
ImGuiMouseButton.Right
|
||||
];
|
||||
|
||||
private static Payload? _hovered;
|
||||
private static Payload? _lastLink;
|
||||
private static readonly List<(Vector2, Vector2)> PayloadBounds = new();
|
||||
private static Payload? Hovered;
|
||||
private static Payload? LastLink;
|
||||
private static readonly List<(Vector2, Vector2)> PayloadBounds = [];
|
||||
|
||||
internal static void PostPayload(Chunk chunk, PayloadHandler? handler)
|
||||
{
|
||||
var payload = chunk.Link;
|
||||
if (payload != null && ImGui.IsItemHovered())
|
||||
{
|
||||
_hovered = payload;
|
||||
Hovered = payload;
|
||||
ImGui.SetMouseCursor(ImGuiMouseCursor.Hand);
|
||||
handler?.Hover(payload);
|
||||
}
|
||||
else if (!ReferenceEquals(_hovered, payload))
|
||||
else if (!ReferenceEquals(Hovered, payload))
|
||||
{
|
||||
_hovered = null;
|
||||
Hovered = null;
|
||||
}
|
||||
|
||||
if (handler == null)
|
||||
@@ -61,12 +61,12 @@ internal static class ImGuiUtil
|
||||
ImGuiNative.igTextUnformatted(text, textEnd);
|
||||
PostPayload(chunk, handler);
|
||||
|
||||
if (!ReferenceEquals(_lastLink, chunk.Link))
|
||||
if (!ReferenceEquals(LastLink, chunk.Link))
|
||||
PayloadBounds.Clear();
|
||||
|
||||
_lastLink = chunk.Link;
|
||||
LastLink = chunk.Link;
|
||||
|
||||
if (_hovered != null && ReferenceEquals(_hovered, chunk.Link))
|
||||
if (Hovered != null && ReferenceEquals(Hovered, chunk.Link))
|
||||
{
|
||||
defaultText.W = 0.25f;
|
||||
var actualCol = ColourUtil.Vector4ToAbgr(defaultText);
|
||||
@@ -78,14 +78,14 @@ internal static class ImGuiUtil
|
||||
PayloadBounds.Clear();
|
||||
}
|
||||
|
||||
if (_hovered == null && chunk.Link != null)
|
||||
if (Hovered == null && chunk.Link != null)
|
||||
PayloadBounds.Add((oldPos, ImGui.GetItemRectSize()));
|
||||
}
|
||||
|
||||
if (csText.Length == 0)
|
||||
return;
|
||||
|
||||
foreach (var part in csText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
|
||||
foreach (var part in csText.Split(["\r\n", "\r", "\n"], StringSplitOptions.None))
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(part);
|
||||
fixed (byte* rawText = bytes)
|
||||
@@ -199,17 +199,11 @@ internal static class ImGuiUtil
|
||||
internal static void HelpText(string text)
|
||||
{
|
||||
var colour = ImGui.GetStyle().Colors[(int) ImGuiCol.TextDisabled];
|
||||
ImGui.PushStyleColor(ImGuiCol.Text, colour);
|
||||
ImGui.PushTextWrapPos();
|
||||
|
||||
try
|
||||
using (ImRaii.PushColor(ImGuiCol.Text, colour))
|
||||
{
|
||||
ImGui.PushTextWrapPos();
|
||||
ImGui.TextUnformatted(text);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ImGui.PopTextWrapPos();
|
||||
ImGui.PopStyleColor();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,22 +211,26 @@ internal static class ImGuiUtil
|
||||
{
|
||||
var style = StyleModel.GetConfiguredStyle() ?? StyleModel.GetFromCurrent();
|
||||
var dalamudOrange = style.BuiltInColors?.DalamudOrange;
|
||||
if (dalamudOrange != null)
|
||||
ImGui.PushStyleColor(ImGuiCol.Text, dalamudOrange.Value);
|
||||
|
||||
if (wrap) ImGui.PushTextWrapPos();
|
||||
ImGui.TextUnformatted(text);
|
||||
if (wrap) ImGui.PopTextWrapPos();
|
||||
var push = dalamudOrange != null;
|
||||
var color = dalamudOrange ?? Vector4.Zero;
|
||||
using (ImRaii.PushColor(ImGuiCol.Text, color, push))
|
||||
{
|
||||
if (wrap)
|
||||
ImGui.PushTextWrapPos();
|
||||
|
||||
if (dalamudOrange != null)
|
||||
ImGui.PopStyleColor();
|
||||
ImGui.TextUnformatted(text);
|
||||
|
||||
if (wrap)
|
||||
ImGui.PopTextWrapPos();
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool BeginComboVertical(string label, string previewValue, ImGuiComboFlags flags = ImGuiComboFlags.None)
|
||||
internal static ImRaii.IEndObject BeginComboVertical(string label, string previewValue, ImGuiComboFlags flags = ImGuiComboFlags.None)
|
||||
{
|
||||
ImGui.TextUnformatted(label);
|
||||
ImGui.SetNextItemWidth(-1);
|
||||
return ImGui.BeginCombo($"##{label}", previewValue, flags);
|
||||
return ImRaii.Combo($"##{label}", previewValue, flags);
|
||||
}
|
||||
|
||||
internal static bool DragFloatVertical(string label, ref float value, float vSpeed = 1.0f, float vMin = float.MinValue, float vMax = float.MaxValue, string? format = null, ImGuiSliderFlags flags = ImGuiSliderFlags.None)
|
||||
@@ -265,13 +263,10 @@ internal static class ImGuiUtil
|
||||
internal static bool CtrlShiftButton(string label, string tooltip = "")
|
||||
{
|
||||
var ctrlShiftHeld = ImGui.GetIO() is { KeyCtrl: true, KeyShift: true };
|
||||
if (!ctrlShiftHeld)
|
||||
ImGui.BeginDisabled();
|
||||
|
||||
var ret = ImGui.Button(label) && ctrlShiftHeld;
|
||||
|
||||
if (!ctrlShiftHeld)
|
||||
ImGui.EndDisabled();
|
||||
bool ret;
|
||||
using (ImRaii.Disabled(!ctrlShiftHeld))
|
||||
ret = ImGui.Button(label) && ctrlShiftHeld;
|
||||
|
||||
if (!string.IsNullOrEmpty(tooltip) && ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled))
|
||||
ImGui.SetTooltip(tooltip);
|
||||
|
||||
Reference in New Issue
Block a user