import * as Utils from '../utils'; // @ts-ignore import { Cartesian3 } from 'cesium'; import { Category, CesiumTypes, PolylineStyle, Viewer } from '../type'; import Basic from '../bastic'; export class CurvedLineArrow extends Basic { points: Cartesian3[] = []; arrowLengthScale: number = 5; maxArrowLength: number = 3000000; t: number; minPointsForShape: number; constructor(cesium: CesiumTypes, viewer: Viewer, style?: PolylineStyle) { super(cesium, viewer, style); this.cesium = cesium; this.t = 0.3; this.minPointsForShape = 2; this.setState('drawing'); this.onDoubleClick(); } getCategory(): Category { return 'polyline' } addPoint(cartesian: Cartesian3) { this.points.push(cartesian); if (this.points.length < 2) { this.onMouseMove(); } } updateMovingPoint(cartesian: Cartesian3) { const tempPoints = [...this.points, cartesian]; let geometryPoints = this.createGraphic(tempPoints); this.setGeometryPoints(geometryPoints); this.drawLine(); this.mainEntity = this.lineEntity } createStraightArrow(positions: Cartesian3[]) { const [pnt1, pnt2] = positions.map(this.cartesianToLnglat); const distance = Utils.MathDistance(pnt1, pnt2); let len = distance / this.arrowLengthScale; len = len > this.maxArrowLength ? this.maxArrowLength : len; const leftPnt = Utils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len / 2, false); const rightPnt = Utils.getThirdPoint(pnt1, pnt2, Math.PI / 6, len / 2, true); const points = [...pnt1, ...pnt2, ...leftPnt, ...pnt2, ...rightPnt]; const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(points); return cartesianPoints; } updateDraggingPoint(cartesian: Cartesian3, index: number) { this.points[index] = cartesian; const geometryPoints = this.createGraphic(this.points); this.setGeometryPoints(geometryPoints); this.drawLine(); this.mainEntity = this.lineEntity } createGraphic(positions: Cartesian3[]) { const lnglatPoints = positions.map((pnt) => { return this.cartesianToLnglat(pnt); }); if (positions.length === 2) { return this.createStraightArrow(positions); } const curvePoints = Utils.getCurvePoints(this.t, lnglatPoints); // const pnt1 = lnglatPoints[lnglatPoints.length - 2]; const pnt2 = lnglatPoints[lnglatPoints.length - 1]; const distance = Utils.wholeDistance(lnglatPoints); let len = distance / this.arrowLengthScale; len = len > this.maxArrowLength ? this.maxArrowLength : len; const leftPnt = Utils.getThirdPoint(curvePoints[curvePoints.length - 2], curvePoints[curvePoints.length - 1], Math.PI / 6, len / 2, false); const rightPnt = Utils.getThirdPoint(curvePoints[curvePoints.length - 2], curvePoints[curvePoints.length - 1], Math.PI / 6, len / 2, true); const temp = [].concat(...curvePoints); const points = [...temp, ...leftPnt, ...pnt2, ...rightPnt]; const cartesianPoints = this.cesium.Cartesian3.fromDegreesArray(points); return cartesianPoints; } getPoints() { return this.points; } }