// @ts-nocheck import { Axis, type Activeble, type Clearable, type Disposable } from '../../Common' import { SdkError } from '../../Error' import Utils from '../../Utils' import { ActionManager } from '../../action/ActionManager' import { RotateIndicator } from "./RotateIndicator"; import { SimplePolygonEle } from '../../map_element/element/Polygon'; import { SimpleLineEle } from '../../map_element/element/Line'; import { LineType } from '../../map_element/Common'; import { ModuleCode } from '../../Module'; import { Authorize } from '../../Authorize'; enum ClipMode { Horizontal, Vertical, Custom } export class ClipHelper implements Activeble, Clearable, Disposable { readonly id: string; readonly lineLength: number; readonly areaSize: Cesium.Cartesian2; private position: Cesium.Cartesian3 | undefined = undefined; rotateIndicator: RotateIndicator | undefined = undefined; directionIndicator: SimpleLineEle | undefined = undefined; clipPlane: SimplePolygonEle | undefined = undefined; private layerList: Array | undefined = undefined; readonly mode: ClipMode = ClipMode.Custom; static create(id: string, lineLength?: number, areaSize?: Cesium.Cartesian2): ClipHelper { return new ClipHelper(id, ClipMode.Custom, lineLength, areaSize); } static createH(id: string, lineLength?: number, areaSize?: Cesium.Cartesian2): ClipHelper { Authorize.check(ModuleCode.ClipH); return new ClipHelper(id, ClipMode.Horizontal, lineLength, areaSize); } static createV(id: string, lineLength?: number, areaSize?: Cesium.Cartesian2): ClipHelper { Authorize.check(ModuleCode.ClipV); return new ClipHelper(id, ClipMode.Vertical, lineLength, areaSize); } private constructor(id: string, mode: ClipMode, lineLength: number = 5, areaSize: Cesium.Cartesian2 = new Cesium.Cartesian2(12, 8)) { this.id = id; this.lineLength = lineLength; this.areaSize = areaSize; this.mode = mode; } dispose(): void { // 移除回调 this.deactive(); // 清除指示器/剖切 this.clear(); this.position = undefined; } /** * 清除图元/剖切 */ clear(): ClipHelper { // 销毁指示器 this.clearIndicator(); // 清除剖切 this.clearClip(); return this; } /** * 清除指示器 */ clearIndicator(): ClipHelper { this.clipPlane?.dispose(); this.clipPlane = undefined; this.directionIndicator?.dispose(); this.directionIndicator = undefined; this.rotateIndicator?.dispose(); this.rotateIndicator = undefined; return this; } /** * 清除剖切 */ clearClip(): ClipHelper { if (this.layerList != undefined) { this.layerList.forEach(layer => { layer.clearCustomClipBox(); }) } this.layerList = undefined; return this; } active(): ClipHelper { if((Authorize.without(ModuleCode.ClipH) || Authorize.without(ModuleCode.ClipH))){ return this; } this.clearClip(); this.rotateIndicator?.active(); ActionManager.newHelper(this.id) .simple.active() .simple.onLeftTap((helper, info) => { if (info.elementId == undefined && this.clipPlane == undefined) { this.position = info.position; this.createIndicators(); } else if (this.id == info.elementId) { helper.drag.active(); this.clipPlane?.startEdit(); this.directionIndicator?.startEdit(); } }) .simple.onRightTap((helper, info) => { helper.simple.deactive(); helper.drag.deactive(); if (this.clipPlane?.isEditing()) { this.clipPlane?.stopEdit(); this.directionIndicator?.stopEdit(); } }) .drag.active() .drag.onMove((helper, info) => { const vector = Utils.Vector.create(info.lp, info.position); this.position = Utils.Vector.move(this.position, vector); this.clipPlane?.move(vector); this.directionIndicator?.move(vector); this.rotateIndicator?.move(vector); }) .drag.onStart((helper, info) => { this.rotateIndicator?.deactive(); }) .drag.onEnd((helper, info) => this.activeRotate()); return this; } private activeRotate() { const axis = Axis.create(this.position); if (this.mode == ClipMode.Vertical) { this.rotateIndicator?.activeZ(); } else if (this.mode == ClipMode.Custom) { this.rotateIndicator?.active(); } this.rotateIndicator?.onRotate((hpr) => { this.clipPlane?.repositeArea(this.createPlanePositionList(axis, hpr)); this.directionIndicator?.repositeArea(this.createLinePositionList(axis, hpr)); }); } private createIndicators() { const axis = Axis.create(this.position); let hpr: Cesium.HeadingPitchRoll = undefined; if (this.mode == ClipMode.Horizontal) { hpr = new Cesium.HeadingPitchRoll(0, Utils.Radian_90, 0); } this.rotateIndicator = new RotateIndicator(this.id + '_rotate', this.position, hpr); this.activeRotate(); this.directionIndicator = SimpleLineEle.create(this.id + '_direction', this.createLinePositionList(axis, hpr)).init() .custom((des) => { des.width = 8; des.type = LineType.Arrow; }) .inflate(); this.clipPlane = SimplePolygonEle.create(this.id, this.createPlanePositionList(axis, hpr)).init() .inflate(); } private createLinePositionList(axis: Axis, hpr?: Cesium.HeadingPitchRoll): Array { const positionList = new Array(2); positionList[0] = this.position; let matrix = undefined; if (hpr != undefined) { matrix = Cesium.Matrix3.fromHeadingPitchRoll(hpr, new Cesium.Matrix3()); } positionList[1] = this.calculatePosition(axis, matrix, new Cesium.Cartesian3(this.lineLength, 0, 0)); return positionList; } private createPlanePositionList(axis: Axis, hpr?: Cesium.HeadingPitchRoll): Array { const positionList = new Array(4); const x = this.areaSize.x / 2; const y = this.areaSize.y / 2; let matrix = undefined; if (hpr != undefined) { matrix = Cesium.Matrix3.fromHeadingPitchRoll(hpr, new Cesium.Matrix3()); } positionList[0] = this.calculatePosition(axis, matrix, new Cesium.Cartesian3(0, x, y)); positionList[1] = this.calculatePosition(axis, matrix, new Cesium.Cartesian3(0, x, -y)); positionList[2] = this.calculatePosition(axis, matrix, new Cesium.Cartesian3(0, -x, -y)); positionList[3] = this.calculatePosition(axis, matrix, new Cesium.Cartesian3(0, -x, y)); return positionList; } private calculatePosition(axis: Axis, matrix: Cesium.Matrix3, position: Cesium.Cartesian3): Cesium.Cartesian3 { if (matrix != undefined) { position = Cesium.Matrix3.multiplyByVector(matrix, position, new Cesium.Cartesian3()); } return axis.toWorld(position); } deactive(): ClipHelper { ActionManager.removeHelper(this.id); this.rotateIndicator?.deactive(); return this; } executeClip(layerList: Array): ClipHelper { if((Authorize.without(ModuleCode.ClipH) || Authorize.without(ModuleCode.ClipH))){ return this; } this.layerList = layerList; this.deactive(); if (this.clipPlane != undefined) { const positionList = this.clipPlane.info.positionList; layerList.forEach(layer => { layer.setCustomClipPlane(positionList[2], positionList[1], positionList[0]); }) } return this; } customAngle(callback: (indicator: RotateIndicator) => void): ClipHelper { if((Authorize.without(ModuleCode.ClipH) || Authorize.without(ModuleCode.ClipH))){ return this; } if (this.rotateIndicator == undefined) { throw SdkError.createDefault('DataError', '未创建RitateIndicattor'); } callback(this.rotateIndicator); return this; } }