Skip to content

Commit 99adbf0

Browse files
committed
Made TrackballControls a bit nicer
1 parent 3946176 commit 99adbf0

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

examples/js/controls/TrackballControls.js

+28-44
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ THREE.TrackballControls = function ( object, domElement ) {
66

77
THREE.EventTarget.call( this );
88

9-
var _this = this,
10-
STATE = { NONE : -1, ROTATE : 0, ZOOM : 1, PAN : 2 };
9+
var _this = this;
10+
var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 };
1111

1212
this.object = object;
1313
this.domElement = ( domElement !== undefined ) ? domElement : document;
@@ -41,8 +41,8 @@ THREE.TrackballControls = function ( object, domElement ) {
4141

4242
var lastPosition = new THREE.Vector3();
4343

44-
var _keyPressed = false,
45-
_state = STATE.NONE,
44+
var _state = STATE.NONE,
45+
_prevState = STATE.NONE,
4646

4747
_eye = new THREE.Vector3(),
4848

@@ -268,8 +268,10 @@ THREE.TrackballControls = function ( object, domElement ) {
268268
function keydown( event ) {
269269

270270
if ( ! _this.enabled ) return;
271+
272+
window.removeEventListener( 'keydown', keydown );
271273

272-
//event.preventDefault();
274+
_prevState = _state;
273275

274276
if ( _state !== STATE.NONE ) {
275277

@@ -288,24 +290,16 @@ THREE.TrackballControls = function ( object, domElement ) {
288290
_state = STATE.PAN;
289291

290292
}
291-
292-
if ( _state !== STATE.NONE ) {
293-
294-
_keyPressed = true;
295-
296-
}
297-
293+
298294
}
299295

300296
function keyup( event ) {
301297

302298
if ( ! _this.enabled ) return;
303-
304-
if ( _state !== STATE.NONE ) {
305-
306-
_state = STATE.NONE;
307-
308-
}
299+
300+
_state = _prevState;
301+
302+
window.addEventListener( 'keydown', keydown, false );
309303

310304
}
311305

@@ -317,46 +311,35 @@ THREE.TrackballControls = function ( object, domElement ) {
317311
event.stopPropagation();
318312

319313
if ( _state === STATE.NONE ) {
320-
314+
321315
_state = event.button;
316+
317+
}
322318

323-
if ( _state === STATE.ROTATE && !_this.noRotate ) {
324-
325-
_rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
319+
if ( _state === STATE.ROTATE && !_this.noRotate ) {
326320

327-
} else if ( _state === STATE.ZOOM && !_this.noZoom ) {
321+
_rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
328322

329-
_zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
323+
} else if ( _state === STATE.ZOOM && !_this.noZoom ) {
330324

331-
} else if ( !this.noPan ) {
325+
_zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
332326

333-
_panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
327+
} else if ( _state === STATE.PAN && !_this.noPan ) {
334328

335-
}
329+
_panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
336330

337331
}
332+
333+
document.addEventListener( 'mousemove', mousemove, false );
334+
document.addEventListener( 'mouseup', mouseup, false );
338335

339336
}
340337

341338
function mousemove( event ) {
342339

343340
if ( ! _this.enabled ) return;
344341

345-
if ( _keyPressed ) {
346-
347-
_rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
348-
_zoomStart = _zoomEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
349-
_panStart = _panEnd = _this.getMouseOnScreen( event.clientX, event.clientY );
350-
351-
_keyPressed = false;
352-
353-
}
354-
355-
if ( _state === STATE.NONE ) {
356-
357-
return;
358-
359-
} else if ( _state === STATE.ROTATE && !_this.noRotate ) {
342+
if ( _state === STATE.ROTATE && !_this.noRotate ) {
360343

361344
_rotateEnd = _this.getMouseProjectionOnBall( event.clientX, event.clientY );
362345

@@ -380,6 +363,9 @@ THREE.TrackballControls = function ( object, domElement ) {
380363
event.stopPropagation();
381364

382365
_state = STATE.NONE;
366+
367+
document.removeEventListener( 'mousemove', mousemove );
368+
document.removeEventListener( 'mouseup', mouseup );
383369

384370
}
385371

@@ -408,9 +394,7 @@ THREE.TrackballControls = function ( object, domElement ) {
408394

409395
this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
410396

411-
this.domElement.addEventListener( 'mousemove', mousemove, false );
412397
this.domElement.addEventListener( 'mousedown', mousedown, false );
413-
this.domElement.addEventListener( 'mouseup', mouseup, false );
414398
this.domElement.addEventListener( 'DOMMouseScroll', mousewheel, false );
415399
this.domElement.addEventListener( 'mousewheel', mousewheel, false );
416400

0 commit comments

Comments
 (0)