Skip to content

Commit 992fa06

Browse files
authored
FlyControls: Introduce enabled property. (#26154)
* FlyControls supports turn it on/off * Changing scope variable to `this` keyword
1 parent e2ea766 commit 992fa06

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

examples/jsm/controls/FlyControls.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class FlyControls extends EventDispatcher {
1717

1818
// API
1919

20+
// Set to false to disable this control
21+
this.enabled = true;
22+
2023
this.movementSpeed = 1.0;
2124
this.rollSpeed = 0.005;
2225

@@ -44,7 +47,7 @@ class FlyControls extends EventDispatcher {
4447

4548
this.keydown = function ( event ) {
4649

47-
if ( event.altKey ) {
50+
if ( event.altKey || this.enabled === false ) {
4851

4952
return;
5053

@@ -82,6 +85,8 @@ class FlyControls extends EventDispatcher {
8285

8386
this.keyup = function ( event ) {
8487

88+
if ( this.enabled === false ) return;
89+
8590
switch ( event.code ) {
8691

8792
case 'ShiftLeft':
@@ -114,6 +119,8 @@ class FlyControls extends EventDispatcher {
114119

115120
this.pointerdown = function ( event ) {
116121

122+
if ( this.enabled === false ) return;
123+
117124
if ( this.dragToLook ) {
118125

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

136143
this.pointermove = function ( event ) {
137144

145+
if ( this.enabled === false ) return;
146+
138147
if ( ! this.dragToLook || this.status > 0 ) {
139148

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

153162
this.pointerup = function ( event ) {
154163

164+
if ( this.enabled === false ) return;
165+
155166
if ( this.dragToLook ) {
156167

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

176187
};
177188

189+
this.contextMenu = function ( event ) {
190+
191+
if ( this.enabled === false ) return;
192+
193+
event.preventDefault();
194+
195+
};
196+
178197
this.update = function ( delta ) {
179198

199+
if ( this.enabled === false ) return;
200+
180201
const moveMult = delta * scope.movementSpeed;
181202
const rotMult = delta * scope.rollSpeed;
182203

@@ -244,7 +265,7 @@ class FlyControls extends EventDispatcher {
244265

245266
this.dispose = function () {
246267

247-
this.domElement.removeEventListener( 'contextmenu', contextmenu );
268+
this.domElement.removeEventListener( 'contextmenu', _contextmenu );
248269
this.domElement.removeEventListener( 'pointerdown', _pointerdown );
249270
this.domElement.removeEventListener( 'pointermove', _pointermove );
250271
this.domElement.removeEventListener( 'pointerup', _pointerup );
@@ -254,13 +275,14 @@ class FlyControls extends EventDispatcher {
254275

255276
};
256277

278+
const _contextmenu = this.contextMenu.bind( this );
257279
const _pointermove = this.pointermove.bind( this );
258280
const _pointerdown = this.pointerdown.bind( this );
259281
const _pointerup = this.pointerup.bind( this );
260282
const _keydown = this.keydown.bind( this );
261283
const _keyup = this.keyup.bind( this );
262284

263-
this.domElement.addEventListener( 'contextmenu', contextmenu );
285+
this.domElement.addEventListener( 'contextmenu', _contextmenu );
264286
this.domElement.addEventListener( 'pointerdown', _pointerdown );
265287
this.domElement.addEventListener( 'pointermove', _pointermove );
266288
this.domElement.addEventListener( 'pointerup', _pointerup );
@@ -275,10 +297,4 @@ class FlyControls extends EventDispatcher {
275297

276298
}
277299

278-
function contextmenu( event ) {
279-
280-
event.preventDefault();
281-
282-
}
283-
284300
export { FlyControls };

0 commit comments

Comments
 (0)