Group available actions together

This commit is contained in:
Asriel Camora
2023-06-14 11:56:23 -07:00
parent 1957baebc6
commit 83abfba4d5
3 changed files with 64 additions and 76 deletions
+33 -55
View File
@@ -1,67 +1,54 @@
using ImGuiNET; using ImGuiNET;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
namespace Craftimizer; namespace Craftimizer;
internal class ImGuiUtils 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(); ImGui.BeginGroup();
var cursorPos = ImGui.GetCursorScreenPos();
var itemSpacing = ImGui.GetStyle().ItemSpacing; var itemSpacing = ImGui.GetStyle().ItemSpacing;
ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, new Vector2(0.0f, 0.0f)); ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero);
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0.0f, 0.0f)); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero);
var frameHeight = ImGui.GetFrameHeight(); 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.BeginGroup();
ImGui.Dummy(new Vector2(frameHeight* 0.5f, 0.0f)); ImGui.Dummy(new Vector2(width < 0 ? ImGui.GetContentRegionAvail().X : width, 0));
ImGui.SameLine(0.0f, 0.0f); 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); ImGui.TextUnformatted(name);
var labelMin = ImGui.GetItemRectMin(); GroupPanelLabelStack.Push((ImGui.GetItemRectMin(), ImGui.GetItemRectMax()));
var labelMax = ImGui.GetItemRectMax(); ImGui.SameLine(0, 0);
ImGui.SameLine(0.0f, 0.0f);
ImGui.Dummy(new Vector2(0f, frameHeight + itemSpacing.Y)); ImGui.Dummy(new Vector2(0f, frameHeight + itemSpacing.Y));
ImGui.BeginGroup(); ImGui.BeginGroup();
ImGui.PopStyleVar(2); ImGui.PopStyleVar(2);
//ImGui.GetCurrentWindow()->ContentRegionRect.Max.x -= frameHeight * 0.5f; ImGui.PushItemWidth(MathF.Max(0, ImGui.CalcItemWidth() - frameHeight));
//ImGui.GetCurrentWindow()->WorkRect.Max.x -= frameHeight * 0.5f;
//ImGui.GetCurrentWindow()->InnerRect.Max.x -= frameHeight * 0.5f;
//ImGui.GetCurrentWindow()->Size.x -= 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(); ImGui.PopItemWidth();
var itemSpacing = ImGui.GetStyle().ItemSpacing; var itemSpacing = ImGui.GetStyle().ItemSpacing;
ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, new Vector2(0.0f, 0.0f)); ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero);
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(0.0f, 0.0f)); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero);
var frameHeight = ImGui.GetFrameHeight(); var frameHeight = ImGui.GetFrameHeight();
@@ -69,51 +56,42 @@ internal class ImGuiUtils
ImGui.EndGroup(); ImGui.EndGroup();
ImGui.SameLine(0.0f, 0.0f); ImGui.SameLine(0, 0);
ImGui.Dummy(new Vector2(frameHeight * 0.5f, 0.0f)); ImGui.Dummy(new Vector2(frameHeight * 0.5f, 0));
ImGui.Dummy(new Vector2(0f, frameHeight - frameHeight * 0.5f - itemSpacing.Y)); ImGui.Dummy(new Vector2(0f, (frameHeight * 0.5f) - itemSpacing.Y));
ImGui.EndGroup(); ImGui.EndGroup();
var itemMin = ImGui.GetItemRectMin(); var itemMin = ImGui.GetItemRectMin();
var itemMax = ImGui.GetItemRectMax(); var itemMax = ImGui.GetItemRectMax();
var labelRect = GroupPanelLabelStack[^1]; var labelRect = GroupPanelLabelStack.Pop();
GroupPanelLabelStack.RemoveAt(GroupPanelLabelStack.Count - 1);
var halfFrame = new Vector2(frameHeight * 0.25f, frameHeight) * 0.5f; 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.Min.X -= itemSpacing.X;
labelRect.Max.X += itemSpacing.X; labelRect.Max.X += itemSpacing.X;
for (var i = 0; i < 4; ++i) for (var i = 0; i < 4; ++i)
{ {
switch (i) var (minClip, maxClip) = i switch
{ {
// left half-plane 0 => (new Vector2(float.NegativeInfinity), new Vector2(labelRect.Min.X, float.PositiveInfinity)),
case 0: ImGui.PushClipRect(new Vector2(float.NegativeInfinity), new Vector2(labelRect.Min.X, float.PositiveInfinity), true); break; 1 => (new Vector2(labelRect.Max.X, float.NegativeInfinity), new Vector2(float.PositiveInfinity)),
// right half-plane 2 => (new Vector2(labelRect.Min.X, float.NegativeInfinity), new Vector2(labelRect.Max.X, labelRect.Min.Y)),
case 1: ImGui.PushClipRect(new Vector2(labelRect.Max.X, float.NegativeInfinity), new Vector2(float.PositiveInfinity), true); break; 3 => (new Vector2(labelRect.Min.X, labelRect.Max.Y), new Vector2(labelRect.Max.X, float.PositiveInfinity)),
// top _ => (Vector2.Zero, Vector2.Zero)
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;
}
ImGui.PushClipRect(minClip, maxClip, true);
ImGui.GetWindowDrawList().AddRect( ImGui.GetWindowDrawList().AddRect(
frameRect.Min, frameRect.Max, frameRect.Min, frameRect.Max,
ImGui.GetColorU32(ImGuiCol.Border), ImGui.GetColorU32(ImGuiCol.Border),
halfFrame.X); halfFrame.X);
ImGui.PopClipRect(); ImGui.PopClipRect();
} }
ImGui.PopStyleVar(2); ImGui.PopStyleVar(2);
//ImGui.GetCurrentWindow()->ContentRegionRect.Max.x += frameHeight * 0.5f; ImGui.Dummy(Vector2.Zero);
//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.EndGroup(); ImGui.EndGroup();
} }
+10 -3
View File
@@ -33,8 +33,11 @@ public class SimulatorWindow : Window
ImGui.TableNextColumn(); ImGui.TableNextColumn();
ImGui.BeginChild("CraftimizerActions", Vector2.Zero, true, ImGuiWindowFlags.NoDecoration); ImGui.BeginChild("CraftimizerActions", Vector2.Zero, true, ImGuiWindowFlags.NoDecoration);
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero); ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, Vector2.Zero);
foreach(var category in AvailableActions.GroupBy(a=>a.Category))
{
var i = 0; var i = 0;
foreach(var action in AvailableActions) ImGuiUtils.BeginGroupPanel(category.Key.ToString());
foreach (var action in category)
{ {
ImGui.BeginDisabled(!action.CanUse); ImGui.BeginDisabled(!action.CanUse);
if (ImGui.ImageButton(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2))) if (ImGui.ImageButton(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2)))
@@ -45,6 +48,8 @@ public class SimulatorWindow : Window
if (++i % 5 != 0) if (++i % 5 != 0)
ImGui.SameLine(); ImGui.SameLine();
} }
ImGuiUtils.EndGroupPanel();
}
ImGui.PopStyleVar(); ImGui.PopStyleVar();
ImGui.EndChild(); ImGui.EndChild();
ImGui.TableNextColumn(); ImGui.TableNextColumn();
@@ -81,8 +86,9 @@ public class SimulatorWindow : Window
ImGui.Text($"> {stepsLeft}"); ImGui.Text($"> {stepsLeft}");
} }
ImGuiHelpers.ScaledDummy(5); ImGuiHelpers.ScaledDummy(5);
i = 0; {
foreach(var action in Simulation.ActionHistory) var i = 0;
foreach (var action in Simulation.ActionHistory)
{ {
ImGui.Image(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2f)); ImGui.Image(action.GetIcon(ClassJob.Carpenter).ImGuiHandle, new Vector2(ImGui.GetFontSize() * 2f));
if (ImGui.IsItemHovered()) if (ImGui.IsItemHovered())
@@ -90,6 +96,7 @@ public class SimulatorWindow : Window
if (++i % 5 != 0) if (++i % 5 != 0)
ImGui.SameLine(); ImGui.SameLine();
} }
}
ImGui.EndChild(); ImGui.EndChild();
ImGui.EndTable(); ImGui.EndTable();
} }
@@ -125,10 +125,13 @@ public abstract class BaseAction
builder.AppendLine($"CP Cost: {CPCost}"); builder.AppendLine($"CP Cost: {CPCost}");
if (DurabilityCost != 0) if (DurabilityCost != 0)
builder.AppendLine($"Durability Cost: {DurabilityCost}"); builder.AppendLine($"Durability Cost: {DurabilityCost}");
if (Efficiency != 0)
{
if (IncreasesProgress) if (IncreasesProgress)
builder.AppendLine($"+{Simulation.CalculateProgressGain(Efficiency)} Progress"); builder.AppendLine($"+{Simulation.CalculateProgressGain(Efficiency)} Progress");
if (IncreasesQuality) if (IncreasesQuality)
builder.AppendLine($"+{Simulation.CalculateQualityGain(Efficiency)} Quality"); builder.AppendLine($"+{Simulation.CalculateQualityGain(Efficiency)} Quality");
}
if (!IncreasesStepCount) if (!IncreasesStepCount)
builder.AppendLine($"Does Not Increase Step Count"); builder.AppendLine($"Does Not Increase Step Count");
if (SuccessRate != 1f) if (SuccessRate != 1f)