Skip to content

Commit 4b5085e

Browse files
authored
refactor: Unwrap a one-liner (#301)
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 booleans check. 3. Put on-select actions as a local function too. 4. Align parameters Google-style. I also added blank lines around some code blocks. QA: tested on a pY project that the corresponding window selects and deselects things as usual. This ticket is a part of #256. It doesn't fix it, but it works towards addressing it.
2 parents 656d51a + 1690a77 commit 4b5085e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ public override void Build(ImGui gui) {
5252
template.RecordUndo().name = newName;
5353
}
5454
}
55+
5556
gui.BuildText("Filter by crafting buildings (Optional):");
5657
using var grid = gui.EnterInlineGrid(2f, 1f);
58+
5759
for (int i = 0; i < template.filterEntities.Count; i++) {
5860
var entity = template.filterEntities[i];
5961
grid.Next();
@@ -62,16 +64,25 @@ public override void Build(ImGui gui) {
6264
template.RecordUndo().filterEntities.RemoveAt(i);
6365
}
6466
}
67+
6568
grid.Next();
69+
6670
if (gui.BuildButton(Icon.Plus, SchemeColor.Primary, SchemeColor.PrimaryAlt, size: 1.5f)) {
67-
// TODO (shpaass/yafc-ce/issues/256): unwrap it into something more readable
68-
SelectSingleObjectPanel.Select(Database.allCrafters.Where(x => x.allowedEffects != AllowedEffects.None && !template.filterEntities.Contains(x)),
69-
"Add module template filter", sel => {
70-
template.RecordUndo().filterEntities.Add(sel);
71-
gui.Rebuild();
72-
});
71+
bool canBeAffected(EntityCrafter x) => x.allowedEffects != AllowedEffects.None;
72+
bool isNotSelected(EntityCrafter x) => !template.filterEntities.Contains(x);
73+
bool isSuitable(EntityCrafter x) => canBeAffected(x) && isNotSelected(x);
74+
75+
void doToSelectedItem(EntityCrafter selectedCrafter) {
76+
template.RecordUndo().filterEntities.Add(selectedCrafter);
77+
gui.Rebuild();
78+
}
79+
80+
SelectSingleObjectPanel.Select(Database.allCrafters.Where(isSuitable),
81+
"Add module template filter",
82+
doToSelectedItem);
7383
}
7484
}
85+
7586
if (modules == null) {
7687
if (gui.BuildButton("Enable custom modules")) {
7788
modules = new ModuleTemplateBuilder();
@@ -87,7 +98,9 @@ public override void Build(ImGui gui) {
8798
else {
8899
gui.BuildText("This building doesn't have module slots, but can be affected by beacons");
89100
}
101+
90102
gui.BuildText("Beacon modules:", Font.subheader);
103+
91104
if (modules.beacon == null) {
92105
gui.BuildText("Use default parameters");
93106
if (gui.BuildButton("Override beacons as well")) {

0 commit comments

Comments
 (0)