Skip to content

Commit

Permalink
Fix 5676ce7, where returning to the Welcome screen and opening a proj…
Browse files Browse the repository at this point in the history
…ect again could break panels in the main window. (#157)

To reproduce this, open the Milestones Editor, NEI Explorer, Preferences
screen, a single-object selection screen (the full list of fuels or
modules is most likely), the Shopping list, the module customization
screen, or the module templates screen. Then select "Return to starting
screen" from the main menu, load or create any project, and try to
interact with any screen you previously opened.

Escape and maybe Enter will work, but mouse interaction will
not work.
  • Loading branch information
shpaass authored Jun 15, 2024
2 parents 270b776 + 19bf23b commit f22fe80
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 41 deletions.
3 changes: 1 addition & 2 deletions Yafc/Windows/MilestonesEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Yafc {
public class MilestonesEditor : PseudoScreen {
private static readonly MilestonesEditor Instance = new MilestonesEditor();
private readonly VirtualScrollList<FactorioObject> milestoneList;

public MilestonesEditor() : base(50) => milestoneList = new VirtualScrollList<FactorioObject>(30f, new Vector2(float.PositiveInfinity, 3f), MilestoneDrawer, MainScreen.Instance.InputSystem);
Expand All @@ -15,7 +14,7 @@ public override void Open() {
milestoneList.data = Project.current.settings.milestones;
}

public static void Show() => _ = MainScreen.Instance.ShowPseudoScreen(Instance);
public static void Show() => _ = MainScreen.Instance.ShowPseudoScreen(new MilestonesEditor());

private void MilestoneDrawer(ImGui gui, FactorioObject element, int index) {
using (gui.EnterRow()) {
Expand Down
19 changes: 14 additions & 5 deletions Yafc/Windows/NeverEnoughItemsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Yafc {
public class NeverEnoughItemsPanel : PseudoScreen, IComparer<NeverEnoughItemsPanel.RecipeEntry> {
private static readonly NeverEnoughItemsPanel Instance = new NeverEnoughItemsPanel();
private static NeverEnoughItemsPanel? Instance;
private Goods current = null!; // null-forgiving: Set by Show.
private Goods? changing;
private float currentFlow;
Expand Down Expand Up @@ -71,9 +71,11 @@ private NeverEnoughItemsPanel() : base(76f) {
/// It is only necessary to call this from screens that could be displayed on top of the NEIE display.
/// </summary>
public static void Refresh() {
var item = Instance.current;
Instance.current = null!; // null-forgiving: We immediately reset this.
Instance.SetItem(item);
if (Instance != null) {
var item = Instance.current;
Instance.current = null!; // null-forgiving: We immediately reset this.
Instance.SetItem(item);
}
}

private void SetItem(Goods current) {
Expand Down Expand Up @@ -384,13 +386,20 @@ public static void Show(Goods goods) {
// easily handle that since the setting updates happen after the milestones screens are closed.
Refresh();

if (Instance.opened) {
if (Instance?.opened ?? false) {
Instance.changing = goods;
return;
}
Instance ??= new();
Instance.SetItem(goods);
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
}

protected override void Close(bool save = true) {
Instance = null;
base.Close(save);
}

int IComparer<RecipeEntry>.Compare(RecipeEntry x, RecipeEntry y) {
if (x.entryStatus != y.entryStatus) {
return y.entryStatus - x.entryStatus;
Expand Down
4 changes: 1 addition & 3 deletions Yafc/Windows/PreferencesScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Yafc {
public class PreferencesScreen : PseudoScreen {
private static readonly PreferencesScreen Instance = new PreferencesScreen();

public override void Build(ImGui gui) {
BuildHeader(gui, "Preferences");
gui.BuildText("Unit of time:", Font.subheader);
Expand Down Expand Up @@ -183,6 +181,6 @@ private void BuildUnitPerTime(ImGui gui, bool fluid, ProjectPreferences preferen
}
}

public static void Show() => _ = MainScreen.Instance.ShowPseudoScreen(Instance);
public static void Show() => _ = MainScreen.Instance.ShowPseudoScreen(new PreferencesScreen());
}
}
5 changes: 2 additions & 3 deletions Yafc/Windows/SelectSingleObjectPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Yafc {
/// Represents a panel that can generate a result by selecting zero or one <see cref="FactorioObject"/>s. (But doesn't have to, if the user selects a close or cancel button.)
/// </summary>
public class SelectSingleObjectPanel : SelectObjectPanel<FactorioObject> {
private static readonly SelectSingleObjectPanel Instance = new SelectSingleObjectPanel();
public SelectSingleObjectPanel() : base() { }

/// <summary>
Expand All @@ -19,7 +18,7 @@ public SelectSingleObjectPanel() : base() { }
/// <param name="selectItem">An action to be called for the selected item when the panel is closed.</param>
/// <param name="ordering">An optional ordering specifying how to sort the displayed items. If <see langword="null"/>, defaults to <see cref="DataUtils.DefaultOrdering"/>.</param>
public static void Select<T>(IEnumerable<T> list, string header, Action<T> selectItem, IComparer<T>? ordering = null) where T : FactorioObject
=> Instance.Select(list, header, selectItem!, ordering, (obj, mappedAction) => mappedAction(obj), false); // null-forgiving: selectItem will not be called with null, because allowNone is false.
=> new SelectSingleObjectPanel().Select(list, header, selectItem!, ordering, (obj, mappedAction) => mappedAction(obj), false); // null-forgiving: selectItem will not be called with null, because allowNone is false.

/// <summary>
/// Opens a <see cref="SelectSingleObjectPanel"/> to allow the user to select one <see cref="FactorioObject"/>, or to clear the current selection by selecting
Expand All @@ -30,7 +29,7 @@ public static void Select<T>(IEnumerable<T> list, string header, Action<T> selec
/// <param name="selectItem">An action to be called for the selected item when the panel is closed. The parameter will be <see langword="null"/> if the "none" or "clear" option is selected.</param>
/// <param name="ordering">An optional ordering specifying how to sort the displayed items. If <see langword="null"/>, defaults to <see cref="DataUtils.DefaultOrdering"/>.</param>
public static void SelectWithNone<T>(IEnumerable<T> list, string header, Action<T?> selectItem, IComparer<T>? ordering = null) where T : FactorioObject
=> Instance.Select(list, header, selectItem, ordering, (obj, mappedAction) => mappedAction(obj), true);
=> new SelectSingleObjectPanel().Select(list, header, selectItem, ordering, (obj, mappedAction) => mappedAction(obj), true);

protected override void NonNullElementDrawer(ImGui gui, FactorioObject element, int index) {
if (gui.BuildFactorioObjectButton(element, 2.5f, MilestoneDisplay.Contained, extendHeader: extendHeader, useScale: true) == Click.Left) {
Expand Down
19 changes: 10 additions & 9 deletions Yafc/Windows/ShoppingListScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Yafc {
public class ShoppingListScreen : PseudoScreen {
private static readonly ShoppingListScreen Instance = new ShoppingListScreen();

private readonly VirtualScrollList<(FactorioObject, float)> list;
private float shoppingCost, totalBuildings, totalModules;
private bool decomposed = false;
Expand All @@ -25,9 +23,8 @@ private void ElementDrawer(ImGui gui, (FactorioObject obj, float count) element,

public static void Show(Dictionary<FactorioObject, int> counts) {
float cost = 0f, buildings = 0f, modules = 0f;
Instance.decomposed = false;
Instance.list.data = counts.Select(x => (x.Key, Value: (float)x.Value)).OrderByDescending(x => x.Value).ToArray();
foreach (var (obj, count) in Instance.list.data) {
var data = counts.Select(x => (x.Key, Value: (float)x.Value)).OrderByDescending(x => x.Value).ToArray();
foreach ((FactorioObject obj, float count) in data) {
if (obj is Entity) {
buildings += count;
}
Expand All @@ -37,10 +34,14 @@ public static void Show(Dictionary<FactorioObject, int> counts) {

cost += obj.Cost() * count;
}
Instance.shoppingCost = cost;
Instance.totalBuildings = buildings;
Instance.totalModules = modules;
_ = MainScreen.Instance.ShowPseudoScreen(Instance);

ShoppingListScreen screen = new() {
shoppingCost = cost,
totalBuildings = buildings,
totalModules = modules,
list = { data = data }
};
_ = MainScreen.Instance.ShowPseudoScreen(screen);
}

public override void Build(ImGui gui) {
Expand Down
9 changes: 3 additions & 6 deletions Yafc/Windows/WizardPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace Yafc {
public class WizardPanel : PseudoScreen {
public static readonly WizardPanel Instance = new WizardPanel();

private readonly List<PageBuilder> pages = [];
private string? header;
private Action? finish;
Expand All @@ -17,10 +15,9 @@ public class WizardPanel : PseudoScreen {
public delegate Action WizardBuilder(List<PageBuilder> pages);

public static void Show(string header, WizardBuilder builder) {
Instance.pages.Clear();
Instance.finish = builder(Instance.pages);
Instance.header = header;
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
WizardPanel panel = new() { header = header };
panel.finish = builder(panel.pages);
_ = MainScreen.Instance.ShowPseudoScreen(panel);
}

public override void Open() {
Expand Down
20 changes: 10 additions & 10 deletions Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@

namespace Yafc {
public class ModuleCustomizationScreen : PseudoScreen {
private static readonly ModuleCustomizationScreen Instance = new ModuleCustomizationScreen();

private RecipeRow? recipe;
private ProjectModuleTemplate? template;
private ModuleTemplate? modules;

public static void Show(RecipeRow recipe) {
Instance.template = null;
Instance.recipe = recipe;
Instance.modules = recipe.modules;
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
ModuleCustomizationScreen screen = new() {
recipe = recipe,
modules = recipe.modules
};
_ = MainScreen.Instance.ShowPseudoScreen(screen);
}

public static void Show(ProjectModuleTemplate template) {
Instance.recipe = null;
Instance.template = template;
Instance.modules = template.template;
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
ModuleCustomizationScreen screen = new() {
template = template,
modules = template.template
};
_ = MainScreen.Instance.ShowPseudoScreen(screen);
}

public override void Build(ImGui gui) {
Expand Down
6 changes: 3 additions & 3 deletions Yafc/Workspace/ProductionTable/ModuleTemplateConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Yafc {
public class ModuleTemplateConfiguration : PseudoScreen {
private static readonly ModuleTemplateConfiguration Instance = new ModuleTemplateConfiguration();
private readonly VirtualScrollList<ProjectModuleTemplate> templateList;
private ProjectModuleTemplate? pageToDelete;
private string newPageName = "";
Expand All @@ -13,8 +12,9 @@ public class ModuleTemplateConfiguration : PseudoScreen {
reorder: (from, to) => Project.current.RecordUndo().sharedModuleTemplates.MoveListElementIndex(from, to));

public static void Show() {
Instance.RefreshList();
_ = MainScreen.Instance.ShowPseudoScreen(Instance);
ModuleTemplateConfiguration screen = new();
screen.RefreshList();
_ = MainScreen.Instance.ShowPseudoScreen(screen);
}

private void RefreshList() {
Expand Down

0 comments on commit f22fe80

Please sign in to comment.