Skip to content

Commit

Permalink
FlyControls: Introduce enabled property. (#26154)
Browse files Browse the repository at this point in the history
* FlyControls supports turn it on/off

* Changing scope variable to `this` keyword
  • Loading branch information
andredsm authored Jun 27, 2023
1 parent e2ea766 commit 992fa06
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions examples/jsm/controls/FlyControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class FlyControls extends EventDispatcher {

// API

// Set to false to disable this control
this.enabled = true;

this.movementSpeed = 1.0;
this.rollSpeed = 0.005;

Expand Down Expand Up @@ -44,7 +47,7 @@ class FlyControls extends EventDispatcher {

this.keydown = function ( event ) {

if ( event.altKey ) {
if ( event.altKey || this.enabled === false ) {

return;

Expand Down Expand Up @@ -82,6 +85,8 @@ class FlyControls extends EventDispatcher {

this.keyup = function ( event ) {

if ( this.enabled === false ) return;

switch ( event.code ) {

case 'ShiftLeft':
Expand Down Expand Up @@ -114,6 +119,8 @@ class FlyControls extends EventDispatcher {

this.pointerdown = function ( event ) {

if ( this.enabled === false ) return;

if ( this.dragToLook ) {

this.status ++;
Expand All @@ -135,6 +142,8 @@ class FlyControls extends EventDispatcher {

this.pointermove = function ( event ) {

if ( this.enabled === false ) return;

if ( ! this.dragToLook || this.status > 0 ) {

const container = this.getContainerDimensions();
Expand All @@ -152,6 +161,8 @@ class FlyControls extends EventDispatcher {

this.pointerup = function ( event ) {

if ( this.enabled === false ) return;

if ( this.dragToLook ) {

this.status --;
Expand All @@ -175,8 +186,18 @@ class FlyControls extends EventDispatcher {

};

this.contextMenu = function ( event ) {

if ( this.enabled === false ) return;

event.preventDefault();

};

this.update = function ( delta ) {

if ( this.enabled === false ) return;

const moveMult = delta * scope.movementSpeed;
const rotMult = delta * scope.rollSpeed;

Expand Down Expand Up @@ -244,7 +265,7 @@ class FlyControls extends EventDispatcher {

this.dispose = function () {

this.domElement.removeEventListener( 'contextmenu', contextmenu );
this.domElement.removeEventListener( 'contextmenu', _contextmenu );
this.domElement.removeEventListener( 'pointerdown', _pointerdown );
this.domElement.removeEventListener( 'pointermove', _pointermove );
this.domElement.removeEventListener( 'pointerup', _pointerup );
Expand All @@ -254,13 +275,14 @@ class FlyControls extends EventDispatcher {

};

const _contextmenu = this.contextMenu.bind( this );
const _pointermove = this.pointermove.bind( this );
const _pointerdown = this.pointerdown.bind( this );
const _pointerup = this.pointerup.bind( this );
const _keydown = this.keydown.bind( this );
const _keyup = this.keyup.bind( this );

this.domElement.addEventListener( 'contextmenu', contextmenu );
this.domElement.addEventListener( 'contextmenu', _contextmenu );
this.domElement.addEventListener( 'pointerdown', _pointerdown );
this.domElement.addEventListener( 'pointermove', _pointermove );
this.domElement.addEventListener( 'pointerup', _pointerup );
Expand All @@ -275,10 +297,4 @@ class FlyControls extends EventDispatcher {

}

function contextmenu( event ) {

event.preventDefault();

}

export { FlyControls };

0 comments on commit 992fa06

Please sign in to comment.