From 83e7ca8cf1c109ae2eb6ac151b1858235fad9dc1 Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Sat, 27 Jul 2024 12:56:57 -0700 Subject: [PATCH] Consolidate progress bar code, add different styles --- Craftimizer/Configuration.cs | 8 +++++++ Craftimizer/Utils/Colors.cs | 38 ++++++++++++++++++++++++------ Craftimizer/Utils/DynamicBars.cs | 38 ++++++++++++++++++++++++++++++ Craftimizer/Windows/MacroEditor.cs | 16 +------------ Craftimizer/Windows/RecipeNote.cs | 36 +++++++++++++++------------- Craftimizer/Windows/Settings.cs | 28 ++++++++++++++++++++++ Craftimizer/Windows/SynthHelper.cs | 22 +---------------- 7 files changed, 127 insertions(+), 59 deletions(-) diff --git a/Craftimizer/Configuration.cs b/Craftimizer/Configuration.cs index d567b67..42ba8d4 100644 --- a/Craftimizer/Configuration.cs +++ b/Craftimizer/Configuration.cs @@ -168,6 +168,13 @@ public class MacroCopyConfiguration [JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)] public partial class Configuration { + public enum ProgressBarType + { + Colorful, + Simple, + None + } + public static event Action? OnMacroListChanged; [JsonInclude] [JsonPropertyName("Macros")] @@ -193,6 +200,7 @@ public partial class Configuration public bool SynthHelperDisplayOnlyFirstStep { get; set; } public bool SynthHelperAbilityAnts { get; set; } public bool CheckDelineations { get; set; } = true; + public ProgressBarType ProgressType { get; set; } = ProgressBarType.Colorful; public bool PinSynthHelperToWindow { get; set; } = true; public bool PinRecipeNoteToWindow { get; set; } = true; diff --git a/Craftimizer/Utils/Colors.cs b/Craftimizer/Utils/Colors.cs index 02e2142..cb8d98d 100644 --- a/Craftimizer/Utils/Colors.cs +++ b/Craftimizer/Utils/Colors.cs @@ -1,5 +1,7 @@ +using Craftimizer.Plugin; using Dalamud.Interface.Colors; using ImGuiNET; +using System; using System.Numerics; namespace Craftimizer.Utils; @@ -15,7 +17,8 @@ public static class Colors private static Vector4 SolverProgressBg => ImGui.ColorConvertU32ToFloat4(ImGui.GetColorU32(ImGuiCol.TableBorderLight)); private static Vector4 SolverProgressFgBland => ImGuiColors.DalamudWhite2; - private static readonly Vector4[] SolverProgressFg = + + private static readonly Vector4[] SolverProgressFgColorful = [ new(0.87f, 0.19f, 0.30f, 1f), new(0.96f, 0.62f, 0.12f, 1f), @@ -26,6 +29,16 @@ public static class Colors new(0.70f, 0.49f, 0.88f, 1f), ]; + private static readonly Vector4[] SolverProgressFgMonochromatic = + [ + new(0.33f, 0.33f, 0.33f, 1f), + new(0.44f, 0.44f, 0.44f, 1f), + new(0.56f, 0.56f, 0.56f, 1f), + new(0.68f, 0.68f, 0.68f, 1f), + new(0.81f, 0.81f, 0.81f, 1f), + new(0.93f, 0.93f, 0.93f, 1f), + ]; + public static readonly Vector4[] CollectabilityThreshold = [ new(0.47f, 0.78f, 0.93f, 1f), // Blue @@ -33,10 +46,21 @@ public static class Colors new(0.75f, 1f, 0.75f, 1f), // Green ]; - public static (Vector4 Background, Vector4 Foreground) GetSolverProgressColors(int? stageValue) => - stageValue is not { } stage ? - (SolverProgressBg, SolverProgressFgBland) : - stage == 0 ? - (SolverProgressBg, SolverProgressFg[0]) : - (SolverProgressFg[(stage - 1) % SolverProgressFg.Length], SolverProgressFg[stage % SolverProgressFg.Length]); + public static (Vector4 Background, Vector4 Foreground) GetSolverProgressColors(int? stageValue) + { + var fg = Service.Configuration.ProgressType switch + { + Configuration.ProgressBarType.Colorful => SolverProgressFgColorful, + Configuration.ProgressBarType.Simple => SolverProgressFgMonochromatic, + _ => throw new InvalidOperationException("No progress bar should be visible") + }; + + if (stageValue is not { } stage) + return (SolverProgressBg, SolverProgressFgBland); + + if (stage == 0) + return (SolverProgressBg, fg[0]); + + return (fg[(stage - 1) % fg.Length], fg[stage % fg.Length]); + } } diff --git a/Craftimizer/Utils/DynamicBars.cs b/Craftimizer/Utils/DynamicBars.cs index 7ed8268..7df26d8 100644 --- a/Craftimizer/Utils/DynamicBars.cs +++ b/Craftimizer/Utils/DynamicBars.cs @@ -170,4 +170,42 @@ internal static class DynamicBars } } } + + public static void DrawProgressBar(Solver.Solver solver, float? availSpace = null) + { + var spacing = ImGui.GetStyle().ItemSpacing.X; + availSpace ??= ImGui.GetContentRegionAvail().X; + + var fraction = (float)solver.ProgressValue / solver.ProgressMax; + if (Service.Configuration.ProgressType == Configuration.ProgressBarType.None) + { + ImGui.AlignTextToFramePadding(); + ImGuiUtils.TextCentered($"{fraction * 100:N0}%", availSpace.Value); + + if (ImGui.IsItemHovered()) + DrawProgressBarTooltip(solver); + return; + } + + var percentWidth = ImGui.CalcTextSize("100%").X; + var progressWidth = availSpace.Value - percentWidth - spacing; + var progressColors = Colors.GetSolverProgressColors(solver.ProgressStage); + + using (ImRaii.PushColor(ImGuiCol.FrameBg, progressColors.Background)) + using (ImRaii.PushColor(ImGuiCol.PlotHistogram, progressColors.Foreground)) + ImGui.ProgressBar(Math.Clamp(fraction, 0, 1), new(progressWidth, ImGui.GetFrameHeight()), string.Empty); + if (ImGui.IsItemHovered()) + DrawProgressBarTooltip(solver); + ImGui.SameLine(0, spacing); + ImGui.AlignTextToFramePadding(); + ImGuiUtils.TextRight($"{fraction * 100:N0}%", percentWidth); + } + + public static void DrawProgressBarTooltip(Solver.Solver solver) + { + var tooltip = $"Solver Progress: {solver.ProgressValue:N0} / {solver.ProgressMax:N0}"; + if (solver.ProgressValue > solver.ProgressMax) + tooltip += $"\n\nThis is taking longer than expected. Check to see if your gear stats are good and the solver settings are adequate."; + ImGuiUtils.TooltipWrapped(tooltip); + } } diff --git a/Craftimizer/Windows/MacroEditor.cs b/Craftimizer/Windows/MacroEditor.cs index 58af39d..a1d5b43 100644 --- a/Craftimizer/Windows/MacroEditor.cs +++ b/Craftimizer/Windows/MacroEditor.cs @@ -1224,21 +1224,7 @@ public sealed class MacroEditor : Window, IDisposable ImGui.GetWindowDrawList().AddLine(pos, pos + new Vector2(availSpace, 0), ImGui.GetColorU32(ImGuiCol.Border)); ImGui.Dummy(default); if (SolverRunning && SolverObject is { } solver) - { - var percentWidth = ImGui.CalcTextSize("100%").X; - var progressWidth = availSpace - percentWidth - spacing; - var fraction = (float)solver.ProgressValue / solver.ProgressMax; - var progressColors = Colors.GetSolverProgressColors(solver.ProgressStage); - - using (ImRaii.PushColor(ImGuiCol.FrameBg, progressColors.Background)) - using (ImRaii.PushColor(ImGuiCol.PlotHistogram, progressColors.Foreground)) - ImGui.ProgressBar(Math.Clamp(fraction, 0, 1), new(progressWidth, ImGui.GetFrameHeight()), string.Empty); - if (ImGui.IsItemHovered()) - RecipeNote.DrawSolverTooltip(solver); - ImGui.SameLine(0, spacing); - ImGui.AlignTextToFramePadding(); - ImGuiUtils.TextRight($"{fraction * 100:N0}%", percentWidth); - } + DynamicBars.DrawProgressBar(solver, availSpace); DrawMacroActions(availSpace); } diff --git a/Craftimizer/Windows/RecipeNote.cs b/Craftimizer/Windows/RecipeNote.cs index 670d945..9908d90 100644 --- a/Craftimizer/Windows/RecipeNote.cs +++ b/Craftimizer/Windows/RecipeNote.cs @@ -715,14 +715,6 @@ public sealed unsafe class RecipeNote : Window, IDisposable public Action>? MacroEditorSetter; } - public static void DrawSolverTooltip(Solver.Solver solver) - { - var tooltip = $"Solver Progress: {solver.ProgressValue:N0} / {solver.ProgressMax:N0}"; - if (solver.ProgressValue > solver.ProgressMax) - tooltip += $"\n\nThis is taking longer than expected. Check to see if your gear stats are good and the solver settings are adequate."; - ImGuiUtils.TooltipWrapped(tooltip); - } - private void DrawMacro(in MacroTaskState state, float panelWidth) { var panelTitle = state.Type switch @@ -794,19 +786,31 @@ public sealed unsafe class RecipeNote : Window, IDisposable var calcTextSize = ImGui.CalcTextSize("Calculating..."); var spacing = ImGui.GetStyle().ItemSpacing.X; var fraction = Math.Clamp((float)solver.ProgressValue / solver.ProgressMax, 0, 1); - var progressColors = Colors.GetSolverProgressColors(solver.ProgressStage); var c = ImGui.GetCursorPos(); ImGuiUtils.AlignCentered(windowHeight + spacing + calcTextSize.X, ImGui.GetContentRegionAvail().X - stepsAvailWidthOffset); - ImGuiUtils.ArcProgress( - fraction, - windowHeight / 2f + 2, - .5f, - ImGui.ColorConvertFloat4ToU32(progressColors.Background), - ImGui.ColorConvertFloat4ToU32(progressColors.Foreground)); + if (Service.Configuration.ProgressType == Configuration.ProgressBarType.None) + { + var textSize = ImGui.CalcTextSize($"{fraction * 100:N0}%"); + var cursor = ImGui.GetCursorPos(); + ImGuiUtils.AlignMiddle(textSize, new(windowHeight)); + ImGui.TextUnformatted($"{fraction * 100:N0}%"); + ImGui.SetCursorPos(cursor); + ImGui.Dummy(new Vector2(windowHeight + 4)); + } + else + { + var progressColors = Colors.GetSolverProgressColors(solver.ProgressStage); + ImGuiUtils.ArcProgress( + fraction, + windowHeight / 2f + 2, + .5f, + ImGui.ColorConvertFloat4ToU32(progressColors.Background), + ImGui.ColorConvertFloat4ToU32(progressColors.Foreground)); + } if (ImGui.IsItemHovered()) - DrawSolverTooltip(solver); + DynamicBars.DrawProgressBarTooltip(solver); ImGui.SameLine(0, spacing); diff --git a/Craftimizer/Windows/Settings.cs b/Craftimizer/Windows/Settings.cs index df3bfb8..0221590 100644 --- a/Craftimizer/Windows/Settings.cs +++ b/Craftimizer/Windows/Settings.cs @@ -178,6 +178,24 @@ public sealed class Settings : Window, IDisposable _ => "Unknown" }; + private static string GetProgressBarTypeName(Configuration.ProgressBarType type) => + type switch + { + Configuration.ProgressBarType.Colorful => "Colorful", + Configuration.ProgressBarType.Simple => "Simple", + Configuration.ProgressBarType.None => "None", + _ => "Unknown", + }; + + private static string GetProgressBarTooltip(Configuration.ProgressBarType type) => + type switch + { + Configuration.ProgressBarType.Colorful => "Colorful, rainbow colors", + Configuration.ProgressBarType.Simple => "Simple, grayscale colors", + Configuration.ProgressBarType.None => "No progress bar; only percent completion is shown", + _ => "Unknown" + }; + public override void Draw() { if (ImGui.BeginTabBar("settingsTabBar")) @@ -244,6 +262,16 @@ public sealed class Settings : Window, IDisposable ref isDirty ); + DrawOption( + "Progress Bar Style", + "The style of progress bar to use when solving for a macro.", + GetProgressBarTypeName, + GetProgressBarTooltip, + Config.ProgressType, + v => Config.ProgressType = v, + ref isDirty + ); + ImGuiHelpers.ScaledDummy(5); using (var panel = ImRaii2.GroupPanel("Copying Settings", -1, out _)) diff --git a/Craftimizer/Windows/SynthHelper.cs b/Craftimizer/Windows/SynthHelper.cs index 3eac878..9ff251d 100644 --- a/Craftimizer/Windows/SynthHelper.cs +++ b/Craftimizer/Windows/SynthHelper.cs @@ -265,7 +265,7 @@ public sealed unsafe class SynthHelper : Window, IDisposable if (SolverRunning && SolverObject is { } solver) { ImGuiHelpers.ScaledDummy(5); - DrawHelperTaskProgress(solver); + DynamicBars.DrawProgressBar(solver); } } @@ -429,26 +429,6 @@ public sealed unsafe class SynthHelper : Window, IDisposable } } - private void DrawHelperTaskProgress(Solver.Solver solver) - { - var spacing = ImGui.GetStyle().ItemSpacing.X; - var availSpace = ImGui.GetContentRegionAvail().X; - - var percentWidth = ImGui.CalcTextSize("100%").X; - var progressWidth = availSpace - percentWidth - spacing; - var fraction = (float)solver.ProgressValue / solver.ProgressMax; - var progressColors = Colors.GetSolverProgressColors(solver.ProgressStage); - - using (ImRaii.PushColor(ImGuiCol.FrameBg, progressColors.Background)) - using (ImRaii.PushColor(ImGuiCol.PlotHistogram, progressColors.Foreground)) - ImGui.ProgressBar(Math.Clamp(fraction, 0, 1), new(progressWidth, ImGui.GetFrameHeight()), string.Empty); - if (ImGui.IsItemHovered()) - RecipeNote.DrawSolverTooltip(solver); - ImGui.SameLine(0, spacing); - ImGui.AlignTextToFramePadding(); - ImGuiUtils.TextRight($"{fraction * 100:N0}%", percentWidth); - } - private void DrawMacroActions() { if (SolverRunning)