ChangeExecutionController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System.Collections.Generic;
  2. using Avalonia.Input;
  3. using ChunkyImageLib.DataHolders;
  4. using PixiEditor.AvaloniaUI.Models.DocumentModels.UpdateableChangeExecutors;
  5. using PixiEditor.AvaloniaUI.Models.Handlers;
  6. using PixiEditor.AvaloniaUI.Models.Position;
  7. using PixiEditor.AvaloniaUI.Models.Tools;
  8. using PixiEditor.AvaloniaUI.Views.Overlays.SymmetryOverlay;
  9. using PixiEditor.ChangeableDocument.Enums;
  10. using PixiEditor.DrawingApi.Core.Numerics;
  11. namespace PixiEditor.AvaloniaUI.Models.DocumentModels;
  12. #nullable enable
  13. internal class ChangeExecutionController
  14. {
  15. public bool LeftMousePressed { get; private set; }
  16. public ShapeCorners LastTransformState { get; private set; }
  17. public VecI LastPixelPosition => lastPixelPos;
  18. public VecD LastPrecisePosition => lastPrecisePos;
  19. public bool IsChangeActive => currentSession is not null;
  20. private readonly IDocument document;
  21. private readonly IServiceProvider services;
  22. private readonly DocumentInternalParts internals;
  23. private VecI lastPixelPos;
  24. private VecD lastPrecisePos;
  25. private UpdateableChangeExecutor? currentSession = null;
  26. private UpdateableChangeExecutor? _queuedExecutor = null;
  27. public ChangeExecutionController(IDocument document, DocumentInternalParts internals, IServiceProvider services)
  28. {
  29. this.document = document;
  30. this.internals = internals;
  31. this.services = services;
  32. }
  33. public ExecutorType GetCurrentExecutorType()
  34. {
  35. if (currentSession is null)
  36. return ExecutorType.None;
  37. return currentSession.Type;
  38. }
  39. public bool TryStartExecutor<T>(bool force = false)
  40. where T : UpdateableChangeExecutor, new()
  41. {
  42. if (CanStartExecutor(force))
  43. return false;
  44. if (force)
  45. currentSession?.ForceStop();
  46. T executor = new T();
  47. return TryStartExecutorInternal(executor);
  48. }
  49. public bool TryStartExecutor(UpdateableChangeExecutor brandNewExecutor, bool force = false)
  50. {
  51. if (CanStartExecutor(force))
  52. return false;
  53. if (force)
  54. currentSession?.ForceStop();
  55. return TryStartExecutorInternal(brandNewExecutor);
  56. }
  57. private bool CanStartExecutor(bool force)
  58. {
  59. return (currentSession is not null || _queuedExecutor is not null) && !force;
  60. }
  61. private bool TryStartExecutorInternal(UpdateableChangeExecutor executor)
  62. {
  63. executor.Initialize(document, internals, services, this, EndExecutor);
  64. if (executor.StartMode == ExecutorStartMode.OnMouseLeftButtonDown)
  65. {
  66. _queuedExecutor = executor;
  67. return true;
  68. }
  69. return StartExecutor(executor);
  70. }
  71. private bool StartExecutor(UpdateableChangeExecutor brandNewExecutor)
  72. {
  73. if (brandNewExecutor.Start() == ExecutionState.Success)
  74. {
  75. currentSession = brandNewExecutor;
  76. return true;
  77. }
  78. return false;
  79. }
  80. private void EndExecutor(UpdateableChangeExecutor executor)
  81. {
  82. if (executor != currentSession)
  83. throw new InvalidOperationException();
  84. currentSession = null;
  85. _queuedExecutor = null;
  86. }
  87. public bool TryStopActiveExecutor()
  88. {
  89. if (currentSession is null)
  90. return false;
  91. currentSession.ForceStop();
  92. currentSession = null;
  93. return true;
  94. }
  95. public void MidChangeUndoInlet() => currentSession?.OnMidChangeUndo();
  96. public void MidChangeRedoInlet() => currentSession?.OnMidChangeRedo();
  97. public void ConvertedKeyDownInlet(Key key)
  98. {
  99. currentSession?.OnConvertedKeyDown(key);
  100. }
  101. public void ConvertedKeyUpInlet(Key key)
  102. {
  103. currentSession?.OnConvertedKeyUp(key);
  104. }
  105. public void MouseMoveInlet(VecD newCanvasPos)
  106. {
  107. //update internal state
  108. VecI newPixelPos = (VecI)newCanvasPos.Floor();
  109. bool pixelPosChanged = false;
  110. if (lastPixelPos != newPixelPos)
  111. {
  112. lastPixelPos = newPixelPos;
  113. pixelPosChanged = true;
  114. }
  115. lastPrecisePos = newCanvasPos;
  116. //call session events
  117. if (currentSession is not null)
  118. {
  119. if (pixelPosChanged)
  120. currentSession.OnPixelPositionChange(newPixelPos);
  121. currentSession.OnPrecisePositionChange(newCanvasPos);
  122. }
  123. }
  124. public void OpacitySliderDragStartedInlet() => currentSession?.OnOpacitySliderDragStarted();
  125. public void OpacitySliderDraggedInlet(float newValue)
  126. {
  127. currentSession?.OnOpacitySliderDragged(newValue);
  128. }
  129. public void OpacitySliderDragEndedInlet() => currentSession?.OnOpacitySliderDragEnded();
  130. public void SymmetryDragStartedInlet(SymmetryAxisDirection dir) => currentSession?.OnSymmetryDragStarted(dir);
  131. public void SymmetryDraggedInlet(SymmetryAxisDragInfo info)
  132. {
  133. currentSession?.OnSymmetryDragged(info);
  134. }
  135. public void SymmetryDragEndedInlet(SymmetryAxisDirection dir) => currentSession?.OnSymmetryDragEnded(dir);
  136. public void LeftMouseButtonDownInlet(VecD canvasPos)
  137. {
  138. //update internal state
  139. LeftMousePressed = true;
  140. if (_queuedExecutor != null && currentSession == null)
  141. {
  142. StartExecutor(_queuedExecutor);
  143. }
  144. //call session event
  145. currentSession?.OnLeftMouseButtonDown(canvasPos);
  146. }
  147. public void LeftMouseButtonUpInlet()
  148. {
  149. //update internal state
  150. LeftMousePressed = false;
  151. //call session events
  152. currentSession?.OnLeftMouseButtonUp();
  153. }
  154. public void TransformMovedInlet(ShapeCorners corners)
  155. {
  156. LastTransformState = corners;
  157. currentSession?.OnTransformMoved(corners);
  158. }
  159. public void TransformAppliedInlet() => currentSession?.OnTransformApplied();
  160. public void LineOverlayMovedInlet(VecD start, VecD end)
  161. {
  162. currentSession?.OnLineOverlayMoved(start, end);
  163. }
  164. public void SelectedObjectNudgedInlet(VecI distance)
  165. {
  166. currentSession?.OnSelectedObjectNudged(distance);
  167. }
  168. }