ActionHistory.cs 3.6 KB

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