|
6 | 6 | using YAFC.UI; |
7 | 7 |
|
8 | 8 | namespace YAFC { |
9 | | - public class SelectObjectPanel : PseudoScreen<FactorioObject> { |
10 | | - private static readonly SelectObjectPanel Instance = new SelectObjectPanel(); |
11 | | - private readonly SearchableList<FactorioObject> list; |
12 | | - private string header; |
13 | | - private Rect searchBox; |
14 | | - private bool extendHeader; |
15 | | - public SelectObjectPanel() : base(40f) { |
16 | | - list = new SearchableList<FactorioObject>(30, new Vector2(2.5f, 2.5f), ElementDrawer, ElementFilter); |
17 | | - } |
| 9 | + public abstract class SelectObjectPanel<T> : PseudoScreen<T> { |
| 10 | + protected readonly SearchableList<FactorioObject> list; |
| 11 | + protected string header; |
| 12 | + protected Rect searchBox; |
| 13 | + protected bool extendHeader; |
18 | 14 |
|
19 | | - private bool ElementFilter(FactorioObject data, SearchQuery query) { |
20 | | - return data.Match(query); |
| 15 | + protected SelectObjectPanel() : base(40f) { |
| 16 | + list = new SearchableList<FactorioObject>(30, new Vector2(2.5f, 2.5f), ElementDrawer, ElementFilter); |
21 | 17 | } |
22 | 18 |
|
23 | | - public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, IComparer<T> ordering, bool allowNone) where T : FactorioObject { |
24 | | - _ = MainScreen.Instance.ShowPseudoScreen(Instance); |
25 | | - Instance.extendHeader = typeof(T) == typeof(FactorioObject); |
26 | | - List<T> data = new List<T>(list); |
| 19 | + protected void Select<U>(IEnumerable<U> list, string header, Action<U> select, IComparer<U> ordering, Action<T, Action<FactorioObject>> process, bool allowNone) where U : FactorioObject { |
| 20 | + _ = MainScreen.Instance.ShowPseudoScreen(this); |
| 21 | + extendHeader = typeof(U) == typeof(FactorioObject); |
| 22 | + List<U> data = new List<U>(list); |
27 | 23 | data.Sort(ordering); |
28 | 24 | if (allowNone) { |
29 | 25 | data.Insert(0, null); |
30 | 26 | } |
31 | 27 |
|
32 | | - Instance.list.filter = default; |
33 | | - Instance.list.data = data; |
34 | | - Instance.header = header; |
35 | | - Instance.Rebuild(); |
36 | | - Instance.complete = (selected, x) => { |
37 | | - if (x is T t) { |
38 | | - if (ordering is DataUtils.FavoritesComparer<T> favoritesComparer) { |
39 | | - favoritesComparer.AddToFavorite(t); |
| 28 | + this.list.filter = default; |
| 29 | + this.list.data = data; |
| 30 | + this.header = header; |
| 31 | + Rebuild(); |
| 32 | + complete = (selected, x) => process(x, x => { |
| 33 | + if (x is U u) { |
| 34 | + if (ordering is DataUtils.FavoritesComparer<U> favoritesComparer) { |
| 35 | + favoritesComparer.AddToFavorite(u); |
40 | 36 | } |
41 | 37 |
|
42 | | - select(t); |
| 38 | + select(u); |
43 | 39 | } |
44 | 40 | else if (allowNone && selected) { |
45 | 41 | select(null); |
46 | 42 | } |
47 | | - }; |
| 43 | + }); |
48 | 44 | } |
49 | 45 |
|
50 | | - public static void Select<T>(IEnumerable<T> list, string header, Action<T> select, bool allowNone = false) where T : FactorioObject { |
51 | | - Select(list, header, select, DataUtils.DefaultOrdering, allowNone); |
| 46 | + protected void Select<U>(IEnumerable<U> list, string header, Action<U> select, Action<T, Action<FactorioObject>> process, bool allowNone = false) where U : FactorioObject { |
| 47 | + Select(list, header, select, DataUtils.DefaultOrdering, process, allowNone); |
52 | 48 | } |
53 | 49 |
|
54 | 50 | private void ElementDrawer(ImGui gui, FactorioObject element, int index) { |
55 | 51 | if (element == null) { |
56 | 52 | if (gui.BuildRedButton(Icon.Close)) { |
57 | | - CloseWithResult(null); |
| 53 | + CloseWithResult(default); |
58 | 54 | } |
59 | 55 | } |
60 | 56 | else { |
61 | | - if (gui.BuildFactorioObjectButton(element, display: MilestoneDisplay.Contained, extendHeader: extendHeader)) { |
62 | | - CloseWithResult(element); |
63 | | - } |
| 57 | + NonNullElementDrawer(gui, element, index); |
64 | 58 | } |
65 | 59 | } |
66 | 60 |
|
| 61 | + protected abstract void NonNullElementDrawer(ImGui gui, FactorioObject element, int index); |
| 62 | + |
| 63 | + private bool ElementFilter(FactorioObject data, SearchQuery query) { |
| 64 | + return data.Match(query); |
| 65 | + } |
| 66 | + |
67 | 67 | public override void Build(ImGui gui) { |
68 | 68 | BuildHeader(gui, header); |
69 | 69 | if (gui.BuildSearchBox(list.filter, out var newFilter, "Start typing for search")) { |
|
0 commit comments