From 1690a779f4223ee728eab8915ec5d50b7f22a867 Mon Sep 17 00:00:00 2001 From: shpaass Date: Fri, 20 Sep 2024 12:51:22 +0200 Subject: [PATCH] refactor: unwrap a one-liner The idea was to make this part of code more readable. For this, the following steps were taken: 1. Split booleans into one equality or expression per boolean. 2. Explain with local names what the boolean conditions check. 3. Put on-select actions as a local function too. 4. Align parameters Google-style. QA: tested on a pY project that the corresponding window selects and deselects things as usual. --- .../ModuleCustomizationScreen.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs b/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs index a345e836..efeb8cbd 100644 --- a/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs +++ b/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs @@ -52,8 +52,10 @@ public override void Build(ImGui gui) { template.RecordUndo().name = newName; } } + gui.BuildText("Filter by crafting buildings (Optional):"); using var grid = gui.EnterInlineGrid(2f, 1f); + for (int i = 0; i < template.filterEntities.Count; i++) { var entity = template.filterEntities[i]; grid.Next(); @@ -62,16 +64,25 @@ public override void Build(ImGui gui) { template.RecordUndo().filterEntities.RemoveAt(i); } } + grid.Next(); + if (gui.BuildButton(Icon.Plus, SchemeColor.Primary, SchemeColor.PrimaryAlt, size: 1.5f)) { - // TODO (shpaass/yafc-ce/issues/256): unwrap it into something more readable - SelectSingleObjectPanel.Select(Database.allCrafters.Where(x => x.allowedEffects != AllowedEffects.None && !template.filterEntities.Contains(x)), - "Add module template filter", sel => { - template.RecordUndo().filterEntities.Add(sel); - gui.Rebuild(); - }); + bool canBeAffected(EntityCrafter x) => x.allowedEffects != AllowedEffects.None; + bool isNotSelected(EntityCrafter x) => !template.filterEntities.Contains(x); + bool isSuitable(EntityCrafter x) => canBeAffected(x) && isNotSelected(x); + + void doToSelectedItem(EntityCrafter selectedCrafter) { + template.RecordUndo().filterEntities.Add(selectedCrafter); + gui.Rebuild(); + } + + SelectSingleObjectPanel.Select(Database.allCrafters.Where(isSuitable), + "Add module template filter", + doToSelectedItem); } } + if (modules == null) { if (gui.BuildButton("Enable custom modules")) { modules = new ModuleTemplateBuilder(); @@ -87,7 +98,9 @@ public override void Build(ImGui gui) { else { gui.BuildText("This building doesn't have module slots, but can be affected by beacons"); } + gui.BuildText("Beacon modules:", Font.subheader); + if (modules.beacon == null) { gui.BuildText("Use default parameters"); if (gui.BuildButton("Override beacons as well")) {