Consolidate progress bar code, add different styles

This commit is contained in:
Asriel Camora
2024-07-27 12:56:57 -07:00
parent a4dd7d7aab
commit 83e7ca8cf1
7 changed files with 127 additions and 59 deletions
+8
View File
@@ -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;
+31 -7
View File
@@ -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]);
}
}
+38
View File
@@ -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);
}
}
+1 -15
View File
@@ -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);
}
+14 -10
View File
@@ -715,14 +715,6 @@ public sealed unsafe class RecipeNote : Window, IDisposable
public Action<IEnumerable<ActionType>>? 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);
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);
+28
View File
@@ -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 _))
+1 -21
View File
@@ -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)