From 30b1a4d383b86dea0b6f3ca90258e2d3f000d753 Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Sat, 16 Jul 2022 00:18:34 +0200 Subject: [PATCH] feat: ImGuiHelpers.Center helpers, toggle switch component --- .../ImGuiComponents.ToggleSwitch.cs | 72 +++++++++++++++++++ Dalamud/Interface/ImGuiHelpers.cs | 30 ++++++++ 2 files changed, 102 insertions(+) create mode 100644 Dalamud/Interface/Components/ImGuiComponents.ToggleSwitch.cs diff --git a/Dalamud/Interface/Components/ImGuiComponents.ToggleSwitch.cs b/Dalamud/Interface/Components/ImGuiComponents.ToggleSwitch.cs new file mode 100644 index 000000000..83c238c06 --- /dev/null +++ b/Dalamud/Interface/Components/ImGuiComponents.ToggleSwitch.cs @@ -0,0 +1,72 @@ +using System.Numerics; + +using ImGuiNET; + +namespace Dalamud.Interface.Components +{ + /// + /// Component for toggle buttons. + /// + public static partial class ImGuiComponents + { + /// + /// Draw a toggle button. + /// + /// The id of the button. + /// The state of the switch. + /// If the button has been interacted with this frame. + public static bool ToggleButton(string id, ref bool v) + { + var colors = ImGui.GetStyle().Colors; + var p = ImGui.GetCursorScreenPos(); + var drawList = ImGui.GetWindowDrawList(); + + var height = ImGui.GetFrameHeight(); + var width = height * 1.55f; + var radius = height * 0.50f; + + // TODO: animate + + var changed = false; + ImGui.InvisibleButton(id, new Vector2(width, height)); + if (ImGui.IsItemClicked()) + { + v = !v; + changed = true; + } + + if (ImGui.IsItemHovered()) + drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), ImGui.GetColorU32(v ? colors[(int)ImGuiCol.ButtonActive] : new Vector4(0.78f, 0.78f, 0.78f, 1.0f)), height * 0.5f); + else + drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), ImGui.GetColorU32(v ? colors[(int)ImGuiCol.Button] : new Vector4(0.55f, 0.55f, 0.55f, 1.0f)), height * 0.50f); + drawList.AddCircleFilled(new Vector2(p.X + radius + ((v ? 1 : 0) * (width - (radius * 2.0f))), p.Y + radius), radius - 1.5f, ImGui.ColorConvertFloat4ToU32(new Vector4(255, 255, 255, 255))); + + return changed; + } + + /// + /// Draw a disabled toggle button. + /// + /// The id of the button. + /// The state of the switch. + public static void DisabledToggleButton(string id, bool v) + { + var colors = ImGui.GetStyle().Colors; + var p = ImGui.GetCursorScreenPos(); + var drawList = ImGui.GetWindowDrawList(); + + var height = ImGui.GetFrameHeight(); + var width = height * 1.55f; + var radius = height * 0.50f; + + // TODO: animate + // TODO: dim colors + ImGui.InvisibleButton(id, new Vector2(width, height)); + + var dimFactor = 0.5f; + + drawList.AddRectFilled(p, new Vector2(p.X + width, p.Y + height), ImGui.GetColorU32(v ? colors[(int)ImGuiCol.Button] * dimFactor : new Vector4(0.55f, 0.55f, 0.55f, 1.0f) * dimFactor), height * 0.50f); + drawList.AddCircleFilled(new Vector2(p.X + radius + ((v ? 1 : 0) * (width - (radius * 2.0f))), p.Y + radius), radius - 1.5f, ImGui.ColorConvertFloat4ToU32(new Vector4(255, 255, 255, 255) * dimFactor)); + } + } +} diff --git a/Dalamud/Interface/ImGuiHelpers.cs b/Dalamud/Interface/ImGuiHelpers.cs index 67c0c687d..8aaf860dc 100644 --- a/Dalamud/Interface/ImGuiHelpers.cs +++ b/Dalamud/Interface/ImGuiHelpers.cs @@ -237,6 +237,36 @@ namespace Dalamud.Interface return (VirtualKey)ImGui_Input_Impl_Direct.ImGuiKeyToVirtualKey(key); } + /// + /// Show centered text. + /// + /// Text to show. + public static void CenteredText(string text) + { + CenterCursorForText(text); + ImGui.TextUnformatted(text); + } + + /// + /// Center the ImGui cursor for a certain text. + /// + /// The text to center for. + public static void CenterCursorForText(string text) + { + var textWidth = ImGui.CalcTextSize(text).X; + CenterCursorFor((int)textWidth); + } + + /// + /// Center the ImGui cursor for an item with a certain width. + /// + /// The width to center for. + public static void CenterCursorFor(int itemWidth) + { + var window = (int)ImGui.GetWindowWidth(); + ImGui.SetCursorPosX((window / 2) - (itemWidth / 2)); + } + /// /// Get data needed for each new frame. ///