-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouseKeyboardEvents.js
267 lines (202 loc) · 6.07 KB
/
mouseKeyboardEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
function moveKeys() // if the arrow keys are pressed, the selected charge moves
{
let canvas = foreGroundCanvas;
charges.forEach(charge => {
if (charge.selected)
{
if (canvas.keyIsDown(canvas.RIGHT_ARROW))
{
charge.position.x += 3;
}
if (canvas.keyIsDown(canvas.LEFT_ARROW))
{
charge.position.x -= 3;
}
if (canvas.keyIsDown(canvas.DOWN_ARROW))
{
charge.position.y += 3;
}
if (canvas.keyIsDown(canvas.UP_ARROW))
{
charge.position.y -= 3;
}
}
})
}
function whenMouseClicked() // this is an inbuilt p5 function that runs everytime any mouse button is clicked
{
if (document.getElementById("popup").style.visibility == "visible" && mousePosition.x > innerWidth/2 && mousePosition.y < innerHeight / 2)
{
closeHelp();
}
buttons.forEach(button => { // this will loop through all the buttons
if (button.visible)
{
if (pointIsInsideRect(mousePosition, button)) // if the point where the user clicks is inside the button
{
button.clicked();
return;
}
}
})
checkBoxes.forEach(checkBox => { // these will loop through all the checkBoxes
if (checkBox.visible)
{
if (pointIsInsideRect(mousePosition, checkBox)) // if the point where the user clicks is inside the checkbox
{
checkBox.clicked();
return;
}
}
})
if (showContextMenu)
{
contextMenuButtons.forEach(button => { // this will loop through all the buttons
if (button.visible)
{
if (pointIsInsideRect(mousePosition, button)) // if the point where the user clicks is inside the button
{
button.clicked();
return;
}
}
})
}
if (showEquipotentialLines && mousePosition.x < innerWidth - sidePanelWidth)
{
let mouseClickPosition = new p5.Vector(mousePosition.x, mousePosition.y);
createEquipotentialLine(mouseClickPosition);
}
if (testChargeMode) // if test charge mode is on, this will create a test charge wherever the user clicks
{
testCharges.push(new TestCharge(mousePosition, testChargeCharge));
}
charges.forEach(charge => {charge.dragging = false; charge.selected = false;} )
let selectedCharge = charges.find(charge => pointIsInsideCircle(mousePosition, charge))
if (selectedCharge != undefined)
{
selectedCharge.selected = true;
}
hideContextMenu();
}
function whenMouseMoved()
{
buttons.forEach(button => { // this will loop through all the buttons
if (button.visible)
{
button.hovering = false;
if (pointIsInsideRect(mousePosition, button)) // if the mouse position is over a button
{
button.hovering = true;
}
}
})
checkBoxes.forEach(checkBox => { // this will loop through all the checkBoxes
if (checkBox.visible)
{
checkBox.hovering = false;
if (pointIsInsideRect(mousePosition, checkBox)) // if the mouse position is over a checkBox
{
checkBox.hovering = true;
}
}
})
if (showContextMenu)
{
contextMenuButtons.forEach(button => { // this will loop through all the context Menu Buttons
if (button.visible)
{
button.hovering = false;
if (pointIsInsideRect(mousePosition, button))
{
button.hovering = true;
}
}
})
}
}
function whenMouseDragged()
{
let noChargeIsBeingDragged = !charges.some(charge => charge.dragging) // this will be true if no charge is currently being dragged.
if (noChargeIsBeingDragged) // if no charge is being dragged, check if the mouse is over a charge and is dragging
{
charges.forEach(charge => {
if (pointIsInsideCircle(mousePosition, charge)) charge.dragging = true; // if the mouse is hovering over a charge while it's being dragged, set it's dragging property to true
else charge.dragging = false;
})
}
let chargeToMove = charges.find(charge => charge.dragging) // this searches the charges array and finds the first charge with a true dragging property and sets it equal to the variable
if (mousePosition.x < innerWidth - sidePanelWidth && chargeToMove != undefined) // if the mouse isn't over the side panel
{
if (!snapToGrid)
{
chargeToMove.position = mousePosition; // make the charges position equal to the mouse position
}
else
{
chargeToMove.position = roundVectorToNearestGrid(mousePosition)// the charge position will round to the nearest grid
}
equiLines = []; // if a charge is being dragged, clear all of the equipotential lines
createDataFromSidePanel(); // recalculate whatever is toggled in the side panel
}
}
function whenDoubleClicked()
{
if (!testChargeMode)
{
let notTouching = true;
charges.forEach(charge => {
let distance = mousePosition.dist(charge.position);
if (distance < chargeDiameter)
{
notTouching = false;
}
})
if (notTouching && mousePosition.x < innerWidth - sidePanelWidth)
{
createPointCharge(mousePosition);
}
}
}
function whenKeyPressed()
{
let canvas = foreGroundCanvas;
let chargeSelected = charges.some(charge => charge.selected); // if a charge is currently selected, this will be true
if (chargeSelected)
{
charges.forEach((charge, i) => {
if (charge.selected)
{
if (canvas.keyCode === canvas.DELETE)
{
removeCharge(i);
createDataFromSidePanel();
}
if (canvas.keyCode === 107) // plus key pressed
{
charge.slider.value(charges[i].slider.value() + 1);
}
if (canvas.keyCode === 109) // minus key pressed
{
charge.slider.value(charges[i].slider.value() - 1);
}
}
})
}
else
{
if (canvas.keyCode == 109)
{
createPointCharge(mousePosition, -5);
}
if (canvas.keyCode == 107)
{
createPointCharge(mousePosition, 5);
}
if (canvas.keyCode === 27) // escape key pressed
{
closeHelp();
}
}
createDataFromSidePanel();
}