using System.Collections.Generic; namespace PaintDotNet.Measurement.HistoryMementos { /// /// Lets you combine multiple HistoryMementos that can be undon/redone /// in a single operation, and be referred to by one name. /// The actions will be undone in the reverse order they are given to /// the constructor via the actions array. /// You can use 'null' for a HistoryMemento and it will be ignored. /// public class CompoundHistoryMemento : HistoryMemento { private List actions; protected override void OnFlush() { for (int i = 0; i < actions.Count; ++i) { if (actions[i] != null) { actions[i].Flush(); } } } protected override HistoryMemento OnUndo() { List redoActions = new List(actions.Count); for (int i = 0; i < actions.Count; ++i) { HistoryMemento ha = actions[actions.Count - i - 1]; HistoryMemento rha = null; if (ha != null) { rha = ha.PerformUndo(); } redoActions.Add(rha); } CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, redoActions); return cha; } public void PushNewAction(HistoryMemento newHA) { actions.Add(newHA); } public CompoundHistoryMemento(string name, ImageResource image, List actions) : base(name, image) { this.actions = new List(actions); } public CompoundHistoryMemento(string name, ImageResource image, HistoryMemento[] actions) : base(name, image) { this.actions = new List(actions); } public CompoundHistoryMemento(string name, ImageResource image) : this(name, image, new HistoryMemento[0]) { } } }