SwapLayerHistoryMemento.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System;
  10. namespace PaintDotNet.Measurement.HistoryMementos
  11. {
  12. public class SwapLayerHistoryMemento
  13. : HistoryMemento
  14. {
  15. private int layerIndex1;
  16. private int layerIndex2;
  17. private IDocumentWorkspace historyWorkspace;
  18. protected override HistoryMemento OnUndo()
  19. {
  20. SwapLayerHistoryMemento slha = new SwapLayerHistoryMemento(this.Name, this.Image,
  21. this.historyWorkspace, this.layerIndex2, this.layerIndex1);
  22. Layer layer1 = (Layer)this.historyWorkspace.GetDocument().Layers[this.layerIndex1];
  23. Layer layer2 = (Layer)this.historyWorkspace.GetDocument().Layers[this.layerIndex2];
  24. int firstIndex = Math.Min(layerIndex1, layerIndex2);
  25. int secondIndex = Math.Max(layerIndex1, layerIndex2);
  26. if (secondIndex - firstIndex == 1)
  27. {
  28. this.historyWorkspace.GetDocument().Layers.RemoveAt(layerIndex1);
  29. this.historyWorkspace.GetDocument().Layers.Insert(layerIndex2, layer1);
  30. }
  31. else
  32. {
  33. // general version
  34. this.historyWorkspace.GetDocument().Layers[layerIndex1] = layer2;
  35. this.historyWorkspace.GetDocument().Layers[layerIndex2] = layer1;
  36. }
  37. ((Layer)this.historyWorkspace.GetDocument().Layers[this.layerIndex1]).Invalidate();
  38. ((Layer)this.historyWorkspace.GetDocument().Layers[this.layerIndex2]).Invalidate();
  39. return slha;
  40. }
  41. public SwapLayerHistoryMemento(string name, ImageResource image, IDocumentWorkspace historyWorkspace, int layerIndex1, int layerIndex2)
  42. : base(name, image)
  43. {
  44. this.historyWorkspace = historyWorkspace;
  45. this.layerIndex1 = layerIndex1;
  46. this.layerIndex2 = layerIndex2;
  47. if (this.layerIndex1 < 0 || this.layerIndex2 < 0 ||
  48. this.layerIndex1 >= this.historyWorkspace.GetDocument().Layers.Count ||
  49. this.layerIndex2 >= this.historyWorkspace.GetDocument().Layers.Count)
  50. {
  51. throw new ArgumentOutOfRangeException("layerIndex[1|2]", "out of range");
  52. }
  53. }
  54. }
  55. }