FlipLayerHistoryMemento.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using PaintDotNet.Measurement.Enum;
  2. using System;
  3. namespace PaintDotNet.Measurement.HistoryMementos
  4. {
  5. public class FlipLayerHistoryMemento
  6. : HistoryMemento
  7. {
  8. private IDocumentWorkspace historyWorkspace;
  9. private int layerIndex;
  10. private FlipType flipType;
  11. private void Flip(Surface surface)
  12. {
  13. switch (this.flipType)
  14. {
  15. case FlipType.Horizontal:
  16. for (int y = 0; y < surface.Height; ++y)
  17. {
  18. for (int x = 0; x < surface.Width / 2; ++x)
  19. {
  20. ColorBgra temp = surface[x, y];
  21. surface[x, y] = surface[surface.Width - x - 1, y];
  22. surface[surface.Width - x - 1, y] = temp;
  23. }
  24. }
  25. break;
  26. case FlipType.Vertical:
  27. for (int x = 0; x < surface.Width; ++x)
  28. {
  29. for (int y = 0; y < surface.Height / 2; ++y)
  30. {
  31. ColorBgra temp = surface[x, y];
  32. surface[x, y] = surface[x, surface.Height - y - 1];
  33. surface[x, surface.Height - y - 1] = temp;
  34. }
  35. }
  36. break;
  37. case FlipType.Center:
  38. for (int y = 0; y < surface.Height; ++y)
  39. {
  40. for (int x = 0; x < surface.Width / 2; ++x)
  41. {
  42. ColorBgra temp = surface[x, y];
  43. surface[x, y] = surface[surface.Width - x - 1, surface.Height - y - 1];
  44. surface[surface.Width - x - 1, surface.Height - y - 1] = temp;
  45. }
  46. }
  47. break;
  48. default:
  49. throw new InvalidOperationException("FlipType was invalid");
  50. }
  51. return;
  52. }
  53. protected override HistoryMemento OnUndo()
  54. {
  55. FlipLayerHistoryMemento fha = new FlipLayerHistoryMemento(this.Name, this.Image,
  56. this.historyWorkspace, layerIndex, flipType);
  57. BitmapLayer layer = (BitmapLayer)this.historyWorkspace.GetDocument().Layers[layerIndex];
  58. Flip(layer.Surface);
  59. layer.Invalidate();
  60. return fha;
  61. }
  62. public FlipLayerHistoryMemento(string name, ImageResource image, IDocumentWorkspace historyWorkspace, int layerIndex, FlipType flipType)
  63. : base(name, image)
  64. {
  65. this.historyWorkspace = historyWorkspace;
  66. this.layerIndex = layerIndex;
  67. this.flipType = flipType;
  68. }
  69. }
  70. }