BitmapManager.cs 9.8 KB

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