ActionHistory.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /// Adds a new action to the action history.
  22. /// </summary>
  23. /// <param name="newAction">The newly added action.</param>
  24. /// <returns>The message to be displayed</returns>
  25. public String AddNewAction(SketchAction newAction)
  26. {
  27. //The current Action is before the last action taken, delete everything after the current action.
  28. if (currentAction.Item1 < actionHistory.Count - 1)
  29. {
  30. actionHistory.RemoveRange(currentAction.Item1 + 1, actionHistory.Count - (currentAction.Item1 + 1));
  31. }
  32. actionHistory.Add(newAction);
  33. currentAction = new Tuple<int, SketchAction>(actionHistory.Count - 1, newAction);
  34. return UpdateStatusLabel();
  35. }
  36. /// <summary>
  37. /// Changes the currentAction.
  38. /// </summary>
  39. /// <param name="moveBack">If True, moves the current action back one slot, if False, moves it forward.</param>
  40. /// <returns>The message to be displayed</returns>
  41. public String MoveAction(bool moveBack)
  42. {
  43. if (moveBack && CanUndo())
  44. {
  45. currentAction = new Tuple<int, SketchAction>(currentAction.Item1 - 1, actionHistory[currentAction.Item1 - 1]);
  46. }
  47. if (!moveBack && CanRedo())
  48. {
  49. currentAction = new Tuple<int, SketchAction>(currentAction.Item1 + 1, actionHistory[currentAction.Item1 + 1]);
  50. }
  51. return UpdateStatusLabel();
  52. }
  53. /// <summary>
  54. /// Returns the current action.
  55. /// </summary>
  56. /// <returns>The current action.</returns>
  57. public SketchAction GetCurrentAction()
  58. {
  59. return currentAction.Item2;
  60. }
  61. /// <summary>
  62. /// Return whether or not an action can be undone.
  63. /// </summary>
  64. /// <returns>True if an action can be undone.</returns>
  65. public bool CanUndo()
  66. {
  67. if (currentAction.Item1 > 0) { return true; }
  68. else { return false; }
  69. }
  70. /// <summary>
  71. /// Return whether or not an action can be redone.
  72. /// </summary>
  73. /// <returns>True if an action can be redone.</returns>
  74. public bool CanRedo()
  75. {
  76. if (currentAction.Item1 < actionHistory.Count - 1) { return true; }
  77. else { return false; }
  78. }
  79. /// <summary>
  80. /// Returns whether or not the history is empty.
  81. /// </summary>
  82. /// <returns>true if the history is empty, otherwise false</returns>
  83. public bool IsEmpty()
  84. {
  85. if (actionHistory.Count == 1) { return true; }
  86. else { return false; }
  87. }
  88. /// <summary>
  89. /// Updates the status label if there is one given.
  90. /// </summary>
  91. /// <returns>The message to be displayed</returns>
  92. private String UpdateStatusLabel()
  93. {
  94. return "Last Action: " + currentAction.Item2.GetActionInformation();
  95. }
  96. }
  97. }