SwapLayerFunction.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using PaintDotNet.Measurement.HistoryMementos;
  2. using System;
  3. namespace PaintDotNet.Measurement.HistoryFunctions
  4. {
  5. public sealed class SwapLayerFunction
  6. : HistoryFunction
  7. {
  8. private int layer1Index;
  9. private int layer2Index;
  10. public static string StaticName
  11. {
  12. get
  13. {
  14. return PdnResources.GetString("SwapLayerFunction.Name");
  15. }
  16. }
  17. public static ImageResource StaticImage
  18. {
  19. get
  20. {
  21. // TODO: find a real icon for this?
  22. //return PdnResources.GetImageResource("todo.png");
  23. return PdnResources.GetImageResource("Icons.MenuLayersMoveLayerUpIcon.png");
  24. }
  25. }
  26. public override HistoryMemento OnExecute(IDocumentWorkspace historyWorkspace)
  27. {
  28. if (layer1Index < 0 || layer1Index >= historyWorkspace.GetDocument().Layers.Count ||
  29. layer2Index < 0 || layer2Index >= historyWorkspace.GetDocument().Layers.Count)
  30. {
  31. throw new ArgumentOutOfRangeException("layer1Index = " + this.layer1Index + ", layer2Index = " + layer2Index + ", expected [0," + historyWorkspace.GetDocument().Layers.Count + ")");
  32. }
  33. SwapLayerHistoryMemento slhm = new SwapLayerHistoryMemento(
  34. StaticName,
  35. StaticImage,
  36. historyWorkspace,
  37. layer1Index,
  38. layer2Index);
  39. Layer layer1 = historyWorkspace.GetDocument().Layers.GetAt(layer1Index);
  40. Layer layer2 = historyWorkspace.GetDocument().Layers.GetAt(layer2Index);
  41. EnterCriticalRegion();
  42. historyWorkspace.GetDocument().Layers[layer1Index] = layer2;
  43. historyWorkspace.GetDocument().Layers[layer2Index] = layer1;
  44. layer1.Invalidate();
  45. layer2.Invalidate();
  46. return slhm;
  47. }
  48. public SwapLayerFunction(int layer1Index, int layer2Index)
  49. : base(PaintDotNet.Measurement.Enum.ActionFlags.None)
  50. {
  51. this.layer1Index = layer1Index;
  52. this.layer2Index = layer2Index;
  53. }
  54. }
  55. }