UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

3 lines (2 loc) 10.4 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as t}from"tslib";import{clamp as e,acosClamped as i,deg2rad as a}from"../../../../core/mathUtils.js";import{property as r,subclass as o}from"../../../../core/accessorSupport/decorators.js";import{translate as n,rotate as s,identity as c}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as l}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{subtract as m,transformMat4 as h,add as p,scale as d,copy as u,negate as f,length as _,set as v,normalize as g,cross as C,distance as y,direction as w,dot as b}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as S}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{getReferenceEllipsoid as O}from"../../../../geometry/ellipsoidUtils.js";import{wrap as x}from"../../../../geometry/support/ray.js";import{Sphere as M}from"../../../../geometry/support/sphere.js";import{angle as j}from"../../../../geometry/support/vector.js";import{sv3d as z}from"../../../../geometry/support/vectorStacks.js";import{applyAll as T}from"../../camera/constraintUtils.js";import{ConstraintOptions as A}from"../../camera/constraintUtils/ConstraintOptions.js";import{TiltRange as D}from"../Constraints.js";import{InteractiveController as R}from"./InteractiveController.js";import{panMotionToRotationMatrix as H,onSurfaceTiltToEyeTiltGlobal as E,offSurfaceTiltToEyeTiltGlobal as U}from"../utils/navigationUtils.js";import{viewAngle as k}from"../utils/viewUtils.js";import{headingTiltToDirectionUp as F}from"../../support/cameraUtils.js";import{createDirectionUp as G}from"../../support/cameraUtilsInternal.js";import P from"../../webgl/RenderCamera.js";import{extractTransformation as B,isZeroTransformation as I,extractTransformationKeyboard as V}from"../../../navigation/gamepadAndKeyboardUtils.js";let L=class extends R{constructor(t){super(t),this._filteredSurfaceElevation=0,this._transformation={translation:[0,0,0],heading:0,tilt:0,zoom:0},this._keysButtonState=[0,0,0,0,0,0,0,0,0,0,0,0],this._tmpCamera=new P,this._headingStart=0,this._constraintOptions=new A(15,0,0,new P,null,1)}handleEventGamepad(t){const e=B(t,this.view.navigation.gamepad,this._transformation);("end"===t.action||I(e))&&this.finishController()}activateDirection(t){this._keysButtonState[t]=1,V(this._keysButtonState,this._transformation)}directionActive(t){return 1===this._keysButtonState[t]}countActiveDirections(){return this._keysButtonState.reduce((t,e)=>e>0?t+1:t,0)}deactivateDirection(t){this._keysButtonState[t]=0;const e=V(this._keysButtonState,this._transformation);I(e)&&this.finishController()}onControllerStart(t){this._filteredSurfaceElevation=this.view.pointsOfInterest.cameraOnSurface.location.z,this._headingStart=this.view.camera.heading,super.onControllerStart(t)}_updateFilteredSurfaceElevation(t){const e=this.view.pointsOfInterest.cameraOnSurface.location.z,i=1;this._filteredSurfaceElevation+=i*(e-this._filteredSurfaceElevation)*t}stepController(t,e){this._updateStartHeading(),this._updateFilteredSurfaceElevation(t),this.currentCamera.copyViewFrom(e),this._updateCameraCenter(),this._constraintOptions.interactionStartCamera?.copyFrom(this.currentCamera),this._calculateControlTransformation(t,this.currentCamera,W),this._applyDisabledMovementTypes(W),this._applyPan(W.pan),this._applyRotate(W.rotate),this._applyZoom(W.zoom),this._applyAscend(W.ascend),this._constraintOptions.interactionType=0,this._constraintOptions.selection=8,T(this.view,this.currentCamera,this._constraintOptions),super.stepController(t,e)}_updateStartHeading(){0!==this._transformation.heading&&(this._headingStart=this.view.camera.heading)}_applyRotate(t){if(!t.enabled)return;const e=this.currentCamera;m(Y,e.center,e.eye),h(Y,Y,t.matrix),e.center=p(Y,Y,e.eye),e.up=h(Y,e.up,t.matrix),this._constraintOptions.interactionType=3,this._constraintOptions.selection=7,T(this.view,e,this._constraintOptions)}_applyPan(t,e=this.currentCamera){if(!t.enabled)return;e.eye=h(Y,e.eye,t.matrix),e.center=h(Y,e.center,t.matrix);this.view.state.isGlobal&&(e.up=h(Y,e.up,t.matrix)),this._constraintOptions.interactionType=4,this._constraintOptions.selection=15,T(this.view,e,this._constraintOptions)}_applyZoom(t){if(!t)return;const e=this.currentCamera.viewForward;this.currentCamera.eye=p(Y,this.currentCamera.eye,d(z.get(),e,t)),u($,e),f($,$),this._constraintOptions.interactionDirection=$,this._constraintOptions.interactionType=1,this._constraintOptions.selection=7,T(this.view,this.currentCamera,this._constraintOptions),this._constraintOptions.interactionDirection=null}_applyAscend(t){if(!t)return;const e=this.view.renderCoordsHelper.worldUpAtPosition(this.currentCamera.eye,z.get());this._constraintOptions.interactionDirection=u($,e);if(this.view.state.isGlobal){const e=_(this.currentCamera.eye),i=(e+t)/e;this.currentCamera.eye=d(Y,this.currentCamera.eye,i),this.currentCamera.center=d(Y,this.currentCamera.center,i)}else{const i=d(z.get(),e,t);this.currentCamera.eye=p(Y,this.currentCamera.eye,i),this.currentCamera.center=p(Y,this.currentCamera.center,i)}this._updateCameraCenter(),this._constraintOptions.interactionType=5,this._constraintOptions.selection=8,T(this.view,this.currentCamera,this._constraintOptions)&&this._updateCameraCenter(),this._constraintOptions.selection=7,T(this.view,this.currentCamera,this._constraintOptions),this._constraintOptions.interactionDirection=null}_calculateControlTransformation(t,e,i){it(i);const a=this._computeVelocities(t);this.view.state.isLocal?this._calculateControlTransformationLocal(a,e,i):this._calculateControlTransformationGlobal(a,e,i)}_updateCameraCenter(){const t=this.view.pointsOfInterest.centerOnSurfaceFrequent.estimatedSurfaceAltitude,e=this.view.renderCoordsHelper,i=this.currentCamera.ray;this.currentCamera.center=e.intersectManifoldClosestSilhouette(i,t,Y)}_calculateControlTransformationLocal(t,i,a){const{viewRight:r,viewForward:o}=i,c=this._transformation,l=this.view.navigation.gamepad,m=v(z.get(),o[0],o[1],0);g(m,m);const h=c.translation[0]*t.pan;if(0!==h){const t=d(z.get(),r,h);n(a.pan.matrix,a.pan.matrix,t),a.pan.enabled=!0}switch(l.mode){case"pan":{const e=-c.translation[1]*t.pan;if(0!==e){const t=d(z.get(),m,e);n(a.pan.matrix,a.pan.matrix,t),a.pan.enabled=!0}a.zoom=c.zoom*t.zoom;break}case"zoom":a.zoom=(-c.translation[1]+c.zoom)*t.zoom;break;default:l.mode}const p=c.translation[2]*t.ascend;a.ascend=p;const u=-c.heading*t.rotate;0!==u&&(s(a.rotate.matrix,a.rotate.matrix,u,this.view.renderCoordsHelper.worldUpAtPosition(i.eye,z.get())),a.rotate.enabled=!0);const f=c.tilt*t.rotate,_=k(this.view.renderCoordsHelper,i.center,i.eye),C=e(_+f,D.min,D.max)-_;C&&(s(a.rotate.matrix,a.rotate.matrix,C,r),a.rotate.enabled=!0)}_calculateControlTransformationGlobal(t,e,i){const{eye:a,viewRight:r}=e,o=this._transformation,n=this.view.navigation.gamepad,c=C(z.get(),r,a);g(c,c),f(c,c),H(this.startCamera,e,o,t,this.view.camera.heading,this._headingStart,this.view.camera.tilt,i,n),this._tmpCamera.copyFrom(this.currentCamera),this._applyPan(W.pan,this._tmpCamera);const l=this.view.pointsOfInterest.centerOnSurfaceFrequent.estimatedSurfaceAltitude,m=o.translation[2]*t.ascend;i.ascend=m;const h=-o.heading*t.rotate;0!==h&&(s(i.rotate.matrix,i.rotate.matrix,h,this._tmpCamera.eye),i.rotate.enabled=!0);const p=o.tilt*t.rotate,d=this._clampTiltDeltaGlobalToValidRange(p,e.ray,l);0!==d&&(s(i.rotate.matrix,i.rotate.matrix,d,this._tmpCamera.viewRight),i.rotate.enabled=!0),i.zoom+=o.zoom*t.zoom}_clampTiltDeltaGlobalToValidRange(t,i,a){const r=O(this.view.spatialReference),o=E(D.min,i.origin,a,r);let n=0,s=0;const c=z.get();if(this.view.renderCoordsHelper.intersectManifold(i,a,c)){const t=k(this.view.renderCoordsHelper,c,i.origin);n=E(t,i.origin,a,r),s=E(D.max,i.origin,a,r)}else{et.radius=a+r.radius,et.closestPointOnSilhouette(i,c);const t=Math.PI+j(i.direction,c);n=U(t,i.origin,a,r),s=U(D.max,i.origin,a,r)}return e(n+t,o,s)-n}_getPointAbsoluteSurfaceElevation(t,e,i){const{renderCoordsHelper:a}=this.view,r=a.getAltitude(t),o=e+Math.abs(r-e);return a.setAltitude(i,o,t),o}_clampedDistanceToSurface(t,e){const{renderCoordsHelper:i}=this.view,{camera:a}=this.view.state,{direction:r}=F(this.view,e,0,K,tt),o=i.intersectManifoldClosestSilhouette(x(e,r),t,z.get()),n=y(e,o),s=i.intersectManifoldClosestSilhouette(x(e,w(z.get(),e,a.center)),t,z.get()),c=y(e,s);return Math.min(n,c)}_computeHeadingRotateRadius(t){const{renderCoordsHelper:e,state:a}=this.view,{camera:r,isGlobal:o}=a,n=e.intersectManifoldClosestSilhouette(r.ray,this._filteredSurfaceElevation,z.get());if(o){const e=m(z.get(),t,n),a=_(e);d(e,e,1/a);const r=g(z.get(),t),o=i(b(r,e));return a*Math.sin(Math.min(Z,o))}{const i=u(z.get(),t);return e.setAltitude(i,this._filteredSurfaceElevation),y(n,i)}}_minimumAscendVelocity(){return this.view.state.constraints.collision.enabled?0:J}_computeVelocities(t){const i=this._filteredSurfaceElevation,a=i+O(this.view.spatialReference).radius,{camera:r,isGlobal:o}=this.view.state,n=z.get(),s=this._getPointAbsoluteSurfaceElevation(r.eye,i,n),c=this._clampedDistanceToSurface(i,n),l=r.width/2,m=X*r.width,h=X*r.width,p=c*Math.tan(.5*r.fovX)/l,d=p/a,u=p/this._computeHeadingRotateRadius(n),f=s-i;return{pan:(o?d:p)*m*t,ascend:Math.max(this._minimumAscendVelocity()*t,2**(m*t/l)*f-f),zoom:2**(m*t/l)*c-c,rotate:e(u*h,N,Q)*t}}_applyDisabledMovementTypes(t){null==this.disableMovements||void 0!==this.disableMovements.mode&&this.view.state.viewingMode!==this.disableMovements.mode||(t.zoom=this.disableMovements.zoom?0:t.zoom,t.ascend=this.disableMovements.ascend?0:t.ascend,t.pan.enabled=!this.disableMovements.pan,this.disableMovements.pan&&c(t.pan.matrix),t.rotate.enabled=!this.disableMovements.rotate,this.disableMovements.rotate&&c(t.rotate.matrix))}static activatesFor(t,e){const i=B(e,t.navigation.gamepad,q);return!("end"===e.action||I(i))}};t([r({constructOnly:!0})],L.prototype,"gamepadDevice",void 0),t([r({constructOnly:!0})],L.prototype,"disableMovements",void 0),L=t([o("esri.views.3d.state.controllers.GamepadKeyboardController")],L);const q={translation:[0,0,0],heading:0,tilt:0,zoom:0},K=80,Z=a(K),X=.75,J=5,N=a(30),Q=a(80),W={zoom:0,ascend:0,pan:{enabled:!1,matrix:l()},rotate:{enabled:!1,matrix:l()}},Y=S(),$=S(),tt=G(),et=new M;function it(t){t.zoom=0,t.ascend=0,t.pan.enabled=!1,c(t.pan.matrix),t.rotate.enabled=!1,c(t.rotate.matrix)}export{L as GamepadKeyboardController};