From e6a2740ddb3c866a8b993cc5293b8f9c7a9b6567 Mon Sep 17 00:00:00 2001 From: DawnTheWitch Date: Sun, 19 Nov 2023 12:57:17 -0500 Subject: [PATCH 1/6] Working Iteration, no highlighting because that was a bitch --- src/DrawModes/DrawUtils.ts | 2 +- src/ProofTools/IterationTool.ts | 132 ++++++++++++++++++++++++++++++++ src/index.html | 2 +- src/index.ts | 21 ++++- src/treeContext.ts | 1 + 5 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/ProofTools/IterationTool.ts diff --git a/src/DrawModes/DrawUtils.ts b/src/DrawModes/DrawUtils.ts index 1ec6d45d..90bcea50 100644 --- a/src/DrawModes/DrawUtils.ts +++ b/src/DrawModes/DrawUtils.ts @@ -140,6 +140,6 @@ function redrawCut(incomingNode: CutNode) { * @param incomingNode The Atom Node to be redrawn * @param offset The difference between the actual graph and the current canvas */ -function redrawAtom(incomingNode: AtomNode) { +export function redrawAtom(incomingNode: AtomNode) { drawAtom(incomingNode, placedColor(), false); } diff --git a/src/ProofTools/IterationTool.ts b/src/ProofTools/IterationTool.ts new file mode 100644 index 00000000..c64ba645 --- /dev/null +++ b/src/ProofTools/IterationTool.ts @@ -0,0 +1,132 @@ +/** + * A tool used to iterated subgraphs on the AEG + * @author + */ + +import {Point} from "../AEG/Point"; +import {AtomNode} from "../AEG/AtomNode"; +import {CutNode} from "../AEG/CutNode"; +import {treeContext} from "../treeContext"; +import {offset} from "../DrawModes/DragTool"; +import {drawAtom, redrawTree} from "../DrawModes/DrawUtils"; +import {legalColor, illegalColor} from "../Themes"; +import {validateChildren, drawAltered, insertChildren, alterAtom} from "../DrawModes/EditModeUtils"; + +//The initial point the user pressed down. +let startingPoint: Point; + +//The current node and its children we will be moving. +let currentNode: CutNode | AtomNode | null = null; + +//The parent of the currentNode. +let currentParent: CutNode | null = null; + +//Whether or not the node is allowed to be moved (not the sheet). +let legalNode: boolean; + +/** + * Sets the starting point for future use as well as obtaining the lowest node containing this point. + * Also obtains the parent of this lowest point for future use as well. + * @param event The mouse down event while using the iteration tool + */ +export function iterationMouseDown(event: MouseEvent) { + startingPoint = new Point(event.x - offset.x, event.y - offset.y); + currentNode = treeContext.tree.getLowestNode(startingPoint); + currentParent = treeContext.tree.getLowestParent(startingPoint); + + //So long as we have obtained a node that isn't the sheet we are allowed to select this. + if (currentNode !== treeContext.tree.sheet && currentNode !== null) { + legalNode = true; + } else { + legalNode = false; + } +} + +/** + * If we have selected a legal node then determines how far it has moved from the original node. + * If it is a cut node determines if this new position is legal for all of its children and draws + * that in its correct color. If it's an atom it just checks to see if that is in the correct position. + * @param event The mouse move event while using the iteration tool + */ +export function iterationMouseMove(event: MouseEvent) { + if (legalNode) { + const moveDifference: Point = new Point( + event.x - startingPoint.x, + event.y - startingPoint.y + ); + + redrawTree(treeContext.tree); + if (currentNode instanceof CutNode) { + const color = isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y)) + ? legalColor() + : illegalColor(); + drawAltered(currentNode, color, moveDifference); + } else if (currentNode instanceof AtomNode) { + const tempAtom: AtomNode = alterAtom(currentNode, moveDifference); + const color = isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y)) + ? legalColor() + : illegalColor(); + drawAtom(tempAtom, color, true); + } + } +} + +/** + * Checks to see if the node and any of its children are illegal in the new position. If any are + * illegal does not insert any of them. Redraws the canvas to update this. + * @param event The mouse out event while using the iteration tool + */ +export function iterationMouseUp(event: MouseEvent) { + if (legalNode) { + const moveDifference: Point = new Point( + event.x - startingPoint.x, + event.y - startingPoint.y + ); + + if (currentNode instanceof CutNode) { + if (isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y))) { + insertChildren(currentNode, moveDifference); + } + } else if (currentNode instanceof AtomNode) { + const tempAtom: AtomNode = alterAtom(currentNode, moveDifference); + + if (isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y))) { + treeContext.tree.insert(tempAtom); + } + } + } + redrawTree(treeContext.tree); + legalNode = false; +} + +/** + * If the mouse has left the canvas then assume it is now illegal and reset the tree. + */ +export function iterationMouseOut() { + legalNode = false; + redrawTree(treeContext.tree); +} + +/** + * If the current point is legally iterated (within the parent of the original node) + * checks if the altered nodes can be inserted, if all of them can returns true. + * @param moveDifference The change from the currentNodes original position + * @param currentPoint The current point on the graph the mouse is + * @returns Whether or not this location is legal + */ +function isLegal(moveDifference: Point, currentPoint: Point): boolean { + if (currentParent !== null && currentParent.containsPoint(currentPoint)) { + if (currentNode instanceof CutNode && validateChildren(currentNode, moveDifference)) { + return true; + } else if ( + currentNode instanceof AtomNode && + treeContext.tree.canInsert(alterAtom(currentNode, moveDifference)) + ) { + return true; + } else { + return false; + } + } + + return false; +} diff --git a/src/index.html b/src/index.html index 84fe6f88..ff2705b0 100644 --- a/src/index.html +++ b/src/index.html @@ -115,7 +115,7 @@
-
From 74cfa2d66476758b39c985573068ad86ac2dcf4b Mon Sep 17 00:00:00 2001 From: DawnTheWitch Date: Thu, 23 Nov 2023 20:00:01 -0500 Subject: [PATCH 3/6] Working iteration I am 90% sure this time --- src/ProofTools/IterationTool.ts | 45 +++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/ProofTools/IterationTool.ts b/src/ProofTools/IterationTool.ts index c64ba645..8965ddfe 100644 --- a/src/ProofTools/IterationTool.ts +++ b/src/ProofTools/IterationTool.ts @@ -4,13 +4,20 @@ */ import {Point} from "../AEG/Point"; +import {Ellipse} from "../AEG/Ellipse"; import {AtomNode} from "../AEG/AtomNode"; import {CutNode} from "../AEG/CutNode"; import {treeContext} from "../treeContext"; import {offset} from "../DrawModes/DragTool"; import {drawAtom, redrawTree} from "../DrawModes/DrawUtils"; import {legalColor, illegalColor} from "../Themes"; -import {validateChildren, drawAltered, insertChildren, alterAtom} from "../DrawModes/EditModeUtils"; +import { + validateChildren, + drawAltered, + insertChildren, + alterAtom, + alterCut, +} from "../DrawModes/EditModeUtils"; //The initial point the user pressed down. let startingPoint: Point; @@ -116,7 +123,11 @@ export function iterationMouseOut() { */ function isLegal(moveDifference: Point, currentPoint: Point): boolean { if (currentParent !== null && currentParent.containsPoint(currentPoint)) { - if (currentNode instanceof CutNode && validateChildren(currentNode, moveDifference)) { + if ( + currentNode instanceof CutNode && + validateChildren(currentNode, moveDifference) && + insertChildless(treeContext.tree.sheet, alterCut(currentNode, moveDifference).ellipse!) + ) { return true; } else if ( currentNode instanceof AtomNode && @@ -130,3 +141,33 @@ function isLegal(moveDifference: Point, currentPoint: Point): boolean { return false; } + +/** + * Determines if the current location and shape of the ellipse would have any children in + * it's current position, acts recursively for cut nodes starting with the sheet. + * @param currentNode The current node we are checking in the recursive function starting with the sheet + * @param currentEllipse The ellipse of the altered cut node we are trying to place + * @returns Whether or not the node would have any children + */ +function insertChildless(currentNode: CutNode, currentEllipse: Ellipse): boolean { + for (let i = 0; i < currentNode.children.length; i++) { + if (currentNode.children[i] instanceof CutNode) { + //So long as the ellipse is not null, and either our current cut or any of it's + //Children have a child that is contained by our ellipse then it is false + if ( + (currentNode.children[i] as CutNode).ellipse instanceof Ellipse && + (currentEllipse.contains((currentNode.children[i] as CutNode).ellipse!) || + !insertChildless(currentNode.children[i] as CutNode, currentEllipse)) + ) { + return false; + } + } else if ( + currentNode.children[i] instanceof AtomNode && + currentEllipse.contains((currentNode.children[i] as AtomNode).calcRect()) + ) { + return false; + } + } + + return true; +} From 696c839eff6e38b2e30c2cf5092cc6acd9c890b5 Mon Sep 17 00:00:00 2001 From: DawnTheWitch Date: Fri, 24 Nov 2023 17:39:45 -0500 Subject: [PATCH 4/6] Fixes --- src/ProofTools/IterationTool.ts | 34 +++++++++++---------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/ProofTools/IterationTool.ts b/src/ProofTools/IterationTool.ts index 8965ddfe..332fc372 100644 --- a/src/ProofTools/IterationTool.ts +++ b/src/ProofTools/IterationTool.ts @@ -42,11 +42,7 @@ export function iterationMouseDown(event: MouseEvent) { currentParent = treeContext.tree.getLowestParent(startingPoint); //So long as we have obtained a node that isn't the sheet we are allowed to select this. - if (currentNode !== treeContext.tree.sheet && currentNode !== null) { - legalNode = true; - } else { - legalNode = false; - } + legalNode = currentNode !== treeContext.tree.sheet && currentNode !== null; } /** @@ -63,16 +59,12 @@ export function iterationMouseMove(event: MouseEvent) { ); redrawTree(treeContext.tree); + const currentPoint = new Point(event.x - offset.x, event.y - offset.y); + const color = isLegal(moveDifference, currentPoint) ? legalColor() : illegalColor(); if (currentNode instanceof CutNode) { - const color = isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y)) - ? legalColor() - : illegalColor(); drawAltered(currentNode, color, moveDifference); } else if (currentNode instanceof AtomNode) { const tempAtom: AtomNode = alterAtom(currentNode, moveDifference); - const color = isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y)) - ? legalColor() - : illegalColor(); drawAtom(tempAtom, color, true); } } @@ -90,14 +82,11 @@ export function iterationMouseUp(event: MouseEvent) { event.y - startingPoint.y ); - if (currentNode instanceof CutNode) { - if (isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y))) { + if (isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y))) { + if (currentNode instanceof CutNode) { insertChildren(currentNode, moveDifference); - } - } else if (currentNode instanceof AtomNode) { - const tempAtom: AtomNode = alterAtom(currentNode, moveDifference); - - if (isLegal(moveDifference, new Point(event.x - offset.x, event.y - offset.y))) { + } else if (currentNode instanceof AtomNode) { + const tempAtom: AtomNode = alterAtom(currentNode, moveDifference); treeContext.tree.insert(tempAtom); } } @@ -122,24 +111,23 @@ export function iterationMouseOut() { * @returns Whether or not this location is legal */ function isLegal(moveDifference: Point, currentPoint: Point): boolean { + let legal = false; if (currentParent !== null && currentParent.containsPoint(currentPoint)) { if ( currentNode instanceof CutNode && validateChildren(currentNode, moveDifference) && insertChildless(treeContext.tree.sheet, alterCut(currentNode, moveDifference).ellipse!) ) { - return true; + legal = true; } else if ( currentNode instanceof AtomNode && treeContext.tree.canInsert(alterAtom(currentNode, moveDifference)) ) { - return true; - } else { - return false; + legal = true; } } - return false; + return legal; } /** From bdb0aa8242c308bdb1941c3c1ad4fd29e1a99bf8 Mon Sep 17 00:00:00 2001 From: DawnTheWitch Date: Fri, 24 Nov 2023 18:12:54 -0500 Subject: [PATCH 5/6] It is now a single line but I think it looks ugly --- src/ProofTools/IterationTool.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ProofTools/IterationTool.ts b/src/ProofTools/IterationTool.ts index 332fc372..81bbcdcd 100644 --- a/src/ProofTools/IterationTool.ts +++ b/src/ProofTools/IterationTool.ts @@ -111,23 +111,23 @@ export function iterationMouseOut() { * @returns Whether or not this location is legal */ function isLegal(moveDifference: Point, currentPoint: Point): boolean { - let legal = false; - if (currentParent !== null && currentParent.containsPoint(currentPoint)) { - if ( - currentNode instanceof CutNode && + //If the current parent exists and contains our current point then checks whether the node + //Is an atom or cut for their own individual legality check. + return ( + currentParent !== null && + currentParent.containsPoint(currentPoint) && + //If the currentNode is a cut, then it is legal if it and all if it's children can be placed + //legally, and if the node we have selected out not be inserted over something else. + ((currentNode instanceof CutNode && validateChildren(currentNode, moveDifference) && - insertChildless(treeContext.tree.sheet, alterCut(currentNode, moveDifference).ellipse!) - ) { - legal = true; - } else if ( - currentNode instanceof AtomNode && - treeContext.tree.canInsert(alterAtom(currentNode, moveDifference)) - ) { - legal = true; - } - } - - return legal; + insertChildless( + treeContext.tree.sheet, + alterCut(currentNode, moveDifference).ellipse! + )) || + //AtomNodes are legal if they can be inserted in their current location. + (currentNode instanceof AtomNode && + treeContext.tree.canInsert(alterAtom(currentNode, moveDifference)))) + ); } /** From 3eb4153927f96e77f3f5aa11768830533f5b0639 Mon Sep 17 00:00:00 2001 From: DawnTheWitch Date: Fri, 24 Nov 2023 18:23:21 -0500 Subject: [PATCH 6/6] Minor word mistake I win --- src/ProofTools/IterationTool.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ProofTools/IterationTool.ts b/src/ProofTools/IterationTool.ts index 81bbcdcd..f5c7f06f 100644 --- a/src/ProofTools/IterationTool.ts +++ b/src/ProofTools/IterationTool.ts @@ -117,7 +117,7 @@ function isLegal(moveDifference: Point, currentPoint: Point): boolean { currentParent !== null && currentParent.containsPoint(currentPoint) && //If the currentNode is a cut, then it is legal if it and all if it's children can be placed - //legally, and if the node we have selected out not be inserted over something else. + //legally, and if the node we have selected can not be inserted over something else. ((currentNode instanceof CutNode && validateChildren(currentNode, moveDifference) && insertChildless(