using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; using System.Globalization; using System.Windows.Forms; namespace UltraParadise { internal sealed class ModEntry { public string Section, Key, Kind, Value, Desc, Min, Max; public string[] Opts; } internal sealed class ModInfo { public string Guid, Name, Version, Stat = ""; public bool CanTake, Taken; public readonly List> Actions = new List>(); public readonly List Entries = new List(); } internal sealed class MyModsPanel : Control { private const char REC = '\u001d', SEP = '\u001c'; public readonly List Mods = new List(); public bool Linked; public bool Loaded; public bool Outdated; public Action Send; public Action TakeChanged; public Action Say; private string selected; private readonly List> hits = new List>(); private Rectangle hover = Rectangle.Empty; private int scroll, contentH, top; private readonly Font headFont = Theme.F("Segoe UI Semibold", 8f); private readonly Font textFont = Theme.F("Segoe UI", 9f); private readonly Font smallFont = Theme.F("Segoe UI", 8.5f); private readonly Font boldFont = Theme.F("Segoe UI Semibold", 9.5f); private readonly Font btnFont = Theme.F("Segoe UI Semibold", 8.5f); public MyModsPanel() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.Selectable, true); BackColor = Theme.Back; } private int Pad { get { return Theme.S(18); } } private int HeadH { get { return Theme.S(34); } } private int LeftW { get { return Theme.S(260); } } public void Parse(string body) { Mods.Clear(); Loaded = true; ModInfo cur = null; foreach (string rec in body.Split(REC)) { string[] f = rec.Split(SEP); if (f.Length >= 7 && f[0] == "M") { cur = new ModInfo(); cur.Guid = f[1]; cur.Name = f[2]; cur.Version = f[3]; cur.CanTake = f[4] == "1"; cur.Taken = f[5] == "1"; foreach (string a in f[6].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) { int eq = a.IndexOf('='); if (eq > 0) cur.Actions.Add(new KeyValuePair(a.Substring(0, eq), a.Substring(eq + 1))); } Mods.Add(cur); } else if (f.Length >= 10 && f[0] == "E" && cur != null && cur.Guid == f[1]) { ModEntry e = new ModEntry(); 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]; e.Opts = f[9].Length > 0 ? f[9].Split('|') : new string[0]; cur.Entries.Add(e); } } if (selected == null || Mods.Find(delegate (ModInfo m) { return m.Guid == selected; }) == null) selected = Mods.Count > 0 ? Mods[0].Guid : null; Invalidate(); } public void Stats(string body) { foreach (string rec in body.Split(REC)) { string[] f = rec.Split(SEP); if (f.Length < 2) continue; foreach (ModInfo m in Mods) if (m.Guid == f[0]) m.Stat = f[1]; } if (Visible) Invalidate(); } public void Clear() { Mods.Clear(); Loaded = false; Invalidate(); } private void Head(Graphics g, string title, int x, int y, int w) { StringFormat mid = new StringFormat(); mid.LineAlignment = StringAlignment.Center; mid.FormatFlags = StringFormatFlags.NoWrap; 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)); string sp = Theme.Spaced(title); using (SolidBrush orange = new SolidBrush(Theme.Orange)) g.DrawString(sp, headFont, orange, new RectangleF(x + Theme.S(9), y + Theme.S(8), w, HeadH - Theme.S(12)), mid); int tw = (int)g.MeasureString(sp, headFont).Width + Theme.S(16); int ly = y + Theme.S(8) + (HeadH - Theme.S(12)) / 2; 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)) g.FillRectangle(lb, x + tw, ly, Math.Max(1, w - tw), 1); } private int Button(Graphics g, int x, int y, string text, bool primary, Action a) { int w = (int)Math.Ceiling(g.MeasureString(text, btnFont).Width) + Theme.S(20); Rectangle r = new Rectangle(x, y, w, Theme.S(26)); bool h = r == hover; Color fill = primary ? (h ? Theme.AccentHover : Theme.Orange) : (h ? Theme.Hover : Theme.Panel); using (SolidBrush b = new SolidBrush(fill)) g.FillRectangle(b, r); using (Pen p = new Pen(primary ? Theme.Orange : (h ? Theme.Orange : Theme.Edge))) g.DrawRectangle(p, r); StringFormat c = new StringFormat(); c.Alignment = StringAlignment.Center; c.LineAlignment = StringAlignment.Center; using (SolidBrush t = new SolidBrush(primary ? Theme.OnAccent : Theme.Text)) g.DrawString(text, btnFont, t, r, c); hits.Add(new KeyValuePair(r, a)); return w; } private int Para(Graphics g, string s, Font f, Color c, int x, int y, int w) { SizeF sz = g.MeasureString(s, f, w); using (SolidBrush b = new SolidBrush(c)) g.DrawString(s, f, b, new RectangleF(x, y, w, sz.Height + 2)); return (int)Math.Ceiling(sz.Height); } private void Switch(Graphics g, Rectangle r, bool on) { Theme.FillRound(g, on ? Theme.Orange : Theme.Edge, r, r.Height / 2); int d = r.Height - Theme.S(6); Rectangle k = new Rectangle(on ? r.Right - d - Theme.S(3) : r.X + Theme.S(3), r.Y + Theme.S(3), d, d); Theme.FillRound(g, on ? Theme.OnAccent : Theme.Text, k, d / 2); } private ModInfo Selected() { foreach (ModInfo m in Mods) if (m.Guid == selected) return m; return null; } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; g.SmoothingMode = SmoothingMode.AntiAlias; Theme.PaintBack(g, this); hits.Clear(); int x = Pad, y = Theme.S(6), w = LeftW; Head(g, "MY MODS IN THE GAME", x, y, w); y += HeadH + Theme.S(4); if (!Linked) { 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); return; } 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; } if (Mods.Count == 0) { 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); return; } foreach (ModInfo m in Mods) { Rectangle r = new Rectangle(x, y, w, Theme.S(48)); bool sel = m.Guid == selected, h = r == hover; using (SolidBrush b = new SolidBrush(sel ? Theme.Hover : h ? Theme.Panel : Theme.Back)) g.FillRectangle(b, r); using (Pen p = new Pen(sel ? Theme.Orange : Theme.Line)) g.DrawRectangle(p, r); StringFormat nw = new StringFormat(); nw.Trimming = StringTrimming.EllipsisCharacter; nw.FormatFlags = StringFormatFlags.NoWrap; 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); string sub = "v" + m.Version + (m.Taken ? " · used from the trainer" : m.CanTake ? " · in-game window" : ""); 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); string id = m.Guid; hits.Add(new KeyValuePair(r, delegate { selected = id; scroll = 0; Invalidate(); })); y += Theme.S(54); } ModInfo s = Selected(); if (s == null) return; int rx = x + w + Theme.S(22), rw = Width - rx - Pad; Head(g, s.Name.ToUpperInvariant(), rx, Theme.S(6), rw); top = Theme.S(6) + HeadH + Theme.S(4); Region clip = g.Clip; g.SetClip(new Rectangle(rx - Theme.S(2), top, rw + Theme.S(10), Height - top)); int ry = top - scroll; int start = ry; if (s.CanTake) { Rectangle card = new Rectangle(rx, ry, rw, Theme.S(64)); using (SolidBrush b = new SolidBrush(Theme.Panel)) g.FillRectangle(b, card); using (Pen p = new Pen(s.Taken ? Theme.Orange : Theme.Line)) g.DrawRectangle(p, card); Rectangle sw = new Rectangle(card.Right - Theme.S(54), card.Y + (card.Height - Theme.S(22)) / 2, Theme.S(42), Theme.S(22)); Switch(g, sw, s.Taken); 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)); 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)); string id = s.Guid; bool to = !s.Taken; hits.Add(new KeyValuePair(card, delegate { if (TakeChanged != null) TakeChanged(id, to); })); ry += card.Height + Theme.S(12); } if (s.Stat.Length > 0) { ry += Para(g, s.Stat, boldFont, Theme.On, rx, ry, rw) + Theme.S(8); } if (s.Actions.Count > 0) { int bx = rx; foreach (KeyValuePair a in s.Actions) { string cmd = "MODACT " + s.Guid + " " + a.Key; bx += Button(g, bx, ry, a.Value, bx == rx, delegate { if (Send != null) Send(cmd); }) + Theme.S(8); } ry += Theme.S(40); } Head(g, "SETTINGS", rx, ry - Theme.S(6), rw); ry += HeadH - Theme.S(2); if (s.Entries.Count == 0) ry += Para(g, "This mod has no settings.", textFont, Theme.Dim, rx, ry, rw) + Theme.S(8); string lastSection = null; foreach (ModEntry en in s.Entries) { if (en.Section != lastSection) { lastSection = en.Section; using (SolidBrush t = new SolidBrush(Theme.Soft)) g.DrawString(en.Section, btnFont, t, rx, ry); ry += Theme.S(22); } int vw = Theme.S(150); Rectangle val = new Rectangle(rx + rw - vw, ry, vw, Theme.S(26)); bool hv = val == hover; ModEntry cap = en; ModInfo owner = s; if (en.Kind == "bool") { bool on = en.Value.Equals("true", StringComparison.OrdinalIgnoreCase); Rectangle sr = new Rectangle(val.Right - Theme.S(42), val.Y + Theme.S(2), Theme.S(42), Theme.S(22)); Switch(g, sr, on); hits.Add(new KeyValuePair(val, delegate { SetValue(owner, cap, on ? "false" : "true"); })); } else { using (SolidBrush b = new SolidBrush(hv ? Theme.Hover : Theme.Panel)) g.FillRectangle(b, val); using (Pen p = new Pen(hv ? Theme.Orange : Theme.Edge)) g.DrawRectangle(p, val); StringFormat c = new StringFormat(); c.LineAlignment = StringAlignment.Center; c.Trimming = StringTrimming.EllipsisCharacter; c.FormatFlags = StringFormatFlags.NoWrap; 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); hits.Add(new KeyValuePair(val, delegate { Edit(owner, cap); })); } int lw = rw - vw - Theme.S(14); 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))); int dh = en.Desc.Length > 0 ? Para(g, en.Desc, smallFont, Theme.Dim, rx, ry + Theme.S(24), lw) : 0; ry += Math.Max(Theme.S(32), Theme.S(26) + dh) + Theme.S(10); } contentH = ry - start; g.Clip = clip; int view = Height - top; if (contentH > view) { int tH = Math.Max(Theme.S(30), view * view / contentH); int tY = top + (view - tH) * scroll / Math.Max(1, contentH - view); using (SolidBrush sb = new SolidBrush(Theme.Scroll)) g.FillRectangle(sb, Width - Theme.S(6), tY, Theme.S(3), tH); } for (int i = hits.Count - 1; i >= 0; i--) if (hits[i].Key.X >= rx && (hits[i].Key.Bottom <= top || hits[i].Key.Top >= Height)) hits.RemoveAt(i); } private static string Nice(string key) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < key.Length; i++) { char c = key[i]; if (i > 0 && char.IsUpper(c) && !char.IsUpper(key[i - 1])) sb.Append(' '); sb.Append(c); } return sb.ToString(); } private void SetValue(ModInfo m, ModEntry e, string v) { e.Value = v; if (Send != null) Send("MODCFG " + m.Guid + " " + e.Section + SEP + e.Key + SEP + v); Invalidate(); } private void Edit(ModInfo m, ModEntry e) { using (Form f = new Form()) { f.Text = m.Name + ": " + Nice(e.Key); f.FormBorderStyle = FormBorderStyle.FixedDialog; f.MaximizeBox = false; f.MinimizeBox = false; f.ShowInTaskbar = false; f.StartPosition = FormStartPosition.CenterParent; f.BackColor = Theme.Panel; f.ForeColor = Theme.Text; f.Font = textFont; f.ClientSize = new Size(Theme.S(380), Theme.S(150)); Label l = new Label(); l.Text = e.Desc.Length > 0 ? e.Desc : Nice(e.Key); if (e.Min.Length > 0) l.Text += " (" + e.Min + " to " + e.Max + ")"; l.SetBounds(Theme.S(12), Theme.S(10), Theme.S(356), Theme.S(54)); f.Controls.Add(l); Control input; if (e.Opts.Length > 0) { ComboBox cb = new ComboBox(); cb.DropDownStyle = ComboBoxStyle.DropDownList; cb.Items.AddRange(e.Opts); cb.SelectedItem = e.Value; if (cb.SelectedIndex < 0 && cb.Items.Count > 0) cb.SelectedIndex = 0; input = cb; } else { TextBox tb = new TextBox(); tb.Text = e.Value; tb.BackColor = Theme.Back; tb.ForeColor = Theme.Text; tb.BorderStyle = BorderStyle.FixedSingle; input = tb; } input.SetBounds(Theme.S(12), Theme.S(70), Theme.S(356), Theme.S(26)); f.Controls.Add(input); System.Windows.Forms.Button ok = new System.Windows.Forms.Button(); ok.Text = "Save"; ok.DialogResult = DialogResult.OK; ok.FlatStyle = FlatStyle.Flat; ok.BackColor = Theme.Orange; ok.ForeColor = Theme.OnAccent; ok.SetBounds(Theme.S(196), Theme.S(110), Theme.S(82), Theme.S(28)); System.Windows.Forms.Button no = new System.Windows.Forms.Button(); no.Text = "Cancel"; no.DialogResult = DialogResult.Cancel; no.FlatStyle = FlatStyle.Flat; no.SetBounds(Theme.S(286), Theme.S(110), Theme.S(82), Theme.S(28)); f.Controls.Add(ok); f.Controls.Add(no); f.AcceptButton = ok; f.CancelButton = no; if (f.ShowDialog(FindForm()) != DialogResult.OK) return; string v = input.Text.Trim(); if (e.Kind == "int" || e.Kind == "float") { double d; if (!double.TryParse(v.Replace(',', '.'), NumberStyles.Float, CultureInfo.InvariantCulture, out d)) { if (Say != null) Say("That isn't a number.", true); return; } double lo, hi; 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)); v = e.Kind == "int" ? ((int)Math.Round(d)).ToString(CultureInfo.InvariantCulture) : d.ToString("0.###", CultureInfo.InvariantCulture); } SetValue(m, e, v); } } protected override void OnMouseMove(MouseEventArgs e) { Rectangle h = Rectangle.Empty; foreach (KeyValuePair kv in hits) if (kv.Key.Contains(e.Location)) h = kv.Key; if (h != hover) { hover = h; Invalidate(); } Cursor = h != Rectangle.Empty ? Cursors.Hand : Cursors.Default; } protected override void OnMouseLeave(EventArgs e) { if (hover != Rectangle.Empty) { hover = Rectangle.Empty; Invalidate(); } } protected override void OnMouseDown(MouseEventArgs e) { Focus(); if (e.Button != MouseButtons.Left) return; foreach (KeyValuePair kv in hits) if (kv.Key.Contains(e.Location)) { kv.Value(); return; } } protected override void OnMouseWheel(MouseEventArgs e) { int view = Height - top; int max = Math.Max(0, contentH - view); int s = Math.Max(0, Math.Min(max, scroll - e.Delta / 120 * Theme.S(60))); if (s != scroll) { scroll = s; Invalidate(); } } } }