Skip to content

feat(RenderWindowInteractor): commit mouse movement data in pointerlock #3241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,30 @@ function vtkMouseCameraTrackballFirstPersonManipulator(publicAPI, model) {
const internal = {
interactor: null,
renderer: null,
previousPosition: null,
};

//--------------------------------------------------------------------------

publicAPI.onButtonDown = (interactor, renderer, position) => {
internal.previousPosition = position;

if (model.usePointerLock && !interactor.isPointerLocked()) {
Object.assign(internal, { interactor, renderer });
interactor.requestPointerLock();
publicAPI.startPointerLockInteraction();
}
};

//--------------------------------------------------------------------------

publicAPI.startPointerLockInteraction = () => {
const { interactor } = internal;

// TODO: at some point, this should perhaps be done in
// RenderWindowInteractor instead of here.
// We need to hook into mousemove directly for two reasons:
// 1. We need to keep receiving mouse move events after the mouse button
// is released. This is currently not possible with
// vtkInteractorStyleManipulator.
// 2. Since the mouse is stationary in pointer lock mode, we need the
// event.movementX and event.movementY info, which are not currently
// passed via interactor.onMouseMove.
document.addEventListener('mousemove', publicAPI.onPointerLockMove);

let subscription = null;
const endInteraction = () => {
document.removeEventListener('mousemove', publicAPI.onPointerLockMove);
subscription.unsubscribe();
};
subscription = interactor.onEndPointerLock(endInteraction);
};

//--------------------------------------------------------------------------

publicAPI.onPointerLockMove = (e) => {
const sensitivity = model.sensitivity;
const yaw = -1 * e.movementX * sensitivity;
const pitch = -1 * e.movementY * sensitivity;

publicAPI.moveCamera(yaw, pitch);
};

//--------------------------------------------------------------------------

publicAPI.onMouseMove = (interactor, renderer, position) => {
// This is currently only being called for non pointer lock mode
if (!position) {
return;
}

const { previousPosition } = internal;

const sensitivity = model.sensitivity;
const yaw = (previousPosition.x - position.x) * sensitivity;
const pitch = (position.y - previousPosition.y) * sensitivity;
const yaw = -position.movementX * sensitivity;
const pitch = -position.movementY * sensitivity;

Object.assign(internal, { interactor, renderer });
publicAPI.moveCamera(yaw, pitch);

internal.previousPosition = position;
};

//--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import vtkInteractorStyle from '../../../Rendering/Core/InteractorStyle';
import {
Device,
Input,
MouseButton,
} from '../../../Rendering/Core/RenderWindowInteractor/Constants';
import { Nullable, Vector3 } from '../../../types';

Expand Down Expand Up @@ -120,7 +121,7 @@ export interface vtkInteractorStyleManipulator extends vtkInteractorStyle {
* @param alt alt enabled
*/
findMouseManipulator(
button: number,
button: MouseButton,
shift: boolean,
scroll: boolean,
alt: boolean
Expand Down Expand Up @@ -285,13 +286,13 @@ export interface vtkInteractorStyleManipulator extends vtkInteractorStyle {
* @param button which button
* @param callData event data
*/
onButtonDown(button: number, callData: unknown): void;
onButtonDown(button: MouseButton, callData: unknown): void;

/**
* Handles a button up event.
* @param button which button
*/
onButtonUp(button: number): void;
onButtonUp(button: MouseButton): void;

/**
* Sets the rotation factor.
Expand Down
23 changes: 15 additions & 8 deletions Sources/Interaction/Style/InteractorStyleManipulator/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import macro from 'vtk.js/Sources/macros';
import { MouseButton } from 'vtk.js/Sources/Rendering/Core/RenderWindowInteractor/Constants';
import vtkInteractorStyle from 'vtk.js/Sources/Rendering/Core/InteractorStyle';

const { vtkDebugMacro } = macro;
Expand Down Expand Up @@ -268,19 +269,19 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
//-------------------------------------------------------------------------
publicAPI.handleLeftButtonPress = (callData) => {
model.previousPosition = callData.position;
publicAPI.onButtonDown(1, callData);
publicAPI.onButtonDown(MouseButton.LeftButton, callData);
};

//-------------------------------------------------------------------------
publicAPI.handleMiddleButtonPress = (callData) => {
model.previousPosition = callData.position;
publicAPI.onButtonDown(2, callData);
publicAPI.onButtonDown(MouseButton.MiddleButton, callData);
};

//-------------------------------------------------------------------------
publicAPI.handleRightButtonPress = (callData) => {
model.previousPosition = callData.position;
publicAPI.onButtonDown(3, callData);
publicAPI.onButtonDown(MouseButton.RightButton, callData);
};

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -397,17 +398,17 @@ function vtkInteractorStyleManipulator(publicAPI, model) {

//-------------------------------------------------------------------------
publicAPI.handleLeftButtonRelease = () => {
publicAPI.onButtonUp(1);
publicAPI.onButtonUp(MouseButton.LeftButton);
};

//-------------------------------------------------------------------------
publicAPI.handleMiddleButtonRelease = () => {
publicAPI.onButtonUp(2);
publicAPI.onButtonUp(MouseButton.MiddleButton);
};

//-------------------------------------------------------------------------
publicAPI.handleRightButtonRelease = () => {
publicAPI.onButtonUp(3);
publicAPI.onButtonUp(MouseButton.RightButton);
};

//-------------------------------------------------------------------------
Expand All @@ -421,12 +422,18 @@ function vtkInteractorStyleManipulator(publicAPI, model) {
) {
model.currentManipulator.onButtonUp(model._interactor);
model.currentManipulator.endInteraction();
model.currentManipulator = null;
model._interactor.cancelAnimation(publicAPI.onButtonDown);
if (!model._interactor.isPointerLocked()) {
model.currentManipulator = null;
}
publicAPI.invokeEndInteractionEvent(END_INTERACTION_EVENT);
}
};

//-------------------------------------------------------------------------
publicAPI.handleEndPointerLock = () => {
model.currentManipulator = null;
};

//-------------------------------------------------------------------------
publicAPI.handleStartMouseWheel = (callData) => {
// Must not be processing a wheel interaction to start another.
Expand Down
7 changes: 7 additions & 0 deletions Sources/Rendering/Core/RenderWindowInteractor/Constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ export declare enum Axis {
ThumbstickY = 4,
}

export declare enum MouseButton {
LeftButton = 1,
MiddleButton = 2,
RightButton = 3,
}

declare const _default: {
Device: typeof Device;
Input: typeof Input;
Axis: typeof Axis;
MouseButton: typeof MouseButton;
};
export default _default;
7 changes: 7 additions & 0 deletions Sources/Rendering/Core/RenderWindowInteractor/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ export const Axis = {
ThumbstickY: 4,
};

export const MouseButton = {
LeftButton: 1,
MiddleButton: 2,
RightButton: 3,
};

export default {
Device,
Input,
Axis,
MouseButton,
};
3 changes: 2 additions & 1 deletion Sources/Rendering/Core/RenderWindowInteractor/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vtkObject, vtkSubscription } from '../../../interfaces';
import { Nullable } from '../../../types';
import vtkRenderer from '../Renderer';
import { Axis, Device, Input } from './Constants';
import { Axis, Device, Input, MouseButton } from './Constants';

declare enum handledEvents {
'StartAnimation',
Expand Down Expand Up @@ -1397,5 +1397,6 @@ export declare const vtkRenderWindowInteractor: {
Device: typeof Device;
Input: typeof Input;
Axis: typeof Axis;
MouseButton: typeof MouseButton;
};
export default vtkRenderWindowInteractor;
2 changes: 2 additions & 0 deletions Sources/Rendering/Core/RenderWindowInteractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ function vtkRenderWindowInteractor(publicAPI, model) {
x: scaleX * (source.clientX - bounds.left),
y: scaleY * (bounds.height - source.clientY + bounds.top),
z: 0,
movementX: scaleX * source.movementX,
movementY: scaleY * source.movementY,
};

// if multitouch, do not update the current renderer
Expand Down