IoViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.Controllers;
  3. using PixiEditor.Models.Controllers.Shortcuts;
  4. using System;
  5. using System.Windows.Input;
  6. namespace PixiEditor.ViewModels.SubViewModels.Main
  7. {
  8. public class IoViewModel : SubViewModel<ViewModelMain>
  9. {
  10. public RelayCommand MouseMoveCommand { get; set; }
  11. public RelayCommand MouseDownCommand { get; set; }
  12. public RelayCommand MouseUpCommand { get; set; }
  13. public RelayCommand KeyDownCommand { get; set; }
  14. public RelayCommand KeyUpCommand { get; set; }
  15. private bool restoreToolOnKeyUp = false;
  16. private MouseInputFilter filter = new();
  17. public IoViewModel(ViewModelMain owner)
  18. : base(owner)
  19. {
  20. MouseDownCommand = new RelayCommand(filter.MouseDown);
  21. MouseMoveCommand = new RelayCommand(filter.MouseMove);
  22. MouseUpCommand = new RelayCommand(filter.MouseUp);
  23. GlobalMouseHook.OnMouseUp += filter.MouseUp;
  24. KeyDownCommand = new RelayCommand(OnKeyDown);
  25. KeyUpCommand = new RelayCommand(OnKeyUp);
  26. filter.OnMouseDown += OnMouseDown;
  27. filter.OnMouseMove += OnMouseMove;
  28. filter.OnMouseUp += OnMouseUp;
  29. }
  30. private void OnKeyDown(object parameter)
  31. {
  32. KeyEventArgs args = (KeyEventArgs)parameter;
  33. if (args.IsRepeat && !restoreToolOnKeyUp && Owner.ShortcutController.LastShortcut != null &&
  34. Owner.ShortcutController.LastShortcut.Command == Owner.ToolsSubViewModel.SelectToolCommand)
  35. {
  36. restoreToolOnKeyUp = true;
  37. ShortcutController.BlockShortcutExecution = true;
  38. }
  39. Owner.ShortcutController.KeyPressed(args.Key, Keyboard.Modifiers);
  40. if (Owner.BitmapManager.ActiveDocument != null)
  41. Owner.BitmapManager.InputTarget.OnKeyDown(args.Key);
  42. }
  43. private void OnKeyUp(object parameter)
  44. {
  45. KeyEventArgs args = (KeyEventArgs)parameter;
  46. if (restoreToolOnKeyUp && Owner.ShortcutController.LastShortcut != null &&
  47. Owner.ShortcutController.LastShortcut.ShortcutKey == args.Key)
  48. {
  49. restoreToolOnKeyUp = false;
  50. Owner.ToolsSubViewModel.SetActiveTool(Owner.ToolsSubViewModel.LastActionTool);
  51. ShortcutController.BlockShortcutExecution = false;
  52. }
  53. if (Owner.BitmapManager.ActiveDocument != null)
  54. Owner.BitmapManager.InputTarget.OnKeyUp(args.Key);
  55. }
  56. private void OnMouseDown(object sender, MouseButton button)
  57. {
  58. if (button == MouseButton.Left)
  59. {
  60. BitmapManager bitmapManager = Owner.BitmapManager;
  61. var activeDocument = bitmapManager.ActiveDocument;
  62. if (activeDocument == null)
  63. return;
  64. bitmapManager.InputTarget.OnLeftMouseButtonDown(activeDocument.MouseXOnCanvas, activeDocument.MouseYOnCanvas);
  65. }
  66. }
  67. private void OnMouseMove(object sender, EventArgs args)
  68. {
  69. var activeDocument = Owner.BitmapManager.ActiveDocument;
  70. if (activeDocument == null)
  71. return;
  72. Owner.BitmapManager.InputTarget.OnMouseMove(activeDocument.MouseXOnCanvas, activeDocument.MouseYOnCanvas);
  73. }
  74. private void OnMouseUp(object sender, MouseButton button)
  75. {
  76. if (Owner.BitmapManager.ActiveDocument == null)
  77. return;
  78. if (button == MouseButton.Left)
  79. Owner.BitmapManager.InputTarget.OnLeftMouseButtonUp();
  80. }
  81. }
  82. }