@@ -22,14 +22,14 @@ const ctx: CanvasRenderingContext2D = res;
22
22
//HTML letter display
23
23
const atomDisplay = < HTMLParagraphElement > document . getElementById ( "atomDisplay" ) ;
24
24
25
- //Allows font measurement in pixels to creature atom bounding box.
26
- let atomMetrics : TextMetrics ;
27
-
28
25
//Tracks if the mouse has ever left canvas disallowing future movements.
29
26
let wasOut : boolean ;
30
27
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 ) ) ;
33
33
34
34
/**
35
35
* 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;
38
38
export function atomKeyPress ( event : KeyboardEvent ) {
39
39
const regex = new RegExp ( / ^ [ A - Z a - z ] $ / ) ;
40
40
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
+ }
43
47
}
44
48
}
45
49
@@ -50,57 +54,41 @@ export function atomKeyPress(event: KeyboardEvent) {
50
54
* @returns Whether or not the mouse event took place
51
55
*/
52
56
export function atomMouseDown ( event : MouseEvent ) {
53
- atomMetrics = ctx . measureText ( identifier ) ;
54
57
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 )
60
62
) ;
61
-
62
- redrawTree ( treeContext . tree ) ;
63
- const color = treeContext . tree . canInsert ( currentAtom ) ? legalColor ( ) : illegalColor ( ) ;
64
- drawAtom ( currentAtom , color , true ) ;
63
+ drawLegal ( ) ;
65
64
}
66
65
67
66
/**
68
67
* Moves the current atom to the current mouse position, redraws the canvas and redraws the atom.
69
68
* @param event The mouse move event
70
69
*/
71
70
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 )
77
74
) ;
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 ( ) ;
87
76
}
88
77
89
78
/**
90
79
* If the atom is in a valid place, adds it to the tree. Redraws the canvas and resets currentAtom.
91
80
* @param event The mouse up event
92
81
*/
93
82
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 )
99
86
) ;
100
87
if ( treeContext . tree . canInsert ( currentAtom ) && ! wasOut ) {
101
88
treeContext . tree . insert ( currentAtom ) ;
102
89
}
103
90
redrawTree ( treeContext . tree ) ;
91
+ hasMouseDown = false ;
104
92
}
105
93
106
94
/**
@@ -110,3 +98,34 @@ export function atomMouseOut() {
110
98
wasOut = true ;
111
99
redrawTree ( treeContext . tree ) ;
112
100
}
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