// @ts-nocheck import Utils from '../../Utils' import { Enviroment } from '../../Enviroment' import { AreableElement } from "../BaseElement" import { type AreaInfo, ElementOperator, LineType } from '../Common' import { type OutlineDes, SimpleOutlineOperator, GroundOutlineOperator } from './Outline' export type PolygonInfo = AreaInfo & { height?: number, } export type PolygonDes = { color: Cesium.Color, editColor: Cesium.Color, // 编辑时颜色 outline: OutlineDes } export abstract class PolygonEle> extends AreableElement{ protected outlineOperator: ElementOperator | undefined = undefined; protected createDes(): PolygonDes { return PolygonEle.defaultDes(); } static defaultDes(): PolygonDes { return { color: Cesium.Color.RED.withAlpha(0.2), editColor: Cesium.Color.YELLOW.withAlpha(0.2), outline: { show: true, width: 1, color: Cesium.Color.RED, editColor: Cesium.Color.YELLOW, type: LineType.Simple } } } dispose(): void { this.outlineOperator?.remove() super.dispose(); } inflate(): T { super.inflate() this.outlineOperator?.inflate(); return this.asType(); } protected repositeGeo(): void { super.repositeGeo(); this.outlineOperator?.reposite(); } refreshGeo(): void { super.refreshGeo(); this.outlineOperator?.refresh(); } /** * 重置数据 * @param positionList 点集合 * */ repositeArea(positionList: Array, height?: number): T { this.info.height = height; super.repositeArea(positionList); this.outlineOperator?.reposite(); return this.asType(); } } abstract class PolygonOperator extends ElementOperator{ protected prim?: Cesium.Primitive = undefined; protected abstract createPrimitive(): Cesium.Primitive; protected createElement(): Cesium.GeometryInstance { const color = (this.stateble.isEditing() ? this.des.editColor : this.des.color); // 复制数据,避免数据污染 const positionList = Utils.Position.copyList(this.info.positionList); return new Cesium.GeometryInstance({ id: this.id, geometry: new Cesium.PolygonGeometry({ polygonHierarchy: new Cesium.PolygonHierarchy(positionList), height: this.info.height, vertexFormat: Cesium.PerInstanceColorAppearance.VERTEXT_FORMAT, perPositionHeight: (this.info.height == undefined), }), attributes: { color: Cesium.ColorGeometryInstanceAttribute.fromColor(color), show: new Cesium.ShowGeometryInstanceAttribute(this.stateble.isVisible()) } }); }; inflate(): void { if (this.prim != undefined || this.info.positionList.length < 3) { return; } this.prim = Enviroment.primitives.add(this.createPrimitive()); } remove(): void { if (this.prim == undefined) { return; } Enviroment.primitives.remove(this.prim); this.prim.destroy(); this.prim = undefined; } refresh(): void { if (this.prim == undefined) { return; } this.prim.show = this.stateble.isVisible(); const color = (this.stateble.isEditing() ? this.des.editColor : this.des.color); const geoAttr: Cesium.GeometryAttributes = this.prim.getGeometryInstanceAttributes(this.id); geoAttr.color = Cesium.ColorGeometryInstanceAttribute.toValue(color); geoAttr.show = new Cesium.ShowGeometryInstanceAttribute.toValue(this.stateble.isVisible()); } } export class SimplePolygonEle extends PolygonEle { static create(id: string, positionList: Array, height?: number, des?: PolygonDes): SimplePolygonEle { return new SimplePolygonEle(id, { positionList: positionList, height: height }, des); } init(): SimplePolygonEle { this.operator = new SimplePolygonOperator(this.id, this, this.info, this.des); this.outlineOperator = new SimpleOutlineOperator(this.id, this, this.info, this.des.outline); return this; } } class SimplePolygonOperator extends PolygonOperator { protected createPrimitive(): Cesium.Primitive { return new Cesium.Primitive({ geometryInstances: this.createElement(), appearance: new Cesium.PerInstanceColorAppearance({ flat: true, }), modelMatrix: this.getMatrix(), asynchronous: false, show: this.stateble.isVisible() }); } reposite(): void { this.prim.modelMatrix = this.getMatrix(); } getMatrix(): Cesium.Matrix4 { if (this.info.vector == undefined) { return; } const matrix = Cesium.Matrix4.multiplyByTranslation(this.prim.modelMatrix, this.info.vector, new Cesium.Matrix4()); return matrix; } } export class GroundPolygonEle extends PolygonEle { static create(id: string, positionList: Array, height?: number, des?: PolygonDes): GroundPolygonEle { return new GroundPolygonEle(id, { positionList: positionList, height: height }, des); } init(): GroundPolygonEle { this.operator = new GroundPolygonOperator(this.id, this, this.info, this.des); this.outlineOperator = new GroundOutlineOperator(this.id, this, this.info, this.des.outline); return this; } _answerDrag(): boolean { return false; } } class GroundPolygonOperator extends PolygonOperator { protected createPrimitive(): Cesium.Primitive { return new Cesium.GroundPrimitive({ geometryInstances: this.createElement(), appearance: new Cesium.PerInstanceColorAppearance({ flat: true, }), asynchronous: false, show: this.stateble.isVisible() }); } reposite(): void { if (this.prim == undefined) { return; } this.remove(); this.inflate(); } }