Refactor: 重构 HCesiumManager
This commit is contained in:
124
src/components/h-cesium-viewer/managers/HCesiumManager.站点.ts
Normal file
124
src/components/h-cesium-viewer/managers/HCesiumManager.站点.ts
Normal file
@ -0,0 +1,124 @@
|
||||
import * as Cesium from 'cesium';
|
||||
|
||||
import type { HCesiumManager } from './HCesiumManager';
|
||||
import type { I站点 } from './HCesiumManager.types';
|
||||
|
||||
export class GroundStationManager {
|
||||
private viewerManager: HCesiumManager;
|
||||
// 用于存储当前由此管理器管理的地面站实体的 Map
|
||||
private currentStationEntities: Map<string, Cesium.Entity> = new Map();
|
||||
|
||||
constructor(viewerManager: HCesiumManager) {
|
||||
this.viewerManager = viewerManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向视图中添加或更新地面站实体。
|
||||
* 注意:当前实现仅添加,如果已存在则警告并返回现有实体。
|
||||
* @param options - 地面站的选项参数
|
||||
* @returns 添加的实体对象,如果已存在或添加失败则返回 null 或现有实体。
|
||||
*/
|
||||
addOrUpdateStation(options: I站点): Cesium.Entity | null {
|
||||
const existingEntity = this.currentStationEntities.get(options.id);
|
||||
if (existingEntity) {
|
||||
console.warn(`ID 为 "${options.id}" 的地面站实体已由管理器追踪,跳过添加。`);
|
||||
// 未来可以扩展为更新逻辑
|
||||
return existingEntity;
|
||||
}
|
||||
|
||||
// 解构赋值获取站点信息
|
||||
const { height = 0, id, latitude, longitude, name, pixelSize = 10 } = options;
|
||||
const position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height);
|
||||
|
||||
const groundStationEntity = new Cesium.Entity({
|
||||
id, // 使用传入的 id 作为实体的唯一标识符
|
||||
name,
|
||||
position,
|
||||
point: {
|
||||
pixelSize,
|
||||
color: Cesium.Color.fromRandom({ alpha: 1 }), // 确保 alpha 为 1
|
||||
outlineColor: Cesium.Color.WHITE,
|
||||
outlineWidth: 2,
|
||||
},
|
||||
label: {
|
||||
text: name,
|
||||
font: '14pt sans-serif',
|
||||
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
|
||||
outlineWidth: 2,
|
||||
verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
|
||||
pixelOffset: new Cesium.Cartesian2(0, -12), // 标签略微偏移到点的上方
|
||||
},
|
||||
});
|
||||
|
||||
const addedEntity = this.viewerManager.addEntity(groundStationEntity);
|
||||
if (addedEntity) {
|
||||
// 添加成功后,将其存入 Map
|
||||
this.currentStationEntities.set(id, addedEntity);
|
||||
return addedEntity;
|
||||
} else {
|
||||
console.error(`通过 ViewerManager 添加 ID 为 "${id}" 的地面站实体失败。`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从视图中移除指定的地面站实体 (通过 ID)。
|
||||
* @param entityId - 要移除的地面站实体的 ID。
|
||||
* @returns 如果成功移除则返回 true,否则返回 false。
|
||||
*/
|
||||
removeStation(entityId: string): boolean {
|
||||
const entityToRemove = this.currentStationEntities.get(entityId);
|
||||
if (!entityToRemove) {
|
||||
// console.warn(`未在 GroundStationManager 中找到 ID 为 "${entityId}" 的实体,无法移除。`);
|
||||
return false; // 不在管理范围内
|
||||
}
|
||||
|
||||
const removed = this.viewerManager.removeEntity(entityToRemove); // 优先使用实体对象移除
|
||||
if (removed) {
|
||||
// 移除成功后,从 Map 中删除
|
||||
this.currentStationEntities.delete(entityId);
|
||||
return true;
|
||||
} else {
|
||||
console.warn(`尝试通过 ViewerManager 移除 ID 为 "${entityId}" 的地面站实体失败。`);
|
||||
// 即使 ViewerManager 移除失败,也尝试从内部 Map 中移除,以保持一致性?
|
||||
// 或者保留在 Map 中以供调试?暂时选择移除。
|
||||
this.currentStationEntities.delete(entityId);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有由此管理器管理的地面站实体。
|
||||
*/
|
||||
clearAllStations(): void {
|
||||
// 迭代 Map 的 ID 进行移除,避免在迭代 Map 值时修改 Map
|
||||
const idsToRemove = [...this.currentStationEntities.keys()];
|
||||
let removalFailed = false;
|
||||
for (const id of idsToRemove) {
|
||||
if (!this.removeStation(id)) {
|
||||
removalFailed = true; // 记录是否有移除失败的情况
|
||||
}
|
||||
}
|
||||
if (removalFailed) {
|
||||
console.warn('清除部分地面站实体时遇到问题。');
|
||||
}
|
||||
// 确保最终清空 Map
|
||||
this.currentStationEntities.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前管理的地面站实体 Map。
|
||||
* @returns 返回包含当前地面站实体的 Map。
|
||||
*/
|
||||
getCurrentStationEntities(): Map<string, Cesium.Entity> {
|
||||
return this.currentStationEntities;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁管理器,清理资源。
|
||||
*/
|
||||
destroy(): void {
|
||||
this.clearAllStations();
|
||||
// console.log('GroundStationManager 已销毁。');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user