// @ts-nocheck import { type ErrorCallback } from '../../Common' import { MarkerEle, type MarkerDes } from '../../map_element/element/Marker' import { LineEle, type LineDes, SimpleLineEle } from '../../map_element/element/Line' import { ActionManager } from '../../action/ActionManager' import { BaseEditor } from '../../editor/BaseEditor' import { LabelEle, type LabelDes } from '../../map_element/element/Label' import Utils from '../../Utils' type MeasureEditorDes = { line: LineDes, marker: MarkerDes, label: LabelDes } export type MeasureHolder = { line: LineEle, markerList: Array, labelList: Array } export abstract class MeasureDrawer> extends BaseEditor { readonly holder: MeasureHolder; protected errorCallback: ErrorCallback | undefined = undefined; constructor(id: string) { super(id); this.editorDes.marker.point.pixelSize = 8; this.holder = this.createHolder(); } protected createHolder(): MeasureHolder { return { line: SimpleLineEle.create(this.id, [], this.editorDes.line).init().inflate(), markerList: [], labelList: [] } } protected createEditorDes(): MeasureEditorDes { return { line: LineEle.defaultDes(), marker: MarkerEle.defaultDes(), label: LabelEle.defaultDes() } } protected disposeData(): void { ActionManager.removeHelper(this.id); this.holder.line.dispose(); this.disposeMarkers(); this.disposeLabels(); } protected disposeMarkers(): void { this.holder.markerList.forEach((marker) => { ActionManager.removeHelper(marker.id); marker.dispose(); }) this.holder.markerList.splice(0); } protected disposeLabels(): void { this.holder.labelList.forEach((label) => label.dispose()); this.holder.labelList.splice(0); } onError(callback: ErrorCallback): T { this.errorCallback = callback; return this as any as T; } /** * 初始化,可设置内部数据 * @returns */ init(): T { this.holder.line.init().repositeArea([]).inflate(); return this as any as T; } /** * 清除所有添加的地图元素 */ clear(): T { this.holder.line.repositeArea([]); this.holder.line.startEdit(); this.disposeMarkers(); this.disposeLabels(); return this as any as T; } /** * 开启功能(注册事件回调) */ active(): T { // 点 this.holder.markerList.forEach(marker => this.activeMarker(marker)); // 全局/线 ActionManager.newHelper(this.id) .simple.active() .simple.onLeftTap((helper, info) => { if (this.holder.line.isEditing() && info.elementId == undefined) { const result = this.leftTapCheck(); if (result != undefined) { this.errorCallback?.(result); return; } const marker = this.newMarker(info.position).inflate(); this.activeMarker(marker); this.refreshLineData(); } else if (this.id == info.elementId) { helper.drag.active(); this.holder.line.startEdit(); this.holder.markerList.forEach(marker => { ActionManager.findHelper(marker.id)?.drag.active(); marker.startEdit(); }); this.focusedCallback?.(this as any as T); } }) .simple.onRightTap((helper, info) => { const result = this.rightTapCheck(); if (result != undefined) { this.errorCallback?.(result); return; } helper.simple.deactive(); helper.drag.deactive(); this.holder.markerList.forEach((marker) => ActionManager.findHelper(marker.id)?.drag.deactive()) if (this.holder.line.isEditing()) { this.holder.line.stopEdit(); this.holder.markerList.forEach(marker => marker.stopEdit()); this.onEditFinish(); this.defocusCallback?.(this as any as T); } }) return this as any as T; } protected abstract leftTapCheck(): string | undefined; protected abstract rightTapCheck(): string | undefined; protected abstract onEditFinish(): void; /** * 关闭功能(移除所有注册的事件回调) */ deactive(): T { ActionManager.removeHelper(this.id); this.holder.markerList.forEach((marker) => ActionManager.removeHelper(marker.id)) return this as any as T; } protected newMarker(position: Cesium.Cartesian3): MarkerEle { const id = this.id + '_point_' + this.holder.markerList.length; const marker = MarkerEle.create(id, position, undefined, undefined, this.editorDes.marker) .init(true, true, false) .custom((des => des.point.pixelSize = this.editorDes.marker.point.pixelSize)); this.holder.markerList.push(marker); return marker; } protected activeMarker(marker: MarkerEle, onMove?: () => void) { ActionManager.newHelper(marker.id) .drag.active() .drag.onMove((helper, info) => { marker.reposite(info.position); this.refreshLineData(); onMove?.(); }) .drag.onStart((helper, info) => { if (info.position != undefined) { marker.reposite(info.position); } this.changeLabelVisibleState(false); }) .drag.onEnd((helper, info) => { if (info.position != undefined) { marker.reposite(info.position); } this.changeLabelVisibleState(true); this.onEditFinish(); }) } protected newLabel(position: Cesium.Cartesian3): LabelEle { const id = this.id + '_label_' + this.holder.labelList.length; const label = LabelEle.create(id, position, '', this.editorDes.label).init(); this.holder.labelList.push(label); return label; } protected changeLabelVisibleState(visible: boolean) { this.holder.markerList.forEach(marker => marker.custom(des => des.label.show = visible).refreshGeo()) this.holder.labelList.forEach(label => label.custom(des => des.show = visible).refreshGeo()) } protected abstract refreshLineData(): void; refreshGeo(): void { this.holder.line.refreshGeo(); this.holder.markerList.forEach(marker => marker.refreshGeo()); this.holder.labelList.forEach(marker => marker.refreshGeo()); } /** * 获取所有绘制的点 */ getPositionList(): Array { return Utils.Position.copyList(this.holder.line!.info.positionList); } }