-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
193 lines (170 loc) · 5.98 KB
/
controls.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
const cspf = {
placeSquare : function(e) {
if (!simControls.coordIsInButton(e)) spf.placeSquare(e);
},
deleteSquare : function(e) {
if (!simControls.coordIsInButton(e)) spf.deleteSquare(e);
}
}
// handle keyboard inputs
// this function handles tap inputs
function handleKeypress(press) {
switch(press.key) {
case 'r': // reset
simControls.reset();
break;
case 's': // start/stop
simControls.playPause();
break;
default:
break;
}
}
// this function handles whenever a key is pushed and held (down) or (up)
function handleKeydownUp(press) {
if (press.type == "keydown") {
switch(press.key) {
case ' ':
simControls.pressSpace(true);
break;
case 'Control':
simControls.pressControl(true);
break;
case 'Shift':
simControls.pressShift(true);
break;
default:
break;
}
}
else { // keyup
switch(press.key) {
case ' ':
simControls.pressSpace(false);
break;
case 'Control':
simControls.pressControl(false);
break;
case 'Shift':
simControls.pressShift(false);
break;
default:
break;
}
}
}
document.addEventListener('keypress', handleKeypress);
document.addEventListener('keydown', handleKeydownUp);
document.addEventListener('keyup', handleKeydownUp);
// mouse event handlers & helpers
// Gets the relevant location from a mouse or single touch event
function getEventLocation(e) {
if (e.touches && e.touches.length == 1) {
return { x:e.touches[0].clientX, y: e.touches[0].clientY }
}
else if (e.clientX && e.clientY) {
return { x: e.clientX, y: e.clientY }
}
}
// this function sets game states. When the pointer is pressed down.
function onPointerDown(e) {
if (e.buttons === 1) { // if left click
simControls.holdLclick = true;
// drag mode
if (simControls.isReadyToDrag()) {
simControls.setDragging(true);
simControls.setDragCoords(
getEventLocation(e).x/MUStates.cameraZoom - MUStates.cameraOffset.x,
getEventLocation(e).y/MUStates.cameraZoom - MUStates.cameraOffset.y
);
}
// place mode
if (simControls.isReadyToPlace()) { // if not holding space and control clicking
simControls.setPlacing(true);
cspf.placeSquare(e);
}
// delete mode
if (simControls.isReadyToDelete()) { // if not holding space and control clicking
simControls.setDeleting(true);
cspf.deleteSquare(e);
}
}
if (e.buttons === 2) { // right click
}
}
// This function resets gamestates and active controls. When the mouse press/hold is done.
function onPointerUp(e) {
// console.log(e.buttons, e.isPrimary);
if (e.buttons === 0) { // if left click
simControls.holdLclick = false;
simControls.setDragging(false);
simControls.setPlacing(false);
simControls.setDeleting(false);
}
}
// This function checks gamestates. Whenever the mouse is moved, period.
function onPointerMove(e) {
let ec = getEventLocation(e);
MUStates.previousCoord = {x: ec.x, y: ec.y, target: e.target};
if (simControls.dragging) {
let d = simControls.getDrag();
MUStates.cameraOffset.x = ec.x/MUStates.cameraZoom - d.x;
MUStates.cameraOffset.y = ec.y/MUStates.cameraZoom - d.y;
MUStates.gridIsUpToDate = false;
}
if (simControls.placing) {
cspf.placeSquare(e);
}
if (simControls.deleting) {
cspf.deleteSquare(ec);
}
}
// touchscreen compatability functions
function handleTouch(e, singleTouchHandler) {
if ( e.touches.length == 1 ) {
singleTouchHandler(e)
}
else if (e.type == "touchmove" && e.touches.length == 2) {
simControls.dragging = false
handlePinch(e)
}
}
function handlePinch(e) {
e.preventDefault()
let touch1 = { x: e.touches[0].clientX, y: e.touches[0].clientY }
let touch2 = { x: e.touches[1].clientX, y: e.touches[1].clientY }
// This is distance squared, but no need for an expensive sqrt as it's only used in ratio
let currentDistance = (touch1.x - touch2.x)**2 + (touch1.y - touch2.y)**2
if (simControls.initialPinchDistance == null) {
simControls.initialPinchDistance = currentDistance
}
else {
adjustZoom( null, currentDistance/simControls.initialPinchDistance )
}
}
// controls the zoom
function adjustZoom(zoomAmount, zoomFactor) {
if (!simControls.dragging && !simControls.isReadyToDelete()) {
if (zoomAmount) {
MUStates.cameraZoom -= zoomAmount
}
else if (zoomFactor) {
console.log(zoomFactor)
MUStates.cameraZoom = zoomFactor*lastZoom
}
MUStates.cameraZoom = Math.min( MUStates.cameraZoom, MAX_ZOOM )
MUStates.cameraZoom = Math.max( MUStates.cameraZoom, MIN_ZOOM )
MUStates.gridIsUpToDate = false;
}
}
function adjustdeleteDiameter(scrollAmount) {
if (!simControls.isReadyToDelete()) return; // returns if not in "ready to delete" state
grf.setDeleteDiameter(scrollAmount);
}
canvasContainer.addEventListener('mousedown', onPointerDown)
canvasContainer.addEventListener('touchstart', (e) => handleTouch(e, onPointerDown))
canvasContainer.addEventListener('mouseup', onPointerUp)
canvasContainer.addEventListener('touchend', (e) => handleTouch(e, onPointerUp))
canvasContainer.addEventListener('mousemove', onPointerMove)
canvasContainer.addEventListener('touchmove', (e) => handleTouch(e, onPointerMove))
canvasContainer.addEventListener('wheel', (e) => {adjustZoom(e.deltaY*SCROLL_SENSITIVITY); adjustdeleteDiameter(e.deltaY*SCROLL_SENSITIVITY)})