Skip to content

Commit b71b063

Browse files
committed
added keylistening to TrackballCamera
1 parent 3174dc2 commit b71b063

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/extras/cameras/TrackballCamera.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* noZoom: <bool>,
1717
* noPan: <bool>,
1818
19+
* keys: <Array>, // [ rotateKey, zoomKey, panKey ]
20+
1921
* domElement: <HTMLElement>,
2022
* }
2123
*/
@@ -33,7 +35,10 @@ THREE.TrackballCamera = function ( parameters ) {
3335

3436
this.noZoom = false;
3537
this.noPan = false;
36-
38+
39+
this.keys = [ 65, 83, 68 ];
40+
this.keyPressed = false;
41+
3742
this.domElement = document;
3843

3944
if ( parameters ) {
@@ -46,6 +51,8 @@ THREE.TrackballCamera = function ( parameters ) {
4651
if ( parameters.noZoom !== undefined ) this.noZoom = parameters.noZoom;
4752
if ( parameters.noPan !== undefined ) this.noPan = parameters.noPan;
4853

54+
if ( parameters.keys !== undefined ) this.keys = parameters.keys;
55+
4956
if ( parameters.domElement !== undefined ) this.domElement = parameters.domElement;
5057

5158
}
@@ -103,14 +110,37 @@ THREE.TrackballCamera.prototype.handleEvent = function ( event ) {
103110
};
104111

105112
THREE.TrackballCamera.prototype.keydown = function( event ) {
113+
114+
if ( this.state !== this.STATE.NONE ) {
115+
116+
return;
117+
118+
} else if ( event.keyCode === this.keys[ this.STATE.ROTATE ] ) {
119+
120+
this.state = this.STATE.ROTATE;
121+
this.keyPressed = true;
122+
123+
} else if ( event.keyCode === this.keys[ this.STATE.ZOOM ] ) {
106124

125+
this.state = this.STATE.ZOOM;
126+
this.keyPressed = true;
107127

128+
} else if ( event.keyCode === this.keys[ this.STATE.PAN ] ) {
129+
130+
this.state = this.STATE.PAN;
131+
this.keyPressed = true;
132+
133+
}
108134

109135
};
110136

111137
THREE.TrackballCamera.prototype.keyup = function( event ) {
112138

139+
if ( this.state !== this.STATE.NONE ) {
113140

141+
this.state = this.STATE.NONE;
142+
143+
}
114144

115145
};
116146

@@ -138,6 +168,15 @@ THREE.TrackballCamera.prototype.mousedown = function(event) {
138168
};
139169

140170
THREE.TrackballCamera.prototype.mousemove = function( event ) {
171+
172+
if ( this.keyPressed ) {
173+
174+
this.start = this.getMouseProjectionOnBall( event.clientX, event.clientY );
175+
this.mouse = this.getMouseOnScreen( event.clientX, event.clientY );
176+
177+
this.keyPressed = false;
178+
179+
}
141180

142181
if ( this.state === this.STATE.NONE ) {
143182

@@ -171,7 +210,7 @@ THREE.TrackballCamera.prototype.mouseup = function( event ) {
171210
THREE.TrackballCamera.prototype.getScreenDimensions = function() {
172211

173212
if ( this.domElement != document ) {
174-
213+
175214
return {
176215
width : this.domElement.offsetWidth,
177216
height : this.domElement.offsetHeight,
@@ -275,8 +314,8 @@ THREE.TrackballCamera.prototype.panCamera = function( clientX, clientY ) {
275314
var pan = this.position.clone().crossSelf( this.up ).setLength( mouseChange.x );
276315
pan.addSelf( this.up.clone().setLength( mouseChange.y ) );
277316

278-
this.position.addSelf(pan);
279-
this.target.position.addSelf(pan);
317+
this.position.addSelf( pan );
318+
this.target.position.addSelf( pan );
280319

281320
this.mouse = newMouse;
282321

0 commit comments

Comments
 (0)