// @ts-nocheck import { Enviroment } from "../../Enviroment"; import { PositebleElement } from "../BaseElement"; import { ElementOperator, type PositeInfo } from "../Common"; export type LabelInfo = PositeInfo & { text?: string } export type LabelDes = { show: boolean, // 显示标签 textSize: number, // 文字大小 fontFamily: string, // 字体 textColor: Cesium.Color, // 文字颜色 offset?: Cesium.Cartesian2, // 偏移 - 右下为正 showBg: boolean, // 显示背景 bgColor?: Cesium.Color, // 背景色 bgPadding?: Cesium.Cartesian2, // 背景padding scaleByDistance?: Cesium.NearFarScalar, // 距离 - 缩放 distanceDisplayCondition?: Cesium.DistanceDisplayCondition,// 距离 - 显隐 } export class LabelEle extends PositebleElement{ static create(id: string, position: Cesium.Cartesian3, text: string, des?: LabelDes): LabelEle { return new LabelEle(id, { position: position, text: text }, des); } protected createDes(): LabelDes { return LabelEle.defaultDes(); } static defaultDes(): LabelDes { return { show: true, textSize: 14, fontFamily: 'sans-serif', textColor: Cesium.Color.RED, bgColor: Cesium.Color.BLACK.withAlpha(0.2), bgPadding: new Cesium.Cartesian2(4, 2), showBg: true, }; } init(): LabelEle { this.operator = new LabelOperator(this.id, this, this.info, this.des); return this; } /** * 重置文本数据 * @param text 标签文本 * */ resetText(text: string): LabelEle { this.info.text = text; return this; } } export class LabelOperator extends ElementOperator { private label?: Cesium.Label = undefined; inflate(): void { if (this.label != undefined) { return; } this.label = Enviroment.labels.add({ id: this.id, text: this.info.text, position: this.info.position, font: this.des.textSize + 'px ' + this.des.fontFamily, fillColor: this.des.textColor, showBackground: this.des.showBg, backgroundColor: this.des.bgColor, backgroundPadding: this.des.bgPadding, pixelOffset: this.getOffset(), disableDepthTestDistance: Number.POSITIVE_INFINITY, // 禁用深度测试 scaleByDistance: this.des.scaleByDistance, distanceDisplayCondition: this.des.distanceDisplayCondition, show: this.isVisible() }); } isVisible(): boolean { return (this.info.text != undefined && this.info.text != '' && this.stateble.isVisible() && this.des.show) } remove(): void { if (this.label == undefined) { return; } Enviroment.labels.remove(this.label); this.label = undefined; } refresh(): void { if (this.label == undefined || this.info.text == undefined) { return; } this.label.text = this.info.text; this.label.font = this.des.textSize + 'px ' + this.des.fontFamily; this.label.fillColor = this.des.textColor; this.label.showBackground = this.des.showBg; this.label.backgroundColor = this.des.bgColor; this.label.backgroundPadding = this.des.bgPadding; this.label.pixelOffset = this.des.offset; this.label.scaleByDistance = this.des.scaleByDistance; this.label.distanceDisplayCondition = this.des.distanceDisplayCondition; this.label.show = this.isVisible(); } reposite(): void { if (this.label == undefined) { return; } this.label.position = this.info.position; } protected getOffset(): Cesium.Cartesian2 { return this.des.offset; } }