ToolsManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. _layer.LayerBitmap.Clear();
  46. _layer.LayerBitmap.Blit(new Rect(new Size(_layer.Width, _layer.Height)), _clonedBitmap, new Rect(new Size(_layer.Width, _layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
  47. }
  48. BitmapPixelChanges changes = SelectedTool.Use(_layer, _startCoordinates, _color, _toolSzie);
  49. if (!SelectedTool.ExecutesItself)
  50. {
  51. _layer.ApplyPixels(changes, changes.PixelsColor);
  52. }
  53. _layer.LayerBitmap.Unlock();
  54. });
  55. }
  56. public void SetTool(ToolType tool)
  57. {
  58. SelectedTool = Tools.Find(x => x.ToolType == tool);
  59. }
  60. public void StopExectuingTool()
  61. {
  62. _loopTimer.Enabled = false;
  63. _toolRecievedData = false;
  64. _clonedBitmap = null;
  65. }
  66. private void StartTimer()
  67. {
  68. _toolRecievedData = true;
  69. _loopTimer.Enabled = true;
  70. }
  71. private void CloneBitmapIfToolIsShape()
  72. {
  73. if (SelectedTool.GetType().BaseType == typeof(ShapeTool))
  74. {
  75. _clonedBitmap = _layer.LayerBitmap.Clone();
  76. }
  77. }
  78. /// <summary>
  79. /// Executes tool action
  80. /// </summary>
  81. /// <param name="layer">Layer to operate on.</param>
  82. /// <param name="startingCoords">Click coordinates.</param>
  83. /// <param name="color">Color that tool will use.</param>
  84. /// <param name="toolSize">Size/thickness of tool</param>
  85. /// <param name="tool">Tool to execute</param>
  86. /// <returns></returns>
  87. public void ExecuteTool(Layer layer, Coordinates startingCoords, Color color, int toolSize)
  88. {
  89. if (toolSize < 1)
  90. return;
  91. if(_toolRecievedData == false || (_toolRecievedData == true && SelectedTool.GetType().BaseType != typeof(ShapeTool)))
  92. {
  93. _startCoordinates = startingCoords;
  94. _layer = layer;
  95. _color = color;
  96. _toolSzie = toolSize;
  97. }
  98. if (_loopTimer.Enabled == false)
  99. {
  100. StartTimer();
  101. CloneBitmapIfToolIsShape();
  102. }
  103. }
  104. }
  105. }