Skip to content

Commit

Permalink
refactor: unwrap a one-liner
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shpaass committed Oct 5, 2024
1 parent 656d51a commit 1690a77
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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")) {
Expand Down

0 comments on commit 1690a77

Please sign in to comment.