ActionHistory.cs 3.7 KB

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