Skip to content

Commit 572238a

Browse files
Jo-Byrfloryst
authored andcommitted
feat(RenderWindowInteractor): commit mouse movement data in pointerlock
Modify RenderWindowInteractor to return mouse movement data in screen event position Maintain InteractorStyleManipulator manipulator during pointerLock Update MouseCameraTrackballFirstPersonManipulator
1 parent 7a330c0 commit 572238a

File tree

7 files changed

+39
-57
lines changed

7 files changed

+39
-57
lines changed

Sources/Interaction/Manipulators/MouseCameraTrackballFirstPersonManipulator/index.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,73 +15,30 @@ function vtkMouseCameraTrackballFirstPersonManipulator(publicAPI, model) {
1515
const internal = {
1616
interactor: null,
1717
renderer: null,
18-
previousPosition: null,
1918
};
2019

2120
//--------------------------------------------------------------------------
2221

2322
publicAPI.onButtonDown = (interactor, renderer, position) => {
24-
internal.previousPosition = position;
25-
2623
if (model.usePointerLock && !interactor.isPointerLocked()) {
2724
Object.assign(internal, { interactor, renderer });
2825
interactor.requestPointerLock();
29-
publicAPI.startPointerLockInteraction();
3026
}
3127
};
3228

3329
//--------------------------------------------------------------------------
3430

35-
publicAPI.startPointerLockInteraction = () => {
36-
const { interactor } = internal;
37-
38-
// TODO: at some point, this should perhaps be done in
39-
// RenderWindowInteractor instead of here.
40-
// We need to hook into mousemove directly for two reasons:
41-
// 1. We need to keep receiving mouse move events after the mouse button
42-
// is released. This is currently not possible with
43-
// vtkInteractorStyleManipulator.
44-
// 2. Since the mouse is stationary in pointer lock mode, we need the
45-
// event.movementX and event.movementY info, which are not currently
46-
// passed via interactor.onMouseMove.
47-
document.addEventListener('mousemove', publicAPI.onPointerLockMove);
48-
49-
let subscription = null;
50-
const endInteraction = () => {
51-
document.removeEventListener('mousemove', publicAPI.onPointerLockMove);
52-
subscription.unsubscribe();
53-
};
54-
subscription = interactor.onEndPointerLock(endInteraction);
55-
};
56-
57-
//--------------------------------------------------------------------------
58-
59-
publicAPI.onPointerLockMove = (e) => {
60-
const sensitivity = model.sensitivity;
61-
const yaw = -1 * e.movementX * sensitivity;
62-
const pitch = -1 * e.movementY * sensitivity;
63-
64-
publicAPI.moveCamera(yaw, pitch);
65-
};
66-
67-
//--------------------------------------------------------------------------
68-
6931
publicAPI.onMouseMove = (interactor, renderer, position) => {
70-
// This is currently only being called for non pointer lock mode
7132
if (!position) {
7233
return;
7334
}
7435

75-
const { previousPosition } = internal;
76-
7736
const sensitivity = model.sensitivity;
78-
const yaw = (previousPosition.x - position.x) * sensitivity;
79-
const pitch = (position.y - previousPosition.y) * sensitivity;
37+
const yaw = -position.movementX * sensitivity;
38+
const pitch = -position.movementY * sensitivity;
8039

8140
Object.assign(internal, { interactor, renderer });
8241
publicAPI.moveCamera(yaw, pitch);
83-
84-
internal.previousPosition = position;
8542
};
8643

8744
//--------------------------------------------------------------------------

Sources/Interaction/Style/InteractorStyleManipulator/index.d.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import vtkInteractorStyle from '../../../Rendering/Core/InteractorStyle';
66
import {
77
Device,
88
Input,
9+
MouseButton,
910
} from '../../../Rendering/Core/RenderWindowInteractor/Constants';
1011
import { Nullable, Vector3 } from '../../../types';
1112

@@ -120,7 +121,7 @@ export interface vtkInteractorStyleManipulator extends vtkInteractorStyle {
120121
* @param alt alt enabled
121122
*/
122123
findMouseManipulator(
123-
button: number,
124+
button: MouseButton,
124125
shift: boolean,
125126
scroll: boolean,
126127
alt: boolean
@@ -285,13 +286,13 @@ export interface vtkInteractorStyleManipulator extends vtkInteractorStyle {
285286
* @param button which button
286287
* @param callData event data
287288
*/
288-
onButtonDown(button: number, callData: unknown): void;
289+
onButtonDown(button: MouseButton, callData: unknown): void;
289290

290291
/**
291292
* Handles a button up event.
292293
* @param button which button
293294
*/
294-
onButtonUp(button: number): void;
295+
onButtonUp(button: MouseButton): void;
295296

296297
/**
297298
* Sets the rotation factor.

Sources/Interaction/Style/InteractorStyleManipulator/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import macro from 'vtk.js/Sources/macros';
2+
import { MouseButton } from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor/Constants';
23
import vtkInteractorStyle from 'vtk.js/Sources/Rendering/Core/InteractorStyle';
34

45
const { vtkDebugMacro } = macro;
@@ -268,19 +269,19 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
268269
//-------------------------------------------------------------------------
269270
publicAPI.handleLeftButtonPress = (callData) => {
270271
model.previousPosition = callData.position;
271-
publicAPI.onButtonDown(1, callData);
272+
publicAPI.onButtonDown(MouseButton.LeftButton, callData);
272273
};
273274

274275
//-------------------------------------------------------------------------
275276
publicAPI.handleMiddleButtonPress = (callData) => {
276277
model.previousPosition = callData.position;
277-
publicAPI.onButtonDown(2, callData);
278+
publicAPI.onButtonDown(MouseButton.MiddleButton, callData);
278279
};
279280

280281
//-------------------------------------------------------------------------
281282
publicAPI.handleRightButtonPress = (callData) => {
282283
model.previousPosition = callData.position;
283-
publicAPI.onButtonDown(3, callData);
284+
publicAPI.onButtonDown(MouseButton.RightButton, callData);
284285
};
285286

286287
//-------------------------------------------------------------------------
@@ -397,17 +398,17 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
397398

398399
//-------------------------------------------------------------------------
399400
publicAPI.handleLeftButtonRelease = () => {
400-
publicAPI.onButtonUp(1);
401+
publicAPI.onButtonUp(MouseButton.LeftButton);
401402
};
402403

403404
//-------------------------------------------------------------------------
404405
publicAPI.handleMiddleButtonRelease = () => {
405-
publicAPI.onButtonUp(2);
406+
publicAPI.onButtonUp(MouseButton.MiddleButton);
406407
};
407408

408409
//-------------------------------------------------------------------------
409410
publicAPI.handleRightButtonRelease = () => {
410-
publicAPI.onButtonUp(3);
411+
publicAPI.onButtonUp(MouseButton.RightButton);
411412
};
412413

413414
//-------------------------------------------------------------------------
@@ -421,12 +422,18 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
421422
) {
422423
model.currentManipulator.onButtonUp(model._interactor);
423424
model.currentManipulator.endInteraction();
424-
model.currentManipulator = null;
425-
model._interactor.cancelAnimation(publicAPI.onButtonDown);
425+
if (!model._interactor.isPointerLocked()) {
426+
model.currentManipulator = null;
427+
}
426428
publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT);
427429
}
428430
};
429431

432+
//-------------------------------------------------------------------------
433+
publicAPI.handleEndPointerLock = () => {
434+
model.currentManipulator = null;
435+
};
436+
430437
//-------------------------------------------------------------------------
431438
publicAPI.handleStartMouseWheel = (callData) => {
432439
// Must not be processing a wheel interaction to start another.

Sources/Rendering/Core/RenderWindowInteractor/Constants.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ export declare enum Axis {
2323
ThumbstickY = 4,
2424
}
2525

26+
export declare enum MouseButton {
27+
LeftButton = 1,
28+
MiddleButton = 2,
29+
RightButton = 3,
30+
}
31+
2632
declare const _default: {
2733
Device: typeof Device;
2834
Input: typeof Input;
2935
Axis: typeof Axis;
36+
MouseButton: typeof MouseButton;
3037
};
3138
export default _default;

Sources/Rendering/Core/RenderWindowInteractor/Constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ export const Axis = {
2323
ThumbstickY: 4,
2424
};
2525

26+
export const MouseButton = {
27+
LeftButton: 1,
28+
MiddleButton: 2,
29+
RightButton: 3,
30+
};
31+
2632
export default {
2733
Device,
2834
Input,
2935
Axis,
36+
MouseButton,
3037
};

Sources/Rendering/Core/RenderWindowInteractor/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vtkObject, vtkSubscription } from '../../../interfaces';
22
import { Nullable } from '../../../types';
33
import vtkRenderer from '../Renderer';
4-
import { Axis, Device, Input } from './Constants';
4+
import { Axis, Device, Input, MouseButton } from './Constants';
55

66
declare enum handledEvents {
77
'StartAnimation',
@@ -1397,5 +1397,6 @@ export declare const vtkRenderWindowInteractor: {
13971397
Device: typeof Device;
13981398
Input: typeof Input;
13991399
Axis: typeof Axis;
1400+
MouseButton: typeof MouseButton;
14001401
};
14011402
export default vtkRenderWindowInteractor;

Sources/Rendering/Core/RenderWindowInteractor/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ function vtkRenderWindowInteractor(publicAPI, model) {
184184
x: scaleX * (source.clientX - bounds.left),
185185
y: scaleY * (bounds.height - source.clientY + bounds.top),
186186
z: 0,
187+
movementX: scaleX * source.movementX,
188+
movementY: scaleY * source.movementY,
187189
};
188190

189191
// if multitouch, do not update the current renderer

0 commit comments

Comments
 (0)