CommandCollection.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CommandCollection.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. //-----------------------------------------------------------------------------
  10. // CommandCollection.cs
  11. //
  12. // Microsoft XNA Community Game Platform
  13. // Copyright (C) Microsoft Corporation. All rights reserved.
  14. //-----------------------------------------------------------------------------
  15. #region Using Statements
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. #endregion
  20. namespace Xna.Tools
  21. {
  22. /// <summary>
  23. /// This class manages ICommands for Undo/Redo.
  24. /// Also, CommandQueue works like ICommand. So you can capture multiple
  25. /// commands as one command.
  26. /// </summary>
  27. public class CommandCollection : ICommand, ICollection<ICommand>
  28. {
  29. #region Properties
  30. /// <summary>
  31. /// It returns true if it can process undo; otherwise it returns false.
  32. /// </summary>
  33. public bool CanUndo { get { return commandIndex > 0; } }
  34. /// <summary>
  35. /// It returns true if it can process redo; otherwise it returns false.
  36. /// </summary>
  37. public bool CanRedo { get { return commandIndex < commands.Count; } }
  38. /// <summary>
  39. /// Return number of commands in this queue.
  40. /// </summary>
  41. public int Count { get { return commands.Count; } }
  42. /// <summary>
  43. /// Current command index.
  44. /// </summary>
  45. public int Index { get { return commandIndex; } }
  46. /// <summary>
  47. /// Gets the element at the specfied index.
  48. /// </summary>
  49. /// <param name="index">The zero-based index of the element to get.</param>
  50. /// <returns>The element at the specfied index.</</returns>
  51. public ICommand this[int index] { get { return commands[index]; } }
  52. #endregion
  53. #region Public Methods
  54. /// <summary>
  55. /// Add command to queue.
  56. /// </summary>
  57. /// <param name="command"></param>
  58. public void Add(ICommand item)
  59. {
  60. // Discard rest of commands from commandIndex.
  61. commands.RemoveRange(commandIndex, commands.Count - commandIndex);
  62. // Add command to commands.
  63. commands.Add(item);
  64. commandIndex = commands.Count;
  65. }
  66. /// <summary>
  67. /// Undo command.
  68. /// </summary>
  69. public bool Undo()
  70. {
  71. if (!CanUndo) return false;
  72. commands[--commandIndex].Unexecute();
  73. return true;
  74. }
  75. /// <summary>
  76. /// Redo command.
  77. /// </summary>
  78. public bool Redo()
  79. {
  80. if (!CanRedo) return false;
  81. commands[commandIndex++].Execute();
  82. return true;
  83. }
  84. #region ICommand Members
  85. public void Execute()
  86. {
  87. // Execute all commands.
  88. foreach (ICommand command in commands)
  89. command.Execute();
  90. }
  91. public void Unexecute()
  92. {
  93. // Unexecute all commands.
  94. for (int i = commands.Count - 1; i >= 0; --i)
  95. commands[i].Unexecute();
  96. }
  97. #endregion
  98. #endregion
  99. #region Protected members
  100. /// <summary>
  101. /// For store commands.
  102. /// </summary>
  103. List<ICommand> commands = new List<ICommand>();
  104. /// <summary>
  105. /// Current command index.
  106. /// </summary>
  107. int commandIndex;
  108. #endregion
  109. #region ICollection<ICommand> Members
  110. public void Clear()
  111. {
  112. commands.Clear();
  113. commandIndex = 0;
  114. }
  115. public bool Contains(ICommand item)
  116. {
  117. return commands.Contains(item);
  118. }
  119. public void CopyTo(ICommand[] array, int arrayIndex)
  120. {
  121. throw new NotImplementedException();
  122. }
  123. public bool IsReadOnly
  124. {
  125. get { return false; }
  126. }
  127. public bool Remove(ICommand item)
  128. {
  129. throw new NotImplementedException();
  130. }
  131. #endregion
  132. #region IEnumerable<ICommand> Members
  133. public IEnumerator<ICommand> GetEnumerator()
  134. {
  135. return commands.GetEnumerator();
  136. }
  137. #endregion
  138. #region IEnumerable Members
  139. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  140. {
  141. return commands.GetEnumerator();
  142. }
  143. #endregion
  144. }
  145. }