import {CssType, HtmlPanel} from '@deep/engine'; /** * 传感器面板类 */ export class SensorModal extends HtmlPanel { private sensorData: { sensors: Array<{ type: string; channel: number; position: { x: number; y: number; z: number; }; }>; } = { sensors: [] }; /** * 构造函数 * @param type CSS渲染类型 */ constructor(type: CssType = CssType.CSS3D) { super(type); this.updateContentFromData(); } /** * 更新传感器数据 * @param data 新的传感器数据 */ updateData(data: { sensors: Array<{ type: string; channel: number; position: { x: number; y: number; z: number; }; }>; }): void { this.sensorData = data; this.updateContentFromData(); } /** * 根据数据更新弹窗内容 */ private updateContentFromData(): void { // 使用 cssObject 的 uuid 作为唯一标识 作为 关闭按键的id const cssObject = this.getCssObject(); const closeButtonId = cssObject.uuid; const content = `

传感器数据

${this.sensorData.sensors.length > 0 ? this.sensorData.sensors.map((sensor, index) => `

传感器 ${index + 1}

类型: ${sensor.type}
通道: ${sensor.channel}
位置: (${sensor.position.x}, ${sensor.position.y}, ${sensor.position.z})
`).join('') : '

暂无传感器数据

' }
`; this.updateContent(content); this.attachCloseButtonListener(closeButtonId); } /** * 附加关闭按钮事件监听器 * @param buttonId 按钮ID */ private attachCloseButtonListener(buttonId: string): void { // 使用 setTimeout 确保 DOM 已更新 setTimeout(() => { const closeButton = document.getElementById(buttonId); if (closeButton) { closeButton.onclick = () => { this.hide(); }; } }, 0); } }