diff --git a/Yafc/Windows/MilestonesEditor.cs b/Yafc/Windows/MilestonesEditor.cs index 6483114a..0492d176 100644 --- a/Yafc/Windows/MilestonesEditor.cs +++ b/Yafc/Windows/MilestonesEditor.cs @@ -5,7 +5,6 @@ namespace Yafc { public class MilestonesEditor : PseudoScreen { - private static readonly MilestonesEditor Instance = new MilestonesEditor(); private readonly VirtualScrollList milestoneList; public MilestonesEditor() : base(50) => milestoneList = new VirtualScrollList(30f, new Vector2(float.PositiveInfinity, 3f), MilestoneDrawer, MainScreen.Instance.InputSystem); @@ -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()) { diff --git a/Yafc/Windows/NeverEnoughItemsPanel.cs b/Yafc/Windows/NeverEnoughItemsPanel.cs index 029db59f..18d1320e 100644 --- a/Yafc/Windows/NeverEnoughItemsPanel.cs +++ b/Yafc/Windows/NeverEnoughItemsPanel.cs @@ -5,7 +5,7 @@ namespace Yafc { public class NeverEnoughItemsPanel : PseudoScreen, IComparer { - 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; @@ -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. /// 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) { @@ -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.Compare(RecipeEntry x, RecipeEntry y) { if (x.entryStatus != y.entryStatus) { return y.entryStatus - x.entryStatus; diff --git a/Yafc/Windows/PreferencesScreen.cs b/Yafc/Windows/PreferencesScreen.cs index bc95fee3..69e40efa 100644 --- a/Yafc/Windows/PreferencesScreen.cs +++ b/Yafc/Windows/PreferencesScreen.cs @@ -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); @@ -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()); } } diff --git a/Yafc/Windows/SelectSingleObjectPanel.cs b/Yafc/Windows/SelectSingleObjectPanel.cs index 783f9eea..5727702d 100644 --- a/Yafc/Windows/SelectSingleObjectPanel.cs +++ b/Yafc/Windows/SelectSingleObjectPanel.cs @@ -8,7 +8,6 @@ namespace Yafc { /// Represents a panel that can generate a result by selecting zero or one s. (But doesn't have to, if the user selects a close or cancel button.) /// public class SelectSingleObjectPanel : SelectObjectPanel { - private static readonly SelectSingleObjectPanel Instance = new SelectSingleObjectPanel(); public SelectSingleObjectPanel() : base() { } /// @@ -19,7 +18,7 @@ public SelectSingleObjectPanel() : base() { } /// An action to be called for the selected item when the panel is closed. /// An optional ordering specifying how to sort the displayed items. If , defaults to . public static void Select(IEnumerable list, string header, Action selectItem, IComparer? 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. /// /// Opens a to allow the user to select one , or to clear the current selection by selecting @@ -30,7 +29,7 @@ public static void Select(IEnumerable list, string header, Action selec /// An action to be called for the selected item when the panel is closed. The parameter will be if the "none" or "clear" option is selected. /// An optional ordering specifying how to sort the displayed items. If , defaults to . public static void SelectWithNone(IEnumerable list, string header, Action selectItem, IComparer? 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) { diff --git a/Yafc/Windows/ShoppingListScreen.cs b/Yafc/Windows/ShoppingListScreen.cs index 8a471d2a..64eea453 100644 --- a/Yafc/Windows/ShoppingListScreen.cs +++ b/Yafc/Windows/ShoppingListScreen.cs @@ -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; @@ -25,9 +23,8 @@ private void ElementDrawer(ImGui gui, (FactorioObject obj, float count) element, public static void Show(Dictionary 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; } @@ -37,10 +34,14 @@ public static void Show(Dictionary 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) { diff --git a/Yafc/Windows/WizardPanel.cs b/Yafc/Windows/WizardPanel.cs index 3a497c22..d5c15eec 100644 --- a/Yafc/Windows/WizardPanel.cs +++ b/Yafc/Windows/WizardPanel.cs @@ -6,8 +6,6 @@ namespace Yafc { public class WizardPanel : PseudoScreen { - public static readonly WizardPanel Instance = new WizardPanel(); - private readonly List pages = []; private string? header; private Action? finish; @@ -17,10 +15,9 @@ public class WizardPanel : PseudoScreen { public delegate Action WizardBuilder(List 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() { diff --git a/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs b/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs index 60bec4da..8a2a260b 100644 --- a/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs +++ b/Yafc/Workspace/ProductionTable/ModuleCustomizationScreen.cs @@ -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) { diff --git a/Yafc/Workspace/ProductionTable/ModuleTemplateConfiguration.cs b/Yafc/Workspace/ProductionTable/ModuleTemplateConfiguration.cs index 2968d8ec..5ceb6c4d 100644 --- a/Yafc/Workspace/ProductionTable/ModuleTemplateConfiguration.cs +++ b/Yafc/Workspace/ProductionTable/ModuleTemplateConfiguration.cs @@ -4,7 +4,6 @@ namespace Yafc { public class ModuleTemplateConfiguration : PseudoScreen { - private static readonly ModuleTemplateConfiguration Instance = new ModuleTemplateConfiguration(); private readonly VirtualScrollList templateList; private ProjectModuleTemplate? pageToDelete; private string newPageName = ""; @@ -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() {