BitmapManager.cs 9.8 KB

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