2
0

Layer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using PixiEditor.Models.Tools;
  2. using System;
  3. using System.Windows.Media.Imaging;
  4. namespace PixiEditor.Models.Layers
  5. {
  6. public class Layer : BasicLayer
  7. {
  8. private WriteableBitmap _layerBitmap;
  9. private string _name;
  10. public string Name
  11. {
  12. get { return _name; }
  13. set
  14. {
  15. _name = value;
  16. RaisePropertyChanged("Name");
  17. }
  18. }
  19. private bool _isActive = false;
  20. public bool IsActive
  21. {
  22. get => _isActive;
  23. set
  24. {
  25. _isActive = value;
  26. RaisePropertyChanged("IsActive");
  27. }
  28. }
  29. private bool _isVisible = true;
  30. public bool IsVisible
  31. {
  32. get => _isVisible;
  33. set
  34. {
  35. _isVisible = value;
  36. RaisePropertyChanged("IsVisible");
  37. }
  38. }
  39. private bool _isRenaming = false;
  40. public bool IsRenaming
  41. {
  42. get { return _isRenaming; }
  43. set
  44. {
  45. _isRenaming = value;
  46. RaisePropertyChanged("IsRenaming");
  47. }
  48. }
  49. public WriteableBitmap LayerBitmap
  50. {
  51. get => _layerBitmap;
  52. set
  53. {
  54. _layerBitmap = value;
  55. RaisePropertyChanged("LayerBitmap");
  56. }
  57. }
  58. public Layer(string name, int width, int height)
  59. {
  60. Name = name;
  61. Layer layer = LayerGenerator.Generate(width, height);
  62. LayerBitmap = layer.LayerBitmap;
  63. Width = width;
  64. Height = height;
  65. }
  66. public Layer(WriteableBitmap layerBitmap)
  67. {
  68. LayerBitmap = layerBitmap;
  69. Width = (int)layerBitmap.Width;
  70. Height = (int)layerBitmap.Height;
  71. }
  72. public void ApplyPixels(BitmapPixelChanges pixels)
  73. {
  74. LayerBitmap.Lock();
  75. foreach (var coords in pixels.ChangedPixels)
  76. {
  77. if (coords.Key.X > Width - 1 || coords.Key.X < 0 || coords.Key.Y < 0 || coords.Key.Y > Height - 1) continue;
  78. LayerBitmap.SetPixel(Math.Clamp(coords.Key.X, 0, Width - 1), Math.Clamp(coords.Key.Y, 0, Height - 1),
  79. coords.Value);
  80. }
  81. LayerBitmap.Unlock();
  82. }
  83. public void Clear()
  84. {
  85. LayerBitmap.Lock();
  86. LayerBitmap.Clear();
  87. LayerBitmap.Unlock();
  88. }
  89. public byte[] ConvertBitmapToBytes()
  90. {
  91. LayerBitmap.Lock();
  92. byte[] byteArray = LayerBitmap.ToByteArray();
  93. LayerBitmap.Unlock();
  94. return byteArray;
  95. }
  96. public byte[] ConvertBitmapToBytes(WriteableBitmap bitmap)
  97. {
  98. bitmap.Lock();
  99. byte[] byteArray = bitmap.ToByteArray();
  100. bitmap.Unlock();
  101. return byteArray;
  102. }
  103. public void Resize(int newWidth, int newHeight)
  104. {
  105. LayerBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
  106. Height = newHeight;
  107. Width = newWidth;
  108. }
  109. }
  110. }