Skip to content

Commit e4252a5

Browse files
author
Ryan Reilly
committed
Proof undo w/ buttons. Despaghettification soon
1 parent 21ef7e9 commit e4252a5

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

src/History/DrawModeStack.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ export class DrawModeStack {
2828
return poppedNode;
2929
}
3030

31-
public topple(indexToToppleFrom: number): void {
32-
this.history.splice(indexToToppleFrom, this.history.length - 1 - indexToToppleFrom);
33-
}
34-
3531
public clear(): void {
3632
this.history = [];
3733
}

src/Proof/ProofModeStack.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file Contains the ProofModeStack class, which models an undo/redo stack in Proof Mode.
3+
*
4+
* @author Ryan R
5+
*/
6+
7+
import {ProofNode} from "./ProofNode";
8+
9+
export class ProofModeStack {
10+
public history: ProofNode[];
11+
12+
public constructor() {
13+
this.history = [];
14+
}
15+
16+
public push(incomingMove: ProofNode): void {
17+
this.history.push(incomingMove);
18+
}
19+
20+
public pop(): ProofNode | null {
21+
if (this.history.length === 0) {
22+
return null;
23+
}
24+
25+
const poppedNode: ProofNode = this.history[this.history.length - 1];
26+
27+
this.history.splice(this.history.length - 1, 1);
28+
return poppedNode;
29+
}
30+
31+
public clear(): void {
32+
this.history = [];
33+
}
34+
35+
public peek(): ProofNode {
36+
return this.history.length !== 0 ? this.history[this.history.length - 1] : new ProofNode();
37+
}
38+
}

src/TreeContext.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {DrawModeMove} from "./History/DrawModeMove";
1010
import {DrawModeNode} from "./History/DrawModeNode";
1111
import {DrawModeStack} from "./History/DrawModeStack";
1212
import {ProofModeMove} from "./Proof/ProofModeMove";
13+
import {ProofModeStack} from "./Proof/ProofModeStack";
1314
import {ProofNode} from "./Proof/ProofNode";
1415
import {redrawProof, redrawTree} from "./SharedToolUtils/DrawUtils";
1516

@@ -52,7 +53,11 @@ export class TreeContext {
5253

5354
public static drawHistoryRedoStack: DrawModeStack = new DrawModeStack();
5455

55-
private static recentlyUndoneOrRedoneMove = false;
56+
private static recentlyUndoneOrRedoneDrawMove = false;
57+
58+
public static proofHistoryRedoStack: ProofModeStack = new ProofModeStack();
59+
60+
private static recentlyUndoneOrRedoneProofMove = false;
5661

5762
//The proof is a series of ProofNodes.
5863
public static proof: ProofNode[] = [];
@@ -75,9 +80,9 @@ export class TreeContext {
7580
* @param newlyAppliedStep Incoming DrawModeMove.
7681
*/
7782
public static pushToDrawStack(newlyAppliedStep: DrawModeMove): void {
78-
if (this.recentlyUndoneOrRedoneMove) {
83+
if (this.recentlyUndoneOrRedoneDrawMove) {
7984
this.drawHistoryRedoStack.clear();
80-
this.recentlyUndoneOrRedoneMove = false;
85+
this.recentlyUndoneOrRedoneDrawMove = false;
8186
}
8287
this.drawHistoryUndoStack.push(new DrawModeNode(this.tree, newlyAppliedStep));
8388
}
@@ -100,7 +105,7 @@ export class TreeContext {
100105

101106
redrawTree(this.tree);
102107

103-
this.recentlyUndoneOrRedoneMove = true;
108+
this.recentlyUndoneOrRedoneDrawMove = true;
104109
}
105110

106111
public static redoDrawStep(): void {
@@ -112,29 +117,48 @@ export class TreeContext {
112117

113118
this.drawHistoryUndoStack.push(mostRecentStep);
114119

115-
//i seem to have accidentally made the correct procedure by copy/pasting the incorrect name
116-
//here and below are all supposed to be drawHistoryRedoStack but that ruins the behavior
117120
this.tree = new AEGTree(this.drawHistoryUndoStack.peek().tree.sheet);
118121

119122
redrawTree(this.tree);
120123

121-
this.recentlyUndoneOrRedoneMove = true;
124+
this.recentlyUndoneOrRedoneDrawMove = true;
122125
}
123126

124127
public static undoProofStep(): void {
125128
if (this.proof.length <= 1) {
129+
this.clearProof();
126130
return;
127131
}
128132

129-
const stepToRemove: ProofNode = this.proof[this.proof.length - 1 - 1];
133+
const stepToRemove: ProofNode = this.proof[this.proof.length - 1];
134+
135+
deleteMostRecentButton();
130136

131-
this.tree = new AEGTree(stepToRemove.tree.sheet);
132-
this.proof.splice(this.proof.length - 1, 1)[0];
137+
this.proofHistoryRedoStack.push(stepToRemove);
138+
139+
this.proof.splice(this.proof.length - 1, 1)[0]; //.pop();
133140
stepBack(this.proof[this.proof.length - 1]);
134141
redrawProof();
142+
143+
this.recentlyUndoneOrRedoneProofMove = true;
135144
}
136145

137146
public static redoProofStep(): void {
147+
if (this.proofHistoryRedoStack.history.length === 0) {
148+
return;
149+
}
150+
151+
const mostRecentStep: ProofNode | null = this.proofHistoryRedoStack.pop();
152+
153+
if (mostRecentStep === null || this.proof[this.proof.length - 1] === null) {
154+
return;
155+
}
156+
157+
this.recentlyUndoneOrRedoneProofMove = false;
158+
159+
this.pushToProof(mostRecentStep);
160+
161+
stepBack(this.proof[this.proof.length - 1]);
138162
redrawProof();
139163
}
140164

@@ -159,6 +183,11 @@ export class TreeContext {
159183
* @param newStep Incoming ProofNode.
160184
*/
161185
public static pushToProof(newStep: ProofNode): void {
186+
if (this.recentlyUndoneOrRedoneProofMove) {
187+
this.proofHistoryRedoStack.clear();
188+
this.recentlyUndoneOrRedoneProofMove = false;
189+
}
190+
162191
if (newStep.appliedRule === ProofModeMove.PASTE_GRAPH) {
163192
this.proof.pop();
164193
document.getElementById("Row: 1")?.remove();

0 commit comments

Comments
 (0)