From 992fa06eebfea97d85e4e6889bf2a986b63a6e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Moreira?= Date: Tue, 27 Jun 2023 15:29:28 -0300 Subject: [PATCH] FlyControls: Introduce `enabled` property. (#26154) * FlyControls supports turn it on/off * Changing scope variable to `this` keyword --- examples/jsm/controls/FlyControls.js | 34 ++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/examples/jsm/controls/FlyControls.js b/examples/jsm/controls/FlyControls.js index 0c6bb3a5442c30..88943de7b7fde0 100644 --- a/examples/jsm/controls/FlyControls.js +++ b/examples/jsm/controls/FlyControls.js @@ -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; @@ -44,7 +47,7 @@ class FlyControls extends EventDispatcher { this.keydown = function ( event ) { - if ( event.altKey ) { + if ( event.altKey || this.enabled === false ) { return; @@ -82,6 +85,8 @@ class FlyControls extends EventDispatcher { this.keyup = function ( event ) { + if ( this.enabled === false ) return; + switch ( event.code ) { case 'ShiftLeft': @@ -114,6 +119,8 @@ class FlyControls extends EventDispatcher { this.pointerdown = function ( event ) { + if ( this.enabled === false ) return; + if ( this.dragToLook ) { this.status ++; @@ -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(); @@ -152,6 +161,8 @@ class FlyControls extends EventDispatcher { this.pointerup = function ( event ) { + if ( this.enabled === false ) return; + if ( this.dragToLook ) { this.status --; @@ -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; @@ -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 ); @@ -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 ); @@ -275,10 +297,4 @@ class FlyControls extends EventDispatcher { } -function contextmenu( event ) { - - event.preventDefault(); - -} - export { FlyControls };