CompoundHistoryMemento.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Collections.Generic;
  2. namespace PaintDotNet.Measurement.HistoryMementos
  3. {
  4. /// <summary>
  5. /// Lets you combine multiple HistoryMementos that can be undon/redone
  6. /// in a single operation, and be referred to by one name.
  7. /// The actions will be undone in the reverse order they are given to
  8. /// the constructor via the actions array.
  9. /// You can use 'null' for a HistoryMemento and it will be ignored.
  10. /// </summary>
  11. public class CompoundHistoryMemento
  12. : HistoryMemento
  13. {
  14. private List<HistoryMemento> actions;
  15. protected override void OnFlush()
  16. {
  17. for (int i = 0; i < actions.Count; ++i)
  18. {
  19. if (actions[i] != null)
  20. {
  21. actions[i].Flush();
  22. }
  23. }
  24. }
  25. protected override HistoryMemento OnUndo()
  26. {
  27. List<HistoryMemento> redoActions = new List<HistoryMemento>(actions.Count);
  28. for (int i = 0; i < actions.Count; ++i)
  29. {
  30. HistoryMemento ha = actions[actions.Count - i - 1];
  31. HistoryMemento rha = null;
  32. if (ha != null)
  33. {
  34. rha = ha.PerformUndo();
  35. }
  36. redoActions.Add(rha);
  37. }
  38. CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, redoActions);
  39. return cha;
  40. }
  41. public void PushNewAction(HistoryMemento newHA)
  42. {
  43. actions.Add(newHA);
  44. }
  45. public CompoundHistoryMemento(string name, ImageResource image, List<HistoryMemento> actions)
  46. : base(name, image)
  47. {
  48. this.actions = new List<HistoryMemento>(actions);
  49. }
  50. public CompoundHistoryMemento(string name, ImageResource image, HistoryMemento[] actions)
  51. : base(name, image)
  52. {
  53. this.actions = new List<HistoryMemento>(actions);
  54. }
  55. public CompoundHistoryMemento(string name, ImageResource image)
  56. : this(name, image, new HistoryMemento[0])
  57. {
  58. }
  59. }
  60. }