ActionHistory.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SketchAssistantWPF
  7. {
  8. public class ActionHistory
  9. {
  10. //History of Actions taken
  11. List<SketchAction> actionHistory;
  12. //The current position in the actionHistory
  13. Tuple<int, SketchAction> currentAction;
  14. public ActionHistory()
  15. {
  16. actionHistory = new List<SketchAction>();
  17. currentAction = new Tuple<int, SketchAction>(-1, null);
  18. AddNewAction(new SketchAction(SketchAction.ActionType.Start, -1));
  19. }
  20. /// <summary>
  21. /// Resets the action history to its initial state.
  22. /// </summary>
  23. /// <returns>The new Last Action taken.</returns>
  24. public String Reset()
  25. {
  26. actionHistory.Clear();
  27. currentAction = new Tuple<int, SketchAction>(-1, null);
  28. return AddNewAction(new SketchAction(SketchAction.ActionType.Start, -1));
  29. }
  30. /// <summary>
  31. /// Adds a new action to the action history.
  32. /// </summary>
  33. /// <param name="newAction">The newly added action.</param>
  34. /// <returns>The message to be displayed</returns>
  35. public String AddNewAction(SketchAction newAction)
  36. {
  37. //The current Action is before the last action taken, delete everything after the current action.
  38. if (currentAction.Item1 < actionHistory.Count - 1)
  39. {
  40. actionHistory.RemoveRange(currentAction.Item1 + 1, actionHistory.Count - (currentAction.Item1 + 1));
  41. }
  42. actionHistory.Add(newAction);
  43. currentAction = new Tuple<int, SketchAction>(actionHistory.Count - 1, newAction);
  44. return UpdateStatusLabel();
  45. }
  46. /// <summary>
  47. /// Changes the currentAction.
  48. /// </summary>
  49. /// <param name="moveBack">If True, moves the current action back one slot, if False, moves it forward.</param>
  50. /// <returns>The message to be displayed</returns>
  51. public String MoveAction(bool moveBack)
  52. {
  53. if (moveBack && CanUndo())
  54. {
  55. currentAction = new Tuple<int, SketchAction>(currentAction.Item1 - 1, actionHistory[currentAction.Item1 - 1]);
  56. }
  57. if (!moveBack && CanRedo())
  58. {
  59. currentAction = new Tuple<int, SketchAction>(currentAction.Item1 + 1, actionHistory[currentAction.Item1 + 1]);
  60. }
  61. return UpdateStatusLabel();
  62. }
  63. /// <summary>
  64. /// Returns the current action.
  65. /// </summary>
  66. /// <returns>The current action.</returns>
  67. public SketchAction GetCurrentAction()
  68. {
  69. return currentAction.Item2;
  70. }
  71. /// <summary>
  72. /// Return whether or not an action can be undone.
  73. /// </summary>
  74. /// <returns>True if an action can be undone.</returns>
  75. public bool CanUndo()
  76. {
  77. if (currentAction.Item1 > 0) { return true; }
  78. else { return false; }
  79. }
  80. /// <summary>
  81. /// Return whether or not an action can be redone.
  82. /// </summary>
  83. /// <returns>True if an action can be redone.</returns>
  84. public bool CanRedo()
  85. {
  86. if (currentAction.Item1 < actionHistory.Count - 1) { return true; }
  87. else { return false; }
  88. }
  89. /// <summary>
  90. /// Returns whether or not the history is empty.
  91. /// </summary>
  92. /// <returns>true if the history is empty, otherwise false</returns>
  93. public bool IsEmpty()
  94. {
  95. if (actionHistory.Count == 1) { return true; }
  96. else { return false; }
  97. }
  98. /// <summary>
  99. /// Updates the status label if there is one given.
  100. /// </summary>
  101. /// <returns>The message to be displayed</returns>
  102. private String UpdateStatusLabel()
  103. {
  104. return "Last Action: " + currentAction.Item2.GetActionInformation();
  105. }
  106. }
  107. }