// @ts-nocheck import Utils from '../Utils' import { PointEle, type PointDes } from '../map_element/element/Point' import { PolygonEle, type PolygonDes, GroundPolygonEle, SimplePolygonEle } from '../map_element/element/Polygon' import { Manager as ActionManager } from '../action/Action' import { BaseDrawer } from './BaseDrawer' type PolygonEditor = { polygon: PolygonDes, point: PointDes } type PolygonHolder = { polygon?: PolygonEle, pointList: Array } export class PolygonDrawer extends BaseDrawer { readonly holder: PolygonHolder; constructor(id: string) { super(id); this.editor.point.pixelSize = 8; this.holder = { polygon: undefined, pointList: [] } } protected createEditor(): PolygonEditor { return { polygon: PolygonEle.defaultDes(), point: PointEle.defaultDes(), } } protected disposeData(): void { ActionManager.removeHelper(this.holder.polygon!.id); this.holder.polygon?.dispose(); this.disposePoints(); } protected disposePoints(): void { this.holder.pointList.forEach((point) => { ActionManager.removeHelper(point.id); point.dispose(); }); this.holder.pointList.splice(0); } /** * 创建新的图形 * @param id * @returns */ newSimplePolygon(id: string): PolygonDrawer { this.unbindPolygon(); this.holder.polygon = SimplePolygonEle.create(id, []).init().inflate(); this.editor.polygon = PolygonEle.defaultDes(); return this; } /** * 创建新的贴地图形 * @param id * @returns */ newGroundPolygon(id: string): PolygonDrawer { this.unbindPolygon(); this.holder.polygon = GroundPolygonEle.create(id, []).init().inflate(); this.editor.polygon = PolygonEle.defaultDes(); return this; } /** * 绑定新的图形 * @param polygon * @returns */ bindPolygon(polygon: PolygonEle): PolygonDrawer { this.unbindPolygon(); this.holder.polygon = polygon; this.editor.polygon = polygon.des; this.initPoint(polygon.info.positionList); return this; } private initPoint(positionList: Array) { positionList.forEach(position => this.newPoint(position).inflate()); } /** * 取消图形绑定 * @returns */ unbindPolygon(): PolygonDrawer { this.deactive(); this.disposePoints(); this.holder.polygon = undefined; this.editor.polygon = PolygonEle.defaultDes(); return this; } /** * 清除所有添加的地图元素 */ clear(): PolygonDrawer { this.holder.polygon?.repositeArea([]); this.disposePoints(); return this; } /** * 开启功能(注册事件回调) */ active(): PolygonDrawer { // 点 this.holder.pointList.forEach(point => this.activePoint(point)); // 全局/面 const actionHelper = ActionManager.newHelper(this.holder.polygon!.id) .simple.active() .simple.onLeftTap((helper, info) => { if (this.holder.polygon!.isEditing() && info.elementId == undefined) { const point = this.newPoint(info.position).inflate(); this.activePoint(point); this.refreshPolygonData(); } else if (this.holder.polygon!.id == info.elementId) { if (this.holder.polygon!._answerDrag()) { helper.drag.active(); } this.holder.polygon!.startEdit(); this.holder.pointList.forEach(point => { ActionManager.findHelper(point.id)?.drag.active(); point.startEdit(); }); this.focusedCallback?.(this); } }) .simple.onRightTap((helper, info) => { helper.simple.deactive(); helper.drag.deactive(); this.holder.pointList.forEach((point) => ActionManager.findHelper(point.id)?.drag.deactive()) if (this.holder.polygon!.isEditing()) { this.holder.polygon!.stopEdit(); this.holder.pointList.forEach(point => point.stopEdit()); this.defocusCallback?.(this); } }) if (this.holder.polygon!._answerDrag()) { actionHelper.drag.active() .drag.onMove((helper, info) => { const vector = Utils.Vector.create(info.lp, info.position); this.holder.polygon!.move(vector); this.holder.pointList.forEach(point => point.move(vector)); }); } return this; } /** * 关闭功能(移除所有注册的事件回调) */ deactive(): PolygonDrawer { if (this.holder.polygon != undefined) { this.holder.polygon.stopEdit(); ActionManager.removeHelper(this.holder.polygon.id); } this.holder.pointList.forEach((point) => { point.stopEdit(); ActionManager.removeHelper(point.id); }) return this; } private newPoint(position: Cesium.Cartesian3): PointEle { const id = this.holder.polygon!.id + '_point_' + this.holder.pointList.length; const point = PointEle.create(id, position, this.editor.point).init(); this.holder.pointList.push(point); return point; } private activePoint(point: PointEle) { ActionManager.newHelper(point.id) .drag.active() .drag.onMove((helper, info) => { point.reposite(info.position); this.refreshPolygonData(); }) } private refreshPolygonData() { const positionList: Cesium.Cartesian3 = []; this.holder.pointList.forEach(point => { positionList.push(point.info.position.clone()); }) this.holder.polygon!.repositeArea(positionList); } /** * 刷新地图元素 */ refreshGeo(): void { this.holder.polygon!.refreshGeo(); this.holder.pointList.forEach(point => point.refreshGeo()); } /** * 获取所有绘制的点 */ getPositionList(): Array { return Utils.Position.copyList(this.holder.polygon!.info.positionList); } } const PolygonUpdaterId = 'PolygonUpdaterId'; export const PolygonUpdater = new PolygonDrawer(PolygonUpdaterId);