BitmapManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Imaging;
  9. using PixiEditor.Helpers;
  10. using PixiEditor.Models.DataHolders;
  11. using PixiEditor.Models.Events;
  12. using PixiEditor.Models.ImageManipulation;
  13. using PixiEditor.Models.Layers;
  14. using PixiEditor.Models.Position;
  15. using PixiEditor.Models.Tools;
  16. using PixiEditor.Models.Tools.Tools;
  17. using PixiEditor.Models.Tools.ToolSettings.Settings;
  18. namespace PixiEditor.Models.Controllers
  19. {
  20. public class BitmapManager : NotifyableObject
  21. {
  22. private Document activeDocument;
  23. private Tool selectedTool;
  24. public BitmapManager()
  25. {
  26. MouseController = new MouseMovementController();
  27. MouseController.StartedRecordingChanges += MouseController_StartedRecordingChanges;
  28. MouseController.MousePositionChanged += Controller_MousePositionChanged;
  29. MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
  30. MouseController.OnMouseDown += MouseController_OnMouseDown;
  31. MouseController.OnMouseUp += MouseController_OnMouseUp;
  32. BitmapOperations = new BitmapOperationsUtility(this);
  33. ReadonlyToolUtility = new ReadonlyToolUtility();
  34. }
  35. public event EventHandler<DocumentChangedEventArgs> DocumentChanged;
  36. public MouseMovementController MouseController { get; set; }
  37. public Tool SelectedTool
  38. {
  39. get => selectedTool;
  40. private set
  41. {
  42. selectedTool = value;
  43. RaisePropertyChanged("SelectedTool");
  44. }
  45. }
  46. public Layer ActiveLayer => ActiveDocument.ActiveLayer;
  47. public Color PrimaryColor { get; set; }
  48. public int ToolSize
  49. {
  50. get => SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize") != null
  51. ? SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize").Value
  52. : 1;
  53. set
  54. {
  55. if (SelectedTool.Toolbar.GetSetting<SizeSetting>("ToolSize") is SizeSetting toolSize)
  56. {
  57. toolSize.Value = value;
  58. HighlightPixels(MousePositionConverter.CurrentCoordinates);
  59. }
  60. }
  61. }
  62. public BitmapOperationsUtility BitmapOperations { get; set; }
  63. public ReadonlyToolUtility ReadonlyToolUtility { get; set; }
  64. public Document ActiveDocument
  65. {
  66. get => activeDocument;
  67. set
  68. {
  69. activeDocument = value;
  70. RaisePropertyChanged("ActiveDocument");
  71. DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(value));
  72. }
  73. }
  74. public ObservableCollection<Document> Documents { get; set; } = new ObservableCollection<Document>();
  75. /// <summary>
  76. /// Returns if tool is BitmapOperationTool.
  77. /// </summary>
  78. public static bool IsOperationTool(Tool tool)
  79. {
  80. return tool is BitmapOperationTool;
  81. }
  82. public void CloseDocument(Document document)
  83. {
  84. int nextIndex = 0;
  85. if (document == ActiveDocument)
  86. {
  87. nextIndex = Documents.Count > 1 ? Documents.IndexOf(document) : -1;
  88. nextIndex += nextIndex > 0 ? -1 : 0;
  89. }
  90. Documents.Remove(document);
  91. ActiveDocument = nextIndex >= 0 ? Documents[nextIndex] : null;
  92. }
  93. public void ExecuteTool(Coordinates newPosition, bool clickedOnCanvas)
  94. {
  95. if (SelectedTool.CanStartOutsideCanvas || clickedOnCanvas)
  96. {
  97. if (IsOperationTool(SelectedTool))
  98. {
  99. BitmapOperations.ExecuteTool(newPosition, MouseController.LastMouseMoveCoordinates.ToList(), (BitmapOperationTool)SelectedTool);
  100. }
  101. else
  102. {
  103. ReadonlyToolUtility.ExecuteTool(MouseController.LastMouseMoveCoordinates.ToArray(), (ReadonlyTool)SelectedTool);
  104. }
  105. }
  106. }
  107. public WriteableBitmap GetCombinedLayersBitmap()
  108. {
  109. return BitmapUtils.CombineLayers(ActiveDocument.Layers.Where(x => x.IsVisible).ToArray(), ActiveDocument.Width, ActiveDocument.Height);
  110. }
  111. /// <summary>
  112. /// Returns if selected tool is BitmapOperationTool.
  113. /// </summary>
  114. public bool IsOperationTool()
  115. {
  116. return IsOperationTool(SelectedTool);
  117. }
  118. public void SetActiveTool(Tool tool)
  119. {
  120. if (ActiveDocument != null)
  121. {
  122. ActiveDocument.PreviewLayer = null;
  123. }
  124. SelectedTool?.Toolbar.SaveToolbarSettings();
  125. SelectedTool = tool;
  126. SelectedTool.Toolbar.LoadSharedSettings();
  127. }
  128. private void Controller_MousePositionChanged(object sender, MouseMovementEventArgs e)
  129. {
  130. SelectedTool.OnMouseMove(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
  131. if (Mouse.LeftButton == MouseButtonState.Pressed && !IsDraggingViewport() && ActiveDocument != null)
  132. {
  133. ExecuteTool(e.NewPosition, MouseController.ClickedOnCanvas);
  134. }
  135. else if (Mouse.LeftButton == MouseButtonState.Released)
  136. {
  137. HighlightPixels(e.NewPosition);
  138. }
  139. }
  140. private void MouseController_OnMouseDown(object sender, MouseEventArgs e)
  141. {
  142. SelectedTool.OnMouseDown(e);
  143. }
  144. private void MouseController_OnMouseUp(object sender, MouseEventArgs e)
  145. {
  146. SelectedTool.OnMouseUp(e);
  147. }
  148. private bool IsDraggingViewport()
  149. {
  150. return SelectedTool is MoveViewportTool;
  151. }
  152. private void MouseController_StartedRecordingChanges(object sender, EventArgs e)
  153. {
  154. SelectedTool.OnRecordingLeftMouseDown(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
  155. if (ActiveDocument != null)
  156. {
  157. ActiveDocument.PreviewLayer = null;
  158. }
  159. }
  160. private void MouseController_StoppedRecordingChanges(object sender, EventArgs e)
  161. {
  162. SelectedTool.OnStoppedRecordingMouseUp(new MouseEventArgs(Mouse.PrimaryDevice, (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
  163. if (IsOperationTool(SelectedTool) && ((BitmapOperationTool)SelectedTool).RequiresPreviewLayer)
  164. {
  165. BitmapOperations.ApplyPreviewLayer();
  166. }
  167. }
  168. private void HighlightPixels(Coordinates newPosition)
  169. {
  170. if (ActiveDocument == null || ActiveDocument.Layers.Count == 0 || SelectedTool.HideHighlight)
  171. {
  172. return;
  173. }
  174. IEnumerable<Coordinates> highlightArea = CoordinatesCalculator.RectangleToCoordinates(
  175. CoordinatesCalculator.CalculateThicknessCenter(newPosition, ToolSize));
  176. if (CanChangeHighlightOffset(highlightArea))
  177. {
  178. Coordinates start = highlightArea.First();
  179. ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
  180. }
  181. else if (!IsInsideBounds(highlightArea))
  182. {
  183. ActiveDocument.PreviewLayer = null;
  184. }
  185. else
  186. {
  187. ActiveDocument.GeneratePreviewLayer();
  188. ActiveDocument.PreviewLayer.SetPixels(
  189. BitmapPixelChanges.FromSingleColoredArray(highlightArea, Color.FromArgb(77, 0, 0, 0)));
  190. }
  191. }
  192. private bool CanChangeHighlightOffset(IEnumerable<Coordinates> highlightArea)
  193. {
  194. int count = highlightArea.Count();
  195. return count > 0 && ActiveDocument.PreviewLayer != null &&
  196. IsInsideBounds(highlightArea) && count == ActiveDocument.PreviewLayer.Width * ActiveDocument.PreviewLayer.Height;
  197. }
  198. private bool IsInsideBounds(IEnumerable<Coordinates> highlightArea)
  199. {
  200. Coordinates start = highlightArea.First();
  201. Coordinates end = highlightArea.Last();
  202. return start.X <= ActiveDocument.Width - 1 &&
  203. start.Y <= ActiveDocument.Height - 1 &&
  204. end.X >= 0 && end.Y >= 0;
  205. }
  206. }
  207. }