ToolsManager.cs 3.6 KB

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