Skip to content

Commit

Permalink
refactor: Use BuildRadioGroup for the Shopping List options.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleStan committed Sep 9, 2024
1 parent 68e09ad commit 4e2c968
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
14 changes: 12 additions & 2 deletions Yafc.UI/ImGui/ImGuiUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SDL2;

Expand Down Expand Up @@ -232,10 +233,19 @@ public static ButtonEvent BuildRadioButton(this ImGui gui, string option, bool s
return click;
}

public static bool BuildRadioGroup(this ImGui gui, IReadOnlyList<string> options, int selected, out int newSelected, SchemeColor color = SchemeColor.None) {
public static bool BuildRadioGroup(this ImGui gui, IReadOnlyList<string> options, int selected, out int newSelected,
SchemeColor textColor = SchemeColor.None, bool enabled = true)
=> gui.BuildRadioGroup([.. options.Select(o => (o, (string?)null))], selected, out newSelected, textColor, enabled);

public static bool BuildRadioGroup(this ImGui gui, IReadOnlyList<(string option, string? tooltip)> options, int selected,
out int newSelected, SchemeColor textColor = SchemeColor.None, bool enabled = true) {
newSelected = selected;
for (int i = 0; i < options.Count; i++) {
if (BuildRadioButton(gui, options[i], selected == i, color)) {
ButtonEvent evt = BuildRadioButton(gui, options[i].option, selected == i, textColor, enabled);
if (!string.IsNullOrEmpty(options[i].tooltip)) {
evt.WithTooltip(gui, options[i].tooltip!);
}
if (evt) {
newSelected = i;
}
}
Expand Down
28 changes: 12 additions & 16 deletions Yafc/Windows/ShoppingListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,30 @@ private void RebuildData() {
totalModules = modules;
}

private static readonly (string, string?)[] displayStateOptions = [
("Total buildings", "Display the total number of buildings required, ignoring the built building count."),
("Built buildings", "Display the number of buildings that are reported in built building count."),
("Missing buildings", "Display the number of additional buildings that need to be built.")];
private static readonly (string, string?)[] assumeAdequateOptions = [
("No buildings", "When the built building count is not specified, behave as if it was set to 0."),
("Enough buildings", "When the built building count is not specified, behave as if it matches the required building count.")];

public override void Build(ImGui gui) {
BuildHeader(gui, "Shopping list");
gui.BuildText(
"Total cost of all objects: " + DataUtils.FormatAmount(shoppingCost, UnitOfMeasure.None, "¥") + ", buildings: " +
DataUtils.FormatAmount(totalBuildings, UnitOfMeasure.None) + ", modules: " + DataUtils.FormatAmount(totalModules, UnitOfMeasure.None), TextBlockDisplayStyle.Centered);
using (gui.EnterRow()) {
if (gui.BuildRadioButton("Total buildings", displayState == DisplayState.Total).WithTooltip(gui, "Display the total number of buildings required, ignoring the built building count.")) {
displayState = DisplayState.Total;
RebuildData();
}
if (gui.BuildRadioButton("Built buildings", displayState == DisplayState.Built).WithTooltip(gui, "Display the number of buildings that are reported in built building count.")) {
displayState = DisplayState.Built;
RebuildData();
}
if (gui.BuildRadioButton("Missing buildings", displayState == DisplayState.Missing).WithTooltip(gui, "Display the number of additional buildings that need to be built.")) {
displayState = DisplayState.Missing;
if (gui.BuildRadioGroup(displayStateOptions, (int)displayState, out int newSelected)) {
displayState = (DisplayState)newSelected;
RebuildData();
}
}
using (gui.EnterRow()) {
SchemeColor textColor = displayState == DisplayState.Total ? SchemeColor.PrimaryTextFaint : SchemeColor.PrimaryText;
gui.BuildText("When not specified, assume:", TextBlockDisplayStyle.Default(textColor), topOffset: .15f);
if (gui.BuildRadioButton("No buildings", !assumeAdequate, enabled: displayState != DisplayState.Total).WithTooltip(gui, "When the built building count is not specified, behave as if it was set to 0.")) {
assumeAdequate = false;
RebuildData();
}
if (gui.BuildRadioButton("Enough buildings", assumeAdequate, enabled: displayState != DisplayState.Total).WithTooltip(gui, "When the built building count is not specified, behave as if it matches the required building count.")) {
assumeAdequate = true;
if (gui.BuildRadioGroup(assumeAdequateOptions, assumeAdequate ? 1 : 0, out int newSelected, enabled: displayState != DisplayState.Total)) {
assumeAdequate = newSelected == 1;
RebuildData();
}
}
Expand Down

0 comments on commit 4e2c968

Please sign in to comment.