提交 3eff5909 编写于 作者: A Aleksandar Rodic

added touch support for transform controls

improved touch support for editor controls
上级 1fc4e4e1
......@@ -43,8 +43,9 @@ var Viewport = function ( editor ) {
var transformControls = new THREE.TransformControls( camera, container.dom );
transformControls.addEventListener( 'change', function () {
// TODO: Differentiate from transform hovers change and object transform change
controls.enabled = true;
if ( transformControls.axis ) controls.enabled = false;
if (editor.selected) signals.objectChanged.dispatch( editor.selected );
} );
......@@ -98,12 +99,7 @@ var Viewport = function ( editor ) {
y = (event.clientY - rect.top) / rect.height;
onMouseDownPosition.set( x, y );
if ( transformControls.hovered === false ) {
controls.enabled = true;
document.addEventListener( 'mouseup', onMouseUp, false );
}
document.addEventListener( 'mouseup', onMouseUp, false );
};
......@@ -144,8 +140,6 @@ var Viewport = function ( editor ) {
}
controls.enabled = false;
document.removeEventListener( 'mouseup', onMouseUp );
};
......@@ -175,7 +169,6 @@ var Viewport = function ( editor ) {
signals.objectChanged.dispatch( camera );
} );
controls.enabled = false;
// signals
......
......@@ -193,29 +193,37 @@ THREE.EditorControls = function ( object, domElement ) {
// touch
var touch = new THREE.Vector3();
var prevTouch = new THREE.Vector3();
var touches = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
var prevTouches = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
var prevDistance = null;
function touchStart( event ) {
if ( scope.enabled === false ) return;
var touches = event.touches;
switch ( event.touches.length ) {
switch ( touches.length ) {
case 1:
touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
touches[ 1 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
break;
case 2:
var dx = touches[ 0 ].pageX - touches[ 1 ].pageX;
var dy = touches[ 0 ].pageY - touches[ 1 ].pageY;
prevDistance = Math.sqrt( dx * dx + dy * dy );
touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
touches[ 1 ].set( event.touches[ 1 ].pageX, event.touches[ 1 ].pageY, 0 );
prevDistance = touches[ 0 ].distanceTo( touches[ 1 ] );
break;
}
prevTouch.set( touches[ 0 ].pageX, touches[ 0 ].pageY, 0 );
prevTouches[ 0 ].copy( touches[ 0 ] );
prevTouches[ 1 ].copy( touches[ 1 ] );
}
function touchMove( event ) {
if ( scope.enabled === false ) return;
......@@ -223,31 +231,47 @@ THREE.EditorControls = function ( object, domElement ) {
event.preventDefault();
event.stopPropagation();
var touches = event.touches;
var getClosest = function( touch, touches ) {
var closest = touches[ 0 ];
touch.set( touches[ 0 ].pageX, touches[ 0 ].pageY, 0 );
for ( var i in touches ) {
if ( closest.distanceTo(touch) > touches[ i ].distanceTo(touch) ) closest = touches[ i ];
}
switch ( touches.length ) {
return closest;
}
switch ( event.touches.length ) {
case 1:
scope.rotate( touch.sub( prevTouch ).multiplyScalar( - 0.005 ) );
touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
touches[ 1 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
scope.rotate( touches[ 0 ].sub( getClosest( touches[ 0 ] ,prevTouches ) ).multiplyScalar( - 0.005 ) );
break;
case 2:
var dx = touches[ 0 ].pageX - touches[ 1 ].pageX;
var dy = touches[ 0 ].pageY - touches[ 1 ].pageY;
var distance = Math.sqrt( dx * dx + dy * dy );
touches[ 0 ].set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY, 0 );
touches[ 1 ].set( event.touches[ 1 ].pageX, event.touches[ 1 ].pageY, 0 );
distance = touches[ 0 ].distanceTo( touches[ 1 ] );
scope.zoom( new THREE.Vector3( 0, 0, prevDistance - distance ) );
prevDistance = distance;
break;
case 3:
scope.pan( touch.sub( prevTouch ).setX( - touch.x ) );
var offset0 = touches[ 0 ].clone().sub( getClosest( touches[ 0 ] ,prevTouches ) );
var offset1 = touches[ 1 ].clone().sub( getClosest( touches[ 1 ] ,prevTouches ) );
offset0.x = -offset0.x;
offset1.x = -offset1.x;
scope.pan( offset0.add( offset1 ).multiplyScalar( 0.5 ) );
break;
}
prevTouch.set( touches[ 0 ].pageX, touches[ 0 ].pageY, 0 );
prevTouches[ 0 ].copy( touches[ 0 ] );
prevTouches[ 1 ].copy( touches[ 1 ] );
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册