From 83abfba4d5094db9c6c6a5b116cd24c6bfb5e6e3 Mon Sep 17 00:00:00 2001 From: Asriel Camora Date: Wed, 14 Jun 2023 11:56:23 -0700 Subject: [PATCH] Group available actions together --- Craftimizer/ImGuiUtils.cs | 88 ++++++++------------- Craftimizer/Plugin/SimulatorWindow.cs | 41 ++++++---- Craftimizer/Simulator/Actions/BaseAction.cs | 11 ++- 3 files changed, 64 insertions(+), 76 deletions(-) diff --git a/Craftimizer/ImGuiUtils.cs b/Craftimizer/ImGuiUtils.cs index 1a6a7a8..c95f240 100644 --- a/Craftimizer/ImGuiUtils.cs +++ b/Craftimizer/ImGuiUtils.cs @@ -1,67 +1,54 @@ using ImGuiNET; using System; using System.Collections.Generic; -using System.Linq; using System.Numerics; namespace Craftimizer; internal class ImGuiUtils { - static List<(Vector2 Min, Vector2 Max)> GroupPanelLabelStack = new(); + private static readonly Stack<(Vector2 Min, Vector2 Max)> GroupPanelLabelStack = new(); - static void BeginGroupPanel(string name, Vector2 size) + // Adapted from https://github.com/ocornut/imgui/issues/1496#issuecomment-655048353 + public static void BeginGroupPanel(string name, float width = -1) { ImGui.BeginGroup(); - var cursorPos = ImGui.GetCursorScreenPos(); var itemSpacing = ImGui.GetStyle().ItemSpacing; - ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, new Vector2(0.0f, 0.0f)); - ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0.0f, 0.0f)); + ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero); + ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); var frameHeight = ImGui.GetFrameHeight(); - ImGui.BeginGroup(); - var effectiveSize = size; - if (size.X < 0.0f) - effectiveSize.X = ImGui.GetContentRegionAvail().X; - else - effectiveSize.X = size.X; - ImGui.Dummy(new Vector2(effectiveSize.X, 0.0f)); - - ImGui.Dummy(new Vector2(frameHeight* 0.5f, 0.0f)); - ImGui.SameLine(0.0f, 0.0f); ImGui.BeginGroup(); - ImGui.Dummy(new Vector2(frameHeight* 0.5f, 0.0f)); - ImGui.SameLine(0.0f, 0.0f); + ImGui.Dummy(new Vector2(width < 0 ? ImGui.GetContentRegionAvail().X : width, 0)); + ImGui.Dummy(new Vector2(frameHeight * 0.5f, 0)); + ImGui.SameLine(0, 0); + + ImGui.BeginGroup(); + ImGui.Dummy(new Vector2(frameHeight * 0.5f, 0)); + ImGui.SameLine(0, 0); ImGui.TextUnformatted(name); - var labelMin = ImGui.GetItemRectMin(); - var labelMax = ImGui.GetItemRectMax(); - ImGui.SameLine(0.0f, 0.0f); + GroupPanelLabelStack.Push((ImGui.GetItemRectMin(), ImGui.GetItemRectMax())); + ImGui.SameLine(0, 0); ImGui.Dummy(new Vector2(0f, frameHeight + itemSpacing.Y)); + ImGui.BeginGroup(); ImGui.PopStyleVar(2); - //ImGui.GetCurrentWindow()->ContentRegionRect.Max.x -= frameHeight * 0.5f; - //ImGui.GetCurrentWindow()->WorkRect.Max.x -= frameHeight * 0.5f; - //ImGui.GetCurrentWindow()->InnerRect.Max.x -= frameHeight * 0.5f; - //ImGui.GetCurrentWindow()->Size.x -= frameHeight; + ImGui.PushItemWidth(MathF.Max(0, ImGui.CalcItemWidth() - frameHeight)); - var itemWidth = ImGui.CalcItemWidth(); - ImGui.PushItemWidth(MathF.Max(0.0f, itemWidth - frameHeight)); - - GroupPanelLabelStack.Add((labelMin, labelMax)); } - void EndGroupPanel() + public static void EndGroupPanel() { ImGui.PopItemWidth(); var itemSpacing = ImGui.GetStyle().ItemSpacing; - ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, new Vector2(0.0f, 0.0f)); - ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0.0f, 0.0f)); + ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero); + ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); var frameHeight = ImGui.GetFrameHeight(); @@ -69,51 +56,42 @@ internal class ImGuiUtils ImGui.EndGroup(); - ImGui.SameLine(0.0f, 0.0f); - ImGui.Dummy(new Vector2(frameHeight * 0.5f, 0.0f)); - ImGui.Dummy(new Vector2(0f, frameHeight - frameHeight * 0.5f - itemSpacing.Y)); + ImGui.SameLine(0, 0); + ImGui.Dummy(new Vector2(frameHeight * 0.5f, 0)); + ImGui.Dummy(new Vector2(0f, (frameHeight * 0.5f) - itemSpacing.Y)); ImGui.EndGroup(); var itemMin = ImGui.GetItemRectMin(); var itemMax = ImGui.GetItemRectMax(); - var labelRect = GroupPanelLabelStack[^1]; - GroupPanelLabelStack.RemoveAt(GroupPanelLabelStack.Count - 1); + var labelRect = GroupPanelLabelStack.Pop(); var halfFrame = new Vector2(frameHeight * 0.25f, frameHeight) * 0.5f; - (Vector2 Min, Vector2 Max) frameRect = (itemMin + halfFrame, itemMax - new Vector2(halfFrame.X, 0.0f)); + (Vector2 Min, Vector2 Max) frameRect = (itemMin + halfFrame, itemMax - new Vector2(halfFrame.X, 0)); labelRect.Min.X -= itemSpacing.X; labelRect.Max.X += itemSpacing.X; for (var i = 0; i < 4; ++i) { - switch (i) + var (minClip, maxClip) = i switch { - // left half-plane - case 0: ImGui.PushClipRect(new Vector2(float.NegativeInfinity), new Vector2(labelRect.Min.X, float.PositiveInfinity), true); break; - // right half-plane - case 1: ImGui.PushClipRect(new Vector2(labelRect.Max.X, float.NegativeInfinity), new Vector2(float.PositiveInfinity), true); break; - // top - case 2: ImGui.PushClipRect(new Vector2(labelRect.Min.X, float.NegativeInfinity), new Vector2(labelRect.Max.X, labelRect.Min.Y), true); break; - // bottom - case 3: ImGui.PushClipRect(new Vector2(labelRect.Min.X, labelRect.Max.Y), new Vector2(labelRect.Max.X, float.PositiveInfinity), true); break; - } + 0 => (new Vector2(float.NegativeInfinity), new Vector2(labelRect.Min.X, float.PositiveInfinity)), + 1 => (new Vector2(labelRect.Max.X, float.NegativeInfinity), new Vector2(float.PositiveInfinity)), + 2 => (new Vector2(labelRect.Min.X, float.NegativeInfinity), new Vector2(labelRect.Max.X, labelRect.Min.Y)), + 3 => (new Vector2(labelRect.Min.X, labelRect.Max.Y), new Vector2(labelRect.Max.X, float.PositiveInfinity)), + _ => (Vector2.Zero, Vector2.Zero) + }; + ImGui.PushClipRect(minClip, maxClip, true); ImGui.GetWindowDrawList().AddRect( frameRect.Min, frameRect.Max, ImGui.GetColorU32(ImGuiCol.Border), halfFrame.X); - ImGui.PopClipRect(); } ImGui.PopStyleVar(2); - //ImGui.GetCurrentWindow()->ContentRegionRect.Max.x += frameHeight * 0.5f; - //ImGui.GetCurrentWindow()->WorkRect.Max.x += frameHeight * 0.5f; - //ImGui.GetCurrentWindow()->InnerRect.Max.x += frameHeight * 0.5f; - //ImGui.GetCurrentWindow()->Size.x += frameHeight; - - ImGui.Dummy(new Vector2(0.0f, 0.0f)); + ImGui.Dummy(Vector2.Zero); ImGui.EndGroup(); } diff --git a/Craftimizer/Plugin/SimulatorWindow.cs b/Craftimizer/Plugin/SimulatorWindow.cs index 85d93ea..0c3a352 100644 --- a/Craftimizer/Plugin/SimulatorWindow.cs +++ b/Craftimizer/Plugin/SimulatorWindow.cs @@ -33,17 +33,22 @@ public class SimulatorWindow : Window ImGui.TableNextColumn(); ImGui.BeginChild("CraftimizerActions", Vector2.Zero, true, ImGuiWindowFlags.NoDecoration); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); - var i = 0; - foreach(var action in AvailableActions) + foreach(var category in AvailableActions.GroupBy(a=>a.Category)) { - ImGui.BeginDisabled(!action.CanUse); - if (ImGui.ImageButton(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2))) - Simulation.Execute(action); - if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) - ImGui.SetTooltip(action.Tooltip); - ImGui.EndDisabled(); - if (++i % 5 != 0) - ImGui.SameLine(); + var i = 0; + ImGuiUtils.BeginGroupPanel(category.Key.ToString()); + foreach (var action in category) + { + ImGui.BeginDisabled(!action.CanUse); + if (ImGui.ImageButton(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2))) + Simulation.Execute(action); + if (ImGui.IsItemHovered(ImGuiHoveredFlags.AllowWhenDisabled)) + ImGui.SetTooltip(action.Tooltip); + ImGui.EndDisabled(); + if (++i % 5 != 0) + ImGui.SameLine(); + } + ImGuiUtils.EndGroupPanel(); } ImGui.PopStyleVar(); ImGui.EndChild(); @@ -81,14 +86,16 @@ public class SimulatorWindow : Window ImGui.Text($"> {stepsLeft}"); } ImGuiHelpers.ScaledDummy(5); - i = 0; - foreach(var action in Simulation.ActionHistory) { - ImGui.Image(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2f)); - if (ImGui.IsItemHovered()) - ImGui.SetTooltip(action.GetName(ClassJob.Carpenter)); - if (++i % 5 != 0) - ImGui.SameLine(); + var i = 0; + foreach (var action in Simulation.ActionHistory) + { + ImGui.Image(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2f)); + if (ImGui.IsItemHovered()) + ImGui.SetTooltip(action.GetName(ClassJob.Carpenter)); + if (++i % 5 != 0) + ImGui.SameLine(); + } } ImGui.EndChild(); ImGui.EndTable(); diff --git a/Craftimizer/Simulator/Actions/BaseAction.cs b/Craftimizer/Simulator/Actions/BaseAction.cs index 7b9416e..4979f79 100644 --- a/Craftimizer/Simulator/Actions/BaseAction.cs +++ b/Craftimizer/Simulator/Actions/BaseAction.cs @@ -125,10 +125,13 @@ public abstract class BaseAction builder.AppendLine($"CP Cost: {CPCost}"); if (DurabilityCost != 0) builder.AppendLine($"Durability Cost: {DurabilityCost}"); - if (IncreasesProgress) - builder.AppendLine($"+{Simulation.CalculateProgressGain(Efficiency)} Progress"); - if (IncreasesQuality) - builder.AppendLine($"+{Simulation.CalculateQualityGain(Efficiency)} Quality"); + if (Efficiency != 0) + { + if (IncreasesProgress) + builder.AppendLine($"+{Simulation.CalculateProgressGain(Efficiency)} Progress"); + if (IncreasesQuality) + builder.AppendLine($"+{Simulation.CalculateQualityGain(Efficiency)} Quality"); + } if (!IncreasesStepCount) builder.AppendLine($"Does Not Increase Step Count"); if (SuccessRate != 1f)