using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SketchAssistantWPF { public class ActionHistory { //History of Actions taken List actionHistory; //The current position in the actionHistory Tuple currentAction; public ActionHistory() { actionHistory = new List(); currentAction = new Tuple(-1, null); AddNewAction(new SketchAction(SketchAction.ActionType.Start, -1)); } /// /// Resets the action history to its initial state. /// /// The new Last Action taken. public String Reset() { actionHistory.Clear(); currentAction = new Tuple(-1, null); return AddNewAction(new SketchAction(SketchAction.ActionType.Start, -1)); } /// /// Adds a new action to the action history. /// /// The newly added action. /// The message to be displayed public String AddNewAction(SketchAction newAction) { //The current Action is before the last action taken, delete everything after the current action. if (currentAction.Item1 < actionHistory.Count - 1) { actionHistory.RemoveRange(currentAction.Item1 + 1, actionHistory.Count - (currentAction.Item1 + 1)); } actionHistory.Add(newAction); currentAction = new Tuple(actionHistory.Count - 1, newAction); return UpdateStatusLabel(); } /// /// Changes the currentAction. /// /// If True, moves the current action back one slot, if False, moves it forward. /// The message to be displayed public String MoveAction(bool moveBack) { if (moveBack && CanUndo()) { currentAction = new Tuple(currentAction.Item1 - 1, actionHistory[currentAction.Item1 - 1]); } if (!moveBack && CanRedo()) { currentAction = new Tuple(currentAction.Item1 + 1, actionHistory[currentAction.Item1 + 1]); } return UpdateStatusLabel(); } /// /// Returns the current action. /// /// The current action. public SketchAction GetCurrentAction() { return currentAction.Item2; } /// /// Return whether or not an action can be undone. /// /// True if an action can be undone. public bool CanUndo() { if (currentAction.Item1 > 0) { return true; } else { return false; } } /// /// Return whether or not an action can be redone. /// /// True if an action can be redone. public bool CanRedo() { if (currentAction.Item1 < actionHistory.Count - 1) { return true; } else { return false; } } /// /// Returns whether or not the history is empty. /// /// true if the history is empty, otherwise false public bool IsEmpty() { if (actionHistory.Count == 1) { return true; } else { return false; } } /// /// Updates the status label if there is one given. /// /// The message to be displayed private String UpdateStatusLabel() { return "Last Action: " + currentAction.Item2.GetActionInformation(); } } }