protected bool ExecuteActionInternal(IUndoableAction action, bool clearRedoStack) { OnBeforeStateChanged(); if (!action.Execute()) { return(false); } if (clearRedoStack) { redoStack.Clear(); } var undoGroup = action.GetUndoGroup(); // Should we group together this undo step with more of the same kind? if (undoGroup != 0) { if (currentGroup == null || currentGroup.CaughtGroup != undoGroup) { currentGroup = new UndoableActionGroup(undoGroup); AddToUndoStack(currentGroup); } currentGroup.Actions.Add(action); } else { AddToUndoStack(action); } OnAfterStateChanged(); return(true); }
/// <summary> /// Force the current undo group to stop. For example, if you're painting some tiles and /// stop for a moment, you might want to split the undo group at that point in order /// to separate undo steps by stroke. Call this function to do so. /// </summary> /// <param name="group">Id of the group we want to end.</param> public void EndUndoGroup(int group) { if (currentGroup != null && currentGroup.CaughtGroup == group) { currentGroup = null; } }
/// <summary> /// Clear entire undo state. /// </summary> public void Clear() { OnBeforeStateChanged(); currentGroup = null; undoStack.Clear(); redoStack.Clear(); OnAfterStateChanged(); }
/// <summary> /// Redo a single action. /// </summary> public void Redo() { currentGroup = null; if (redoStack.Count > 0) { var action = redoStack[redoStack.Count - 1]; redoStack.RemoveAt(redoStack.Count - 1); ExecuteActionInternal(action, false); OnRedoEvent(action); } }
/// <summary> /// Undo a single action. /// </summary> public void Undo() { currentGroup = null; if (undoStack.Count > 0) { OnBeforeStateChanged(); var action = undoStack[undoStack.Count - 1]; undoStack.RemoveAt(undoStack.Count - 1); action.Undo(); redoStack.Add(action); OnUndoEvent(action); OnAfterStateChanged(); } }