// @ts-nocheck import Utils from '../Utils' import { PointEle, type PointDes } from '../map_element/element/Point' import { LineEle, type LineDes, SimpleLineEle, GroundLineEle } from '../map_element/element/Line' import { Manager as ActionManager } from '../action/Action' import { BaseDrawer } from './BaseDrawer' type LineEditor = { line: LineDes, point: PointDes } type LineHolder = { line?: LineEle, pointList: Array } export class LineDrawer extends BaseDrawer { readonly holder: LineHolder; constructor(id: string) { super(id); this.editor.point.pixelSize = 8; this.holder = { line: undefined, pointList: [] } } protected createEditor(): LineEditor { return { line: LineEle.defaultDes(), point: PointEle.defaultDes(), } } protected disposeData(): void { ActionManager.removeHelper(this.holder.line!.id); this.holder.line?.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 */ newSimpleLine(id: string): LineDrawer { this.holder.line = SimpleLineEle.create(id, []).init().inflate(); this.editor.line = LineEle.defaultDes(); return this; } /** * 创建新的贴地图形 * @param id * @returns */ newGroundLine(id: string): LineDrawer { this.holder.line = GroundLineEle.create(id, []).init().inflate(); this.editor.line = LineEle.defaultDes(); return this; } /** * 绑定新的图形 * @param line * @returns */ bindLine(line: LineEle): LineDrawer { this.holder.line?.stopEdit(); this.disposePoints(); this.holder.line = line; this.editor.line = line.des; this.initPoint(line.info.positionList); return this; } private initPoint(positionList: Array) { positionList.forEach(position => this.newPoint(position).inflate()); } /** * 取消图形绑定 * @returns */ unbindLine(): LineDrawer { this.deactive(); this.disposePoints(); this.holder.line = undefined; this.editor.line = LineEle.defaultDes(); return this; } /** * 清除所有添加的地图元素 */ clear(): LineDrawer { this.holder.line?.repositeArea([]); this.disposePoints(); return this; } /** * 开启功能(注册事件回调) */ active(): LineDrawer { // 点 this.holder.pointList.forEach(point => this.activePoint(point)); // 全局/线 const actionHelper = ActionManager.newHelper(this.holder.line!.id) .simple.active() .simple.onLeftTap((helper, info) => { if (this.holder.line!.isEditing() && info.elementId == undefined) { const point = this.newPoint(info.position).inflate(); this.activePoint(point); this.refreshLineData(); } else if (this.holder.line!.id == info.elementId) { if (this.holder.line!._answerDrag()) { helper.drag.active(); } this.holder.line!.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.line!.isEditing()) { this.holder.line!.stopEdit(); this.holder.pointList.forEach(point => point.stopEdit()); this.defocusCallback?.(this); } }) if (this.holder.line!._answerDrag()) { actionHelper.drag.active() .drag.onMove((helper, info) => { const vector = Utils.Vector.create(info.lp, info.position); this.holder.line!.move(vector); this.holder.pointList.forEach(point => point.move(vector)); }); } return this; } /** * 关闭功能(移除所有注册的事件回调) */ deactive(): LineDrawer { if (this.holder.line != undefined) { this.holder.line.stopEdit(); ActionManager.removeHelper(this.holder.line.id); } this.holder.pointList.forEach((point) => { point.stopEdit(); ActionManager.removeHelper(point.id); }) return this; } private newPoint(position: Cesium.Cartesian3): PointEle { const id = this.holder.line!.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.refreshLineData(); }) } private refreshLineData(): void { const positionList: Cesium.Cartesian3 = []; this.holder.pointList.forEach(point => { positionList.push(point.info.position.clone()); }) this.holder.line!.repositeArea(positionList); } /** * 刷新地图元素 */ refreshGeo(): void { this.holder.line!.refreshGeo(); this.holder.pointList.forEach(point => point.refreshGeo()); } /** * 获取所有绘制的点 */ getPositionList(): Array { return Utils.Position.copyList(this.holder.line!.info.positionList); } } const LineUpdaterId = 'LineUpdaterId'; export const LineUpdater = new LineDrawer(LineUpdaterId);