ToolsManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. using System.Windows.Input;
  7. using System.Windows.Media.Imaging;
  8. using PixiEditor.ViewModels;
  9. using System.Timers;
  10. using System.Windows.Threading;
  11. using System.Threading;
  12. using PixiEditorDotNetCore3.Models.Layers;
  13. using PixiEditorDotNetCore3.Models.Position;
  14. namespace PixiEditorDotNetCore3.Models.Tools
  15. {
  16. public class ToolsManager
  17. {
  18. public List<Tool> Tools { get; set; } = new List<Tool>();
  19. public Tool SelectedTool { get; private set; }
  20. private bool _toolRecievedData = false;
  21. private System.Timers.Timer _loopTimer;
  22. private Layer _layer;
  23. private WriteableBitmap _clonedBitmap;
  24. private Coordinates _startCoordinates;
  25. private Color _color;
  26. private int _toolSzie;
  27. public ToolsManager(List<Tool> tools)
  28. {
  29. Tools = tools;
  30. _loopTimer = new System.Timers.Timer
  31. {
  32. Interval = 15,
  33. Enabled = false,
  34. AutoReset = true
  35. };
  36. _loopTimer.Elapsed += LoopTimer_Elapsed;
  37. }
  38. private void LoopTimer_Elapsed(object sender, ElapsedEventArgs e)
  39. {
  40. Application.Current.Dispatcher.Invoke(() =>
  41. {
  42. _layer.LayerBitmap.Lock();
  43. if (_clonedBitmap != null)
  44. {
  45. RestoreLastBitmap();
  46. }
  47. BitmapPixelChanges changes = SelectedTool.Use(_layer, _startCoordinates, _color, _toolSzie);
  48. if (!SelectedTool.ExecutesItself)
  49. {
  50. _layer.ApplyPixels(changes, changes.PixelsColor);
  51. }
  52. _layer.LayerBitmap.Unlock();
  53. });
  54. }
  55. private void RestoreLastBitmap()
  56. {
  57. _layer.LayerBitmap.Clear();
  58. _layer.LayerBitmap.Blit(new Rect(new Size(_layer.Width, _layer.Height)), _clonedBitmap, new Rect(new Size(_layer.Width, _layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
  59. }
  60. public void SetTool(ToolType tool)
  61. {
  62. SelectedTool = Tools.Find(x => x.ToolType == tool);
  63. }
  64. public void StopExectuingTool()
  65. {
  66. _loopTimer.Enabled = false;
  67. _toolRecievedData = false;
  68. _clonedBitmap = null;
  69. }
  70. private void StartTimer()
  71. {
  72. _toolRecievedData = true;
  73. _loopTimer.Enabled = true;
  74. }
  75. private void CloneBitmapIfToolIsShape()
  76. {
  77. if (SelectedTool.GetType().BaseType == typeof(ShapeTool))
  78. {
  79. _clonedBitmap = _layer.LayerBitmap.Clone();
  80. }
  81. }
  82. /// <summary>
  83. /// Executes tool action
  84. /// </summary>
  85. /// <param name="layer">Layer to operate on.</param>
  86. /// <param name="startingCoords">Click coordinates.</param>
  87. /// <param name="color">Color that tool will use.</param>
  88. /// <param name="toolSize">Size/thickness of tool</param>
  89. /// <param name="tool">Tool to execute</param>
  90. /// <returns></returns>
  91. public void ExecuteTool(Layer layer, Coordinates startingCoords, Color color, int toolSize)
  92. {
  93. if (toolSize < 1)
  94. return;
  95. if (_toolRecievedData == false || (_toolRecievedData == true && SelectedTool.GetType().BaseType != typeof(ShapeTool)))
  96. {
  97. _startCoordinates = startingCoords;
  98. _layer = layer;
  99. _color = color;
  100. _toolSzie = toolSize;
  101. }
  102. if (_loopTimer.Enabled == false)
  103. {
  104. StartTimer();
  105. CloneBitmapIfToolIsShape();
  106. }
  107. }
  108. }
  109. }