-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUnlockGraces.xaml.cs
More file actions
463 lines (380 loc) · 15.6 KB
/
Copy pathUnlockGraces.xaml.cs
File metadata and controls
463 lines (380 loc) · 15.6 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace EldenRingTool
{
public partial class UnlockGraces : Window, INotifyPropertyChanged
{
public const string EMPTY_PROFILE_NAME = "<New Profile>";
public ObservableCollection<AreaGroup> AvailableGracesGrouped { get; set; } = new ObservableCollection<AreaGroup>();
public ObservableCollection<Grace> SelectedGraces { get; set; } = new ObservableCollection<Grace>();
private List<AreaGroup> _allGraces = new List<AreaGroup>(); // master copy for filtering
private bool _hasUnsavedChanges;
private string ProfilesFile => MainWindow.getGraceProfilesFileAppData();
public bool HasUnsavedChanges
{
get { return _hasUnsavedChanges; }
set { _hasUnsavedChanges = value; }
}
public UnlockGraces(ERProcess process)
{
InitializeComponent();
DataContext = this;
_process = process;
var excludedGraces = new List<string> { "Show underground", "Table of Lost Grace / Roundtable Hold" };
var grouped = GraceDB.Graces
.Where(g => !excludedGraces.Contains(g.Area))
.GroupBy(g => g.Area)
.Select(a => new AreaGroup
{
Area = a.Key,
SubAreas = a.GroupBy(s => s.SubArea)
.Select(sa => new SubAreaGroup
{
SubArea = sa.Key,
Graces = sa.ToList()
}).ToList()
});
foreach (var area in grouped)
AvailableGracesGrouped.Add(area);
foreach (var area in AvailableGracesGrouped)
{
AreaGroup copyArea = new AreaGroup
{
Area = area.Area,
SubAreas = new List<SubAreaGroup>()
};
foreach (var sub in area.SubAreas)
{
SubAreaGroup copySub = new SubAreaGroup
{
SubArea = sub.SubArea,
Graces = new List<Grace>(sub.Graces)
};
copyArea.SubAreas.Add(copySub);
}
_allGraces.Add(copyArea);
}
SelectedGraces.CollectionChanged += (s, e) =>
{
ActivateSelectedButton.IsEnabled = SelectedGraces.Any();
SaveProfileButton.IsEnabled = SelectedGraces.Any();
};
LoadProfilesIntoComboBox();
SaveProfileButton.IsEnabled = SelectedGraces.Any();
ActivateSelectedButton.IsEnabled = SelectedGraces.Any();
DeleteProfileButton.IsEnabled = ProfileComboBox.SelectedItem != null
&& ProfileComboBox.SelectedItem.ToString() != EMPTY_PROFILE_NAME;
}
private ERProcess _process;
#region Tree/Listbox
private void AvailableGraces_DoubleClick(object sender, RoutedEventArgs e)
{
if (AvailableGracesTree.SelectedItem is Grace g && !SelectedGraces.Contains(g))
SelectedGraces.Add(g);
}
private void SelectedGracesList_DoubleClick(object sender, RoutedEventArgs e)
{
RemoveGraceFromSelected();
}
private void AvailableGraces_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
ActivateSelectedButton.IsEnabled = (AvailableGracesTree.SelectedItem is Grace);
}
private void RemoveGraceFromSelected()
{
if (SelectedGracesList.SelectedItem is Grace g)
{
SelectedGraces.Remove(g);
ActivateSelectedButton.IsEnabled = SelectedGraces.Any();
_hasUnsavedChanges = true;
}
}
private void AddGrace_Click(object sender, RoutedEventArgs e)
{
if (AvailableGracesTree.SelectedItem is Grace g)
{
if (!SelectedGraces.Contains(g))
SelectedGraces.Add(g);
}
else if (AvailableGracesTree.SelectedItem is SubAreaGroup sub)
{
foreach (var grace in sub.Graces)
{
if (!SelectedGraces.Contains(grace))
SelectedGraces.Add(grace);
}
}
else if (AvailableGracesTree.SelectedItem is AreaGroup area)
{
foreach (var subarea in area.SubAreas)
{
foreach (var grace in subarea.Graces)
{
if (!SelectedGraces.Contains(grace))
SelectedGraces.Add(grace);
}
}
}
}
private void RemoveGrace_Click(object sender, RoutedEventArgs e)
{
RemoveGraceFromSelected();
}
private void ActivateSelected_Click(object sender, RoutedEventArgs e)
{
if (AvailableGracesTree.SelectedItem is Grace g)
UnlockGrace(g);
}
private void ActivateGroup_Click(object sender, RoutedEventArgs e)
{
foreach (var selected in SelectedGraces)
UnlockGrace(selected);
}
private void UnlockGrace(Grace g)
{
_process.getSetEventFlag(g.ID, true);
if (g.Area == "Ainsel River" || g.Area == "Nokron, Eternal City" || g.Area == "Deeproot Depths")
_process.getSetEventFlag(82001, true); // underground map
if (g.Area == "Land of the Tower" ||
g.Area == "Gravesite Plain" ||
g.Area == "Scadu Altus" ||
g.Area == "Shadow Keep")
_process.getSetEventFlag(82002, true); // dlc map
}
#endregion
#region Filter
private void FilterBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.IsNullOrEmpty(FilterBox.Text))
{
CollapseAllTreeViewItems();
}
else
{
ApplyFilter(FilterBox.Text);
}
}
private void ClearFilter_Click(object sender, RoutedEventArgs e)
{
FilterBox.Text = "";
ApplyFilter("");
AvailableGracesTree.Dispatcher.BeginInvoke(new Action(() =>
{
foreach (var areaItem in AvailableGracesTree.Items)
{
TreeViewItem tviArea = AvailableGracesTree.ItemContainerGenerator.ContainerFromItem(areaItem) as TreeViewItem;
if (tviArea != null)
{
tviArea.IsExpanded = false;
tviArea.UpdateLayout();
foreach (var subItem in tviArea.Items)
{
TreeViewItem tviSub = tviArea.ItemContainerGenerator.ContainerFromItem(subItem) as TreeViewItem;
if (tviSub != null)
tviSub.IsExpanded = false;
}
}
}
}));
}
private void ApplyFilter(string filter)
{
if (filter == null) filter = "";
filter = filter.ToLower();
AvailableGracesGrouped.Clear();
foreach (AreaGroup area in _allGraces)
{
AreaGroup filteredArea = new AreaGroup { Area = area.Area };
foreach (SubAreaGroup sub in area.SubAreas)
{
List<Grace> matchingGraces = sub.Graces
.Where(g => g.Name.ToLower().Contains(filter))
.ToList();
if (matchingGraces.Any())
{
SubAreaGroup filteredSub = new SubAreaGroup
{
SubArea = sub.SubArea,
Graces = matchingGraces
};
filteredArea.SubAreas.Add(filteredSub);
}
}
if (filteredArea.SubAreas.Any())
AvailableGracesGrouped.Add(filteredArea);
}
AvailableGracesTree.Dispatcher.BeginInvoke(new Action(() =>
{
foreach (object areaItem in AvailableGracesTree.Items)
{
TreeViewItem tviArea = AvailableGracesTree.ItemContainerGenerator.ContainerFromItem(areaItem) as TreeViewItem;
if (tviArea != null)
{
tviArea.IsExpanded = true;
tviArea.UpdateLayout();
foreach (object subItem in tviArea.Items)
{
TreeViewItem tviSub = tviArea.ItemContainerGenerator.ContainerFromItem(subItem) as TreeViewItem;
if (tviSub != null)
tviSub.IsExpanded = true;
}
}
}
}));
}
private void CollapseAllTreeViewItems()
{
AvailableGracesTree.Dispatcher.BeginInvoke(new Action(() =>
{
foreach (var areaItem in AvailableGracesTree.Items)
{
TreeViewItem tviArea = AvailableGracesTree.ItemContainerGenerator.ContainerFromItem(areaItem) as TreeViewItem;
if (tviArea != null)
{
tviArea.IsExpanded = false;
tviArea.UpdateLayout();
foreach (var subItem in tviArea.Items)
{
TreeViewItem tviSub = tviArea.ItemContainerGenerator.ContainerFromItem(subItem) as TreeViewItem;
if (tviSub != null)
tviSub.IsExpanded = false;
}
}
}
}));
}
#endregion
#region Profile management
private void SaveProfile_Click(object sender, RoutedEventArgs e)
{
if (!SelectedGraces.Any())
{
MessageBox.Show("Cannot save an empty profile.");
return;
}
var existingProfiles = File.Exists(ProfilesFile)
? File.ReadAllLines(ProfilesFile).Select(l => l.Split(':')[0])
: Enumerable.Empty<string>();
var dialog = new SaveProfileDialog(existingProfiles, "Save Grace Profile", ProfileComboBox.Text)
{
Owner = this
};
if (dialog.ShowDialog() == true)
{
string profileName = dialog.SelectedProfileName;
var line = profileName + ":" + string.Join(",", SelectedGraces.Select(g => g.ID));
var lines = File.Exists(ProfilesFile) ? File.ReadAllLines(ProfilesFile).ToList() : new List<string>();
lines.RemoveAll(l => l.StartsWith(profileName + ":"));
lines.Add(line);
File.WriteAllLines(ProfilesFile, lines);
LoadProfilesIntoComboBox();
ProfileComboBox.SelectedItem = profileName;
HasUnsavedChanges = false;
MessageBox.Show($"Profile '{profileName}' saved!");
}
}
private void DeleteProfile_Click(object sender, RoutedEventArgs e)
{
if (ProfileComboBox.SelectedItem == null) return;
string profileName = ProfileComboBox.SelectedItem.ToString();
if (!File.Exists(ProfilesFile)) return;
var result = MessageBox.Show(
$"Are you sure you want to delete '{profileName}'?",
"Confirm Delete",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
var lines = File.ReadAllLines(ProfilesFile).ToList();
var removed = lines.RemoveAll(l => l.StartsWith(profileName + ":"));
if (removed > 0)
{
File.WriteAllLines(ProfilesFile, lines);
LoadProfilesIntoComboBox();
SelectedGraces.Clear();
HasUnsavedChanges = false;
MessageBox.Show($"Profile '{profileName}' deleted.");
}
}
}
private void ProfileComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ProfileComboBox.SelectedItem == null) return;
string selectedProfile = ProfileComboBox.SelectedItem.ToString();
DeleteProfileButton.IsEnabled = selectedProfile != EMPTY_PROFILE_NAME;
if (selectedProfile == EMPTY_PROFILE_NAME)
{
SelectedGraces.Clear();
HasUnsavedChanges = false;
return;
}
LoadProfile(selectedProfile);
HasUnsavedChanges = false;
}
private void LoadProfile(string profileName)
{
if (!File.Exists(ProfilesFile)) return;
var line = File.ReadAllLines(ProfilesFile).FirstOrDefault(l => l.StartsWith(profileName + ":"));
if (line == null) return;
var parts = line.Split(':');
if (parts.Length < 2) return;
var ids = parts[1].Split(',').Select(int.Parse).ToHashSet();
SelectedGraces.Clear();
foreach (var area in AvailableGracesGrouped)
{
foreach (var sub in area.SubAreas)
{
foreach (var g in sub.Graces)
if (ids.Contains(g.ID))
SelectedGraces.Add(g);
}
}
}
private void LoadProfilesIntoComboBox()
{
ProfileComboBox.Items.Clear();
ProfileComboBox.Items.Add(EMPTY_PROFILE_NAME);
if (File.Exists(ProfilesFile))
{
foreach (var line in File.ReadAllLines(ProfilesFile))
{
string profileName = line.Split(':')[0];
ProfileComboBox.Items.Add(profileName);
}
}
ProfileComboBox.SelectedIndex = 0;
DeleteProfileButton.IsEnabled = false;
}
#endregion
#region Unsaved Changes on Close
protected override void OnClosing(CancelEventArgs e)
{
if (HasUnsavedChanges)
{
var result = MessageBox.Show(
"You have unsaved changes. Are you sure you want to close?",
"Unsaved Changes",
MessageBoxButton.YesNo,
MessageBoxImage.Warning);
if (result == MessageBoxResult.No)
e.Cancel = true;
}
base.OnClosing(e);
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
}
}