UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 6.15 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../../Color.js";import{onLocaleChange as s}from"../../intl.js";import o from"../../core/Accessor.js";import{createTask as r}from"../../core/asyncUtils.js";import{multiplyOpacity as i,getContrast as n}from"../../core/colorUtils.js";import{makeHandle as a}from"../../core/handleUtils.js";import"../../core/has.js";import{deg2rad as l}from"../../core/mathUtils.js";import{formatLength as c}from"../../core/quantityFormatUtils.js";import{watch as h,on as m}from"../../core/reactiveUtils.js";import{createScreenPointArray as p}from"../../core/screenUtils.js";import{getMetersPerUnit as d,getMetersPerVerticalUnitForSR as u}from"../../core/units.js";import{property as f,subclass as g}from"../../core/accessorSupport/decorators.js";import{squaredDistance as b,set as _,normalize as x,scale as y,add as v,lerp as U}from"../../core/libs/gl-matrix-2/math/vec2.js";import{create as j}from"../../core/libs/gl-matrix-2/factories/vec2f64.js";import{isVec3 as L}from"../../core/libs/gl-matrix-2/types/vec3.js";import I from"../../geometry/Polyline.js";import{isClockwise as C}from"../../geometry/support/coordsUtils.js";import{getEndpoint as k,isCoordinate as w}from"../../geometry/support/curves/curveUtils.js";import{interpolate2D as D}from"../../geometry/support/curves/interpolateCurve.js";import{fetchMessageBundle as T}from"../../intl/messages.js";import{getConvertedElevationFromVector as M,absoluteHeightElevationInfo as P}from"../../support/elevationInfoUtils.js";import{getDefaultUnitForView as O}from"../../support/getDefaultUnitForView.js";import S from"../overlay/TextOverlayItem.js";const R=3025,A={default:15,far:25};let V=class extends o{constructor(e){super(e),this.context=null,this.stagedVertex=null,this.visible=!0,this.edgeDistance="default",this._messageUnitsTask=null,this._labelInfos=[],this._nextLabelIndex=0}initialize(){this.addHandles([h(()=>[null!=this.context&&this.getCameraOrExtent(this.context),this.visible,this._edgeDistancePixels,this.stagedVertex,this._messagesUnits],()=>this._update()),m(()=>this.context?.editGeometryOperations,["vertex-add","vertex-remove"],()=>this._update()),m(()=>this.context?.editGeometryOperations,"vertex-update",e=>this._update(e.vertices)),h(()=>this._colors,e=>this._updateStyle(e)),s(()=>this._refreshMessages()),a(()=>this._messageUnitsTask?.abort())]),this._refreshMessages()}destroy(){for(this._nextLabelIndex=0;this._labelInfos.length;)this._destroyLabel(this._labelInfos.pop())}get updating(){return null==this._messagesUnits}get test(){}get _messagesUnits(){return this._messageUnitsTask?.value}get _edgeDistancePixels(){return A[this.edgeDistance]}get _colors(){const e=this.context?.view.effectiveTheme.textColor??t.fromArray([255,255,255]);return{textColor:e,backgroundColor:i(n(e,160),.6)}}_update(e){if(this.destroyed)return;this._nextLabelIndex=0;const{context:t,stagedVertex:s}=this;if(!t)return this._destroyUnusedLabels();const o=t.editGeometryOperations.data,{parts:r,geometry:i,coordinateHelper:n}=o;if(!i)return this._destroyUnusedLabels();const a=r.length;for(let l=0;l<a;++l){const r=this.getRing(i,o,s,n,l),c=r.map(e=>L(e)?e:n.arrayToXYZ(k(e)));if(r.length<2||!G(r,t.view,t.elevationInfo,n.spatialReference))continue;const h=1===a&&!C(c);let m=F,p=X;this.toScreenPointArray(t,c[0],m);for(let s=1;s<r.length;++s){const o=c[s-1],i=c[s],n=r[s];this.toScreenPointArray(t,i,p);const a=s===c.length-1,d=!(!e?.length||e.some(({index:e,part:t})=>t.index===l&&(e===s-1||e===s||a&&0===e)));this._addLabel(t,o,m,i,L(n)?null:n,p,h,d),[m,p]=[p,m]}}this._destroyUnusedLabels()}_updateStyle({textColor:e,backgroundColor:t}){const s=this._nextLabelIndex,o=this._labelInfos;for(let r=0;r<s;++r){const{label:s}=o[r];s.textColor=e,s.backgroundColor=t}}_addLabel(e,t,s,o,r,i,n,a=!1){const{label:l,wasReused:h}=this._getOrCreateLabel(e);if(!this.visible||b(s,i)<R)return void(l.visible=!1);const{spatialReference:m,coordinateHelper:p}=e.editGeometryOperations.data;if(!a||!h){const s=!r||w(r)?e.automaticLengthMeasurementUtils.autoDistance2D(t,o,m):e.automaticLengthMeasurementUtils.autoLength2D(new I({spatialReference:m,curvePaths:[[[t[0],t[1]],r]]})),i=this._messagesUnits,n=O(e.view);l.text=null!=i&&null!=s?c(i,s,n):""}l.visible=!0;const d=i[0]-s[0],u=i[1]-s[1];if(n?_(q,-u,d):_(q,u,-d),x(q,q),y(q,q,this._edgeDistancePixels),r&&!w(r)){const s=p.arrayToXYZ(D(t,r,.5)),o=this.toScreenPointArray(e,s);v(z,o,q)}else U(z,s,i,.5),v(z,z,q);l.position=[z[0],z[1]],Math.abs(q[0])>Math.abs(q[1])?l.anchor=q[0]>0?"left":"right":l.anchor=-q[1]<0?"top":"bottom"}_getOrCreateLabel(e){const t=this._labelInfos.length>this._nextLabelIndex,{textColor:s,backgroundColor:o}=this._colors;if(t){const e=this._labelInfos[this._nextLabelIndex++],{label:t}=e;return t.textColor=s,t.backgroundColor=o,e.wasReused=!0,e}const r=new S({anchor:"center",fontSize:8,textColor:s,backgroundColor:o});e.view.overlay?.items.add(r);const i={label:r,wasReused:!1};return this._labelInfos.push(i),this._nextLabelIndex=this._labelInfos.length,i}_destroyUnusedLabels(){for(;this._labelInfos.length>this._nextLabelIndex;)this._destroyLabel(this._labelInfos.pop())}_destroyLabel({label:e}){this.context?.view.overlay?.items.remove(e),e.destroy()}_refreshMessages(){this._messageUnitsTask?.abort(),this._messageUnitsTask=r(()=>T("esri/core/t9n/Units"))}};function G(e,t,s,o){if("2d"===t.type)return!0;const r=d(o)??1,i=u(o),n=e=>M(t,e,o,s,P)??0;for(let a=1;a<e.length;++a){const t=e[a-1],s=e[a];if(!L(t)||!L(s))return!1;const o=(s[0]-t[0])*r,l=(s[1]-t[1])*r,c=(n(s)-n(t))*i;if(Math.abs(c)/Math.sqrt(o*o+l*l)>H)return!1}return!0}e([f()],V.prototype,"context",void 0),e([f()],V.prototype,"stagedVertex",void 0),e([f()],V.prototype,"visible",void 0),e([f()],V.prototype,"edgeDistance",void 0),e([f()],V.prototype,"updating",null),e([f()],V.prototype,"_messageUnitsTask",void 0),e([f()],V.prototype,"_messagesUnits",null),e([f()],V.prototype,"_edgeDistancePixels",null),e([f()],V.prototype,"_colors",null),V=e([g("esri.views.interactive.SegmentLabels")],V);const H=l(5),q=j(),z=j(),F=p(),X=p();export{V as SegmentLabels};