casualexpiesource

cu-trainer/Ultra Paradise Application/src/MyModsPanel.cs

377 lines · 19.5 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Drawing.Drawing2D;
5using System.Drawing.Text;
6using System.Globalization;
7using System.Windows.Forms;
8
9namespace UltraParadise
10{
11 internal sealed class ModEntry
12 {
13 public string Section, Key, Kind, Value, Desc, Min, Max;
14 public string[] Opts;
15 }
16
17 internal sealed class ModInfo
18 {
19 public string Guid, Name, Version, Stat = "";
20 public bool CanTake, Taken;
21 public readonly List<KeyValuePair<string, string>> Actions = new List<KeyValuePair<string, string>>();
22 public readonly List<ModEntry> Entries = new List<ModEntry>();
23 }
24
25 internal sealed class MyModsPanel : Control
26 {
27 private const char REC = '\u001d', SEP = '\u001c';
28 public readonly List<ModInfo> Mods = new List<ModInfo>();
29 public bool Linked;
30 public bool Loaded;
31 public bool Outdated;
32 public Action<string> Send;
33 public Action<string, bool> TakeChanged;
34 public Action<string, bool> Say;
35 private string selected;
36 private readonly List<KeyValuePair<Rectangle, Action>> hits = new List<KeyValuePair<Rectangle, Action>>();
37 private Rectangle hover = Rectangle.Empty;
38 private int scroll, contentH, top;
39 private readonly Font headFont = Theme.F("Segoe UI Semibold", 8f);
40 private readonly Font textFont = Theme.F("Segoe UI", 9f);
41 private readonly Font smallFont = Theme.F("Segoe UI", 8.5f);
42 private readonly Font boldFont = Theme.F("Segoe UI Semibold", 9.5f);
43 private readonly Font btnFont = Theme.F("Segoe UI Semibold", 8.5f);
44
45 public MyModsPanel()
46 {
47 SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.Selectable, true);
48 BackColor = Theme.Back;
49 }
50
51 private int Pad { get { return Theme.S(18); } }
52 private int HeadH { get { return Theme.S(34); } }
53 private int LeftW { get { return Theme.S(260); } }
54
55 public void Parse(string body)
56 {
57 Mods.Clear();
58 Loaded = true;
59 ModInfo cur = null;
60 foreach (string rec in body.Split(REC))
61 {
62 string[] f = rec.Split(SEP);
63 if (f.Length >= 7 && f[0] == "M")
64 {
65 cur = new ModInfo();
66 cur.Guid = f[1]; cur.Name = f[2]; cur.Version = f[3]; cur.CanTake = f[4] == "1"; cur.Taken = f[5] == "1";
67 foreach (string a in f[6].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
68 {
69 int eq = a.IndexOf('=');
70 if (eq > 0) cur.Actions.Add(new KeyValuePair<string, string>(a.Substring(0, eq), a.Substring(eq + 1)));
71 }
72 Mods.Add(cur);
73 }
74 else if (f.Length >= 10 && f[0] == "E" && cur != null && cur.Guid == f[1])
75 {
76 ModEntry e = new ModEntry();
77 e.Section = f[2]; e.Key = f[3]; e.Kind = f[4]; e.Value = f[5]; e.Desc = f[6]; e.Min = f[7]; e.Max = f[8];
78 e.Opts = f[9].Length > 0 ? f[9].Split('|') : new string[0];
79 cur.Entries.Add(e);
80 }
81 }
82 if (selected == null || Mods.Find(delegate (ModInfo m) { return m.Guid == selected; }) == null) selected = Mods.Count > 0 ? Mods[0].Guid : null;
83 Invalidate();
84 }
85
86 public void Stats(string body)
87 {
88 foreach (string rec in body.Split(REC))
89 {
90 string[] f = rec.Split(SEP);
91 if (f.Length < 2) continue;
92 foreach (ModInfo m in Mods) if (m.Guid == f[0]) m.Stat = f[1];
93 }
94 if (Visible) Invalidate();
95 }
96
97 public void Clear()
98 {
99 Mods.Clear();
100 Loaded = false;
101 Invalidate();
102 }
103
104 private void Head(Graphics g, string title, int x, int y, int w)
105 {
106 StringFormat mid = new StringFormat(); mid.LineAlignment = StringAlignment.Center; mid.FormatFlags = StringFormatFlags.NoWrap;
107 Theme.FillRound(g, Theme.Orange, new Rectangle(x, y + Theme.S(8) + (HeadH - Theme.S(12)) / 2 - Theme.S(6), Theme.S(3), Theme.S(12)), Theme.S(1));
108 string sp = Theme.Spaced(title);
109 using (SolidBrush orange = new SolidBrush(Theme.Orange))
110 g.DrawString(sp, headFont, orange, new RectangleF(x + Theme.S(9), y + Theme.S(8), w, HeadH - Theme.S(12)), mid);
111 int tw = (int)g.MeasureString(sp, headFont).Width + Theme.S(16);
112 int ly = y + Theme.S(8) + (HeadH - Theme.S(12)) / 2;
113 using (LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(x + tw - 1, ly, Math.Max(2, w - tw + 2), 1), Theme.Line, Color.FromArgb(0, Theme.Line), LinearGradientMode.Horizontal))
114 g.FillRectangle(lb, x + tw, ly, Math.Max(1, w - tw), 1);
115 }
116
117 private int Button(Graphics g, int x, int y, string text, bool primary, Action a)
118 {
119 int w = (int)Math.Ceiling(g.MeasureString(text, btnFont).Width) + Theme.S(20);
120 Rectangle r = new Rectangle(x, y, w, Theme.S(26));
121 bool h = r == hover;
122 Color fill = primary ? (h ? Theme.AccentHover : Theme.Orange) : (h ? Theme.Hover : Theme.Panel);
123 using (SolidBrush b = new SolidBrush(fill)) g.FillRectangle(b, r);
124 using (Pen p = new Pen(primary ? Theme.Orange : (h ? Theme.Orange : Theme.Edge))) g.DrawRectangle(p, r);
125 StringFormat c = new StringFormat(); c.Alignment = StringAlignment.Center; c.LineAlignment = StringAlignment.Center;
126 using (SolidBrush t = new SolidBrush(primary ? Theme.OnAccent : Theme.Text)) g.DrawString(text, btnFont, t, r, c);
127 hits.Add(new KeyValuePair<Rectangle, Action>(r, a));
128 return w;
129 }
130
131 private int Para(Graphics g, string s, Font f, Color c, int x, int y, int w)
132 {
133 SizeF sz = g.MeasureString(s, f, w);
134 using (SolidBrush b = new SolidBrush(c)) g.DrawString(s, f, b, new RectangleF(x, y, w, sz.Height + 2));
135 return (int)Math.Ceiling(sz.Height);
136 }
137
138 private void Switch(Graphics g, Rectangle r, bool on)
139 {
140 Theme.FillRound(g, on ? Theme.Orange : Theme.Edge, r, r.Height / 2);
141 int d = r.Height - Theme.S(6);
142 Rectangle k = new Rectangle(on ? r.Right - d - Theme.S(3) : r.X + Theme.S(3), r.Y + Theme.S(3), d, d);
143 Theme.FillRound(g, on ? Theme.OnAccent : Theme.Text, k, d / 2);
144 }
145
146 private ModInfo Selected()
147 {
148 foreach (ModInfo m in Mods) if (m.Guid == selected) return m;
149 return null;
150 }
151
152 protected override void OnPaint(PaintEventArgs e)
153 {
154 Graphics g = e.Graphics;
155 g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
156 g.SmoothingMode = SmoothingMode.AntiAlias;
157 Theme.PaintBack(g, this);
158 hits.Clear();
159
160 int x = Pad, y = Theme.S(6), w = LeftW;
161 Head(g, "MY MODS IN THE GAME", x, y, w);
162 y += HeadH + Theme.S(4);
163 if (!Linked)
164 {
165 Para(g, "Start the game with the trainer attached. Your mods that are installed show up here, and you can change their settings without opening their in-game windows.", textFont, Theme.Dim, x, y, w);
166 return;
167 }
168 if (!Loaded) { Para(g, Outdated ? "The UltraPara.dll in your game folder is older than this trainer, so it can't list your mods. Close the game: the trainer updates it, then start the game again." : "Reading your mods...", textFont, Outdated ? Theme.Bad : Theme.Dim, x, y, w); return; }
169 if (Mods.Count == 0)
170 {
171 Para(g, "None of my mods are loaded in the game. Install them from the Install My Mods tab (they need BepInEx), then restart the game.", textFont, Theme.Dim, x, y, w);
172 return;
173 }
174 foreach (ModInfo m in Mods)
175 {
176 Rectangle r = new Rectangle(x, y, w, Theme.S(48));
177 bool sel = m.Guid == selected, h = r == hover;
178 using (SolidBrush b = new SolidBrush(sel ? Theme.Hover : h ? Theme.Panel : Theme.Back)) g.FillRectangle(b, r);
179 using (Pen p = new Pen(sel ? Theme.Orange : Theme.Line)) g.DrawRectangle(p, r);
180 StringFormat nw = new StringFormat(); nw.Trimming = StringTrimming.EllipsisCharacter; nw.FormatFlags = StringFormatFlags.NoWrap;
181 using (SolidBrush t = new SolidBrush(Theme.Text)) g.DrawString(m.Name, boldFont, t, new RectangleF(r.X + Theme.S(10), r.Y + Theme.S(6), r.Width - Theme.S(20), Theme.S(20)), nw);
182 string sub = "v" + m.Version + (m.Taken ? " · used from the trainer" : m.CanTake ? " · in-game window" : "");
183 using (SolidBrush t = new SolidBrush(m.Taken ? Theme.On : Theme.Dim)) g.DrawString(sub, smallFont, t, new RectangleF(r.X + Theme.S(10), r.Y + Theme.S(26), r.Width - Theme.S(20), Theme.S(18)), nw);
184 string id = m.Guid;
185 hits.Add(new KeyValuePair<Rectangle, Action>(r, delegate { selected = id; scroll = 0; Invalidate(); }));
186 y += Theme.S(54);
187 }
188
189 ModInfo s = Selected();
190 if (s == null) return;
191 int rx = x + w + Theme.S(22), rw = Width - rx - Pad;
192 Head(g, s.Name.ToUpperInvariant(), rx, Theme.S(6), rw);
193 top = Theme.S(6) + HeadH + Theme.S(4);
194 Region clip = g.Clip;
195 g.SetClip(new Rectangle(rx - Theme.S(2), top, rw + Theme.S(10), Height - top));
196 int ry = top - scroll;
197 int start = ry;
198
199 if (s.CanTake)
200 {
201 Rectangle card = new Rectangle(rx, ry, rw, Theme.S(64));
202 using (SolidBrush b = new SolidBrush(Theme.Panel)) g.FillRectangle(b, card);
203 using (Pen p = new Pen(s.Taken ? Theme.Orange : Theme.Line)) g.DrawRectangle(p, card);
204 Rectangle sw = new Rectangle(card.Right - Theme.S(54), card.Y + (card.Height - Theme.S(22)) / 2, Theme.S(42), Theme.S(22));
205 Switch(g, sw, s.Taken);
206 using (SolidBrush t = new SolidBrush(Theme.Text)) g.DrawString("Use it from the trainer", boldFont, t, card.X + Theme.S(12), card.Y + Theme.S(9));
207 Para(g, "Hides " + s.Name + "'s in-game window and turns off its hotkey while the trainer is attached. Use the buttons below instead. Off by default.", smallFont, Theme.Dim, card.X + Theme.S(12), card.Y + Theme.S(29), card.Width - Theme.S(84));
208 string id = s.Guid;
209 bool to = !s.Taken;
210 hits.Add(new KeyValuePair<Rectangle, Action>(card, delegate { if (TakeChanged != null) TakeChanged(id, to); }));
211 ry += card.Height + Theme.S(12);
212 }
213 if (s.Stat.Length > 0) { ry += Para(g, s.Stat, boldFont, Theme.On, rx, ry, rw) + Theme.S(8); }
214 if (s.Actions.Count > 0)
215 {
216 int bx = rx;
217 foreach (KeyValuePair<string, string> a in s.Actions)
218 {
219 string cmd = "MODACT " + s.Guid + " " + a.Key;
220 bx += Button(g, bx, ry, a.Value, bx == rx, delegate { if (Send != null) Send(cmd); }) + Theme.S(8);
221 }
222 ry += Theme.S(40);
223 }
224
225 Head(g, "SETTINGS", rx, ry - Theme.S(6), rw);
226 ry += HeadH - Theme.S(2);
227 if (s.Entries.Count == 0) ry += Para(g, "This mod has no settings.", textFont, Theme.Dim, rx, ry, rw) + Theme.S(8);
228 string lastSection = null;
229 foreach (ModEntry en in s.Entries)
230 {
231 if (en.Section != lastSection)
232 {
233 lastSection = en.Section;
234 using (SolidBrush t = new SolidBrush(Theme.Soft)) g.DrawString(en.Section, btnFont, t, rx, ry);
235 ry += Theme.S(22);
236 }
237 int vw = Theme.S(150);
238 Rectangle val = new Rectangle(rx + rw - vw, ry, vw, Theme.S(26));
239 bool hv = val == hover;
240 ModEntry cap = en;
241 ModInfo owner = s;
242 if (en.Kind == "bool")
243 {
244 bool on = en.Value.Equals("true", StringComparison.OrdinalIgnoreCase);
245 Rectangle sr = new Rectangle(val.Right - Theme.S(42), val.Y + Theme.S(2), Theme.S(42), Theme.S(22));
246 Switch(g, sr, on);
247 hits.Add(new KeyValuePair<Rectangle, Action>(val, delegate { SetValue(owner, cap, on ? "false" : "true"); }));
248 }
249 else
250 {
251 using (SolidBrush b = new SolidBrush(hv ? Theme.Hover : Theme.Panel)) g.FillRectangle(b, val);
252 using (Pen p = new Pen(hv ? Theme.Orange : Theme.Edge)) g.DrawRectangle(p, val);
253 StringFormat c = new StringFormat(); c.LineAlignment = StringAlignment.Center; c.Trimming = StringTrimming.EllipsisCharacter; c.FormatFlags = StringFormatFlags.NoWrap;
254 using (SolidBrush t = new SolidBrush(Theme.Text)) g.DrawString(en.Value.Length == 0 ? "(empty)" : en.Value, textFont, t, new RectangleF(val.X + Theme.S(8), val.Y, val.Width - Theme.S(16), val.Height), c);
255 hits.Add(new KeyValuePair<Rectangle, Action>(val, delegate { Edit(owner, cap); }));
256 }
257 int lw = rw - vw - Theme.S(14);
258 using (SolidBrush t = new SolidBrush(Theme.Text)) g.DrawString(Nice(en.Key), boldFont, t, new RectangleF(rx, ry + Theme.S(3), lw, Theme.S(20)));
259 int dh = en.Desc.Length > 0 ? Para(g, en.Desc, smallFont, Theme.Dim, rx, ry + Theme.S(24), lw) : 0;
260 ry += Math.Max(Theme.S(32), Theme.S(26) + dh) + Theme.S(10);
261 }
262 contentH = ry - start;
263 g.Clip = clip;
264 int view = Height - top;
265 if (contentH > view)
266 {
267 int tH = Math.Max(Theme.S(30), view * view / contentH);
268 int tY = top + (view - tH) * scroll / Math.Max(1, contentH - view);
269 using (SolidBrush sb = new SolidBrush(Theme.Scroll)) g.FillRectangle(sb, Width - Theme.S(6), tY, Theme.S(3), tH);
270 }
271 for (int i = hits.Count - 1; i >= 0; i--)
272 if (hits[i].Key.X >= rx && (hits[i].Key.Bottom <= top || hits[i].Key.Top >= Height)) hits.RemoveAt(i);
273 }
274
275 private static string Nice(string key)
276 {
277 System.Text.StringBuilder sb = new System.Text.StringBuilder();
278 for (int i = 0; i < key.Length; i++)
279 {
280 char c = key[i];
281 if (i > 0 && char.IsUpper(c) && !char.IsUpper(key[i - 1])) sb.Append(' ');
282 sb.Append(c);
283 }
284 return sb.ToString();
285 }
286
287 private void SetValue(ModInfo m, ModEntry e, string v)
288 {
289 e.Value = v;
290 if (Send != null) Send("MODCFG " + m.Guid + " " + e.Section + SEP + e.Key + SEP + v);
291 Invalidate();
292 }
293
294 private void Edit(ModInfo m, ModEntry e)
295 {
296 using (Form f = new Form())
297 {
298 f.Text = m.Name + ": " + Nice(e.Key);
299 f.FormBorderStyle = FormBorderStyle.FixedDialog;
300 f.MaximizeBox = false; f.MinimizeBox = false; f.ShowInTaskbar = false;
301 f.StartPosition = FormStartPosition.CenterParent;
302 f.BackColor = Theme.Panel; f.ForeColor = Theme.Text;
303 f.Font = textFont;
304 f.ClientSize = new Size(Theme.S(380), Theme.S(150));
305 Label l = new Label();
306 l.Text = e.Desc.Length > 0 ? e.Desc : Nice(e.Key);
307 if (e.Min.Length > 0) l.Text += " (" + e.Min + " to " + e.Max + ")";
308 l.SetBounds(Theme.S(12), Theme.S(10), Theme.S(356), Theme.S(54));
309 f.Controls.Add(l);
310 Control input;
311 if (e.Opts.Length > 0)
312 {
313 ComboBox cb = new ComboBox();
314 cb.DropDownStyle = ComboBoxStyle.DropDownList;
315 cb.Items.AddRange(e.Opts);
316 cb.SelectedItem = e.Value;
317 if (cb.SelectedIndex < 0 && cb.Items.Count > 0) cb.SelectedIndex = 0;
318 input = cb;
319 }
320 else
321 {
322 TextBox tb = new TextBox();
323 tb.Text = e.Value;
324 tb.BackColor = Theme.Back; tb.ForeColor = Theme.Text; tb.BorderStyle = BorderStyle.FixedSingle;
325 input = tb;
326 }
327 input.SetBounds(Theme.S(12), Theme.S(70), Theme.S(356), Theme.S(26));
328 f.Controls.Add(input);
329 System.Windows.Forms.Button ok = new System.Windows.Forms.Button();
330 ok.Text = "Save"; ok.DialogResult = DialogResult.OK; ok.FlatStyle = FlatStyle.Flat; ok.BackColor = Theme.Orange; ok.ForeColor = Theme.OnAccent;
331 ok.SetBounds(Theme.S(196), Theme.S(110), Theme.S(82), Theme.S(28));
332 System.Windows.Forms.Button no = new System.Windows.Forms.Button();
333 no.Text = "Cancel"; no.DialogResult = DialogResult.Cancel; no.FlatStyle = FlatStyle.Flat;
334 no.SetBounds(Theme.S(286), Theme.S(110), Theme.S(82), Theme.S(28));
335 f.Controls.Add(ok); f.Controls.Add(no);
336 f.AcceptButton = ok; f.CancelButton = no;
337 if (f.ShowDialog(FindForm()) != DialogResult.OK) return;
338 string v = input.Text.Trim();
339 if (e.Kind == "int" || e.Kind == "float")
340 {
341 double d;
342 if (!double.TryParse(v.Replace(',', '.'), NumberStyles.Float, CultureInfo.InvariantCulture, out d)) { if (Say != null) Say("That isn't a number.", true); return; }
343 double lo, hi;
344 if (e.Min.Length > 0 && double.TryParse(e.Min, NumberStyles.Float, CultureInfo.InvariantCulture, out lo) && double.TryParse(e.Max, NumberStyles.Float, CultureInfo.InvariantCulture, out hi)) d = Math.Max(lo, Math.Min(hi, d));
345 v = e.Kind == "int" ? ((int)Math.Round(d)).ToString(CultureInfo.InvariantCulture) : d.ToString("0.###", CultureInfo.InvariantCulture);
346 }
347 SetValue(m, e, v);
348 }
349 }
350
351 protected override void OnMouseMove(MouseEventArgs e)
352 {
353 Rectangle h = Rectangle.Empty;
354 foreach (KeyValuePair<Rectangle, Action> kv in hits) if (kv.Key.Contains(e.Location)) h = kv.Key;
355 if (h != hover) { hover = h; Invalidate(); }
356 Cursor = h != Rectangle.Empty ? Cursors.Hand : Cursors.Default;
357 }
358
359 protected override void OnMouseLeave(EventArgs e) { if (hover != Rectangle.Empty) { hover = Rectangle.Empty; Invalidate(); } }
360
361 protected override void OnMouseDown(MouseEventArgs e)
362 {
363 Focus();
364 if (e.Button != MouseButtons.Left) return;
365 foreach (KeyValuePair<Rectangle, Action> kv in hits)
366 if (kv.Key.Contains(e.Location)) { kv.Value(); return; }
367 }
368
369 protected override void OnMouseWheel(MouseEventArgs e)
370 {
371 int view = Height - top;
372 int max = Math.Max(0, contentH - view);
373 int s = Math.Max(0, Math.Min(max, scroll - e.Delta / 120 * Theme.S(60)));
374 if (s != scroll) { scroll = s; Invalidate(); }
375 }
376 }
377}