ToolSession.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using PixiEditor.Models.Position;
  2. using PixiEditor.Models.Tools;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows.Input;
  6. using SkiaSharp;
  7. namespace PixiEditor.Models.Controllers
  8. {
  9. public class ToolSession
  10. {
  11. private List<Coordinates> mouseMovement = new();
  12. private bool ended = false;
  13. public IReadOnlyList<Coordinates> MouseMovement => mouseMovement;
  14. public Tool Tool { get; }
  15. public bool IsCtrlDown { get; private set; }
  16. public bool IsShiftDown { get; private set; }
  17. public bool IsAltDown { get; private set; }
  18. private int _smallestX = int.MaxValue;
  19. private int _smallestY = int.MaxValue;
  20. private int _biggestX = int.MinValue;
  21. private int _biggestY = int.MinValue;
  22. public ToolSession(
  23. Tool tool,
  24. double mouseXOnCanvas,
  25. double mouseYOnCanvas,
  26. IReadOnlyDictionary<Key, KeyStates> keyboardStates)
  27. {
  28. if (tool == null)
  29. throw new ArgumentNullException(nameof(tool));
  30. Tool = tool;
  31. Tool.Session = this;
  32. InvokeKeyboardEvents(keyboardStates);
  33. int x = (int)Math.Floor(mouseXOnCanvas);
  34. int y = (int)Math.Floor(mouseYOnCanvas);
  35. mouseMovement.Add(new(x, y));
  36. UpdateMinMax(x, y);
  37. Tool.BeforeUse();
  38. }
  39. private void InvokeKeyboardEvents(IReadOnlyDictionary<Key, KeyStates> keyboardStates)
  40. {
  41. foreach (var pair in keyboardStates)
  42. {
  43. if (pair.Value == KeyStates.None)
  44. OnKeyUp(pair.Key);
  45. else if (pair.Value == KeyStates.Down)
  46. OnKeyDown(pair.Key);
  47. }
  48. }
  49. public void EndSession(IReadOnlyDictionary<Key, KeyStates> keyboardStates)
  50. {
  51. if (ended)
  52. throw new Exception("Session has ended already");
  53. ended = true;
  54. Tool.AfterUse(SKRectI.Create(
  55. _smallestX,
  56. _smallestY,
  57. _biggestX - _smallestX + 1,
  58. _biggestY - _smallestY + 1));
  59. InvokeReleaseKeyboardEvents(keyboardStates);
  60. Tool.Session = null;
  61. }
  62. private void InvokeReleaseKeyboardEvents(IReadOnlyDictionary<Key, KeyStates> keyboardStates)
  63. {
  64. foreach (var pair in keyboardStates)
  65. {
  66. if (pair.Value == KeyStates.Down)
  67. OnKeyUp(pair.Key);
  68. }
  69. }
  70. public void OnKeyDown(Key key)
  71. {
  72. if (key == Key.LeftCtrl)
  73. IsCtrlDown = true;
  74. else if (key == Key.LeftShift)
  75. IsShiftDown = true;
  76. else if (key == Key.LeftAlt)
  77. IsAltDown = true;
  78. Tool.OnKeyDown(key);
  79. }
  80. public void OnKeyUp(Key key)
  81. {
  82. if (key == Key.LeftCtrl)
  83. IsCtrlDown = false;
  84. else if (key == Key.LeftShift)
  85. IsShiftDown = false;
  86. else if (key == Key.LeftAlt)
  87. IsAltDown = false;
  88. Tool.OnKeyUp(key);
  89. }
  90. public void OnPixelPositionChange(Coordinates pos)
  91. {
  92. UpdateMinMax(pos.X, pos.Y);
  93. mouseMovement.Add(pos);
  94. }
  95. private void UpdateMinMax(int x, int y)
  96. {
  97. _smallestX = Math.Min(_smallestX, x);
  98. _smallestY = Math.Min(_smallestY, y);
  99. _biggestX = Math.Max(_biggestX, x);
  100. _biggestY = Math.Max(_biggestY, y);
  101. }
  102. }
  103. }