Skip to content

Commit 806ed01

Browse files
authored
Merge pull request #243 from RAIRLab/213-boxpocalypse-returns
212 and 213! Boxes and drawn atoms update while mouse is down
2 parents a0fbca8 + 266faa3 commit 806ed01

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

src/DrawModes/AtomTool.ts

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ const ctx: CanvasRenderingContext2D = res;
2222
//HTML letter display
2323
const atomDisplay = <HTMLParagraphElement>document.getElementById("atomDisplay");
2424

25-
//Allows font measurement in pixels to creature atom bounding box.
26-
let atomMetrics: TextMetrics;
27-
2825
//Tracks if the mouse has ever left canvas disallowing future movements.
2926
let wasOut: boolean;
3027

31-
let identifier = "A";
32-
atomDisplay.innerHTML = identifier;
28+
//Tracks whether the mouse button is currently down.
29+
let hasMouseDown: boolean;
30+
31+
//The current atom we are creating
32+
let currentAtom: AtomNode = createAtom("A", new Point(0, 0));
3333

3434
/**
3535
* Checks to see if the pressed key is a valid letter, if yes sets it to the atom node.
@@ -38,8 +38,12 @@ atomDisplay.innerHTML = identifier;
3838
export function atomKeyPress(event: KeyboardEvent) {
3939
const regex = new RegExp(/^[A-Za-z]$/);
4040
if (regex.test(event.key)) {
41-
identifier = event.key;
42-
atomDisplay.innerHTML = identifier;
41+
currentAtom = createAtom(event.key, new Point(currentAtom.origin.x, currentAtom.origin.y));
42+
43+
//If the currentAtom is not the default then see if it can be drawn there and draw it.
44+
if (currentAtom.origin.x !== 0 && currentAtom.origin.y !== 0 && hasMouseDown) {
45+
drawLegal();
46+
}
4347
}
4448
}
4549

@@ -50,57 +54,41 @@ export function atomKeyPress(event: KeyboardEvent) {
5054
* @returns Whether or not the mouse event took place
5155
*/
5256
export function atomMouseDown(event: MouseEvent) {
53-
atomMetrics = ctx.measureText(identifier);
5457
wasOut = false;
55-
const currentAtom = new AtomNode(
56-
identifier,
57-
new Point(event.clientX - offset.x, event.clientY - offset.y),
58-
atomMetrics.width,
59-
atomMetrics.fontBoundingBoxDescent + atomMetrics.actualBoundingBoxAscent
58+
hasMouseDown = true;
59+
currentAtom = createAtom(
60+
currentAtom.identifier,
61+
new Point(event.clientX - offset.x, event.clientY - offset.y)
6062
);
61-
62-
redrawTree(treeContext.tree);
63-
const color = treeContext.tree.canInsert(currentAtom) ? legalColor() : illegalColor();
64-
drawAtom(currentAtom, color, true);
63+
drawLegal();
6564
}
6665

6766
/**
6867
* Moves the current atom to the current mouse position, redraws the canvas and redraws the atom.
6968
* @param event The mouse move event
7069
*/
7170
export function atomMouseMove(event: MouseEvent) {
72-
const currentAtom = new AtomNode(
73-
identifier,
74-
new Point(event.clientX - offset.x, event.clientY - offset.y),
75-
atomMetrics.width,
76-
atomMetrics.fontBoundingBoxDescent + atomMetrics.actualBoundingBoxAscent
71+
currentAtom = createAtom(
72+
currentAtom.identifier,
73+
new Point(event.clientX - offset.x, event.clientY - offset.y)
7774
);
78-
79-
redrawTree(treeContext.tree);
80-
if (!wasOut) {
81-
if (treeContext.tree.canInsert(currentAtom)) {
82-
drawAtom(currentAtom, legalColor(), true);
83-
} else {
84-
drawAtom(currentAtom, illegalColor(), true);
85-
}
86-
}
75+
drawLegal();
8776
}
8877

8978
/**
9079
* If the atom is in a valid place, adds it to the tree. Redraws the canvas and resets currentAtom.
9180
* @param event The mouse up event
9281
*/
9382
export function atomMouseUp(event: MouseEvent) {
94-
const currentAtom = new AtomNode(
95-
identifier,
96-
new Point(event.clientX - offset.x, event.clientY - offset.y),
97-
atomMetrics.width,
98-
atomMetrics.fontBoundingBoxDescent + atomMetrics.actualBoundingBoxAscent
83+
currentAtom = createAtom(
84+
currentAtom.identifier,
85+
new Point(event.clientX - offset.x, event.clientY - offset.y)
9986
);
10087
if (treeContext.tree.canInsert(currentAtom) && !wasOut) {
10188
treeContext.tree.insert(currentAtom);
10289
}
10390
redrawTree(treeContext.tree);
91+
hasMouseDown = false;
10492
}
10593

10694
/**
@@ -110,3 +98,34 @@ export function atomMouseOut() {
11098
wasOut = true;
11199
redrawTree(treeContext.tree);
112100
}
101+
102+
/**
103+
* Helper function to construct a new atom node with a created width and height based on the font.
104+
* @param identifier The letter representation of the atom
105+
* @param origin The original place for the atom to be drawn and the bounding box
106+
* @returns The newly made atom
107+
*/
108+
function createAtom(identifier: string, origin: Point): AtomNode {
109+
atomDisplay.innerHTML = identifier;
110+
const atomMetrics: TextMetrics = ctx.measureText(identifier);
111+
return new AtomNode(
112+
identifier,
113+
new Point(origin.x, origin.y),
114+
atomMetrics.width,
115+
atomMetrics.fontBoundingBoxDescent + atomMetrics.actualBoundingBoxAscent
116+
);
117+
}
118+
119+
/**
120+
* Draws the global currentAtom based on if it can be inserted drawing it either legal or illegal.
121+
*/
122+
function drawLegal() {
123+
redrawTree(treeContext.tree);
124+
if (!wasOut) {
125+
if (treeContext.tree.canInsert(currentAtom)) {
126+
drawAtom(currentAtom, legalColor(), true);
127+
} else {
128+
drawAtom(currentAtom, illegalColor(), true);
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)