Skip to content

Commit

Permalink
Add another button to unfold empty nested tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleStan committed Jul 6, 2022
1 parent 115b645 commit 1c0d336
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 9 additions & 1 deletion YAFC/Workspace/ProductionTable/ProductionTableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,15 @@ public override void BuildMenu(ImGui gui)

if (gui.BuildButton("Remove unused recipes") && gui.CloseDropdown())
{
view.model.RecordUndo().RemoveUnusedRecipes();
view.model.RecordUndo().RemoveUnusedRecipes(false);
view.Rebuild();
}

var padding = ImGuiUtils.DefaultButtonPadding;
padding = new Padding(padding.left + 1, padding.right, padding.top, padding.bottom);
if (gui.BuildButton("... and unpack empty tables", indentLevel: 1) && gui.CloseDropdown())
{
view.model.RecordUndo().RemoveUnusedRecipes(true);
view.Rebuild();
}

Expand Down
6 changes: 4 additions & 2 deletions YAFCmodel/Model/ProductionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,16 @@ private void CalculateFlow(RecipeRow include)
flow = flowArr;
}

public void RemoveUnusedRecipes()
public void RemoveUnusedRecipes(bool unpackToo)
{
for (int i = recipes.Count - 1; i >= 0; i--)
if (recipes[i].subgroup != null)
{
recipes[i].subgroup.RemoveUnusedRecipes();
recipes[i].subgroup.RemoveUnusedRecipes(unpackToo);
if (recipes[i].buildingCount == 0 && recipes[i].subgroup.recipes.Count == 0)
recipes.Remove(recipes[i]);
else if (recipes[i].subgroup.recipes.Count == 0 && unpackToo)
recipes[i].subgroup = null;
else if (recipes[i].buildingCount == 0)
// This table header needs to be deleted, without deleting its children.
if (recipes[i].subgroup.recipes.FirstOrDefault(r => r.subgroup == null || r.subgroup.recipes.Count == 0) is RecipeRow newParent)
Expand Down
12 changes: 8 additions & 4 deletions YAFCui/ImGui/ImGuiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ public static bool OnClick(this ImGui gui, Rect rect)
return false;
}

public static bool BuildButton(this ImGui gui, string text, SchemeColor color = SchemeColor.Primary, Padding? padding = null, bool active = true)
public static bool BuildButton(this ImGui gui, string text, SchemeColor color = SchemeColor.Primary, Padding? padding = null, bool active = true, int indentLevel = 0)
{
if (!active)
color = SchemeColor.Grey;
using (gui.EnterGroup(padding ?? DefaultButtonPadding, active ? color+2 : color+3))
gui.BuildText(text, Font.text, align:RectAlignment.Middle);

return gui.BuildButton(gui.lastRect, color, color + 1) && active;
using (gui.EnterGroup(new Padding(indentLevel * 1.5f, 0, 0, 0)))
{
using (gui.EnterGroup(padding ?? DefaultButtonPadding, active ? color + 2 : color + 3))
gui.BuildText(text, Font.text, align: RectAlignment.Middle);

return gui.BuildButton(gui.lastRect, color, color + 1) && active;
}
}

public static ButtonEvent BuildContextMenuButton(this ImGui gui, string text, string rightText = null, Icon icon = default, bool disabled = false)
Expand Down

0 comments on commit 1c0d336

Please sign in to comment.