forked from ShootMe/LiveSplit.HollowKnight
-
Notifications
You must be signed in to change notification settings - Fork 4
/
HollowKnightSettings.cs
315 lines (290 loc) · 13.9 KB
/
HollowKnightSettings.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
namespace LiveSplit.HollowKnight {
public partial class HollowKnightSettings : UserControl {
public List<SplitName> Splits { get; private set; }
public bool Ordered { get; set; }
public bool AutosplitEndRuns { get; set; }
public SplitName? AutosplitStartRuns { get; set; }
private bool isLoading;
private List<string> availableSplits = new List<string>();
private List<string> availableSplitsAlphaSorted = new List<string>();
public HollowKnightSettings() {
isLoading = true;
InitializeComponent();
string version = typeof(HollowKnightComponent).Assembly.GetName().Version.ToString();
#if DEBUG
version += "-dev";
#endif
this.versionLabel.Text = "Autosplitter Version: " + version;
Splits = new List<SplitName>();
isLoading = false;
}
public bool HasSplit(SplitName split) {
return Splits.Contains(split);
}
private void Settings_Load(object sender, EventArgs e) {
LoadSettings();
}
public void LoadSettings() {
isLoading = true;
this.flowMain.SuspendLayout();
for (int i = flowMain.Controls.Count - 1; i > 0; i--) {
flowMain.Controls.RemoveAt(i);
}
chkOrdered.Checked = Ordered;
chkAutosplitEndRuns.Checked = AutosplitEndRuns;
chkAutosplitStartRuns.Checked = AutosplitStartRuns != null;
foreach (SplitName split in Splits) {
MemberInfo info = typeof(SplitName).GetMember(split.ToString())[0];
DescriptionAttribute description = (DescriptionAttribute)info.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
HollowKnightSplitSettings setting = new HollowKnightSplitSettings();
setting.cboName.DataSource = new List<string>() { description.Description };
setting.cboName.Enabled = false;
setting.cboName.Text = description.Description;
AddHandlers(setting);
flowMain.Controls.Add(setting);
}
isLoading = false;
this.flowMain.ResumeLayout(true);
}
private void AddHandlers(HollowKnightSplitSettings setting) {
setting.cboName.SelectedIndexChanged += new EventHandler(ControlChanged);
setting.btnRemove.Click += new EventHandler(btnRemove_Click);
setting.btnEdit.Click += new EventHandler(btnEdit_Click);
setting.btnAddAbove.Click += new EventHandler(btnAddAbove_Click);
setting.btnAddBelow.Click += new EventHandler(btnAddBelow_Click);
}
private void RemoveHandlers(HollowKnightSplitSettings setting) {
setting.cboName.SelectedIndexChanged -= ControlChanged;
setting.btnRemove.Click -= btnRemove_Click;
setting.btnEdit.Click -= btnEdit_Click;
setting.btnAddAbove.Click -= btnAddAbove_Click;
setting.btnAddBelow.Click -= btnAddBelow_Click;
}
public void btnRemove_Click(object sender, EventArgs e) {
for (int i = flowMain.Controls.Count - 1; i > 0; i--) {
if (flowMain.Controls[i].Contains((Control)sender)) {
RemoveHandlers((HollowKnightSplitSettings)((Button)sender).Parent);
flowMain.Controls.RemoveAt(i);
break;
}
}
UpdateSplits();
}
public void btnEdit_Click(object sender, EventArgs e) {
for (int i = flowMain.Controls.Count - 1; i > 0; i--) {
if (flowMain.Controls[i].Contains((Control)sender)) {
HollowKnightSplitSettings setting = (HollowKnightSplitSettings)((Button)sender).Parent;
if (setting.cboName.Enabled) {
disableEdit(setting);
} else {
enableEdit(setting);
}
break;
}
}
}
private void btnAddAbove_Click(object sender, EventArgs e) {
for (int i = flowMain.Controls.Count - 1; i > 0; i--) {
if (flowMain.Controls[i].Contains((Control)sender)) {
HollowKnightSplitSettings setting = (HollowKnightSplitSettings)((Button)sender).Parent;
int index = setting.Parent.Controls.GetChildIndex(setting);
addSplitAtIndex(index);
}
}
}
private void btnAddBelow_Click(object sender, EventArgs e) {
for (int i = flowMain.Controls.Count - 1; i > 0; i--) {
if (flowMain.Controls[i].Contains((Control)sender)) {
HollowKnightSplitSettings setting = (HollowKnightSplitSettings)((Button)sender).Parent;
int index = setting.Parent.Controls.GetChildIndex(setting);
addSplitAtIndex(index + 1);
}
}
}
private void enableEdit(HollowKnightSplitSettings setting) {
string currentText = setting.cboName.Text;
setting.btnEdit.Text = "✔";
setting.cboName.DataSource = GetAvailableSplits();
setting.cboName.Text = currentText;
setting.cboName.Enabled = true;
}
private void disableEdit(HollowKnightSplitSettings setting) {
setting.btnEdit.Text = "✏";
setting.cboName.Enabled = false;
}
public void ControlChanged(object sender, EventArgs e) {
UpdateSplits();
}
public void UpdateSplits() {
if (isLoading) return;
Ordered = chkOrdered.Checked;
AutosplitEndRuns = chkAutosplitEndRuns.Checked;
AutosplitStartRuns = chkAutosplitStartRuns.Checked ?
HollowKnightSplitSettings.GetSplitName(cboStartTriggerName.Text) : null;
Splits.Clear();
foreach (Control c in flowMain.Controls) {
if (c is HollowKnightSplitSettings) {
HollowKnightSplitSettings setting = (HollowKnightSplitSettings)c;
if (!string.IsNullOrEmpty(setting.cboName.Text)) {
SplitName split = HollowKnightSplitSettings.GetSplitName(setting.cboName.Text);
Splits.Add(split);
}
}
}
}
public XmlNode UpdateSettings(XmlDocument document) {
XmlElement xmlSettings = document.CreateElement("Settings");
XmlElement xmlOrdered = document.CreateElement("Ordered");
xmlOrdered.InnerText = Ordered.ToString();
xmlSettings.AppendChild(xmlOrdered);
XmlElement xmlAutosplitEndRuns = document.CreateElement("AutosplitEndRuns");
xmlAutosplitEndRuns.InnerText = AutosplitEndRuns.ToString();
xmlSettings.AppendChild(xmlAutosplitEndRuns);
XmlElement xmlAutosplitStartRuns = document.CreateElement("AutosplitStartRuns");
xmlAutosplitStartRuns.InnerText = AutosplitStartRuns.ToString();
xmlSettings.AppendChild(xmlAutosplitStartRuns);
XmlElement xmlSplits = document.CreateElement("Splits");
xmlSettings.AppendChild(xmlSplits);
foreach (SplitName split in Splits) {
XmlElement xmlSplit = document.CreateElement("Split");
xmlSplit.InnerText = split.ToString();
xmlSplits.AppendChild(xmlSplit);
}
return xmlSettings;
}
public void SetSettings(XmlNode settings) {
XmlNode orderedNode = settings.SelectSingleNode(".//Ordered");
XmlNode AutosplitEndRunsNode = settings.SelectSingleNode(".//AutosplitEndRuns");
XmlNode AutosplitStartRunsNode = settings.SelectSingleNode(".//AutosplitStartRuns");
bool isOrdered = false;
bool isAutosplitEndRuns = false;
if (orderedNode != null) {
bool.TryParse(orderedNode.InnerText, out isOrdered);
}
if (AutosplitEndRunsNode != null) {
bool.TryParse(AutosplitEndRunsNode.InnerText, out isAutosplitEndRuns);
}
if (AutosplitStartRunsNode != null) {
string splitDescription = AutosplitStartRunsNode.InnerText.Trim();
if (!string.IsNullOrEmpty(splitDescription)) {
cboStartTriggerName.DataSource = GetAvailableSplits();
AutosplitStartRuns = HollowKnightSplitSettings.GetSplitName(splitDescription);
MemberInfo info = typeof(SplitName).GetMember(AutosplitStartRuns.ToString())[0];
DescriptionAttribute description = (DescriptionAttribute)info.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
cboStartTriggerName.Text = description.Description;
chkAutosplitStartRuns.Checked = true;
}
}
Ordered = isOrdered;
AutosplitEndRuns = isAutosplitEndRuns;
Splits.Clear();
XmlNodeList splitNodes = settings.SelectNodes(".//Splits/Split");
foreach (XmlNode splitNode in splitNodes) {
string splitDescription = splitNode.InnerText;
SplitName split = HollowKnightSplitSettings.GetSplitName(splitDescription);
Splits.Add(split);
}
}
private HollowKnightSplitSettings createSetting() {
HollowKnightSplitSettings setting = new HollowKnightSplitSettings();
List<string> splitNames = GetAvailableSplits();
setting.cboName.DataSource = splitNames;
setting.cboName.Text = splitNames[0];
setting.btnEdit.Text = "✔";
AddHandlers(setting);
return setting;
}
private void btnAddSplit_Click(object sender, EventArgs e) {
HollowKnightSplitSettings setting = createSetting();
flowMain.Controls.Add(setting);
UpdateSplits();
}
private void addSplitAtIndex(int index) {
HollowKnightSplitSettings setting = createSetting();
flowMain.Controls.Add(setting);
flowMain.Controls.SetChildIndex(setting, index);
UpdateSplits();
}
private List<string> GetAvailableSplits() {
if (availableSplits.Count == 0) {
foreach (SplitName split in Enum.GetValues(typeof(SplitName))) {
MemberInfo info = typeof(SplitName).GetMember(split.ToString())[0];
DescriptionAttribute description = (DescriptionAttribute)info.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
availableSplits.Add(description.Description);
availableSplitsAlphaSorted.Add(description.Description);
}
availableSplitsAlphaSorted.Sort(delegate (string one, string two) {
return one.CompareTo(two);
});
}
return rdAlpha.Checked ? availableSplitsAlphaSorted : availableSplits;
}
private void radio_CheckedChanged(object sender, EventArgs e) {
foreach (Control c in flowMain.Controls) {
if (c is HollowKnightSplitSettings) {
HollowKnightSplitSettings setting = (HollowKnightSplitSettings)c;
if (setting.cboName.Enabled) {
string text = setting.cboName.Text;
setting.cboName.DataSource = GetAvailableSplits();
setting.cboName.Text = text;
}
}
}
if (chkAutosplitStartRuns.Checked) {
string text = cboStartTriggerName.Text;
cboStartTriggerName.DataSource = GetAvailableSplits();
cboStartTriggerName.Text = text;
}
}
private void flowMain_DragDrop(object sender, DragEventArgs e) {
UpdateSplits();
}
private void flowMain_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}
private void flowMain_DragOver(object sender, DragEventArgs e) {
HollowKnightSplitSettings data = (HollowKnightSplitSettings)e.Data.GetData(typeof(HollowKnightSplitSettings));
FlowLayoutPanel destination = (FlowLayoutPanel)sender;
Point p = destination.PointToClient(new Point(e.X, e.Y));
var item = destination.GetChildAtPoint(p);
int index = destination.Controls.GetChildIndex(item, false);
if (index == 0) {
e.Effect = DragDropEffects.None;
} else {
e.Effect = DragDropEffects.Move;
int oldIndex = destination.Controls.GetChildIndex(data);
if (oldIndex != index) {
enableEdit(data);
destination.Controls.SetChildIndex(data, index);
destination.Invalidate();
}
}
}
private void AutosplitEndChanged(object sender, EventArgs e) {
UpdateSplits();
}
private void AutosplitStartChanged(object sender, EventArgs e) {
if (chkAutosplitStartRuns.Checked) {
cboStartTriggerName.Enabled = true;
cboStartTriggerName.DataSource = GetAvailableSplits();
}
else {
cboStartTriggerName.Text = "";
cboStartTriggerName.Enabled = false;
cboStartTriggerName.DataSource = new List<string>();
}
UpdateSplits();
}
private void cboStartTriggerName_SelectedIndexChanged(object sender, EventArgs e) {
UpdateSplits();
}
}
}