casualexpiesource

cu-trainer/Ultra Paradise Linux/app/SearchPage.cs

187 lines · 7.5 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Avalonia;
5using Avalonia.Controls;
6using Avalonia.Input;
7using Avalonia.Layout;
8using UltraParadise.Shared;
9
10namespace UltraParadise.Linux
11{
12 internal sealed partial class MainWindow
13 {
14 private readonly Dictionary<string, string> lastState = new();
15 private readonly List<(string id, Action<string> set)> extraSetters = new();
16 private readonly Dictionary<string, List<Button>> starButtons = new();
17 private readonly List<(string id, Button b)> extraStars = new();
18 private readonly Dictionary<string, string> optTab = new();
19 private readonly StackPanel searchResults = new() { Spacing = 0 };
20 private readonly StackPanel favResults = new() { Spacing = 0 };
21 private readonly TextBox searchBox = new() { Watermark = "Search every cheat and setting...", FontSize = 15, MinHeight = 38, Margin = new Thickness(0, 0, 0, 12) };
22 private List<string> favs;
23 private bool buildingExtra;
24
25 private List<string> Favs()
26 {
27 favs ??= Settings.Get("favs", "").Split(',', StringSplitOptions.RemoveEmptyEntries).ToList();
28 return favs;
29 }
30
31 private void NoteState(string id, string v)
32 {
33 lastState[id] = v;
34 foreach (var (eid, set) in extraSetters) if (eid == id) set(v);
35 }
36
37 private void RegisterSetter(string id, Action<string> set)
38 {
39 if (buildingExtra)
40 {
41 extraSetters.Add((id, set));
42 if (lastState.TryGetValue(id, out string v)) set(v);
43 }
44 else stateSetters[id] = set;
45 }
46
47 private Button StarButton(COpt o)
48 {
49 Button s = new() { Width = 34, MinHeight = 32, Padding = new Thickness(0), HorizontalContentAlignment = HorizontalAlignment.Center, FontSize = 16, Background = Avalonia.Media.Brushes.Transparent, BorderThickness = new Thickness(0), Margin = new Thickness(0, 0, 4, 0) };
50 ToolTip.SetTip(s, "Add to or take out of Favorites");
51 if (buildingExtra) extraStars.Add((o.Id, s));
52 else
53 {
54 if (!starButtons.TryGetValue(o.Id, out List<Button> list)) starButtons[o.Id] = list = new();
55 list.Add(s);
56 }
57 PaintStar(s, Favs().Contains(o.Id));
58 s.Click += (_, _) => ToggleFav(o);
59 return s;
60 }
61
62 private static void PaintStar(Button s, bool on)
63 {
64 s.Content = on ? "★" : "☆";
65 s.Foreground = Ui.B(on ? Ui.Orange : Ui.Dim);
66 }
67
68 private void ToggleFav(COpt o)
69 {
70 List<string> f = Favs();
71 bool on = !f.Contains(o.Id);
72 f.Remove(o.Id);
73 if (on) f.Add(o.Id);
74 Settings.Set("favs", string.Join(",", f));
75 if (starButtons.TryGetValue(o.Id, out List<Button> list)) foreach (Button b in list) PaintStar(b, on);
76 foreach (var (id, b) in extraStars) if (id == o.Id) PaintStar(b, on);
77 Message(on ? o.Name + " is in Favorites now." : o.Name + " was taken out of Favorites.", false);
78 if (page == "Favorites") RebuildFavs();
79 }
80
81 private static bool Searchable(COpt o) => o.Kind is CKind.Toggle or CKind.Action or CKind.Slider;
82
83 private Control ExtraRow(COpt o)
84 {
85 buildingExtra = true;
86 try { return BuildRow(o); }
87 finally { buildingExtra = false; }
88 }
89
90 private void ResetExtras(StackPanel host)
91 {
92 host.Children.Clear();
93 extraSetters.Clear();
94 extraStars.Clear();
95 }
96
97 private void FillGroups(StackPanel host, IEnumerable<COpt> opts)
98 {
99 Grid g = new() { ColumnDefinitions = new ColumnDefinitions("*,*,*") };
100 StackPanel[] cols = new StackPanel[3];
101 int[] load = new int[3];
102 for (int c = 0; c < 3; c++)
103 {
104 cols[c] = new StackPanel { Margin = new Thickness(c == 0 ? 0 : 7, 0, c == 2 ? 0 : 7, 0) };
105 Grid.SetColumn(cols[c], c);
106 g.Children.Add(cols[c]);
107 }
108 foreach (var grp in opts.GroupBy(o => optTab.GetValueOrDefault(o.Id, "Other")))
109 {
110 Border sec = Section(grp.Key.ToUpperInvariant(), out StackPanel body);
111 foreach (COpt o in grp)
112 {
113 Control r = ExtraRow(o);
114 if (r != null) body.Children.Add(r);
115 }
116 int best = 0;
117 for (int c = 1; c < 3; c++) if (load[c] < load[best]) best = c;
118 cols[best].Children.Add(sec);
119 load[best] += grp.Count() + 2;
120 }
121 host.Children.Add(g);
122 }
123
124 private Control SearchPage()
125 {
126 foreach (CTab t in tabs) foreach (CSection s in t.Sections) foreach (COpt o in s.Rows) optTab.TryAdd(o.Id, t.Name);
127 StackPanel sp = new();
128 sp.Children.Add(searchBox);
129 sp.Children.Add(searchResults);
130 searchBox.TextChanged += (_, _) => RebuildSearch();
131 searchBox.KeyDown += (_, e) => { if (e.Key == Key.Escape) { searchBox.Text = ""; e.Handled = true; } };
132 RebuildSearch();
133 return Scroll(sp);
134 }
135
136 private IEnumerable<COpt> AllOpts() => tabs.SelectMany(t => t.Sections).SelectMany(s => s.Rows).Where(Searchable);
137
138 private void RebuildSearch()
139 {
140 ResetExtras(searchResults);
141 string q = (searchBox.Text ?? "").Trim().ToLowerInvariant();
142 if (q.Length == 0)
143 {
144 searchResults.Children.Add(T("Type above to search all " + AllOpts().Count() + " cheats and settings by name or description. Ctrl+F jumps here from any tab, Esc clears it.\n\nClick a result to use it, or click its star to add it to Favorites.", 13, Ui.B(Ui.Dim), false, true));
145 return;
146 }
147 string[] words = q.Split(' ', StringSplitOptions.RemoveEmptyEntries);
148 List<COpt> hits = AllOpts().Where(o =>
149 {
150 string hay = (o.Name + " " + (o.Hint ?? "") + " " + optTab.GetValueOrDefault(o.Id, "")).ToLowerInvariant();
151 return words.All(hay.Contains);
152 }).ToList();
153 if (hits.Count == 0)
154 {
155 searchResults.Children.Add(T("Nothing matches \"" + searchBox.Text.Trim() + "\". Try a shorter word, like \"heal\", \"jump\" or \"loot\".", 13, Ui.B(Ui.Dim), false, true));
156 return;
157 }
158 FillGroups(searchResults, hits);
159 }
160
161 private Control FavsPage()
162 {
163 RebuildFavs();
164 return Scroll(favResults);
165 }
166
167 private void RebuildFavs()
168 {
169 ResetExtras(favResults);
170 Dictionary<string, COpt> all = AllOpts().GroupBy(o => o.Id).ToDictionary(g => g.Key, g => g.First());
171 List<COpt> list = Favs().Where(all.ContainsKey).Select(id => all[id]).ToList();
172 if (list.Count == 0)
173 {
174 favResults.Children.Add(T("Your favorite cheats, all in one place.\n\nClick the star next to any cheat on any tab to pin it here. Click the star again to take it off.", 13, Ui.B(Ui.Dim), false, true));
175 return;
176 }
177 FillGroups(favResults, list);
178 }
179
180 private void OpenSearch()
181 {
182 Show("Search");
183 searchBox.Focus();
184 searchBox.SelectAll();
185 }
186 }
187}