From efc7270b4d27d51b815c99bd590fa8c7894a28f0 Mon Sep 17 00:00:00 2001 From: ditzy Date: Wed, 22 Nov 2023 16:40:24 -0600 Subject: [PATCH] save --- ScoutHelper/Utils.cs | 13 ++++++ ScoutHelper/Windows/ImGuiPlus.cs | 70 ++++++++++--------------------- ScoutHelper/Windows/MainWindow.cs | 8 +++- 3 files changed, 41 insertions(+), 50 deletions(-) diff --git a/ScoutHelper/Utils.cs b/ScoutHelper/Utils.cs index 41d15df..5e8786f 100644 --- a/ScoutHelper/Utils.cs +++ b/ScoutHelper/Utils.cs @@ -3,6 +3,7 @@ using System.Collections.Immutable; using System.IO; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; using Dalamud.Plugin.Services; @@ -82,5 +83,17 @@ public static IReadOnlyDictionary VerifyEnumDictionary(this IDiction return enumDict.ToImmutableDictionary(); } + public static IEnumerable ForEach(this IEnumerable source, Action action) => + source.ForEach((_, value) => action.Invoke(value)); + + public static IEnumerable ForEach(this IEnumerable source, Action action) { + var values = source as T[] ?? source.ToArray(); + for (var i = 0; i < values.Length; ++i) { + action.Invoke(i, values[i]); + } + + return values; + } + #endregion } diff --git a/ScoutHelper/Windows/ImGuiPlus.cs b/ScoutHelper/Windows/ImGuiPlus.cs index 885aaf7..9fa6071 100644 --- a/ScoutHelper/Windows/ImGuiPlus.cs +++ b/ScoutHelper/Windows/ImGuiPlus.cs @@ -7,64 +7,36 @@ namespace ScoutHelper.Windows; public static class ImGuiPlus { - private static readonly IDalamudTextureWrap ImCapActive = - Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-cap-active.png")); - private static readonly IDalamudTextureWrap ImCapInactive = - Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-cap-inactive.png")); + public static bool ToggleBar(string strId, ref uint selection, Vector2 size, params string[] labels) { + using var sharpCorners = ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0f); - private static readonly IDalamudTextureWrap ImBodyActive = - Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-body-active.png")); + var selectionChanged = false; - private static readonly IDalamudTextureWrap ImBodyInactive = - Plugin.PluginInterface.UiBuilder.LoadImage(Utils.PluginFilePath(@"Images\toggle-body-inactive.png")); + labels.ForEach((i, label) => { + ToggleBarButton(label, (uint)i, ref selection, buttonSize); + }); - // the cap textures have a transparent border of 4px and total size of 40px x 72px. - // the border is to prevent color bleeding across the edges because of smooth scaling and texture wrapping. - private static readonly Vector2 CapUvNw = new Vector2(4f / 40, 4f / 72); - private static readonly Vector2 CapUvSe = Vector2.One - CapUvNw; - - public static unsafe bool ToggleButton(string strId, ref bool isOn, string offText, string onText, Vector2 size) { - size *= 4; - size.Y *= 3; - var buttonPressed = ImGui.InvisibleButton(strId, size); - var topLeft = ImGui.GetItemRectMin(); - var bottomRight = ImGui.GetItemRectMax(); - if (buttonPressed) isOn = !isOn; - - var capCenter = new Vector2(size.Y / 2); - var bodySize = size with { X = size.X / 2 - capCenter.X }; - var buttonColor = ImGui.GetColorU32(*ImGui.GetStyleColorVec4(ImGuiCol.Button)); - var thicc = 16f; - var padding = thicc / 2; - var focus1 = topLeft + capCenter; - var focus2 = bottomRight - capCenter; - var radius = size.Y / 2 - padding; - var topCenter = topLeft + (capCenter + bodySize) with { Y = padding }; - var bottomCenter = bottomRight - (capCenter + bodySize) with { Y = padding }; - var padCenter = new Vector2(0, padding); - - ImGui.PushClipRect(topLeft, bottomRight, true); - var drawList = ImGui.GetWindowDrawList(); - - drawList.PathClear(); + return selectionChanged; + } - drawList.PathLineTo(topCenter); - drawList.PathArcToFast(isOn ? focus1 : focus2, radius, 9, isOn ? 3 : 15); - drawList.PathLineTo(bottomCenter); - drawList.PathStroke(buttonColor, ImDrawFlags.None, thicc); + private static unsafe bool ToggleBarButton(string label, uint buttonIndex, ref uint selection, Vector2 size) { + var selected = selection == buttonIndex; - drawList.PathClear(); + var baseButtonColor = *ImGui.GetStyleColorVec4(ImGuiCol.Button); + var baseButtonColorActive = *ImGui.GetStyleColorVec4(ImGuiCol.ButtonActive); + var baseButtonColorHovered = *ImGui.GetStyleColorVec4(ImGuiCol.ButtonHovered); - drawList.PathArcToFast(isOn ? focus2 : focus1, radius + padding, isOn ? 15 : 3, 9); - drawList.PathLineTo(topCenter - padCenter); - drawList.PathLineTo(bottomCenter + padCenter); - drawList.PathFillConvex(buttonColor); + var buttonColor = selected ? baseButtonColorActive : baseButtonColor; + var buttonColorHovered = selected ? baseButtonColorActive : baseButtonColorHovered with { W = 0.4f }; + + using var bc1 = ImRaii.PushColor(ImGuiCol.Button, buttonColor); + using var bc2 = ImRaii.PushColor(ImGuiCol.ButtonHovered, buttonColorHovered); - drawList.PathClear(); + var pressed = ImGui.Button(label, size); - ImGui.PopClipRect(); + if (pressed) selection = buttonIndex; - return buttonPressed; + return pressed; } } diff --git a/ScoutHelper/Windows/MainWindow.cs b/ScoutHelper/Windows/MainWindow.cs index 175386b..1c7e9b5 100644 --- a/ScoutHelper/Windows/MainWindow.cs +++ b/ScoutHelper/Windows/MainWindow.cs @@ -64,7 +64,13 @@ public override void Draw() { private unsafe void DrawModeButtons() { ImGuiHelpers.CenteredText("MODE"); - ImGuiPlus.ToggleButton("abc", ref _copyFormattedText, "link", "full text", _buttonSize); + using var sharpCorners = ImRaii.PushStyle(ImGuiStyleVar.FrameRounding, 0f); + var modeButtonSize = _buttonSize with { X = _buttonSize.X / 2 }; + + ImRaii.PushColor(ImGuiCol.Button) + if (ImGui.Button("link", modeButtonSize)) { + + } } private void DrawGeneratorButtons() {