Skip to content

Commit

Permalink
Ctrl+Y working for draw tools
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanR712 committed Mar 18, 2024
1 parent 5a6f574 commit 20f724c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/History/DrawModeStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
45 changes: 38 additions & 7 deletions src/TreeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ async function handleUndo(): Promise<void> {
}

async function handleRedo(): Promise<void> {
console.log("");
TreeContext.redoDrawStep();
}

//TODO: replace all of this with polymorphism -James
Expand Down

0 comments on commit 20f724c

Please sign in to comment.