Skip to content

Commit

Permalink
refacto(PlanarControls): change focus policy
Browse files Browse the repository at this point in the history
- remove auto focus when hovering the view.domElement
- simplify implementation of focus on mouse click
  • Loading branch information
mgermerie authored and gchoqueux committed Mar 15, 2022
1 parent 3786dcf commit 99fadc0
Showing 1 changed file with 8 additions and 35 deletions.
43 changes: 8 additions & 35 deletions src/Controls/PlanarControls.js
Expand Up @@ -108,8 +108,6 @@ const defaultOptions = {
instantTravel: false,
minZenithAngle: 0,
maxZenithAngle: 82.5,
focusOnMouseOver: true,
focusOnMouseClick: true,
handleCollision: true,
minDistanceCollision: 30,
enableSmartTravel: true,
Expand Down Expand Up @@ -167,8 +165,6 @@ export const PLANAR_CONTROL_EVENT = {
* rotation, in degrees.
* @param {number} [options.maxZenithAngle=82.5] The maximum reachable zenith angle for a camera
* rotation, in degrees.
* @param {boolean} [options.focusOnMouseOver=true] Set the focus on the canvas if hovered.
* @param {boolean} [options.focusOnMouseClick=true] Set the focus on the canvas if clicked.
* @param {boolean} [options.handleCollision=true]
*/
class PlanarControls extends THREE.EventDispatcher {
Expand Down Expand Up @@ -266,8 +262,12 @@ class PlanarControls extends THREE.EventDispatcher {
this.maxZenithAngle = (options.maxZenithAngle || defaultOptions.maxZenithAngle) * Math.PI / 180;

// focus policy options
this.focusOnMouseOver = options.focusOnMouseOver || defaultOptions.focusOnMouseOver;
this.focusOnMouseClick = options.focusOnMouseClick || defaultOptions.focusOnMouseClick;
if (options.focusOnMouseOver) {
console.warn('Planar controls \'focusOnMouseOver\' optional parameter has been removed.');
}
if (options.focusOnMouseClick) {
console.warn('Planar controls \'focusOnMouseClick\' optional parameter has been removed.');
}

// set collision options
this.handleCollision = options.handleCollision === undefined ?
Expand Down Expand Up @@ -297,8 +297,6 @@ class PlanarControls extends THREE.EventDispatcher {
this._handlerOnMouseUp = this.onMouseUp.bind(this);
this._handlerOnMouseMove = this.onMouseMove.bind(this);
this._handlerOnMouseWheel = this.onMouseWheel.bind(this);
this._handlerFocusOnMouseClick = this.onMouseClick.bind(this);
this._handlerFocusOnMouseOver = this.onMouseOver.bind(this);
this._handlerContextMenu = this.onContextMenu.bind(this);
this._handlerUpdate = this.update.bind(this);

Expand Down Expand Up @@ -894,13 +892,6 @@ class PlanarControls extends THREE.EventDispatcher {
this.view.domElement.addEventListener('mouseleave', this._handlerOnMouseUp, false);
this.view.domElement.addEventListener('mousemove', this._handlerOnMouseMove, false);
this.view.domElement.addEventListener('wheel', this._handlerOnMouseWheel, false);
// focus policy
if (this.focusOnMouseOver) {
this.view.domElement.addEventListener('mouseover', this._handlerFocusOnMouseOver, false);
}
if (this.focusOnMouseClick) {
this.view.domElement.addEventListener('click', this._handlerFocusOnMouseClick, false);
}
// prevent the default context menu from appearing when right-clicking
// this allows to use right-click for input without the menu appearing
this.view.domElement.addEventListener('contextmenu', this._handlerContextMenu, false);
Expand All @@ -918,8 +909,6 @@ class PlanarControls extends THREE.EventDispatcher {
this.view.domElement.removeEventListener('mouseleave', this._handlerOnMouseUp, false);
this.view.domElement.removeEventListener('mousemove', this._handlerOnMouseMove, false);
this.view.domElement.removeEventListener('wheel', this._handlerOnMouseWheel, false);
this.view.domElement.removeEventListener('mouseover', this._handlerFocusOnMouseOver, false);
this.view.domElement.removeEventListener('click', this._handlerFocusOnMouseClick, false);
this.view.domElement.removeEventListener('contextmenu', this._handlerContextMenu, false);
}

Expand Down Expand Up @@ -982,6 +971,8 @@ class PlanarControls extends THREE.EventDispatcher {
onMouseDown(event) {
event.preventDefault();

this.view.domElement.focus();

if (STATE.NONE !== this.state) {
return;
}
Expand Down Expand Up @@ -1100,24 +1091,6 @@ class PlanarControls extends THREE.EventDispatcher {
}
}

/**
* Set the focus on view's domElement according to focus policy regarding MouseOver
*
* @ignore
*/
onMouseOver() {
this.view.domElement.focus();
}

/**
* Set the focus on view's domElement according to focus policy regarding MouseClick
*
* @ignore
*/
onMouseClick() {
this.view.domElement.focus();
}

/**
* Catch and manage the event when the context menu is called (by a right-click on the window). We use this
* to prevent the context menu from appearing so we can use right click for other inputs.
Expand Down

0 comments on commit 99fadc0

Please sign in to comment.