-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
1155 lines (981 loc) · 37.1 KB
/
MainWindow.xaml.cs
File metadata and controls
1155 lines (981 loc) · 37.1 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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Controls.Primitives;
using M59AdminTool.ViewModels;
using M59AdminTool.Models;
using M59AdminTool.Services;
using Meridian59EventManager;
namespace M59AdminTool;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private readonly WarpsViewModel _warpsViewModel;
private readonly MonstersViewModel _monstersViewModel;
private readonly ItemsViewModel _itemsViewModel;
private readonly DmCommandsViewModel _dmCommandsViewModel;
private readonly AdminCommandsViewModel _adminCommandsViewModel;
private readonly ConnectionViewModel _connectionViewModel;
private readonly PlayersViewModel _playersViewModel;
private readonly ListReaderViewModel _listReaderViewModel;
private readonly DeepObjectInspectorViewModel _deepObjectInspectorViewModel;
private readonly BGFConverterViewModel _bgfConverterViewModel;
private readonly M59ServerConnectionTransport _sharedEventManagerTransport;
private readonly LocalizationService _localization;
private WindowsFormsHost? _eventManagerHost;
private MainForm? _eventManagerForm;
private System.Windows.Point? _warpDragStart;
private WarpLocation? _dragWarp;
// Mini/Full Mode
private bool _isAdvancedMode = false;
private const double MINI_WIDTH = 400;
private const double MINI_HEIGHT = 600;
private const double FULL_WIDTH = 1000;
private const double FULL_HEIGHT = 700;
private readonly string[] _miniModeTabs = { "DM", "Monsters", "Items", "Warps", "DJ", "Arena" };
private readonly Random _random = new Random();
public MainWindow()
{
InitializeComponent();
AddHandler(UIElement.PreviewMouseWheelEvent, new System.Windows.Input.MouseWheelEventHandler(Window_PreviewMouseWheel), true);
if (MainTabControl != null)
{
MainTabControl.AddHandler(UIElement.PreviewMouseWheelEvent, new System.Windows.Input.MouseWheelEventHandler(MainTabControl_PreviewMouseWheel), true);
}
// Initialize Services
_localization = LocalizationService.Instance;
_localization.LanguageChanged += OnLanguageChanged;
// Initialize ViewModels
_connectionViewModel = new ConnectionViewModel();
_warpsViewModel = new WarpsViewModel();
_monstersViewModel = new MonstersViewModel();
_itemsViewModel = new ItemsViewModel();
_dmCommandsViewModel = new DmCommandsViewModel();
_adminCommandsViewModel = new AdminCommandsViewModel(_connectionViewModel);
_playersViewModel = new PlayersViewModel(_connectionViewModel, _adminCommandsViewModel);
_listReaderViewModel = new ListReaderViewModel(_connectionViewModel);
_deepObjectInspectorViewModel = new DeepObjectInspectorViewModel(_connectionViewModel);
_bgfConverterViewModel = new BGFConverterViewModel();
_sharedEventManagerTransport = new M59ServerConnectionTransport();
_connectionViewModel.PropertyChanged += ConnectionViewModel_PropertyChanged;
// Set DataContext for tabs
SetTabDataContext();
InitializeEventManagerHost();
_ = RefreshSharedConnectionStateAsync();
// Start in Mini Mode
ApplyViewMode(animated: false);
// Start in Warps Tab
if (WarpsTab != null)
{
MainTabControl.SelectedItem = WarpsTab;
}
}
private void SetTabDataContext()
{
// Find Connection tab and set DataContext
if (FindName("ConnectionTab") is TabItem connectionTab)
{
connectionTab.DataContext = new { ConnectionVM = _connectionViewModel };
}
// Find Warps tab and set DataContext
if (FindName("WarpsTab") is TabItem warpsTab)
{
warpsTab.DataContext = _warpsViewModel;
}
// Find Monsters tab and set DataContext
if (FindName("MonstersTab") is TabItem monstersTab)
{
monstersTab.DataContext = _monstersViewModel;
}
// Find Items tab and set DataContext
if (FindName("ItemsTab") is TabItem itemsTab)
{
itemsTab.DataContext = _itemsViewModel;
}
// Find DM tab and set DataContext
if (FindName("DmTab") is TabItem dmTab)
{
dmTab.DataContext = _dmCommandsViewModel;
}
// Find Admin tab and set DataContext
if (FindName("AdminTab") is TabItem adminTab)
{
adminTab.DataContext = _adminCommandsViewModel;
}
// Find Admin Console tab and set DataContext
if (FindName("AdminConsoleTab") is TabItem adminConsoleTab)
{
adminConsoleTab.DataContext = _adminCommandsViewModel;
}
// Event Manager
// Players tab
if (FindName("PlayersTab") is TabItem playersTab)
{
playersTab.DataContext = _playersViewModel;
}
// List Reader tab
if (FindName("ListReaderTab") is TabItem listReaderTab)
{
listReaderTab.DataContext = _listReaderViewModel;
}
// Deep Object Inspector tab
if (FindName("DeepObjectInspectorTab") is TabItem deepInspectorTab)
{
deepInspectorTab.DataContext = _deepObjectInspectorViewModel;
}
// BGF Converter tab
if (FindName("BGFConverterTab") is TabItem bgfConverterTab)
{
bgfConverterTab.DataContext = _bgfConverterViewModel;
}
}
private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is TreeViewItem item)
{
if (item.DataContext is WarpLocation warp)
{
_warpsViewModel.SelectedWarp = warp;
}
else if (item.DataContext is WarpCategory category)
{
_warpsViewModel.SelectedCategory = category;
}
else if (item.DataContext is WarpCategoryView categoryView)
{
_warpsViewModel.SelectedCategory = categoryView.Source;
}
}
}
private void TreeView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"TreeView_MouseDoubleClick fired! OriginalSource: {e.OriginalSource?.GetType().Name}");
// Get the clicked element
var treeView = sender as System.Windows.Controls.TreeView;
if (treeView == null)
{
System.Diagnostics.Debug.WriteLine("Sender is not TreeView");
return;
}
// Check what was actually clicked
var item = e.OriginalSource as FrameworkElement;
if (item == null)
{
System.Diagnostics.Debug.WriteLine("OriginalSource is not FrameworkElement");
return;
}
// Find the data context (should be WarpLocation)
var dataContext = item.DataContext;
System.Diagnostics.Debug.WriteLine($"DataContext type: {dataContext?.GetType().Name}");
// Only execute if it's a WarpLocation (not a Category)
if (dataContext is WarpLocation warp)
{
System.Diagnostics.Debug.WriteLine($"Executing warp: {warp.Name}");
// Make sure it's selected
_warpsViewModel.SelectedWarp = warp;
// Execute the warp
_warpsViewModel.ExecuteWarpCommand.Execute(null);
// Mark as handled so it doesn't bubble up
e.Handled = true;
System.Diagnostics.Debug.WriteLine("Warp executed and event marked as handled");
}
else
{
System.Diagnostics.Debug.WriteLine($"DataContext is not WarpLocation, it's: {dataContext?.GetType().Name ?? "null"}");
}
}
private void DeepInspectorHistory_DoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is not System.Windows.Controls.ListBox listBox)
return;
if (listBox.SelectedItem is not string entry)
return;
if (_deepObjectInspectorViewModel.NavigateToHistoryEntryCommand.CanExecute(entry))
{
_deepObjectInspectorViewModel.NavigateToHistoryEntryCommand.Execute(entry);
}
}
private void BtnLanguage_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"Language button clicked. Current: {_localization.CurrentLanguage}");
// Toggle language
_localization.CurrentLanguage = _localization.CurrentLanguage == Services.Language.German
? Services.Language.English
: Services.Language.German;
System.Diagnostics.Debug.WriteLine($"Language changed to: {_localization.CurrentLanguage}");
}
private void OnLanguageChanged(object? sender, Services.Language newLanguage)
{
System.Diagnostics.Debug.WriteLine($"OnLanguageChanged event fired. New language: {newLanguage}");
// Update displayed names for all warps, monsters and items
_warpsViewModel.RefreshDisplayNames();
_monstersViewModel.RefreshDisplayNames();
_itemsViewModel.RefreshDisplayNames();
System.Diagnostics.Debug.WriteLine("RefreshDisplayNames completed");
}
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
if (sender is System.Windows.Controls.PasswordBox passwordBox)
{
_connectionViewModel.Password = passwordBox.Password;
}
}
private void MonsterListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"MonsterListBox_MouseDoubleClick fired! OriginalSource: {e.OriginalSource?.GetType().Name}");
// Get the clicked element
var listBox = sender as System.Windows.Controls.ListBox;
if (listBox == null)
{
System.Diagnostics.Debug.WriteLine("Sender is not ListBox");
return;
}
// Check what was actually clicked
var item = e.OriginalSource as FrameworkElement;
if (item == null)
{
System.Diagnostics.Debug.WriteLine("OriginalSource is not FrameworkElement");
return;
}
// Find the data context (should be Monster)
var dataContext = item.DataContext;
System.Diagnostics.Debug.WriteLine($"DataContext type: {dataContext?.GetType().Name}");
// Only execute if it's a Monster
if (dataContext is Monster monster)
{
System.Diagnostics.Debug.WriteLine($"Executing monster spawn: {monster.ClassName}");
// Make sure it's selected
_monstersViewModel.SelectedMonster = monster;
// Execute the spawn command
_monstersViewModel.SpawnMonsterCommand.Execute(null);
// Mark as handled so it doesn't bubble up
e.Handled = true;
System.Diagnostics.Debug.WriteLine("Monster spawn executed and event marked as handled");
}
else
{
System.Diagnostics.Debug.WriteLine($"DataContext is not Monster, it's: {dataContext?.GetType().Name ?? "null"}");
}
}
private void ItemListBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine($"ItemListBox_MouseDoubleClick fired! OriginalSource: {e.OriginalSource?.GetType().Name}");
// Get the clicked element
var listBox = sender as System.Windows.Controls.ListBox;
if (listBox == null)
{
System.Diagnostics.Debug.WriteLine("Sender is not ListBox");
return;
}
// Check what was actually clicked
var item = e.OriginalSource as FrameworkElement;
if (item == null)
{
System.Diagnostics.Debug.WriteLine("OriginalSource is not FrameworkElement");
return;
}
// Find the data context (should be Item)
var dataContext = item.DataContext;
System.Diagnostics.Debug.WriteLine($"DataContext type: {dataContext?.GetType().Name}");
// Only execute if it's an Item
if (dataContext is Item itemData)
{
System.Diagnostics.Debug.WriteLine($"Executing item spawn: {itemData.ClassName}");
// Make sure it's selected
_itemsViewModel.SelectedItem = itemData;
// Execute the spawn command
_itemsViewModel.SpawnItemCommand.Execute(null);
// Mark as handled so it doesn't bubble up
e.Handled = true;
System.Diagnostics.Debug.WriteLine("Item spawn executed and event marked as handled");
}
else
{
System.Diagnostics.Debug.WriteLine($"DataContext is not Item, it's: {dataContext?.GetType().Name ?? "null"}");
}
}
private async void PlayersList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is System.Windows.Controls.ListBox list && list.SelectedItem is Models.PlayerEntry entry)
{
_playersViewModel.SelectedPlayer = entry;
_playersViewModel.ShowSelectedPlayerCommand.Execute(entry);
}
}
private async void PlayerDetails_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is System.Windows.Controls.ListBox list && list.SelectedItem is string line)
{
await _playersViewModel.EditDetailLineAsync(line);
}
}
private void ListReaderDetails_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.C &&
(System.Windows.Input.Keyboard.Modifiers & System.Windows.Input.ModifierKeys.Control) == System.Windows.Input.ModifierKeys.Control)
{
if (sender is System.Windows.Controls.ListBox listBox)
{
CopyListBoxItems(listBox, selectedOnly: true);
e.Handled = true;
}
}
}
private void InitializeEventManagerHost()
{
if (FindName("EventManagerHost") is WindowsFormsHost host)
{
_eventManagerHost = host;
var form = new MainForm(_sharedEventManagerTransport)
{
TopLevel = false,
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None,
Dock = DockStyle.Fill
};
host.Child = form;
_eventManagerForm = form;
form.Show();
}
}
private async Task RefreshSharedConnectionStateAsync()
{
if (_connectionViewModel.IsConnected)
{
_sharedEventManagerTransport.AttachConnection(_connectionViewModel.ServerConnection);
if (_eventManagerForm != null)
{
await _eventManagerForm.TryConnectWithSharedTransportAsync();
}
}
else
{
_sharedEventManagerTransport.AttachConnection(null);
_eventManagerForm?.HandleSharedConnectionLost();
}
}
private void ConnectionViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ConnectionViewModel.IsConnected))
{
_ = Dispatcher.InvokeAsync(async () => await RefreshSharedConnectionStateAsync());
}
}
protected override void OnClosed(EventArgs e)
{
_eventManagerForm?.Dispose();
base.OnClosed(e);
}
private void ListReaderDetails_CopySelected_Click(object sender, RoutedEventArgs e)
{
if (TryGetListBoxFromMenuSender(sender, out var listBox))
{
CopyListBoxItems(listBox, selectedOnly: true);
}
}
private void ListReaderDetails_CopyAll_Click(object sender, RoutedEventArgs e)
{
if (TryGetListBoxFromMenuSender(sender, out var listBox))
{
CopyListBoxItems(listBox, selectedOnly: false);
}
}
private async void ListReaderDetails_EditInt_Click(object sender, RoutedEventArgs e)
{
if (TryGetListBoxFromMenuSender(sender, out var listBox))
{
await _listReaderViewModel.EditIntFromDetailLineAsync(listBox.SelectedItem as string);
}
}
private void ListReaderResults_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (_listReaderViewModel.ShowObjectDetailsCommand.CanExecute(null))
{
_listReaderViewModel.ShowObjectDetailsCommand.Execute(null);
}
}
private async void ListReaderDetails_OpenList_Click(object sender, RoutedEventArgs e)
{
if (TryGetListBoxFromMenuSender(sender, out var listBox))
{
await OpenListFromDetailLineAsync(listBox.SelectedItem as string);
}
}
private async void ListReaderDetails_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is System.Windows.Controls.ListBox listBox && listBox.SelectedItem is string line)
{
if (_listReaderViewModel.TryExtractListId(line, out _))
{
await OpenListFromDetailLineAsync(line);
}
else
{
await _listReaderViewModel.EditIntFromDetailLineAsync(line);
}
}
}
private async Task OpenListFromDetailLineAsync(string? line)
{
if (string.IsNullOrWhiteSpace(line))
return;
if (!_listReaderViewModel.TryExtractListId(line, out var listId))
{
_listReaderViewModel.StatusMessage = LocalizationService.Instance.GetString("ListReader_Message_ListIdNotFound");
return;
}
var entries = await _listReaderViewModel.FetchListEntriesAsync(listId);
if (entries.Count == 0)
{
_listReaderViewModel.StatusMessage = LocalizationService.Instance.GetString("ListReader_Message_NoDetailsReceived");
return;
}
var window = new Views.ListPreviewWindow(listId, entries)
{
Owner = this
};
window.Show();
window.Activate();
}
private void PlayersList_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is not System.Windows.Controls.ListBox listBox)
return;
var clicked = e.OriginalSource as DependencyObject;
if (clicked == null)
return;
var item = FindAncestor<System.Windows.Controls.ListBoxItem>(clicked);
if (item?.DataContext is Models.PlayerEntry entry)
{
listBox.SelectedItem = entry;
}
}
private void PlayersOpenDeepInspector_Click(object sender, RoutedEventArgs e)
{
if (sender is not System.Windows.Controls.MenuItem menuItem)
return;
if (menuItem.Parent is not System.Windows.Controls.ContextMenu menu ||
menu.PlacementTarget is not System.Windows.Controls.ListBox listBox)
return;
if (listBox.SelectedItem is not Models.PlayerEntry entry)
return;
if (string.IsNullOrWhiteSpace(entry.ObjectId))
return;
if (FindName("DeepObjectInspectorTab") is TabItem deepInspectorTab)
{
deepInspectorTab.IsSelected = true;
}
_deepObjectInspectorViewModel.ObjectId = entry.ObjectId;
if (_deepObjectInspectorViewModel.LoadObjectCommand.CanExecute(null))
{
_deepObjectInspectorViewModel.LoadObjectCommand.Execute(null);
}
}
private void PlayerDetails_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
HandleListBoxMouseWheel(sender as System.Windows.Controls.ListBox, e);
}
private void ListBox_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
HandleListBoxMouseWheel(sender as System.Windows.Controls.ListBox, e);
}
private void HandleListBoxMouseWheel(System.Windows.Controls.ListBox? listBox, System.Windows.Input.MouseWheelEventArgs e)
{
if (listBox == null)
return;
var scrollViewer = FindVisualChild<ScrollViewer>(listBox);
if (scrollViewer == null)
return;
double offsetChange = e.Delta > 0 ? -1 : 1;
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + offsetChange * 3);
e.Handled = true;
}
private static T? FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T typed)
return typed;
T? result = FindVisualChild<T>(child);
if (result is not null)
return result;
}
return null;
}
private static T? FindAncestor<T>(DependencyObject child) where T : DependencyObject
{
var current = child;
while (current != null)
{
if (current is T typed)
return typed;
current = VisualTreeHelper.GetParent(current);
}
return null;
}
private static bool TryGetListBoxFromMenuSender(object sender, out System.Windows.Controls.ListBox listBox)
{
listBox = null!;
if (sender is not System.Windows.Controls.MenuItem menuItem)
return false;
if (menuItem.Parent is not System.Windows.Controls.ContextMenu menu ||
menu.PlacementTarget is not System.Windows.Controls.ListBox target)
return false;
listBox = target;
return true;
}
private static void CopyListBoxItems(System.Windows.Controls.ListBox listBox, bool selectedOnly)
{
var lines = new List<string>();
if (selectedOnly)
{
foreach (var item in listBox.SelectedItems)
{
if (item is string line)
lines.Add(line);
}
}
else
{
foreach (var item in listBox.Items)
{
if (item is string line)
lines.Add(line);
}
}
if (lines.Count > 0)
{
System.Windows.Clipboard.SetText(string.Join(Environment.NewLine, lines));
}
}
private void AdminScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
if (sender is ScrollViewer sv)
{
double offsetChange = e.Delta > 0 ? -1 : 1;
sv.ScrollToVerticalOffset(sv.VerticalOffset + offsetChange * 3);
e.Handled = true;
}
}
private void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
if (sender is ScrollViewer sv)
{
sv.ScrollToVerticalOffset(sv.VerticalOffset - e.Delta / 3.0);
e.Handled = true;
}
}
private void Window_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
if (TryScrollActiveTab(e))
{
e.Handled = true;
}
}
private void MainTabControl_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
if (TryScrollActiveTab(e))
{
e.Handled = true;
}
}
private bool TryScrollActiveTab(System.Windows.Input.MouseWheelEventArgs e)
{
var targetScrollViewer = GetActiveTabScrollViewer();
if (targetScrollViewer == null || targetScrollViewer.ScrollableHeight <= 0)
return false;
double offsetChange = e.Delta > 0 ? -1 : 1;
targetScrollViewer.ScrollToVerticalOffset(targetScrollViewer.VerticalOffset + offsetChange * 20);
return true;
}
private ScrollViewer? GetActiveTabScrollViewer()
{
if (MainTabControl?.SelectedItem is not TabItem tabItem)
return null;
if (tabItem.Content is DependencyObject contentRoot)
return FindScrollableScrollViewer(contentRoot);
return null;
}
private ScrollViewer? FindScrollableScrollViewer(DependencyObject root)
{
ScrollViewer? fallback = null;
var queue = new Queue<DependencyObject>();
queue.Enqueue(root);
while (queue.Count > 0)
{
var current = queue.Dequeue();
if (current is ScrollViewer sv)
{
if (!IsItemsControlScrollViewer(sv) && !IsTextBoxScrollViewer(sv))
{
fallback ??= sv;
if (sv.ScrollableHeight > 0)
return sv;
}
}
int count = VisualTreeHelper.GetChildrenCount(current);
for (int i = 0; i < count; i++)
{
queue.Enqueue(VisualTreeHelper.GetChild(current, i));
}
}
return fallback;
}
private static bool IsItemsControlScrollViewer(ScrollViewer scrollViewer)
{
if (scrollViewer.TemplatedParent is ItemsControl)
return true;
return false;
}
private static bool IsTextBoxScrollViewer(ScrollViewer scrollViewer)
{
if (scrollViewer.TemplatedParent is System.Windows.Controls.Primitives.TextBoxBase)
return true;
return false;
}
private void Menu_OpenHelp_Click(object sender, RoutedEventArgs e)
{
var window = new Views.HelpWindow
{
Owner = this
};
window.Show();
window.Activate();
}
private void Menu_Configuration_Click(object sender, RoutedEventArgs e)
{
var window = new Views.PathConfigWindow
{
Owner = this
};
window.ShowDialog();
}
private void WarpsTreeView_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (sender is not System.Windows.Controls.TreeView treeView)
return;
var item = FindAncestor<System.Windows.Controls.TreeViewItem>(e.OriginalSource as DependencyObject);
if (item?.DataContext is WarpLocation warp)
{
_warpDragStart = e.GetPosition(treeView);
_dragWarp = warp;
}
else
{
_warpDragStart = null;
_dragWarp = null;
}
}
private void WarpsTreeView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (_dragWarp == null || _warpDragStart == null)
return;
if (e.LeftButton != System.Windows.Input.MouseButtonState.Pressed)
return;
if (sender is not System.Windows.Controls.TreeView treeView)
return;
var currentPos = e.GetPosition(treeView);
var dx = Math.Abs(currentPos.X - _warpDragStart.Value.X);
var dy = Math.Abs(currentPos.Y - _warpDragStart.Value.Y);
if (dx < SystemParameters.MinimumHorizontalDragDistance &&
dy < SystemParameters.MinimumVerticalDragDistance)
{
return;
}
DragDrop.DoDragDrop(treeView, _dragWarp, System.Windows.DragDropEffects.Move);
_warpDragStart = null;
_dragWarp = null;
}
private void WarpsTreeView_DragOver(object sender, System.Windows.DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(WarpLocation)))
{
e.Effects = System.Windows.DragDropEffects.Move;
}
else
{
e.Effects = System.Windows.DragDropEffects.None;
}
e.Handled = true;
}
private void WarpsTreeView_Drop(object sender, System.Windows.DragEventArgs e)
{
if (sender is not System.Windows.Controls.TreeView treeView)
return;
if (e.Data.GetData(typeof(WarpLocation)) is not WarpLocation warp)
return;
var item = FindAncestor<System.Windows.Controls.TreeViewItem>(e.OriginalSource as DependencyObject);
WarpCategory? targetCategory = null;
if (item?.DataContext is WarpCategoryView categoryView)
{
targetCategory = categoryView.Source;
}
else if (item?.DataContext is WarpLocation targetWarp &&
treeView.DataContext is WarpsViewModel viewModel)
{
targetCategory = viewModel.WarpCategories.FirstOrDefault(c => c.Locations.Contains(targetWarp));
}
if (targetCategory == null)
return;
if (treeView.DataContext is WarpsViewModel warpsViewModel)
{
if (warpsViewModel.MoveWarpToCategory(warp, targetCategory))
e.Handled = true;
}
}
#region Mini/Full Mode Toggle
private void BtnAdvanced_Click(object sender, RoutedEventArgs e)
{
_isAdvancedMode = !_isAdvancedMode;
ApplyViewMode(animated: true);
}
private void ApplyViewMode(bool animated)
{
double targetWidth = _isAdvancedMode ? FULL_WIDTH : MINI_WIDTH;
double targetHeight = _isAdvancedMode ? FULL_HEIGHT : MINI_HEIGHT;
// Update button text
if (MenuAdvanced != null)
{
MenuAdvanced.Header = _isAdvancedMode ? "⚡ Simple" : "⚡ Advanced";
}
// Filter tabs
FilterTabsForMode();
// Change tab orientation based on mode
if (MainTabControl != null)
{
MainTabControl.TabStripPlacement = _isAdvancedMode ? Dock.Top : Dock.Left;
}
// Adjust header for mode
if (HeaderTitle != null)
{
HeaderTitle.FontSize = _isAdvancedMode ? 20 : 14;
}
if (HeaderSubtitle != null)
{
HeaderSubtitle.Visibility = _isAdvancedMode ? Visibility.Visible : Visibility.Collapsed;
}
if (HeaderBorder != null)
{
HeaderBorder.Padding = _isAdvancedMode ? new Thickness(15) : new Thickness(8);
}
// Switch DM Tab content between Mini and Full layouts
if (DmContentMini != null)
{
DmContentMini.Visibility = _isAdvancedMode ? Visibility.Collapsed : Visibility.Visible;
}
if (DmContentFull != null)
{
DmContentFull.Visibility = _isAdvancedMode ? Visibility.Visible : Visibility.Collapsed;
}
if (DmHeader != null)
{
DmHeader.FontSize = _isAdvancedMode ? 20 : 14;
}
if (animated)
{
// Spawn magic stars (more stars!)
SpawnMagicStars(70);
// Animate window size
AnimateWindowSize(targetWidth, targetHeight);
}
else
{
Width = targetWidth;
Height = targetHeight;
}
}
private void FilterTabsForMode()
{
if (MainTabControl == null) return;
foreach (TabItem tab in MainTabControl.Items)
{
if (tab.Tag is string tagName)
{
bool shouldShow = _isAdvancedMode || Array.Exists(_miniModeTabs, t => t.Equals(tagName, StringComparison.OrdinalIgnoreCase));
tab.Visibility = shouldShow ? Visibility.Visible : Visibility.Collapsed;
}
}
// Select first visible tab if current is hidden
if (MainTabControl.SelectedItem is TabItem selectedTab && selectedTab.Visibility == Visibility.Collapsed)
{
foreach (TabItem tab in MainTabControl.Items)
{
if (tab.Visibility == Visibility.Visible)
{
MainTabControl.SelectedItem = tab;
break;
}
}
}
}
private void AnimateWindowSize(double targetWidth, double targetHeight)
{
var duration = TimeSpan.FromMilliseconds(400);
var easing = new QuadraticEase { EasingMode = EasingMode.EaseInOut };
// Width animation
var widthAnim = new DoubleAnimation
{
From = Width,
To = targetWidth,
Duration = duration,
EasingFunction = easing
};
// Height animation
var heightAnim = new DoubleAnimation
{
From = Height,
To = targetHeight,
Duration = duration,
EasingFunction = easing
};
// Center the window during resize
double currentCenterX = Left + Width / 2;
double currentCenterY = Top + Height / 2;
double newLeft = currentCenterX - targetWidth / 2;
double newTop = currentCenterY - targetHeight / 2;
var leftAnim = new DoubleAnimation
{
From = Left,