Skip to content

Commit aed5e2e

Browse files
authored
Merge pull request #16997 from Mugen87/dev34
TrackballControls: Fix key states.
2 parents 1039703 + 2ad1afb commit aed5e2e

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

examples/js/controls/TrackballControls.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ THREE.TrackballControls = function ( object, domElement ) {
4444
var lastPosition = new THREE.Vector3();
4545

4646
var _state = STATE.NONE,
47-
_prevState = STATE.NONE,
47+
_keyState = STATE.NONE,
4848

4949
_eye = new THREE.Vector3(),
5050

@@ -324,7 +324,7 @@ THREE.TrackballControls = function ( object, domElement ) {
324324
this.reset = function () {
325325

326326
_state = STATE.NONE;
327-
_prevState = STATE.NONE;
327+
_keyState = STATE.NONE;
328328

329329
_this.target.copy( _this.target0 );
330330
_this.object.position.copy( _this.position0 );
@@ -348,33 +348,31 @@ THREE.TrackballControls = function ( object, domElement ) {
348348

349349
window.removeEventListener( 'keydown', keydown );
350350

351-
_prevState = _state;
352-
353-
if ( _state !== STATE.NONE ) {
351+
if ( _keyState !== STATE.NONE ) {
354352

355353
return;
356354

357355
} else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && ! _this.noRotate ) {
358356

359-
_state = STATE.ROTATE;
357+
_keyState = STATE.ROTATE;
360358

361359
} else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && ! _this.noZoom ) {
362360

363-
_state = STATE.ZOOM;
361+
_keyState = STATE.ZOOM;
364362

365363
} else if ( event.keyCode === _this.keys[ STATE.PAN ] && ! _this.noPan ) {
366364

367-
_state = STATE.PAN;
365+
_keyState = STATE.PAN;
368366

369367
}
370368

371369
}
372370

373-
function keyup( event ) {
371+
function keyup() {
374372

375373
if ( _this.enabled === false ) return;
376374

377-
_state = _prevState;
375+
_keyState = STATE.NONE;
378376

379377
window.addEventListener( 'keydown', keydown, false );
380378

@@ -393,17 +391,19 @@ THREE.TrackballControls = function ( object, domElement ) {
393391

394392
}
395393

396-
if ( _state === STATE.ROTATE && ! _this.noRotate ) {
394+
var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
395+
396+
if ( state === STATE.ROTATE && ! _this.noRotate ) {
397397

398398
_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
399399
_movePrev.copy( _moveCurr );
400400

401-
} else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
401+
} else if ( state === STATE.ZOOM && ! _this.noZoom ) {
402402

403403
_zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
404404
_zoomEnd.copy( _zoomStart );
405405

406-
} else if ( _state === STATE.PAN && ! _this.noPan ) {
406+
} else if ( state === STATE.PAN && ! _this.noPan ) {
407407

408408
_panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
409409
_panEnd.copy( _panStart );
@@ -424,16 +424,18 @@ THREE.TrackballControls = function ( object, domElement ) {
424424
event.preventDefault();
425425
event.stopPropagation();
426426

427-
if ( _state === STATE.ROTATE && ! _this.noRotate ) {
427+
var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
428+
429+
if ( state === STATE.ROTATE && ! _this.noRotate ) {
428430

429431
_movePrev.copy( _moveCurr );
430432
_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
431433

432-
} else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
434+
} else if ( state === STATE.ZOOM && ! _this.noZoom ) {
433435

434436
_zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
435437

436-
} else if ( _state === STATE.PAN && ! _this.noPan ) {
438+
} else if ( state === STATE.PAN && ! _this.noPan ) {
437439

438440
_panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
439441

examples/jsm/controls/TrackballControls.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var TrackballControls = function ( object, domElement ) {
5151
var lastPosition = new Vector3();
5252

5353
var _state = STATE.NONE,
54-
_prevState = STATE.NONE,
54+
_keyState = STATE.NONE,
5555

5656
_eye = new Vector3(),
5757

@@ -331,7 +331,7 @@ var TrackballControls = function ( object, domElement ) {
331331
this.reset = function () {
332332

333333
_state = STATE.NONE;
334-
_prevState = STATE.NONE;
334+
_keyState = STATE.NONE;
335335

336336
_this.target.copy( _this.target0 );
337337
_this.object.position.copy( _this.position0 );
@@ -355,33 +355,31 @@ var TrackballControls = function ( object, domElement ) {
355355

356356
window.removeEventListener( 'keydown', keydown );
357357

358-
_prevState = _state;
359-
360-
if ( _state !== STATE.NONE ) {
358+
if ( _keyState !== STATE.NONE ) {
361359

362360
return;
363361

364362
} else if ( event.keyCode === _this.keys[ STATE.ROTATE ] && ! _this.noRotate ) {
365363

366-
_state = STATE.ROTATE;
364+
_keyState = STATE.ROTATE;
367365

368366
} else if ( event.keyCode === _this.keys[ STATE.ZOOM ] && ! _this.noZoom ) {
369367

370-
_state = STATE.ZOOM;
368+
_keyState = STATE.ZOOM;
371369

372370
} else if ( event.keyCode === _this.keys[ STATE.PAN ] && ! _this.noPan ) {
373371

374-
_state = STATE.PAN;
372+
_keyState = STATE.PAN;
375373

376374
}
377375

378376
}
379377

380-
function keyup( event ) {
378+
function keyup() {
381379

382380
if ( _this.enabled === false ) return;
383381

384-
_state = _prevState;
382+
_keyState = STATE.NONE;
385383

386384
window.addEventListener( 'keydown', keydown, false );
387385

@@ -400,17 +398,19 @@ var TrackballControls = function ( object, domElement ) {
400398

401399
}
402400

403-
if ( _state === STATE.ROTATE && ! _this.noRotate ) {
401+
var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
402+
403+
if ( state === STATE.ROTATE && ! _this.noRotate ) {
404404

405405
_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
406406
_movePrev.copy( _moveCurr );
407407

408-
} else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
408+
} else if ( state === STATE.ZOOM && ! _this.noZoom ) {
409409

410410
_zoomStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
411411
_zoomEnd.copy( _zoomStart );
412412

413-
} else if ( _state === STATE.PAN && ! _this.noPan ) {
413+
} else if ( state === STATE.PAN && ! _this.noPan ) {
414414

415415
_panStart.copy( getMouseOnScreen( event.pageX, event.pageY ) );
416416
_panEnd.copy( _panStart );
@@ -431,16 +431,18 @@ var TrackballControls = function ( object, domElement ) {
431431
event.preventDefault();
432432
event.stopPropagation();
433433

434-
if ( _state === STATE.ROTATE && ! _this.noRotate ) {
434+
var state = ( _keyState !== STATE.NONE ) ? _keyState : _state;
435+
436+
if ( state === STATE.ROTATE && ! _this.noRotate ) {
435437

436438
_movePrev.copy( _moveCurr );
437439
_moveCurr.copy( getMouseOnCircle( event.pageX, event.pageY ) );
438440

439-
} else if ( _state === STATE.ZOOM && ! _this.noZoom ) {
441+
} else if ( state === STATE.ZOOM && ! _this.noZoom ) {
440442

441443
_zoomEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
442444

443-
} else if ( _state === STATE.PAN && ! _this.noPan ) {
445+
} else if ( state === STATE.PAN && ! _this.noPan ) {
444446

445447
_panEnd.copy( getMouseOnScreen( event.pageX, event.pageY ) );
446448

0 commit comments

Comments
 (0)