PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/TEditXna/MainWindow.xaml.cs

https://github.com/Surfpup/Terraria-Map-Editor
C# | 171 lines | 156 code | 6 blank | 9 comment | 85 complexity | 603ba5890ef962f4cd5ca07a1fd7ed7a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using BCCL.Geometry.Primitives;
  16. using TEditXNA.Terraria;
  17. using TEditXna.Editor;
  18. using TEditXna.ViewModel;
  19. namespace TEditXna
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. private WorldViewModel _vm;
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. this.Width = World.AppSize.X;
  31. this.Height = World.AppSize.Y;
  32. DataContext = ViewModelLocator.WorldViewModel;
  33. _vm = (WorldViewModel)DataContext;
  34. AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent);
  35. }
  36. void MainWindow_Loaded(object sender, RoutedEventArgs e)
  37. {
  38. //string fname = Application.Current.Properties["OpenFile"].ToString();
  39. //if (!string.IsNullOrWhiteSpace(fname))
  40. //{
  41. // _vm.LoadWorld(fname);
  42. //}
  43. //e.Handled = false;
  44. }
  45. private void HandleKeyDownEvent(object sender, KeyEventArgs e)
  46. {
  47. if (!(e.Source is View.WorldRenderXna))
  48. return;
  49. if (e.Key == Key.C && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  50. {
  51. if (_vm.CopyCommand.CanExecute(null))
  52. _vm.CopyCommand.Execute(null);
  53. }
  54. else if (e.Key == Key.V && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  55. {
  56. if (_vm.PasteCommand.CanExecute(null))
  57. _vm.PasteCommand.Execute(null);
  58. }
  59. else if (e.Key == Key.Z && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  60. {
  61. _vm.UndoCommand.Execute(null);
  62. }
  63. else if (e.Key == Key.OemPlus && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  64. {
  65. if (_vm.RequestZoomCommand.CanExecute(true))
  66. _vm.RequestZoomCommand.Execute(true);
  67. }
  68. else if (e.Key == Key.OemMinus && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  69. {
  70. if (_vm.RequestZoomCommand.CanExecute(false))
  71. _vm.RequestZoomCommand.Execute(false);
  72. }
  73. else if (e.Key == Key.Y && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  74. {
  75. _vm.RedoCommand.Execute(null);
  76. }
  77. else if (e.Key == Key.A && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  78. {
  79. if (_vm.CurrentWorld != null)
  80. {
  81. _vm.Selection.IsActive = true;
  82. _vm.Selection.SetRectangle(new Vector2Int32(0, 0), new Vector2Int32(_vm.CurrentWorld.TilesWide - 1, _vm.CurrentWorld.TilesHigh - 1));
  83. }
  84. }
  85. else if (e.Key == Key.D && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  86. {
  87. if (_vm.CurrentWorld != null)
  88. {
  89. _vm.Selection.IsActive = false;
  90. }
  91. }
  92. else if (e.Key == Key.S && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  93. {
  94. if (_vm.SaveCommand.CanExecute(null))
  95. _vm.SaveCommand.Execute(null);
  96. }
  97. else if (e.Key == Key.O && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
  98. {
  99. if (_vm.OpenCommand.CanExecute(null))
  100. _vm.OpenCommand.Execute(null);
  101. }
  102. else if (e.Key == Key.Delete)
  103. {
  104. if (_vm.DeleteCommand.CanExecute(null))
  105. _vm.DeleteCommand.Execute(null);
  106. }
  107. else if (e.Key == Key.Escape)
  108. {
  109. if (_vm.ActiveTool != null)
  110. {
  111. if (_vm.ActiveTool.Name == "Paste")
  112. SetActiveTool("Arrow");
  113. else
  114. _vm.Selection.IsActive = false;
  115. }
  116. }
  117. else if (e.Key == Key.Up)
  118. {
  119. if (_vm.RequestScrollCommand.CanExecute(ScrollDirection.Up))
  120. _vm.RequestScrollCommand.Execute(ScrollDirection.Up);
  121. e.Handled = true;
  122. }
  123. else if (e.Key == Key.Down)
  124. {
  125. if (_vm.RequestScrollCommand.CanExecute(ScrollDirection.Down))
  126. _vm.RequestScrollCommand.Execute(ScrollDirection.Down);
  127. e.Handled = true;
  128. }
  129. else if (e.Key == Key.Left)
  130. {
  131. if (_vm.RequestScrollCommand.CanExecute(ScrollDirection.Left))
  132. _vm.RequestScrollCommand.Execute(ScrollDirection.Left);
  133. e.Handled = true;
  134. }
  135. else if (e.Key == Key.Right)
  136. {
  137. if (_vm.RequestScrollCommand.CanExecute(ScrollDirection.Right))
  138. _vm.RequestScrollCommand.Execute(ScrollDirection.Right);
  139. e.Handled = true;
  140. }
  141. else if (World.ShortcutKeys.ContainsKey(e.Key))
  142. {
  143. string command = World.ShortcutKeys[e.Key];
  144. if (string.Equals("Eraser", command, StringComparison.InvariantCultureIgnoreCase))
  145. {
  146. _vm.TilePicker.IsEraser = !_vm.TilePicker.IsEraser;
  147. }
  148. else if (string.Equals("Swap", command, StringComparison.InvariantCultureIgnoreCase))
  149. {
  150. _vm.TilePicker.Swap(Keyboard.Modifiers);
  151. }
  152. else
  153. {
  154. SetActiveTool(command);
  155. }
  156. }
  157. }
  158. private void SetActiveTool(string toolName)
  159. {
  160. var tool = _vm.Tools.FirstOrDefault(t => t.Name == toolName);
  161. if (tool != null)
  162. _vm.SetTool.Execute(tool);
  163. }
  164. }
  165. }