-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlugin.cs
More file actions
1392 lines (1209 loc) · 58.5 KB
/
Plugin.cs
File metadata and controls
1392 lines (1209 loc) · 58.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using GeniePlugin.Interfaces;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
namespace DynamicWindows
{
public class Plugin : IPlugin
{
// ── Public state (accessed by other classes) ─────────────────────────
public ArrayList forms = new ArrayList();
public Hashtable documents = new Hashtable();
public Color formback = Color.Black;
public Color formfore = Color.White;
public bool bPluginEnabled = true;
public float fontSize = 9f;
public string fontFamily = SystemFonts.DefaultFont.FontFamily.Name;
/// <summary>Resolved FontFamily used by all dynamic labels. Falls back to the system default.</summary>
private FontFamily ResolvedFontFamily =>
FontFamily.Families.FirstOrDefault(f => f.Name.Equals(fontFamily, StringComparison.OrdinalIgnoreCase))
?? SystemFonts.DefaultFont.FontFamily;
/// <summary>Linear scale factor relative to the default font size of 9pt.</summary>
private float FontScale => fontSize / 9f;
public ArrayList ignorelist = new ArrayList();
public Dictionary<string, Point> positionList = new Dictionary<string, Point>();
public bool bStowContainer;
public Form pForm;
public IHost ghost;
public bool bDisableOtherInjuries = true;
public bool bDisableSelfInjuries = true;
public LoadSave loadSave;
public string characterName;
// ── Private state ────────────────────────────────────────────────────
private string configPath;
private InjuriesWindow injuriesWindow;
private InjuriesOthersWindow injuriesOthersWindow;
private readonly Dictionary<string, InjuriesOthersWindow> injuryWindows = new Dictionary<string, InjuriesOthersWindow>();
private string lastConnectionStatus = "";
// ── IPlugin metadata ─────────────────────────────────────────────────
public string Name => "Dynamic Windows";
public string Version => "2.2.6";
public string Author => "Multiple Developers";
public string Description => "Displays content windows specified through the XML stream from the game.";
public bool Enabled
{
get => bPluginEnabled;
set => bPluginEnabled = value;
}
// =====================================================================
// IPlugin lifecycle
// =====================================================================
public void Initialize(IHost host)
{
try
{
ghost = host;
pForm = host.ParentForm;
configPath = host.get_Variable("PluginPath");
loadSave = new LoadSave(this, configPath, characterName);
loadSave.Load();
injuriesWindow = new InjuriesWindow(this);
injuriesOthersWindow = new InjuriesOthersWindow(this);
}
catch (Exception ex)
{
host.EchoText("[Plugin Error] Initialization failed: " + ex.Message);
}
}
public void Show()
{
new FormOptionWindow(this) { MdiParent = pForm, TopMost = true }.Show();
}
public void ParentClosing()
{
foreach (SkinnedMDIChild window in forms)
positionList[window.Name] = window.Location;
loadSave.Save();
foreach (SkinnedMDIChild window in forms.Cast<SkinnedMDIChild>().ToList())
window.Close();
forms.Clear();
}
public void VariableChanged(string variable)
{
string connected = ghost.get_Variable("connected");
if (connected != lastConnectionStatus)
{
lastConnectionStatus = connected;
if (connected == "0") // disconnected
{
loadSave.Save();
foreach (SkinnedMDIChild w in forms.Cast<SkinnedMDIChild>().ToList())
w.Close();
forms.Clear();
}
else if (connected == "1") // reconnected
{
characterName = ghost.get_Variable("charactername");
loadSave = new LoadSave(this, configPath, characterName);
loadSave.Load();
}
}
if (variable.Equals("charactername", StringComparison.OrdinalIgnoreCase))
loadSave.Load();
}
// =====================================================================
// Text / input parsing
// =====================================================================
public string ParseText(string text, string window)
{
return (window.Trim().ToLower() == "main" || window.Trim() == string.Empty)
? ParseText(text)
: text;
}
public string ParseText(string text) => text;
public string ParseInput(string text)
{
switch (text.ToLower())
{
case "/debugwindows":
ghost.EchoText("Form Count: " + forms.Count);
foreach (Control c in forms)
ghost.EchoText(" Form: " + c.Name);
foreach (string key in (IEnumerable)documents.Keys)
ghost.EchoText($"Variable: {key} - {documents[key]}");
return "";
case "/injurieswindow":
if (!bDisableSelfInjuries)
{
ghost.EchoText("Re-opening injuries window...");
var match = ignorelist.Cast<string>()
.FirstOrDefault(x => x.Equals("injuries", StringComparison.OrdinalIgnoreCase));
if (match != null) ignorelist.Remove(match);
injuriesWindow.Create(null);
}
return "";
case "/toggleinjuries":
bDisableSelfInjuries = !bDisableSelfInjuries;
ghost.EchoText("[Plugin]: Self injuries window is now " + (bDisableSelfInjuries ? "disabled" : "enabled"));
loadSave?.Save();
return "";
case "/toggleotherinjuries":
bDisableOtherInjuries = !bDisableOtherInjuries;
ghost.EchoText("[Plugin]: Other injuries windows are now " + (bDisableOtherInjuries ? "disabled" : "enabled"));
loadSave?.Save();
return "";
case "/injurieshelp":
case "/injurieswindowshelp":
case "/dynamicwindows help":
ghost.EchoText("Dynamic Windows Help:");
ghost.EchoText(" /injurieswindow - Re-opens your self Injuries window if closed or lost.");
ghost.EchoText(" /toggleInjuries - Enables or disables showing your own Injuries window.");
ghost.EchoText(" /toggleOtherInjuries - Enables or disables all 'other injuries' windows for other players.");
ghost.EchoText(" /debugwindows - Displays the number of open windows and their names.");
ghost.EchoText(" (All commands are case-insensitive and can be used at any time.)");
return "";
}
return text;
}
// =====================================================================
// XML parsing – main dispatch
// =====================================================================
public void ParseXML(string xml)
{
if (!bPluginEnabled) return;
try
{
var doc = new XmlDocument();
doc.LoadXml("<?xml version='1.0'?><root>" + xml + "</root>");
foreach (XmlElement elem in doc.DocumentElement.ChildNodes)
{
string id = elem.GetAttribute("id");
if (!string.IsNullOrEmpty(id) &&
ignorelist.Cast<string>().Any(x => x.Equals(id, StringComparison.OrdinalIgnoreCase)))
continue;
switch (elem.Name)
{
case "exposeStream": Parse_xml_exposestream(elem); continue;
case "pushStream": Parse_xml_pushstream(elem); continue;
case "popStream": Parse_xml_popStream(elem); continue;
case "streamWindow": Parse_xml_streamwindow(elem); continue;
case "closeDialog": Parse_xml_closewindow(elem); continue;
case "exposeDialog": Parse_xml_exposewindow(elem); continue;
case "dynaStream": Parse_set_stream(elem); continue;
case "clearDynaStream": Parse_clear_stream(elem); continue;
case "clearStream": Parse_clear_stream(elem); continue;
case "clearContainer": Parse_container(elem); continue;
case "inv": Parse_inventory(elem); continue;
case "openDialog":
if (id.StartsWith("injuries-"))
{
if (!bDisableOtherInjuries)
injuriesOthersWindow.Create(id, elem.GetAttribute("title"));
}
else if (id == "injuries" && injuriesWindow != null)
{
if (!bDisableSelfInjuries)
injuriesWindow.Create(elem);
}
else
{
Parse_xml_openwindow(elem);
}
continue;
case "dialogData":
if (id.StartsWith("injuries-"))
{
if (!bDisableOtherInjuries)
injuriesOthersWindow.Update(id, elem);
}
else if (id == "injuries")
{
if (!bDisableSelfInjuries)
injuriesWindow.Update(elem);
}
else
{
Parse_xml_updatewindow(elem);
}
continue;
default: continue;
}
}
}
catch (Exception ex)
{
ghost.EchoText("Error parsing XML: " + ex.Message);
}
}
// =====================================================================
// Window open / close / expose / update
// =====================================================================
private void Parse_xml_streamwindow(XmlElement elem)
{
// Only handles the profile help popup
if (elem.GetAttribute("id") != "profileHelp") return;
string id = elem.GetAttribute("id");
int width = elem.HasAttribute("width") ? int.Parse(elem.GetAttribute("width")) : 375;
int height = elem.HasAttribute("height") ? int.Parse(elem.GetAttribute("height")) : 350;
CloseWindowIfOpen(id);
var win = CreateSkinnedWindow(id, elem.GetAttribute("title"), width, height + 22);
win.formBody.Visible = true;
win.formBody.AutoScroll = true;
win.formBody.AutoSize = true;
if (elem.HasAttribute("resident") &&
elem.GetAttribute("resident").Equals("false") &&
!elem.GetAttribute("location").Equals("center"))
return;
var help = new HelpWindows();
var contentBox = new RichTextBox
{
ForeColor = formfore,
BackColor = formback,
ReadOnly = true,
BorderStyle = BorderStyle.None,
Dock = DockStyle.Fill
};
if (win.Text == "Profile RP Help") contentBox.Text = help.RPHelp;
if (win.Text == "Profile PVP Help") contentBox.Text = help.PVPHelp;
if (win.Text == "Profile SPOUSE Help") contentBox.Text = help.SPOUSEHelp;
ghost.SendText("#window remove profileHelp");
win.formBody.Controls.Add(contentBox);
win.ShowForm();
}
public void Parse_xml_openwindow(XmlElement xelem)
{
if (!xelem.GetAttribute("type").Equals("dynamic") ||
!xelem.HasAttribute("width") || !xelem.HasAttribute("height"))
return;
string id = xelem.GetAttribute("id");
if (loadSave.IsIgnored(id)) return;
CloseWindowIfOpen(id);
// Spell/feat choose dialogs have custom layout — scale their window width with font size
int xmlWidth = int.Parse(xelem.GetAttribute("width"));
int xmlHeight = int.Parse(xelem.GetAttribute("height"));
int width, height;
if (id == "spellChoose" || id == "featChoose" || id == "featRemove")
{
width = (int)(xmlWidth * FontScale);
height = (int)(xmlHeight * FontScale);
}
else
{
width = xmlWidth;
height = xmlHeight;
}
var dialog = CreateSkinnedWindow(id, xelem.GetAttribute("title"), width, height + 22);
dialog.formBody.ForeColor = formfore;
dialog.formBody.BackColor = formback;
dialog.formBody.AutoSize = false;
dialog.formBody.BorderStyle = BorderStyle.None;
dialog.formBody.Visible = false;
BuildDialogControls(xelem.FirstChild as XmlElement, dialog);
// For dialogs whose window size comes from XML (not scaled), auto-expand
// to fit content if the larger font pushes controls outside the original bounds.
if (id != "spellChoose" && id != "featChoose" && id != "featRemove")
AutoFitDialog(dialog);
dialog.formBody.Visible = true;
dialog.formBody.AutoScroll = true;
bool isResident = xelem.HasAttribute("resident") && xelem.GetAttribute("resident").Equals("false");
string location = xelem.GetAttribute("location");
// Only suppress showing if the dialog is non-resident and neither centered nor detached
if (isResident && !location.Equals("center") && !location.Equals("detach")) return;
dialog.TopMost = true;
dialog.ShowForm();
// "confirm" dialogs must steal focus immediately
if (id == "confirm")
{
var t = new Timer { Interval = 10 };
t.Tick += (s, e) => { t.Stop(); t.Dispose(); dialog.BringToFront(); dialog.Focus(); };
t.Start();
}
}
private void Parse_xml_updatewindow(XmlElement xelem)
{
var dialog = FindWindowByName(xelem.GetAttribute("id"));
if (dialog == null) return;
dialog.formBody.Visible = false;
BuildDialogControls(xelem, dialog);
AutoFitDialog(dialog);
dialog.formBody.Visible = true;
dialog.formBody.AutoScroll = true;
dialog.formBody.AutoSize = true;
dialog.TopMost = true;
dialog.Update();
dialog.ShowForm();
}
private void Parse_xml_exposewindow(XmlElement elem)
{
var win = FindWindowByName(elem.GetAttribute("id"));
if (win == null) return;
win.TopMost = true;
win.Update();
win.ShowForm();
}
private void Parse_xml_closewindow(XmlElement elem)
{
CloseWindowIfOpen(elem.GetAttribute("id"));
}
private void Parse_xml_exposestream(XmlElement elem)
{
FindWindowByName(elem.GetAttribute("id"))?.Show();
}
private void Parse_xml_pushstream(XmlElement elem)
{
var win = FindWindowByName(elem.GetAttribute("id"));
if (win == null) return;
var rtb = win.formBody.Controls[elem.GetAttribute("id")] as RichTextBox;
rtb?.AppendText(elem.InnerText + Environment.NewLine);
}
private void Parse_xml_popStream(XmlElement elem)
{
var win = FindWindowByName(elem.GetAttribute("id"));
if (win == null) return;
var rtb = win.formBody.Controls[elem.GetAttribute("id")] as RichTextBox;
rtb?.Clear();
}
// =====================================================================
// Stream / inventory helpers
// =====================================================================
private void Parse_container(XmlElement elem)
{
if (bStowContainer)
ghost.SendText("#clear " + elem.GetAttribute("id"));
}
private void Parse_inventory(XmlElement elem)
{
if (bStowContainer)
ghost.SendText("#echo >" + elem.GetAttribute("id") + " " + elem.InnerText);
}
private void Parse_clear_stream(XmlElement xelem)
{
string id = xelem.GetAttribute("id");
foreach (SkinnedMDIChild win in forms)
{
foreach (Control ctrl in win.formBody.Controls)
{
if (ctrl.Name.Equals(id))
ctrl.Text = "";
}
}
documents.Remove(id);
}
public void Parse_set_stream(XmlElement xmlElement)
{
string id = xmlElement.GetAttribute("id");
string value = xmlElement.InnerXml;
documents[id] = value;
foreach (SkinnedMDIChild win in forms)
{
foreach (Control ctrl in win.formBody.Controls)
{
if (!ctrl.Name.Equals(id)) continue;
switch (id)
{
case "spells":
Stream_AppendSpellItems(ctrl as Panel, xmlElement);
break;
case "spellInfo":
if (ctrl is RichTextBox spellRtb)
{
spellRtb.AppendText(xmlElement.InnerText + Environment.NewLine);
int spellListW = (int)(200 * FontScale);
spellRtb.Width = win.formBody.Width - spellListW - 15;
spellRtb.Location = new Point(spellListW + 10, 40);
spellRtb.BackColor = formback;
}
break;
case "featList":
Stream_AppendFeatItems(ctrl as Panel, xmlElement);
break;
case "featInfo":
if (ctrl is RichTextBox featRtb)
{
featRtb.AppendText(xmlElement.InnerText + Environment.NewLine);
int featListW = (int)(250 * FontScale);
featRtb.Width = win.formBody.Width - featListW - 15;
featRtb.Location = new Point(featListW + 10, 60);
featRtb.BackColor = formback;
}
break;
default:
value = Regex.Replace(value, @"(<pushBold\s*/>|<popBold\s*/>)", "");
string innerText = xmlElement.InnerText;
documents[id] = innerText;
foreach (Control ctrl2 in win.formBody.Controls)
{
if (ctrl2.Name.Equals(id))
ctrl2.Text = innerText;
}
break;
}
}
}
}
// =====================================================================
// Stream panel population helpers
// =====================================================================
/// <summary>Appends spell book headers and clickable spell labels to the spells panel.</summary>
private void Stream_AppendSpellItems(Panel panel, XmlElement xmlElement)
{
if (panel == null) return;
panel.SuspendLayout();
if (panel.Controls.Count == 0)
{
panel.Height = 380;
panel.Width = (int)(200 * FontScale);
panel.BackColor = formback;
panel.Controls.Add(new Label { Text = "", AutoSize = true, Location = new Point(0, 0) });
}
int y = panel.Controls[panel.Controls.Count - 1].Bottom + 5;
bool hasSpells = xmlElement.HasChildNodes &&
xmlElement.GetElementsByTagName("d").Count > 0;
if (!hasSpells)
{
// Section header (book name)
var header = new Label
{
Text = xmlElement.InnerXml,
AutoSize = true,
Location = new Point(0, y),
ForeColor = formfore,
Font = new Font(ResolvedFontFamily, fontSize + 1, FontStyle.Bold)
};
header.Click -= SpellLabel_Click;
panel.Controls.Add(header);
}
else
{
foreach (XmlNode node in xmlElement.ChildNodes)
{
if (!(node is XmlElement elem) || elem.Name != "d") continue;
var lbl = new Label
{
Text = elem.InnerText,
AutoSize = true,
Location = new Point(15, y),
ForeColor = formfore,
Font = new Font(ResolvedFontFamily, fontSize, FontStyle.Underline),
Tag = elem.GetAttribute("cmd")
};
lbl.Click += SpellLabel_Click;
panel.Controls.Add(lbl);
y += lbl.Height + 5;
}
}
panel.ResumeLayout(false);
panel.PerformLayout();
}
/// <summary>Appends clickable feat labels to the featList panel.</summary>
private void Stream_AppendFeatItems(Panel panel, XmlElement xmlElement)
{
if (panel == null) return;
// Skip dynaStream calls that carry no real content (the game pads with many empty entries)
bool hasContent = xmlElement.ChildNodes.Cast<XmlNode>()
.Any(n => n is XmlElement e && e.Name == "d" && !string.IsNullOrWhiteSpace(e.InnerText));
if (!hasContent) return;
panel.SuspendLayout();
if (panel.Controls.Count == 0)
{
panel.Height = 380;
panel.Width = (int)(250 * FontScale);
panel.BackColor = formback;
panel.Controls.Add(new Label { Text = "", AutoSize = true, Location = new Point(0, 0) });
}
int y = panel.Controls[panel.Controls.Count - 1].Bottom + 5;
foreach (XmlNode node in xmlElement.ChildNodes)
{
if (!(node is XmlElement elem) || elem.Name != "d" ||
string.IsNullOrWhiteSpace(elem.InnerText)) continue;
var lbl = MakeClickableLabel(elem.InnerText, elem.GetAttribute("cmd"), new Point(5, y), FeatLabel_Click);
panel.Controls.Add(lbl);
y += lbl.Height + 5;
}
panel.ResumeLayout(false);
panel.PerformLayout();
}
// =====================================================================
// Dialog control builders
// =====================================================================
/// <summary>Iterates child XML elements and builds the corresponding WinForms controls.</summary>
private void BuildDialogControls(XmlElement container, SkinnedMDIChild dialog)
{
if (container == null) return;
foreach (XmlElement cbx in container.ChildNodes)
{
switch (cbx.Name)
{
case "label": Parse_labels(cbx, dialog); break;
case "cmdButton": Parse_command_buttons(cbx, dialog); break;
case "closeButton": Parse_close_button(cbx, dialog); break;
case "checkBox": Parse_check_box(cbx, dialog); break;
case "radio": Parse_radio_button(cbx, dialog); break;
case "streamBox": Parse_stream_box(cbx, dialog); break;
case "dropDownBox": Parse_drop_down(cbx, dialog); break;
case "editBox": Parse_edit_box(cbx, dialog); break;
case "upDownEditBox": Parse_numericupdown(cbx, dialog); break;
case "progressBar": Parse_progress_bar(cbx, dialog); break;
case "clearContainer": Parse_container(cbx); break;
}
}
}
private void Parse_stream_box(XmlElement cbx, SkinnedMDIChild dialog)
{
string id = cbx.GetAttribute("id");
switch (id)
{
case "spells":
{
var panel = GetOrCreateControl<Panel>(cbx, dialog);
int spellPanelW = (int)(int.Parse(cbx.GetAttribute("width")) * FontScale);
int spellPanelH = (int)(int.Parse(cbx.GetAttribute("height")) * FontScale);
panel.Size = new Size(spellPanelW, spellPanelH);
panel.BackColor = formback;
panel.Location = SetLocation(cbx, panel, dialog);
panel.AutoScroll = true;
dialog.formBody.Controls.Add(panel);
int y = 0;
foreach (XmlNode node in cbx.ChildNodes)
{
if (!(node is XmlElement elem) || elem.Name != "d") continue;
var lbl = MakeClickableLabel(elem.InnerText, elem.GetAttribute("cmd"), new Point(0, y), SpellLabel_Click);
panel.Controls.Add(lbl);
y += lbl.Height + 5;
}
break;
}
case "spellInfo":
{
var rtb = GetOrCreateControl<RichTextBox>(cbx, dialog);
rtb.BackColor = formback;
rtb.ForeColor = formfore;
int spellListW = (int)(200 * FontScale); // matches spells panel base width
int infoLeft = spellListW + 10;
rtb.Location = new Point(infoLeft, 40);
rtb.Width = dialog.formBody.Width - infoLeft - 5;
rtb.Height = (int)(380 * FontScale);
rtb.Anchor = AnchorStyles.Top | AnchorStyles.Right;
rtb.BorderStyle = BorderStyle.None;
rtb.Multiline = true;
rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
rtb.ReadOnly = true;
rtb.DetectUrls = false;
rtb.LinkClicked += Rtb_LinkClicked;
dialog.formBody.Controls.Add(rtb);
break;
}
case "featList":
{
var panel = GetOrCreateControl<Panel>(cbx, dialog);
int featPanelW = (int)(int.Parse(cbx.GetAttribute("width")) * FontScale);
int featPanelH = (int)(int.Parse(cbx.GetAttribute("height")) * FontScale);
panel.Size = new Size(featPanelW, featPanelH);
panel.BackColor = formback;
panel.Location = SetLocation(cbx, panel, dialog);
panel.AutoScroll = true;
dialog.formBody.Controls.Add(panel);
int y = 0;
foreach (XmlNode node in cbx.ChildNodes)
{
if (!(node is XmlElement elem) || elem.Name != "d" ||
string.IsNullOrWhiteSpace(elem.InnerText)) continue;
var lbl = MakeClickableLabel(elem.InnerText, elem.GetAttribute("cmd"), new Point(0, y), FeatLabel_Click);
panel.Controls.Add(lbl);
y += lbl.Height + 5;
}
break;
}
case "featInfo":
{
var rtb = GetOrCreateControl<RichTextBox>(cbx, dialog);
rtb.BackColor = formback;
rtb.ForeColor = formfore;
int featListW = (int)(250 * FontScale); // matches featList panel base width
int infoLeft = featListW + 10;
rtb.Location = new Point(infoLeft, 60);
rtb.Width = dialog.formBody.Width - infoLeft - 5;
rtb.Height = (int)(380 * FontScale);
rtb.Anchor = AnchorStyles.Top | AnchorStyles.Right;
rtb.BorderStyle = BorderStyle.None;
rtb.Multiline = true;
rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
rtb.ReadOnly = true;
rtb.DetectUrls = false;
dialog.formBody.Controls.Add(rtb);
break;
}
default:
{
var tb = GetOrCreateControl<TextBox>(cbx, dialog);
tb.Text = cbx.GetAttribute("value");
tb.Size = BuildSize(cbx, 200, 75);
tb.Location = SetLocation(cbx, tb, dialog);
tb.Multiline = true;
tb.ScrollBars = ScrollBars.Vertical;
dialog.formBody.Controls.Add(tb);
break;
}
}
}
private void Parse_close_button(XmlElement cbx, SkinnedMDIChild dialog)
{
// Reuse any existing action button (spell choose, feat choose/unlearn)
Control existing = dialog.formBody.Controls.Find("chooseSpell", true).FirstOrDefault()
?? dialog.formBody.Controls.Find("chooseFeat", true).FirstOrDefault()
?? dialog.formBody.Controls.Find("unlearnFeat", true).FirstOrDefault();
if (existing is CmdButton existingBtn)
{
existingBtn.Text = cbx.GetAttribute("value");
existingBtn.cmd_string = cbx.HasAttribute("cmd") ? cbx.GetAttribute("cmd") : "";
existingBtn.Invalidate();
return;
}
var btn = new CmdButton
{
Name = cbx.GetAttribute("id"),
Text = cbx.GetAttribute("value"),
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
cmd_string = cbx.HasAttribute("cmd") ? cbx.GetAttribute("cmd") : ""
};
btn.Location = SetLocation(cbx, btn, dialog);
btn.Click += CbClose;
dialog.formBody.Controls.Add(btn);
dialog.CloseCommand = btn;
}
private void Parse_command_buttons(XmlElement cbx, SkinnedMDIChild dialog)
{
var btn = new CmdButton
{
Name = cbx.GetAttribute("id"),
Text = cbx.GetAttribute("value"),
cmd_string = cbx.GetAttribute("cmd"),
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink
};
// Two specific buttons need a small X offset correction
Point loc = SetLocation(cbx, btn, dialog);
if ((cbx.GetAttribute("id") == "changeCustom" || cbx.GetAttribute("id") == "changeCustomString") && loc.X == 353)
loc.X += 8;
btn.Location = loc;
btn.Click += CbCommand;
dialog.formBody.Controls.Add(btn);
}
private void Parse_labels(XmlElement cbx, SkinnedMDIChild dialog)
{
var lbl = dialog.formBody.Controls.ContainsKey(cbx.GetAttribute("id"))
? (Label)dialog.formBody.Controls[cbx.GetAttribute("id")]
: new Label();
lbl.Text = cbx.GetAttribute("value");
lbl.Name = cbx.GetAttribute("id");
lbl.AutoSize = true;
lbl.Size = BuildSize(cbx, 200, 15);
int measuredWidth = TextRenderer.MeasureText(lbl.Text, lbl.Font).Width;
if (measuredWidth > 0) lbl.Width = measuredWidth;
// Bug dialog has fixed label positions
var bugWin = FindWindowByName("bugDialogBox");
if (bugWin != null)
{
switch (cbx.GetAttribute("id"))
{
case "categoryLabel": lbl.Location = new Point(30, 75); break;
case "titleLabel": lbl.Location = new Point(30, 105); break;
case "detailsLabel": lbl.Location = new Point(30, 135); break;
default: lbl.Location = SetLocation(cbx, lbl, dialog); break;
}
}
else
{
lbl.Location = SetLocation(cbx, lbl, dialog);
}
if (!dialog.formBody.Controls.Contains(lbl))
dialog.formBody.Controls.Add(lbl);
}
private void Parse_check_box(XmlElement cbx, SkinnedMDIChild dialog)
{
var cb = GetOrCreateControl<cbCheckBox>(cbx, dialog);
cb.Text = cbx.GetAttribute("text");
cb.checked_value = cbx.GetAttribute("checked_value");
cb.unchecked_value = cbx.GetAttribute("unchecked_value");
cb.Checked = cbx.HasAttribute("checked");
cb.Size = BuildSize(cbx, 200, 20);
cb.Location = SetLocation(cbx, cb, dialog);
dialog.formBody.Controls.Add(cb);
}
private void Parse_radio_button(XmlElement cbx, SkinnedMDIChild dialog)
{
var rb = GetOrCreateControl<CbRadio>(cbx, dialog);
rb.Text = cbx.GetAttribute("text");
rb.command = cbx.GetAttribute("cmd");
rb.group = cbx.GetAttribute("group");
rb.Checked = !cbx.GetAttribute("value").Contains("0");
rb.Size = BuildSize(cbx, 200, 20);
rb.Location = SetLocation(cbx, rb, dialog);
rb.CheckedChanged += CbRadioSelect;
rb.Click += CbRadioSelect;
dialog.formBody.Controls.Add(rb);
}
private void Parse_numericupdown(XmlElement cbx, SkinnedMDIChild dialog)
{
var nud = GetOrCreateControl<NumericUpDown>(cbx, dialog);
if (cbx.HasAttribute("max")) nud.Maximum = int.Parse(cbx.GetAttribute("max"));
if (cbx.HasAttribute("min")) nud.Minimum = int.Parse(cbx.GetAttribute("min"));
nud.Text = cbx.GetAttribute("value");
nud.Value = int.Parse(cbx.GetAttribute("value"));
nud.Size = BuildSize(cbx, 200, 75);
nud.Location = SetLocation(cbx, nud, dialog);
dialog.formBody.Controls.Add(nud);
}
private void Parse_edit_box(XmlElement cbx, SkinnedMDIChild dialog)
{
var tb = GetOrCreateControl<TextBox>(cbx, dialog);
tb.Text = cbx.GetAttribute("value");
tb.Size = BuildSize(cbx, 200, 75);
tb.Location = SetLocation(cbx, tb, dialog);
tb.Multiline = false;
tb.WordWrap = true;
if (cbx.HasAttribute("maxChars"))
tb.MaxLength = int.Parse(cbx.GetAttribute("maxChars"));
if (FindWindowByName("bugDialogBox") != null)
tb.TextChanged += (s, e) => TextBox_TextChanged(s, e, dialog);
dialog.formBody.Controls.Add(tb);
}
private void Parse_progress_bar(XmlElement cbx, SkinnedMDIChild dialog)
{
var pb = GetOrCreateControl<ProgressBar>(cbx, dialog);
pb.Style = ProgressBarStyle.Continuous;
int.TryParse(cbx.GetAttribute("value"), out int val);
pb.Value = val;
pb.Size = BuildSize(cbx, 200, 20);
pb.Location = SetLocation(cbx, pb, dialog);
dialog.formBody.Controls.Add(pb);
}
private void Parse_drop_down(XmlElement cbx, SkinnedMDIChild dialog)
{
var dd = new cbDropBox
{
Name = cbx.GetAttribute("id"),
Text = cbx.GetAttribute("value"),
content_handler_data = new Hashtable()
};
string[] labels = cbx.GetAttribute("content_text").Split(',');
string[] values = cbx.GetAttribute("content_value").Split(',');
for (int i = 0; i < labels.Length; i++)
{
dd.content_handler_data.Add(labels[i], values[i]);
dd.Items.Add(labels[i]);
}
if (cbx.HasAttribute("cmd"))
{
dd.cmd = cbx.GetAttribute("cmd");
dd.SelectedIndexChanged += Cb_SelectedIndexChanged;
}
dd.Size = BuildSize(cbx, 55, 20);
Point loc = SetLocation(cbx, dd, dialog);
dd.Location = dd.Name == "locationSettingDD" ? new Point(loc.X + 10, loc.Y) : loc;
dialog.formBody.Controls.Add(dd);
}
// =====================================================================
// Event handlers – label clicks
// =====================================================================
private void SpellLabel_Click(object sender, EventArgs e)
{
var label = (Label)sender;
string cmd = (string)label.Tag;
ghost.SendText(cmd);
Form form = label.FindForm();
ResetLabelColors(form, "spells");
label.ForeColor = Color.Blue;
UpdateActionButton(form, "chooseSpell", "Choose " + label.Text, cmd);
}
private void FeatLabel_Click(object sender, EventArgs e)
{
var label = (Label)sender;
string cmd = (string)label.Tag;
Form form = label.FindForm();
ResetLabelColors(form, "featList");
label.ForeColor = Color.Blue;
// Clear info pane for fresh content
if (form.Controls.Find("featInfo", true).FirstOrDefault() is RichTextBox rtb)
rtb.Clear();
// Works for both choose and unlearn dialogs
Control actionBtn = form.Controls.Find("chooseFeat", true).FirstOrDefault()
?? form.Controls.Find("unlearnFeat", true).FirstOrDefault();
if (actionBtn is CmdButton btn)
{
btn.Text = (btn.Name == "unlearnFeat" ? "Unlearn " : "Choose ") + label.Text;
btn.cmd_string = cmd;
}
ghost.SendText(cmd);
}
private void Rtb_LinkClicked(object sender, LinkClickedEventArgs e)
{
var rtb = (RichTextBox)sender;
Point mouse = rtb.PointToClient(Cursor.Position);
int index = rtb.GetCharIndexFromPosition(mouse);
int start = rtb.Text.LastIndexOf("<d", index);
int end = rtb.Text.IndexOf("</d>", index) + 4;
string chunk = rtb.Text.Substring(start, end - start);
var doc = new XmlDocument();
doc.LoadXml("<root>" + chunk + "</root>");
if (doc.DocumentElement.FirstChild is XmlElement elem &&
elem.Name == "d" && elem.HasAttribute("cmd"))
ghost.SendText(elem.GetAttribute("cmd"));
}
// =====================================================================
// Event handlers – buttons
// =====================================================================
public void CbClose(object sender, EventArgs e)
{
var btn = (CmdButton)sender;
var panel = (Panel)btn.Parent;
var dialog = (SkinnedMDIChild)btn.FindForm();
string cmd = btn.cmd_string;
string ddValue = "";
if (cmd.Length > 2)
{