UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 6.18 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import t from"../../../../Color.js";import e from"../../../../core/Handles.js";import{watch as o}from"../../../../core/reactiveUtils.js";import{createScreenPointArray as i,createRenderScreenPointArray as s,createRenderScreenPointArray3 as r}from"../../../../core/screenUtils.js";import{add as a,set as n,scale as h,scaleAndAdd as l,negate as c,normalize as m,length as _}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{add as u}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as d}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{renderScreenSpaceTangent as p}from"../support/viewUtils.js";import{VisualElement as x}from"./VisualElement.js";import b from"../../../overlay/LineOverlayItem.js";import f from"../../../overlay/TextOverlayItem.js";class g extends x{constructor(o){super(o),this._handles=new e,this._textItem=null,this._calloutItem=null,this._showCallout=!0,this._showText=!0,this._geometry=null,this._text="-",this._fontSize=14,this._backgroundColor=new t([0,0,0,.6]),this._calloutColor=new t([0,0,0,.5]),this._textColor=new t([255,255,255]),this._distance=25,this._anchor="right",this.updatePositionOnCameraMove=!0,this.applyProperties(o)}get geometry(){return this._geometry}set geometry(t){this._geometry=t,this.updateLabelPosition()}get isDecoration(){return this._isDecoration}set isDecoration(t){this._isDecoration=t,this._textItem&&(this._textItem.isDecoration=t),this._calloutItem&&(this._calloutItem.isDecoration=t)}get textItem(){return this._textItem}get text(){return this._text}set text(t){this._text=t,this.attached&&(this._textItem.text=this._text)}get fontSize(){return this._fontSize}set fontSize(t){this._fontSize=t,this.attached&&(this._textItem.fontSize=this._fontSize)}get backgroundColor(){return this._backgroundColor}set backgroundColor(t){this._backgroundColor=t,this.attached&&(this._textItem.backgroundColor=this._backgroundColor)}get calloutColor(){return this._calloutColor}set calloutColor(t){this._calloutColor=t,this.attached&&(this._calloutItem.color=this._calloutColor.toRgba())}get textColor(){return this._textColor}set textColor(t){this._textColor=t,this.attached&&(this._textItem.textColor=this._textColor)}get distance(){return this._distance}set distance(t){this._distance!==t&&(this._distance=t,this.updateLabelPosition())}get anchor(){return this._anchor}set anchor(t){this._anchor!==t&&(this._anchor=t,this.updateLabelPosition())}get _camera(){return this.view.state.cssCamera}overlaps(t){return!!this.attached&&(this.textItem.visible&&t.textItem.visible&&!!this.view.overlay?.overlaps(this._textItem,t.textItem))}updateLabelPosition(){if(!this.attached)return;this._showText=!1,this._showCallout=!1;const{geometry:t,view:e,visible:o}=this;if(null!=t&&e.stage)switch(t.type){case"point":if(!this._computeLabelPositionFromPoint(t.point,this._distance,D))break;if(t.callout){const e=this._camera,o=t.callout.distance;a(L,L,[0,t.callout.offset]),e.renderToScreen(L,D),n(k,0,1),h(k,k,o),a(k,k,L),e.renderToScreen(k,R),this._showCallout=this._updatePosition(D,R)}else this._textItem.position=[D[0],D[1]],this._textItem.anchor="center";this._showText=!0;break;case"corner":if(!this._computeLabelPositionFromCorner(t,this._distance,D,R))break;this._showCallout=this._updatePosition(D,R),this._showText=!0;break;case"segment":{if(!this._computeLabelPositionFromSegment(t,this._distance,this._anchor,D,R))break;this._showText=!0;const e=this._updatePosition(D,R);this._showCallout=!1!==t.callout&&e,this._showCallout||(this._textItem.anchor="center")}}this.updateVisibility(o)}_computeLabelPositionFromPoint(t,e,o){const i=this._camera,s=i.projectToRenderScreen(t,L);return!!s&&(!(s[2]<0||s[2]>1)&&(n(k,0,1),l(L,L,k,e),i.renderToScreen(L,o),!0))}_computeLabelPositionFromCorner(t,e,o,i){if(!t)return!1;const s=this._camera,r=C(t.left,1,s,y),n=C(t.right,0,s,P);if(null==r||null==n)return!1;c(y,y),a(k,r,n),c(k,k),m(k,k);const l=s.projectToRenderScreen(t.left.endRenderSpace,j);return!(!l||l[2]<0||l[2]>1)&&(s.renderToScreen(l,o),h(k,k,e),a(k,k,l),s.renderToScreen(k,i),!0)}_computeLabelPositionFromSegment(t,e,o,i,s){if(!t)return!1;const r=t.segment,l=this._camera;p(r.startRenderSpace,r.endRenderSpace,l,y),n(k,-y[1],y[0]);let m=!1;switch(o){case"top":m=k[1]<0;break;case"bottom":m=k[1]>0;break;case"left":m=k[0]>0;break;case"right":m=k[0]<0}if(m&&c(k,k),0===_(k))switch(o){case"top":k[1]=1;break;case"bottom":k[1]=-1;break;case"left":k[0]=-1;break;case"right":k[0]=1}r.eval(z[t.sampleLocation],T);const u=l.projectToRenderScreen(T,j);return!(!u||u[2]<0||u[2]>1)&&(l.renderToScreen(j,i),h(k,k,e),a(k,k,j),l.renderToScreen(k,s),!0)}_updatePosition(t,e){if(e){const o=e[0]-t[0],i=e[1]-t[1];return this._textItem.position=[e[0],e[1]],this._textItem.anchor=Math.abs(o)>Math.abs(i)?o>0?"left":"right":i>0?"top":"bottom",this._calloutItem.startPosition=[t[0],t[1]],this._calloutItem.endPosition=[e[0],e[1]],!0}return this._textItem.position=[t[0],t[1]],this._textItem.anchor="center",!1}createResources(){this._textItem=new f({visible:!0,text:this._text,fontSize:this._fontSize,backgroundColor:this._backgroundColor,textColor:this._textColor,isDecoration:this._isDecoration}),this._calloutItem=new b({color:this._calloutColor.toRgba(),visible:!0,width:2,isDecoration:this._isDecoration}),this.updateLabelPosition(),this.view.overlay?.items.addMany([this._textItem,this._calloutItem]),this.updatePositionOnCameraMove&&this._handles.add(o(()=>this.view.state.camera,()=>this.updateLabelPosition()))}destroyResources(){this.view.overlay&&!this.view.overlay.destroyed&&this.view.overlay.items.removeMany([this._textItem,this._calloutItem]),this._handles.removeAll()}updateVisibility(t){this._textItem.visible=this._showText&&t,this._calloutItem.visible=this._showCallout&&t}forEachMaterial(){}}function C(t,e,o,i){return t.eval(e,I,S),u(w,I,S),p(I,w,o,i)}function v(t){switch(t){case"top":return"bottom";case"right":return"left";case"bottom":return"top";case"left":return"right"}}const I=d(),w=d(),S=d(),y=s(),P=s(),k=s(),T=d(),j=r(),L=s(),D=i(),R=i(),z={start:0,center:.5,end:1};export{g as LabelVisualElement,v as mirrorPosition};