ToolsManager.cs 3.7 KB

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