#region File Description //----------------------------------------------------------------------------- // CommandCollection.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion //----------------------------------------------------------------------------- // CommandCollection.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #region Using Statements using System; using System.Collections.Generic; using System.Text; #endregion namespace Xna.Tools { /// /// This class manages ICommands for Undo/Redo. /// Also, CommandQueue works like ICommand. So you can capture multiple /// commands as one command. /// public class CommandCollection : ICommand, ICollection { #region Properties /// /// It returns true if it can process undo; otherwise it returns false. /// public bool CanUndo { get { return commandIndex > 0; } } /// /// It returns true if it can process redo; otherwise it returns false. /// public bool CanRedo { get { return commandIndex < commands.Count; } } /// /// Return number of commands in this queue. /// public int Count { get { return commands.Count; } } /// /// Current command index. /// public int Index { get { return commandIndex; } } /// /// Gets the element at the specfied index. /// /// The zero-based index of the element to get. /// The element at the specfied index. public ICommand this[int index] { get { return commands[index]; } } #endregion #region Public Methods /// /// Add command to queue. /// /// public void Add(ICommand item) { // Discard rest of commands from commandIndex. commands.RemoveRange(commandIndex, commands.Count - commandIndex); // Add command to commands. commands.Add(item); commandIndex = commands.Count; } /// /// Undo command. /// public bool Undo() { if (!CanUndo) return false; commands[--commandIndex].Unexecute(); return true; } /// /// Redo command. /// public bool Redo() { if (!CanRedo) return false; commands[commandIndex++].Execute(); return true; } #region ICommand Members public void Execute() { // Execute all commands. foreach (ICommand command in commands) command.Execute(); } public void Unexecute() { // Unexecute all commands. for (int i = commands.Count - 1; i >= 0; --i) commands[i].Unexecute(); } #endregion #endregion #region Protected members /// /// For store commands. /// List commands = new List(); /// /// Current command index. /// int commandIndex; #endregion #region ICollection Members public void Clear() { commands.Clear(); commandIndex = 0; } public bool Contains(ICommand item) { return commands.Contains(item); } public void CopyTo(ICommand[] array, int arrayIndex) { throw new NotImplementedException(); } public bool IsReadOnly { get { return false; } } public bool Remove(ICommand item) { throw new NotImplementedException(); } #endregion #region IEnumerable Members public IEnumerator GetEnumerator() { return commands.GetEnumerator(); } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return commands.GetEnumerator(); } #endregion } }