From 20f724c59843caec982981ec7434e76454458835 Mon Sep 17 00:00:00 2001 From: Ryan Reilly Date: Sun, 17 Mar 2024 21:56:39 -0400 Subject: [PATCH] Ctrl+Y working for draw tools --- src/History/DrawModeStack.ts | 8 +++++++ src/TreeContext.ts | 45 ++++++++++++++++++++++++++++++------ src/index.ts | 2 +- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/History/DrawModeStack.ts b/src/History/DrawModeStack.ts index bab308bb..f3926034 100644 --- a/src/History/DrawModeStack.ts +++ b/src/History/DrawModeStack.ts @@ -28,6 +28,14 @@ export class DrawModeStack { return poppedNode; } + public topple(indexToToppleFrom: number): void { + this.history.splice(indexToToppleFrom, this.history.length - 1 - indexToToppleFrom); + } + + public clear(): void { + this.history = []; + } + public peek(): DrawModeNode { return this.history.length !== 0 ? this.history[this.history.length - 1] diff --git a/src/TreeContext.ts b/src/TreeContext.ts index 6bf6817e..c4bed11e 100644 --- a/src/TreeContext.ts +++ b/src/TreeContext.ts @@ -47,8 +47,12 @@ export class TreeContext { //Current AEGTree on canvas. public static tree: AEGTree = new AEGTree(); - //For undoing and redoing changes in Draw Mode. - public static drawHistory: DrawModeStack = new DrawModeStack(); + //For undoing changes in Draw Mode. + public static drawHistoryUndoStack: DrawModeStack = new DrawModeStack(); + + public static drawHistoryRedoStack: DrawModeStack = new DrawModeStack(); + + private static recentlyUndoneOrRedoneMove = false; //The proof is a series of ProofNodes. public static proof: ProofNode[] = []; @@ -71,29 +75,56 @@ export class TreeContext { * @param newlyAppliedStep Incoming DrawModeMove. */ public static pushToDrawStack(newlyAppliedStep: DrawModeMove): void { - this.drawHistory.push(new DrawModeNode(this.tree, newlyAppliedStep)); + if (this.recentlyUndoneOrRedoneMove) { + this.drawHistoryRedoStack.clear(); + } + this.drawHistoryUndoStack.push(new DrawModeNode(this.tree, newlyAppliedStep)); } /** * Pops the most recent draw mode move from drawHistory and changes tree accordingly. */ public static undoDrawStep(): void { - const mostRecentStep: DrawModeNode | null = this.drawHistory.pop(); + const mostRecentStep: DrawModeNode | null = this.drawHistoryUndoStack.pop(); let newTree: AEGTree; - if (mostRecentStep === null || this.drawHistory.peek() === null) { + if (mostRecentStep === null || this.drawHistoryUndoStack.peek() === null) { newTree = new AEGTree(); this.tree = newTree; redrawTree(newTree); return; } - this.tree = new AEGTree(this.drawHistory.peek().tree.sheet); + this.drawHistoryRedoStack.push(mostRecentStep); + + this.tree = new AEGTree(this.drawHistoryUndoStack.peek().tree.sheet); + + newTree = new AEGTree(this.drawHistoryUndoStack.peek().tree.sheet); + + redrawTree(newTree); + + this.recentlyUndoneOrRedoneMove = true; + } + + public static redoDrawStep(): void { + const mostRecentStep: DrawModeNode | null = this.drawHistoryRedoStack.pop(); + + if (mostRecentStep === null || this.drawHistoryUndoStack.peek() === null) { + return; + } + + this.drawHistoryUndoStack.push(mostRecentStep); - newTree = new AEGTree(this.drawHistory.peek().tree.sheet); + //i seem to have accidentally made the correct procedure by copy/pasting the incorrect name + //here and below are all supposed to be drawHistoryRedoStack but that ruins the behavior + this.tree = new AEGTree(this.drawHistoryUndoStack.peek().tree.sheet); + + const newTree: AEGTree = new AEGTree(this.drawHistoryUndoStack.peek().tree.sheet); redrawTree(newTree); + + this.recentlyUndoneOrRedoneMove = true; } /** diff --git a/src/index.ts b/src/index.ts index bf209f0b..67642794 100644 --- a/src/index.ts +++ b/src/index.ts @@ -316,7 +316,7 @@ async function handleUndo(): Promise { } async function handleRedo(): Promise { - console.log(""); + TreeContext.redoDrawStep(); } //TODO: replace all of this with polymorphism -James