BitmapManager.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. using PixiEditor.Helpers;
  8. using PixiEditor.Models.DataHolders;
  9. using PixiEditor.Models.Enums;
  10. using PixiEditor.Models.Events;
  11. using PixiEditor.Models.ImageManipulation;
  12. using PixiEditor.Models.Layers;
  13. using PixiEditor.Models.Position;
  14. using PixiEditor.Models.Tools;
  15. using PixiEditor.Models.Tools.ToolSettings;
  16. namespace PixiEditor.Models.Controllers
  17. {
  18. public class BitmapManager : NotifyableObject
  19. {
  20. public MouseMovementController MouseController { get; set; }
  21. public Tool SelectedTool
  22. {
  23. get => _selectedTool;
  24. private set
  25. {
  26. _selectedTool = value;
  27. RaisePropertyChanged("SelectedTool");
  28. }
  29. }
  30. public Layer PreviewLayer
  31. {
  32. get => _previewLayer;
  33. set
  34. {
  35. _previewLayer = value;
  36. RaisePropertyChanged("PreviewLayer");
  37. }
  38. }
  39. public Layer ActiveLayer => ActiveDocument.ActiveLayer;
  40. public Color PrimaryColor { get; set; }
  41. public int ToolSize
  42. {
  43. get => SelectedTool.Toolbar.GetSetting("ToolSize") != null
  44. ? (int)SelectedTool.Toolbar.GetSetting("ToolSize").Value
  45. : 1;
  46. set
  47. {
  48. if (SelectedTool.Toolbar.GetSetting("ToolSize") is Setting toolSize)
  49. {
  50. toolSize.Value = value;
  51. }
  52. }
  53. }
  54. public BitmapOperationsUtility BitmapOperations { get; set; }
  55. public ReadonlyToolUtility ReadonlyToolUtility { get; set; }
  56. public Document ActiveDocument
  57. {
  58. get => _activeDocument;
  59. set
  60. {
  61. _activeDocument = value;
  62. RaisePropertyChanged("ActiveDocument");
  63. DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(value));
  64. }
  65. }
  66. private Document _activeDocument;
  67. private Layer _previewLayer;
  68. private Tool _selectedTool;
  69. public BitmapManager()
  70. {
  71. MouseController = new MouseMovementController();
  72. MouseController.StartedRecordingChanges += MouseController_StartedRecordingChanges;
  73. MouseController.MousePositionChanged += Controller_MousePositionChanged;
  74. MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
  75. BitmapOperations = new BitmapOperationsUtility(this);
  76. ReadonlyToolUtility = new ReadonlyToolUtility();
  77. }
  78. public event EventHandler<LayersChangedEventArgs> LayersChanged;
  79. public event EventHandler<DocumentChangedEventArgs> DocumentChanged;
  80. public void SetActiveTool(Tool tool)
  81. {
  82. PreviewLayer = null;
  83. SelectedTool?.Toolbar.SaveToolbarSettings();
  84. SelectedTool = tool;
  85. SelectedTool.Toolbar.LoadSharedSettings();
  86. }
  87. public void SetActiveLayer(int index)
  88. {
  89. if (ActiveDocument.ActiveLayerIndex <= ActiveDocument.Layers.Count - 1)
  90. ActiveDocument.ActiveLayer.IsActive = false;
  91. ActiveDocument.ActiveLayerIndex = index;
  92. ActiveDocument.ActiveLayer.IsActive = true;
  93. LayersChanged?.Invoke(this, new LayersChangedEventArgs(index, LayerAction.SetActive));
  94. }
  95. public void AddNewLayer(string name, WriteableBitmap bitmap, bool setAsActive = true)
  96. {
  97. AddNewLayer(name, bitmap.PixelWidth, bitmap.PixelHeight, setAsActive);
  98. ActiveDocument.Layers.Last().LayerBitmap = bitmap;
  99. }
  100. public void AddNewLayer(string name, bool setAsActive = true)
  101. {
  102. AddNewLayer(name, 0, 0, setAsActive);
  103. }
  104. public void AddNewLayer(string name, int width, int height, bool setAsActive = true)
  105. {
  106. ActiveDocument.Layers.Add(new Layer(name, width, height)
  107. {
  108. MaxHeight = ActiveDocument.Height,
  109. MaxWidth = ActiveDocument.Width
  110. });
  111. if (setAsActive) SetActiveLayer(ActiveDocument.Layers.Count - 1);
  112. LayersChanged?.Invoke(this, new LayersChangedEventArgs(0, LayerAction.Add));
  113. }
  114. public void RemoveLayer(int layerIndex)
  115. {
  116. if (ActiveDocument.Layers.Count == 0) return;
  117. bool wasActive = ActiveDocument.Layers[layerIndex].IsActive;
  118. ActiveDocument.Layers.RemoveAt(layerIndex);
  119. if (wasActive)
  120. SetActiveLayer(0);
  121. else if (ActiveDocument.ActiveLayerIndex > ActiveDocument.Layers.Count - 1)
  122. SetActiveLayer(ActiveDocument.Layers.Count - 1);
  123. }
  124. private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
  125. {
  126. SelectedTool.OnMouseMove(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
  127. if (Mouse.LeftButton == MouseButtonState.Pressed && !IsDraggingViewport() && ActiveDocument != null)
  128. {
  129. ExecuteTool(e.NewPosition, MouseController.ClickedOnCanvas);
  130. }
  131. else if (Mouse.LeftButton == MouseButtonState.Released)
  132. {
  133. HighlightPixels(e.NewPosition);
  134. }
  135. }
  136. public void ExecuteTool(Coordinates newPosition, bool clickedOnCanvas)
  137. {
  138. if (SelectedTool.CanStartOutsideCanvas || clickedOnCanvas)
  139. {
  140. if (IsOperationTool(SelectedTool))
  141. {
  142. BitmapOperations.ExecuteTool(newPosition,
  143. MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
  144. }
  145. else
  146. {
  147. ReadonlyToolUtility.ExecuteTool(MouseController.LastMouseMoveCoordinates.ToArray(),
  148. (ReadonlyTool)SelectedTool);
  149. }
  150. }
  151. }
  152. private bool IsDraggingViewport()
  153. {
  154. return Keyboard.IsKeyDown(Key.LeftShift) && !(SelectedTool is ShapeTool);
  155. }
  156. private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
  157. {
  158. SelectedTool.OnMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
  159. PreviewLayer = null;
  160. }
  161. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  162. {
  163. SelectedTool.OnMouseUp(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
  164. if (IsOperationTool(SelectedTool) && ((BitmapOperationTool) SelectedTool).RequiresPreviewLayer)
  165. BitmapOperations.StopAction();
  166. }
  167. public void GeneratePreviewLayer()
  168. {
  169. if (ActiveDocument != null)
  170. PreviewLayer = new Layer("_previewLayer")
  171. {
  172. MaxWidth = ActiveDocument.Width,
  173. MaxHeight = ActiveDocument.Height
  174. };
  175. }
  176. private void HighlightPixels(Coordinates newPosition)
  177. {
  178. if (ActiveDocument == null || ActiveDocument.Layers.Count == 0 || SelectedTool.HideHighlight) return;
  179. Coordinates[] highlightArea = CoordinatesCalculator.RectangleToCoordinates(
  180. CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
  181. if (CanChangeHighlightOffset(highlightArea))
  182. {
  183. PreviewLayer.Offset = new Thickness(highlightArea[0].X, highlightArea[0].Y,0,0);
  184. }
  185. else if (!IsInsideBounds(highlightArea))
  186. {
  187. PreviewLayer = null;
  188. }
  189. else
  190. {
  191. GeneratePreviewLayer();
  192. PreviewLayer.SetPixels(
  193. BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
  194. }
  195. }
  196. private bool CanChangeHighlightOffset(Coordinates[] highlightArea)
  197. {
  198. return highlightArea.Length > 0 && PreviewLayer != null &&
  199. IsInsideBounds(highlightArea) && highlightArea.Length == PreviewLayer.Width * PreviewLayer.Height;
  200. }
  201. private bool IsInsideBounds(Coordinates[] highlightArea)
  202. {
  203. return highlightArea[0].X <= ActiveDocument.Width - 1 &&
  204. highlightArea[0].Y <= ActiveDocument.Height - 1 &&
  205. highlightArea[^1].X >= 0 && highlightArea[^1].Y >= 0;
  206. }
  207. public WriteableBitmap GetCombinedLayersBitmap()
  208. {
  209. return BitmapUtils.CombineLayers(ActiveDocument.Layers.Where(x => x.IsVisible).ToArray(), ActiveDocument.Width, ActiveDocument.Height);
  210. }
  211. /// <summary>
  212. /// Returns if selected tool is BitmapOperationTool
  213. /// </summary>
  214. /// <returns></returns>
  215. public bool IsOperationTool()
  216. {
  217. return IsOperationTool(SelectedTool);
  218. }
  219. /// <summary>
  220. /// Returns if tool is BitmapOperationTool
  221. /// </summary>
  222. /// <param name="tool"></param>
  223. /// <returns></returns>
  224. public static bool IsOperationTool(Tool tool)
  225. {
  226. return tool is BitmapOperationTool;
  227. }
  228. }
  229. public class LayersChangedEventArgs : EventArgs
  230. {
  231. public int LayerAffected { get; set; }
  232. public LayerAction LayerChangeType { get; set; }
  233. public LayersChangedEventArgs(int layerAffected, LayerAction layerChangeType)
  234. {
  235. LayerAffected = layerAffected;
  236. LayerChangeType = layerChangeType;
  237. }
  238. }
  239. }