MapControls.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @author mrdoob / http://mrdoob.com
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. * @author erich666 / http://erichaines.com
  7. * @author moroine / https://github.com/moroine
  8. */
  9. // This set of controls performs orbiting, dollying (zooming), and panning.
  10. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  11. // This is very similar to OrbitControls, another set of touch behavior
  12. //
  13. // Orbit - right mouse, or left mouse + ctrl/meta/shiftKey / touch: two-finger rotate
  14. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  15. // Pan - left mouse, or arrow keys / touch: one-finger move
  16. THREE.MapControls = function ( object, domElement ) {
  17. this.object = object;
  18. this.domElement = ( domElement !== undefined ) ? domElement : document;
  19. // Set to false to disable this control
  20. this.enabled = true;
  21. // "target" sets the location of focus, where the object orbits around
  22. this.target = new THREE.Vector3();
  23. // How far you can dolly in and out ( PerspectiveCamera only )
  24. this.minDistance = 0;
  25. this.maxDistance = Infinity;
  26. // How far you can zoom in and out ( OrthographicCamera only )
  27. this.minZoom = 0;
  28. this.maxZoom = Infinity;
  29. // How far you can orbit vertically, upper and lower limits.
  30. // Range is 0 to Math.PI radians.
  31. this.minPolarAngle = 0; // radians
  32. this.maxPolarAngle = Math.PI; // radians
  33. // How far you can orbit horizontally, upper and lower limits.
  34. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  35. this.minAzimuthAngle = - Infinity; // radians
  36. this.maxAzimuthAngle = Infinity; // radians
  37. // Set to true to enable damping (inertia)
  38. // If damping is enabled, you must call controls.update() in your animation loop
  39. this.enableDamping = false;
  40. this.dampingFactor = 0.25;
  41. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  42. // Set to false to disable zooming
  43. this.enableZoom = true;
  44. this.zoomSpeed = 1.0;
  45. // Set to false to disable rotating
  46. this.enableRotate = true;
  47. this.rotateSpeed = 1.0;
  48. // Set to false to disable panning
  49. this.enablePan = true;
  50. this.panSpeed = 1.0;
  51. this.screenSpacePanning = false; // if true, pan in screen-space
  52. this.keyPanSpeed = 7.0; // pixels moved per arrow key push
  53. // Set to true to automatically rotate around the target
  54. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  55. this.autoRotate = false;
  56. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  57. // Set to false to disable use of the keys
  58. this.enableKeys = true;
  59. // The four arrow keys
  60. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  61. // Mouse buttons
  62. this.mouseButtons = { LEFT: THREE.MOUSE.LEFT, MIDDLE: THREE.MOUSE.MIDDLE, RIGHT: THREE.MOUSE.RIGHT };
  63. // for reset
  64. this.target0 = this.target.clone();
  65. this.position0 = this.object.position.clone();
  66. this.zoom0 = this.object.zoom;
  67. //
  68. // public methods
  69. //
  70. this.getPolarAngle = function () {
  71. return spherical.phi;
  72. };
  73. this.getAzimuthalAngle = function () {
  74. return spherical.theta;
  75. };
  76. this.saveState = function () {
  77. scope.target0.copy( scope.target );
  78. scope.position0.copy( scope.object.position );
  79. scope.zoom0 = scope.object.zoom;
  80. };
  81. this.reset = function () {
  82. scope.target.copy( scope.target0 );
  83. scope.object.position.copy( scope.position0 );
  84. scope.object.zoom = scope.zoom0;
  85. scope.object.updateProjectionMatrix();
  86. scope.dispatchEvent( changeEvent );
  87. scope.update();
  88. state = STATE.NONE;
  89. };
  90. // this method is exposed, but perhaps it would be better if we can make it private...
  91. this.update = function () {
  92. var offset = new THREE.Vector3();
  93. // so camera.up is the orbit axis
  94. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  95. var quatInverse = quat.clone().inverse();
  96. var lastPosition = new THREE.Vector3();
  97. var lastQuaternion = new THREE.Quaternion();
  98. return function update() {
  99. var position = scope.object.position;
  100. offset.copy( position ).sub( scope.target );
  101. // rotate offset to "y-axis-is-up" space
  102. offset.applyQuaternion( quat );
  103. // angle from z-axis around y-axis
  104. spherical.setFromVector3( offset );
  105. if ( scope.autoRotate && state === STATE.NONE ) {
  106. rotateLeft( getAutoRotationAngle() );
  107. }
  108. spherical.theta += sphericalDelta.theta;
  109. spherical.phi += sphericalDelta.phi;
  110. // restrict theta to be between desired limits
  111. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  112. // restrict phi to be between desired limits
  113. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  114. spherical.makeSafe();
  115. spherical.radius *= scale;
  116. // restrict radius to be between desired limits
  117. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  118. // move target to panned location
  119. scope.target.add( panOffset );
  120. offset.setFromSpherical( spherical );
  121. // rotate offset back to "camera-up-vector-is-up" space
  122. offset.applyQuaternion( quatInverse );
  123. position.copy( scope.target ).add( offset );
  124. scope.object.lookAt( scope.target );
  125. if ( scope.enableDamping === true ) {
  126. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  127. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  128. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  129. } else {
  130. sphericalDelta.set( 0, 0, 0 );
  131. panOffset.set( 0, 0, 0 );
  132. }
  133. scale = 1;
  134. // update condition is:
  135. // min(camera displacement, camera rotation in radians)^2 > EPS
  136. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  137. if ( zoomChanged ||
  138. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  139. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  140. scope.dispatchEvent( changeEvent );
  141. lastPosition.copy( scope.object.position );
  142. lastQuaternion.copy( scope.object.quaternion );
  143. zoomChanged = false;
  144. return true;
  145. }
  146. return false;
  147. };
  148. }();
  149. this.dispose = function () {
  150. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  151. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  152. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  153. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  154. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  155. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  156. document.removeEventListener( 'mousemove', onMouseMove, false );
  157. document.removeEventListener( 'mouseup', onMouseUp, false );
  158. window.removeEventListener( 'keydown', onKeyDown, false );
  159. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  160. };
  161. //
  162. // internals
  163. //
  164. var scope = this;
  165. var changeEvent = { type: 'change' };
  166. var startEvent = { type: 'start' };
  167. var endEvent = { type: 'end' };
  168. var STATE = {
  169. NONE: 0,
  170. ROTATE_UP: 1,
  171. ROTATE_LEFT: 2,
  172. ROTATE: 3, // ROTATE_UP | ROTATE_LEFT
  173. DOLLY: 4,
  174. DOLLY_ROTATE: 7, // ROTATE | DOLLY
  175. PAN: 8,
  176. DOLLY_PAN: 12, // DOLLY | PAN
  177. };
  178. var state = STATE.NONE;
  179. var EPS = 0.000001;
  180. // current position in spherical coordinates
  181. var spherical = new THREE.Spherical();
  182. var sphericalDelta = new THREE.Spherical();
  183. var scale = 1;
  184. var panOffset = new THREE.Vector3();
  185. var zoomChanged = false;
  186. var rotateStart = new THREE.Vector2();
  187. var rotateStart2 = new THREE.Vector2();
  188. var rotateEnd = new THREE.Vector2();
  189. var rotateEnd2 = new THREE.Vector2();
  190. var rotateDelta = new THREE.Vector2();
  191. var rotateDelta2 = new THREE.Vector2();
  192. var rotateDeltaStartFingers = new THREE.Vector2();
  193. var rotateDeltaEndFingers = new THREE.Vector2();
  194. var panStart = new THREE.Vector2();
  195. var panEnd = new THREE.Vector2();
  196. var panDelta = new THREE.Vector2();
  197. var dollyStart = new THREE.Vector2();
  198. var dollyEnd = new THREE.Vector2();
  199. var dollyDelta = new THREE.Vector2();
  200. function getAutoRotationAngle() {
  201. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  202. }
  203. function getZoomScale() {
  204. return Math.pow( 0.95, scope.zoomSpeed );
  205. }
  206. function rotateLeft( angle ) {
  207. sphericalDelta.theta -= angle;
  208. }
  209. function rotateUp( angle ) {
  210. sphericalDelta.phi -= angle;
  211. }
  212. var panLeft = function () {
  213. var v = new THREE.Vector3();
  214. return function panLeft( distance, objectMatrix ) {
  215. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  216. v.multiplyScalar( - distance );
  217. panOffset.add( v );
  218. };
  219. }();
  220. var panUp = function () {
  221. var v = new THREE.Vector3();
  222. return function panUp( distance, objectMatrix ) {
  223. if ( scope.screenSpacePanning === true ) {
  224. v.setFromMatrixColumn( objectMatrix, 1 );
  225. } else {
  226. v.setFromMatrixColumn( objectMatrix, 0 );
  227. v.crossVectors( scope.object.up, v );
  228. }
  229. v.multiplyScalar( distance );
  230. panOffset.add( v );
  231. };
  232. }();
  233. // deltaX and deltaY are in pixels; right and down are positive
  234. var pan = function () {
  235. var offset = new THREE.Vector3();
  236. return function pan( deltaX, deltaY ) {
  237. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  238. if ( scope.object.isPerspectiveCamera ) {
  239. // perspective
  240. var position = scope.object.position;
  241. offset.copy( position ).sub( scope.target );
  242. var targetDistance = offset.length();
  243. // half of the fov is center to top of screen
  244. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  245. // we use only clientHeight here so aspect ratio does not distort speed
  246. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  247. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  248. } else if ( scope.object.isOrthographicCamera ) {
  249. // orthographic
  250. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  251. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  252. } else {
  253. // camera neither orthographic nor perspective
  254. console.warn( 'WARNING: MapControls.js encountered an unknown camera type - pan disabled.' );
  255. scope.enablePan = false;
  256. }
  257. };
  258. }();
  259. function dollyIn( dollyScale ) {
  260. if ( scope.object.isPerspectiveCamera ) {
  261. scale /= dollyScale;
  262. } else if ( scope.object.isOrthographicCamera ) {
  263. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  264. scope.object.updateProjectionMatrix();
  265. zoomChanged = true;
  266. } else {
  267. console.warn( 'WARNING: MapControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  268. scope.enableZoom = false;
  269. }
  270. }
  271. function dollyOut( dollyScale ) {
  272. if ( scope.object.isPerspectiveCamera ) {
  273. scale *= dollyScale;
  274. } else if ( scope.object.isOrthographicCamera ) {
  275. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  276. scope.object.updateProjectionMatrix();
  277. zoomChanged = true;
  278. } else {
  279. console.warn( 'WARNING: MapControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  280. scope.enableZoom = false;
  281. }
  282. }
  283. //
  284. // event callbacks - update the object state
  285. //
  286. function handleMouseDownRotate( event ) {
  287. //console.log( 'handleMouseDownRotate' );
  288. rotateStart.set( event.clientX, event.clientY );
  289. }
  290. function handleMouseDownDolly( event ) {
  291. //console.log( 'handleMouseDownDolly' );
  292. dollyStart.set( event.clientX, event.clientY );
  293. }
  294. function handleMouseDownPan( event ) {
  295. //console.log( 'handleMouseDownPan' );
  296. panStart.set( event.clientX, event.clientY );
  297. }
  298. function handleMouseMoveRotate( event ) {
  299. //console.log( 'handleMouseMoveRotate' );
  300. rotateEnd.set( event.clientX, event.clientY );
  301. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  302. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  303. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientHeight ); // yes, height
  304. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  305. rotateStart.copy( rotateEnd );
  306. scope.update();
  307. }
  308. function handleMouseMoveDolly( event ) {
  309. //console.log( 'handleMouseMoveDolly' );
  310. dollyEnd.set( event.clientX, event.clientY );
  311. dollyDelta.subVectors( dollyEnd, dollyStart );
  312. if ( dollyDelta.y > 0 ) {
  313. dollyIn( getZoomScale() );
  314. } else if ( dollyDelta.y < 0 ) {
  315. dollyOut( getZoomScale() );
  316. }
  317. dollyStart.copy( dollyEnd );
  318. scope.update();
  319. }
  320. function handleMouseMovePan( event ) {
  321. //console.log( 'handleMouseMovePan' );
  322. panEnd.set( event.clientX, event.clientY );
  323. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  324. pan( panDelta.x, panDelta.y );
  325. panStart.copy( panEnd );
  326. scope.update();
  327. }
  328. function handleMouseUp( event ) {
  329. // console.log( 'handleMouseUp' );
  330. }
  331. function handleMouseWheel( event ) {
  332. // console.log( 'handleMouseWheel' );
  333. if ( event.deltaY < 0 ) {
  334. dollyOut( getZoomScale() );
  335. } else if ( event.deltaY > 0 ) {
  336. dollyIn( getZoomScale() );
  337. }
  338. scope.update();
  339. }
  340. function handleKeyDown( event ) {
  341. //console.log( 'handleKeyDown' );
  342. switch ( event.keyCode ) {
  343. case scope.keys.UP:
  344. pan( 0, scope.keyPanSpeed );
  345. scope.update();
  346. break;
  347. case scope.keys.BOTTOM:
  348. pan( 0, - scope.keyPanSpeed );
  349. scope.update();
  350. break;
  351. case scope.keys.LEFT:
  352. pan( scope.keyPanSpeed, 0 );
  353. scope.update();
  354. break;
  355. case scope.keys.RIGHT:
  356. pan( - scope.keyPanSpeed, 0 );
  357. scope.update();
  358. break;
  359. }
  360. }
  361. function handleTouchStartRotate( event ) {
  362. // console.log( 'handleTouchStartRotate' );
  363. // First finger
  364. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  365. // Second finger
  366. rotateStart2.set( event.touches[ 1 ].pageX, event.touches[ 1 ].pageY );
  367. }
  368. function handleTouchStartDolly( event ) {
  369. if ( scope.enableZoom ) {
  370. // console.log( 'handleTouchStartDolly' );
  371. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  372. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  373. var distance = Math.sqrt( dx * dx + dy * dy );
  374. dollyStart.set( 0, distance );
  375. }
  376. }
  377. function handleTouchStartPan( event ) {
  378. if ( scope.enablePan ) {
  379. // console.log( 'handleTouchStartPan' );
  380. panStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  381. }
  382. }
  383. function handleTouchMoveRotate( event ) {
  384. if ( scope.enableRotate === false ) return;
  385. if ( ( state & STATE.ROTATE ) === 0 ) return;
  386. // First finger
  387. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  388. // Second finger
  389. rotateEnd2.set( event.touches[ 1 ].pageX, event.touches[ 1 ].pageY );
  390. rotateDelta.subVectors( rotateEnd, rotateStart );
  391. rotateDelta2.subVectors( rotateEnd2, rotateStart2 );
  392. rotateDeltaStartFingers.subVectors( rotateStart2, rotateStart );
  393. rotateDeltaEndFingers.subVectors( rotateEnd2, rotateEnd );
  394. if ( isRotateUp() ) {
  395. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  396. // rotating up and down along whole screen attempts to go 360, but limited to 180
  397. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  398. // Start rotateUp ==> disable all movement to prevent flickering
  399. state = STATE.ROTATE_UP;
  400. } else if ( ( state & STATE.ROTATE_LEFT ) !== 0 ) {
  401. rotateLeft( ( rotateDeltaStartFingers.angle() - rotateDeltaEndFingers.angle() ) * scope.rotateSpeed );
  402. }
  403. rotateStart.copy( rotateEnd );
  404. rotateStart2.copy( rotateEnd2 );
  405. }
  406. function isRotateUp() {
  407. // At start, does the two fingers are aligned horizontally
  408. if ( ! isHorizontal( rotateDeltaStartFingers ) ) {
  409. return false;
  410. }
  411. // At end, does the two fingers are aligned horizontally
  412. if ( ! isHorizontal( rotateDeltaEndFingers ) ) {
  413. return false;
  414. }
  415. // does the first finger moved vertically between start and end
  416. if ( ! isVertical( rotateDelta ) ) {
  417. return false;
  418. }
  419. // does the second finger moved vertically between start and end
  420. if ( ! isVertical( rotateDelta2 ) ) {
  421. return false;
  422. }
  423. // Does the two finger moved in the same direction (prevent moving one finger vertically up while the other goes down)
  424. return rotateDelta.dot( rotateDelta2 ) > 0;
  425. }
  426. var isHorizontal = function () {
  427. var precision = Math.sin( Math.PI / 6 );
  428. return function isHorizontal( vector ) {
  429. return Math.abs( Math.sin( vector.angle() ) ) < precision;
  430. };
  431. }();
  432. var isVertical = function () {
  433. var precision = Math.cos( Math.PI / 2 - Math.PI / 6 );
  434. return function isVertical( vector ) {
  435. return Math.abs( Math.cos( vector.angle() ) ) < precision;
  436. };
  437. }();
  438. function handleTouchMoveDolly( event ) {
  439. if ( scope.enableZoom === false ) return;
  440. if ( ( state & STATE.DOLLY ) === 0 ) return;
  441. // console.log( 'handleTouchMoveDolly' );
  442. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  443. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  444. var distance = Math.sqrt( dx * dx + dy * dy );
  445. dollyEnd.set( 0, distance );
  446. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  447. dollyIn( dollyDelta.y );
  448. dollyStart.copy( dollyEnd );
  449. }
  450. function handleTouchMovePan( event ) {
  451. if ( scope.enablePan === false ) return;
  452. if ( ( state & STATE.PAN ) === 0 ) return;
  453. // console.log( 'handleTouchMovePan' );
  454. panEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  455. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  456. pan( panDelta.x, panDelta.y );
  457. panStart.copy( panEnd );
  458. }
  459. function handleTouchEnd( event ) {
  460. //console.log( 'handleTouchEnd' );
  461. }
  462. //
  463. // event handlers - FSM: listen for events and reset state
  464. //
  465. function onMouseDown( event ) {
  466. if ( scope.enabled === false ) return;
  467. event.preventDefault();
  468. switch ( event.button ) {
  469. case scope.mouseButtons.LEFT:
  470. if ( event.ctrlKey || event.metaKey || event.shiftKey ) {
  471. if ( scope.enableRotate === false ) return;
  472. handleMouseDownRotate( event );
  473. state = STATE.ROTATE;
  474. } else {
  475. if ( scope.enablePan === false ) return;
  476. handleMouseDownPan( event );
  477. state = STATE.PAN;
  478. }
  479. break;
  480. case scope.mouseButtons.MIDDLE:
  481. if ( scope.enableZoom === false ) return;
  482. handleMouseDownDolly( event );
  483. state = STATE.DOLLY;
  484. break;
  485. case scope.mouseButtons.RIGHT:
  486. if ( scope.enableRotate === false ) return;
  487. handleMouseDownRotate( event );
  488. state = STATE.ROTATE;
  489. break;
  490. }
  491. if ( state !== STATE.NONE ) {
  492. document.addEventListener( 'mousemove', onMouseMove, false );
  493. document.addEventListener( 'mouseup', onMouseUp, false );
  494. scope.dispatchEvent( startEvent );
  495. }
  496. }
  497. function onMouseMove( event ) {
  498. if ( scope.enabled === false ) return;
  499. event.preventDefault();
  500. switch ( state ) {
  501. case STATE.ROTATE:
  502. if ( scope.enableRotate === false ) return;
  503. handleMouseMoveRotate( event );
  504. break;
  505. case STATE.DOLLY:
  506. if ( scope.enableZoom === false ) return;
  507. handleMouseMoveDolly( event );
  508. break;
  509. case STATE.PAN:
  510. if ( scope.enablePan === false ) return;
  511. handleMouseMovePan( event );
  512. break;
  513. }
  514. }
  515. function onMouseUp( event ) {
  516. if ( scope.enabled === false ) return;
  517. handleMouseUp( event );
  518. document.removeEventListener( 'mousemove', onMouseMove, false );
  519. document.removeEventListener( 'mouseup', onMouseUp, false );
  520. scope.dispatchEvent( endEvent );
  521. state = STATE.NONE;
  522. }
  523. function onMouseWheel( event ) {
  524. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  525. event.preventDefault();
  526. event.stopPropagation();
  527. scope.dispatchEvent( startEvent );
  528. handleMouseWheel( event );
  529. scope.dispatchEvent( endEvent );
  530. }
  531. function onKeyDown( event ) {
  532. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  533. handleKeyDown( event );
  534. }
  535. function onTouchStart( event ) {
  536. if ( scope.enabled === false ) return;
  537. event.preventDefault();
  538. switch ( event.touches.length ) {
  539. case 1: // one-fingered touch: pan
  540. if ( scope.enablePan === false ) return;
  541. handleTouchStartPan( event );
  542. state = STATE.PAN;
  543. break;
  544. case 2: // two-fingered touch: rotate-dolly
  545. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  546. handleTouchStartRotate( event );
  547. handleTouchStartDolly( event );
  548. state = STATE.DOLLY_ROTATE;
  549. break;
  550. default:
  551. state = STATE.NONE;
  552. }
  553. if ( state !== STATE.NONE ) {
  554. scope.dispatchEvent( startEvent );
  555. }
  556. }
  557. function onTouchMove( event ) {
  558. if ( scope.enabled === false ) return;
  559. event.preventDefault();
  560. event.stopPropagation();
  561. switch ( event.touches.length ) {
  562. case 1: // one-fingered touch: pan
  563. if ( scope.enablePan === false ) return;
  564. if ( state !== STATE.PAN ) return; // is this needed?
  565. handleTouchMovePan( event );
  566. scope.update();
  567. break;
  568. case 2: // two-fingered touch: rotate-dolly
  569. if ( scope.enableZoom === false && scope.enableRotate === false ) return;
  570. if ( ( state & STATE.DOLLY_ROTATE ) === 0 ) return; // is this needed?
  571. handleTouchMoveRotate( event );
  572. handleTouchMoveDolly( event );
  573. scope.update();
  574. break;
  575. default:
  576. state = STATE.NONE;
  577. }
  578. }
  579. function onTouchEnd( event ) {
  580. if ( scope.enabled === false ) return;
  581. handleTouchEnd( event );
  582. scope.dispatchEvent( endEvent );
  583. state = STATE.NONE;
  584. }
  585. function onContextMenu( event ) {
  586. if ( scope.enabled === false ) return;
  587. event.preventDefault();
  588. }
  589. //
  590. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  591. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  592. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  593. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  594. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  595. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  596. window.addEventListener( 'keydown', onKeyDown, false );
  597. // force an update at start
  598. this.update();
  599. };
  600. THREE.MapControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  601. THREE.MapControls.prototype.constructor = THREE.MapControls;
  602. Object.defineProperties( THREE.MapControls.prototype, {
  603. center: {
  604. get: function () {
  605. console.warn( 'THREE.MapControls: .center has been renamed to .target' );
  606. return this.target;
  607. }
  608. },
  609. // backward compatibility
  610. noZoom: {
  611. get: function () {
  612. console.warn( 'THREE.MapControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  613. return ! this.enableZoom;
  614. },
  615. set: function ( value ) {
  616. console.warn( 'THREE.MapControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  617. this.enableZoom = ! value;
  618. }
  619. },
  620. noRotate: {
  621. get: function () {
  622. console.warn( 'THREE.MapControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  623. return ! this.enableRotate;
  624. },
  625. set: function ( value ) {
  626. console.warn( 'THREE.MapControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  627. this.enableRotate = ! value;
  628. }
  629. },
  630. noPan: {
  631. get: function () {
  632. console.warn( 'THREE.MapControls: .noPan has been deprecated. Use .enablePan instead.' );
  633. return ! this.enablePan;
  634. },
  635. set: function ( value ) {
  636. console.warn( 'THREE.MapControls: .noPan has been deprecated. Use .enablePan instead.' );
  637. this.enablePan = ! value;
  638. }
  639. },
  640. noKeys: {
  641. get: function () {
  642. console.warn( 'THREE.MapControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  643. return ! this.enableKeys;
  644. },
  645. set: function ( value ) {
  646. console.warn( 'THREE.MapControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  647. this.enableKeys = ! value;
  648. }
  649. },
  650. staticMoving: {
  651. get: function () {
  652. console.warn( 'THREE.MapControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  653. return ! this.enableDamping;
  654. },
  655. set: function ( value ) {
  656. console.warn( 'THREE.MapControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  657. this.enableDamping = ! value;
  658. }
  659. },
  660. dynamicDampingFactor: {
  661. get: function () {
  662. console.warn( 'THREE.MapControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  663. return this.dampingFactor;
  664. },
  665. set: function ( value ) {
  666. console.warn( 'THREE.MapControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  667. this.dampingFactor = value;
  668. }
  669. }
  670. } );