MapControls.js 24 KB

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