-
Notifications
You must be signed in to change notification settings - Fork 407
/
CameraListener.js
executable file
·422 lines (369 loc) · 12.4 KB
/
CameraListener.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// @flow
// Copyright (c) 2018-present, GM Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found in the LICENSE file in the root directory of this source tree.
// You may not use this file except in compliance with the License.
import normalizeWheel from "normalize-wheel";
import * as React from "react";
import type { CameraKeyMap, CameraAction, Vec2 } from "../types";
import getOrthographicBounds from "../utils/getOrthographicBounds";
import CameraStore from "./CameraStore";
const PAN_SPEED = 4;
const MOUSE_ZOOM_SPEED = 0.3;
const KEYBOARD_MOVE_SPEED = 0.3;
const KEYBOARD_ZOOM_SPEED = 150;
const KEYBOARD_SPIN_SPEED = 1.5;
const DEFAULT_KEYMAP: CameraKeyMap = {
KeyA: "moveLeft",
KeyD: "moveRight",
KeyE: "rotateRight",
KeyF: "tiltUp",
KeyQ: "rotateLeft",
KeyR: "tiltDown",
KeyS: "moveDown",
KeyW: "moveUp",
KeyX: "zoomOut",
KeyZ: "zoomIn",
};
type KeyMotion = { x?: number, y?: number, zoom?: number, yaw?: number, tilt?: number };
type Props = {|
cameraStore: CameraStore,
keyMap?: CameraKeyMap,
shiftKeys: boolean,
children?: React.ChildrenArray<React.Element<any> | null>,
|};
// attaches mouse and keyboard listeners to allow for moving the camera on user input
export default class CameraListener extends React.Component<Props> {
_keyTimer: ?AnimationFrameID;
_keys: Set<string> = new Set();
_buttons: Set<number> = new Set();
_listeners = [];
_shiftKey = false;
_metaKey = false;
_ctrlKey = false;
_el: ?HTMLDivElement;
_rect: ClientRect | DOMRect;
_initialMouse: Vec2;
componentDidMount() {
const { _el } = this;
if (!_el) {
return;
}
this._rect = _el.getBoundingClientRect();
const listen = (target: any, name: string, fn) => {
target.addEventListener(name, fn);
this._listeners.push({ target, name, fn });
};
listen(document, "blur", this._onBlur);
listen(window, "mouseup", this._onWindowMouseUp);
_el.addEventListener("wheel", this._onWheel, { passive: false });
}
componentWillUnmount() {
this._listeners.forEach((listener) => {
listener.target.removeEventListener(listener.name, listener.fn);
});
this._endDragging();
const { _el } = this;
if (!_el) {
return;
}
_el.removeEventListener("wheel", this._onWheel, { passive: false });
}
_getMouseOnScreen = (mouse: MouseEvent) => {
const { clientX, clientY } = mouse;
const { top, left, width, height } = this._rect;
const x = (clientX - left) / width;
const y = (clientY - top) / height;
return [x, y];
};
_onMouseDown = (e: MouseEvent) => {
const { _el } = this;
if (!_el) {
return;
}
e.preventDefault();
this._buttons.add(e.button);
_el.focus();
this._rect = _el.getBoundingClientRect();
this._initialMouse = this._getMouseOnScreen(e);
this.startDragging(e);
};
_isLeftMouseDown() {
return this._buttons.has(0);
}
_isRightMouseDown() {
return this._buttons.has(2);
}
_getMagnitude(base: number = 1) {
return this._shiftKey ? base / 10 : base;
}
_getMoveMagnitude() {
// avoid interference with drawing tools
if (this._ctrlKey) {
return { x: 0, y: 0 };
}
const {
cameraStore: {
state: { distance, perspective },
},
} = this.props;
if (perspective) {
// in perspective mode its more like flying, so move by the magnitude
// we use the camera distance as a heuristic
return { x: distance, y: distance };
}
// in orthographic mode we know the exact viewable area
// which is a square so we can move exactly percentage within it
const { width, height } = this._rect;
const bounds = getOrthographicBounds(distance, width, height);
return { x: bounds.width, y: bounds.height };
}
_onWindowMouseMove = (e: MouseEvent) => {
if (!this._buttons.size) {
return;
}
this._shiftKey = e.shiftKey;
const {
cameraStore: {
cameraMove,
cameraRotate,
state: { perspective },
},
} = this.props;
// compute the amount the mouse has moved
let moveX, moveY;
const mouse = this._getMouseOnScreen(e);
// when pointer lock is enabled, we get movementX and movementY (with direction reversed)
// instead of the screenX/screenY changing... except, when using synergy, they come through
// like regular mousemove events.
if ((document: any).pointerLockElement && (e.movementX || e.movementY)) {
moveX = -e.movementX / this._rect.width;
moveY = -e.movementY / this._rect.height;
} else {
moveX = this._initialMouse[0] - mouse[0];
moveY = this._initialMouse[1] - mouse[1];
}
this._initialMouse = mouse;
if (this._isRightMouseDown()) {
const magnitude = this._getMagnitude(PAN_SPEED);
// in orthographic mode, flip the direction of rotation so "left" means "counterclockwise"
const x = (perspective ? moveX : -moveX) * magnitude;
// do not rotate vertically in orthograhpic mode
const y = perspective ? moveY * magnitude : 0;
cameraRotate([x, y]);
}
if (this._isLeftMouseDown()) {
const { x, y } = this._getMoveMagnitude();
cameraMove([this._getMagnitude(moveX * x), this._getMagnitude(-moveY * y)]);
}
};
_onMouseUp = (e: MouseEvent) => {
this._buttons.delete(e.button);
this._endDragging();
};
_onWindowMouseUp = (e: MouseEvent) => {
const { _el } = this;
if (!_el) {
return;
}
// do nothing if this container had a mouseup, because we catch it in the onMouseUp handler
if (_el.contains((e.target: any)) || e.target === _el) {
return;
}
// If mouseup triggers on the window outside this container, clear any active interactions.
// This will allow a mouseup outside the browser window to be handled; otherwise the mouse
// "sticks" in a down position until another click on this element is received.
this._buttons.clear();
this._endDragging();
};
startDragging(e: MouseEvent) {
if (e.button !== 0 && this._el && typeof this._el.requestPointerLock === "function") {
this._el.requestPointerLock();
}
window.addEventListener("mousemove", this._onWindowMouseMove);
}
_endDragging() {
window.removeEventListener("mousemove", this._onWindowMouseMove);
if (typeof (document: any).exitPointerLock === "function") {
(document: any).exitPointerLock();
}
}
_getKeyMotion = (code: string): ?KeyMotion => {
const moveSpeed = this._getMagnitude(KEYBOARD_MOVE_SPEED);
const zoomSpeed = this._getMagnitude(KEYBOARD_ZOOM_SPEED);
const spinSpeed = this._getMagnitude(KEYBOARD_SPIN_SPEED);
const { keyMap, shiftKeys } = this.props;
const action: CameraAction | false = (keyMap && keyMap[code]) || DEFAULT_KEYMAP[code] || false;
if (this._shiftKey && !shiftKeys) {
return null;
}
switch (action) {
case "moveRight":
return { x: moveSpeed };
case "moveLeft":
return { x: -moveSpeed };
case "moveUp":
return { y: moveSpeed };
case "moveDown":
return { y: -moveSpeed };
case "zoomIn":
return { zoom: zoomSpeed };
case "zoomOut":
return { zoom: -zoomSpeed };
case "rotateLeft":
return { yaw: -spinSpeed };
case "rotateRight":
return { yaw: spinSpeed };
case "tiltUp":
return { tilt: -spinSpeed };
case "tiltDown":
return { tilt: spinSpeed };
case false:
return null;
default:
(action: empty);
console.warn("Unrecognized key action:", action);
return null;
}
};
_moveKeyboard(dt: number) {
const motion = { x: 0, y: 0, zoom: 0, yaw: 0, tilt: 0 };
this._keys.forEach((code) => {
const { x = 0, y = 0, zoom = 0, yaw = 0, tilt = 0 } = this._getKeyMotion(code) || {};
motion.x += x;
motion.y += y;
motion.zoom += zoom;
motion.yaw += yaw;
motion.tilt += tilt;
});
const {
cameraStore: {
cameraMove,
cameraRotate,
cameraZoom,
state: { perspective },
},
} = this.props;
if (motion.x || motion.y) {
const { x, y } = this._getMoveMagnitude();
cameraMove([motion.x * x * dt, motion.y * y * dt]);
}
if (motion.yaw || (perspective && motion.tilt)) {
cameraRotate([motion.yaw * dt, perspective ? motion.tilt * dt : 0]);
}
if (motion.zoom) {
cameraZoom(motion.zoom * dt);
}
}
_startKeyTimer(lastStamp: ?number) {
if (this._keyTimer) {
return;
}
this._keyTimer = requestAnimationFrame((stamp) => {
this._moveKeyboard((lastStamp ? stamp - lastStamp : 0) / 1000);
this._keyTimer = undefined;
// Only start the timer if keys are still pressed.
// We do this rather than stopping the timer in onKeyUp, because keys held
// sometimes actually trigger repeated keyup/keydown, rather than just repeated keydown.
// By checking currently-down keys in the requestAnimationFrame callback, we give the browser enough time to
// handle both the keyup and keydown before checking whether we should restart the timer.
if (this._keys.size) {
this._startKeyTimer(stamp);
}
});
}
_stopKeyTimer() {
if (this._keyTimer) {
cancelAnimationFrame(this._keyTimer);
}
this._keyTimer = undefined;
}
_onKeyDown = (e: SyntheticKeyboardEvent<HTMLDivElement>) => {
const { keyMap } = this.props;
this._shiftKey = e.shiftKey;
this._metaKey = e.metaKey;
this._ctrlKey = e.ctrlKey;
const code = ((e.nativeEvent: any): KeyboardEvent).code;
// ignore repeated keydown events
if (e.repeat || this._keys.has(code)) {
e.stopPropagation();
e.preventDefault();
return;
}
if (e.altKey || e.ctrlKey || e.metaKey) {
// we don't currently handle these modifiers
return;
}
// allow null, false, or empty keymappings which explicitly cancel Worldview from processing that key
if (keyMap && code in keyMap && !keyMap[code]) {
return false;
}
// if we respond to this key, start the update timer
if (this._getKeyMotion(code)) {
this._keys.add(code);
this._startKeyTimer();
e.stopPropagation();
e.preventDefault();
}
};
_onKeyUp = (e: SyntheticKeyboardEvent<HTMLDivElement>) => {
this._shiftKey = e.shiftKey;
this._metaKey = e.metaKey;
this._ctrlKey = e.ctrlKey;
this._keys.delete(((e.nativeEvent: any): KeyboardEvent).code);
};
_onWheel = (e: WheelEvent) => {
// stop the wheel event here, as wheel propagation through the entire dom
// can cause the browser to slow down & thrash
e.preventDefault();
e.stopPropagation();
this._shiftKey = e.shiftKey;
// with osx trackpad scrolling, slow to medium pixelY is around +/- 1 to 10
// external mouse wheels generally come in higher values around +/- 30 to 50
const { pixelX, pixelY } = normalizeWheel(e);
// shift+scroll on an external mouse may scroll in the X direction instead of Y
const wheelAmount = pixelY || pixelX;
// we use positive value to indicate zooming in
// and negative value to zoom out, so reverse the direction of the wheel
const dir = Math.sign(wheelAmount) * -1;
const amount = Math.abs(wheelAmount);
// restrict zoom percentage per tick to between 1 & 50 percent
const percentage = Math.max(1, Math.min(amount, 50));
// support shift+wheel magnitude adjustment
const zoomPercentage = this._getMagnitude(percentage * dir * MOUSE_ZOOM_SPEED);
this.props.cameraStore.cameraZoom(zoomPercentage);
};
// make sure all movements stop if the document loses focus by resetting modifier keys
// to their 'off' position
// (e.g. ctrl+tab leaving the page should not leave the ctrl key in the 'on' position)
_onBlur = (e: MouseEvent) => {
this._keys = new Set();
this._ctrlKey = false;
this._shiftKey = false;
this._metaKey = false;
this._stopKeyTimer();
};
_onContextMenu = (e: MouseEvent) => {
e.preventDefault();
e.stopPropagation();
};
render() {
const { children } = this.props;
return (
<div
tabIndex={0}
style={{ outline: "none" }}
draggable
ref={(el) => (this._el = el)}
onMouseDown={this._onMouseDown}
onMouseUp={this._onMouseUp}
onBlur={this._onBlur}
onContextMenu={this._onContextMenu}
onKeyDown={this._onKeyDown}
onKeyUp={this._onKeyUp}>
{children}
</div>
);
}
}