128 lines
4.6 KiB
TypeScript
128 lines
4.6 KiB
TypeScript
// ItemButton.ts
|
||
import { _decorator, Component, Label, Button, Node, Event } from 'cc';
|
||
import { apiSensors } from '../stores/apiSensors';
|
||
import { EquipmentManager } from '../stores/EquipmentManager';
|
||
import { PulsatingHighlighter } from '../PulsatingHighlighter';
|
||
import { CameraMover } from '../CameraMover';
|
||
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('ItemButton')
|
||
export class ItemButton extends Component {
|
||
@property(Label)
|
||
public nameLabel: Label | null = null;
|
||
|
||
@property(Label)
|
||
public timeLabel: Label | null = null;
|
||
|
||
@property(Button)
|
||
public button: Button | null = null;
|
||
|
||
// Индекс элемента, если требуется
|
||
public itemIndex: number = 0;
|
||
|
||
private itemData: apiSensors | null = null;
|
||
|
||
public initialize(data: apiSensors) {
|
||
this.itemData = data;
|
||
if (this.nameLabel) {
|
||
this.nameLabel.string = data.name;
|
||
}
|
||
if (this.timeLabel) {
|
||
this.timeLabel.string = data.connect_time;
|
||
}
|
||
}
|
||
|
||
onLoad() {
|
||
// Назначаем обработчик на кнопку, а не на весь префаб
|
||
if (this.button) {
|
||
this.button.node.on('click', this.onClick, this);
|
||
} else {
|
||
console.warn('Компонент Button не назначен в ItemButton.');
|
||
}
|
||
}
|
||
|
||
onDestroy() {
|
||
if (this.button) {
|
||
this.button.node.off('click', this.onClick, this);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Рекурсивный поиск узла по имени.
|
||
* @param root Корневой узел для поиска.
|
||
* @param name Имя, которое нужно найти.
|
||
* @returns Найденный узел или null.
|
||
*/
|
||
private _findNodeByName(root: Node, name: string): Node | null {
|
||
if (root.name === name) return root;
|
||
for (let i = 0; i < root.children.length; i++) {
|
||
const found = this._findNodeByName(root.children[i], name);
|
||
if (found) return found;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
public onClick(event?: Event) {
|
||
console.log('Нажата кнопка для элемента:', this.itemData);
|
||
if (!this.itemData) return;
|
||
|
||
// Проверяем наличие EquipmentManager
|
||
if (!EquipmentManager.instance) {
|
||
console.warn('EquipmentManager не доступен!');
|
||
return;
|
||
}
|
||
|
||
// Находим датчик в списке оборудования по имени
|
||
const sensors = EquipmentManager.instance.dataList;
|
||
const sensorData = sensors.find(s => s.name === this.itemData!.name);
|
||
if (!sensorData) {
|
||
console.warn(`Датчик с именем ${this.itemData.name} не найден в EquipmentManager.`);
|
||
return;
|
||
}
|
||
|
||
// Определяем цвет подсветки по значению датчика
|
||
let color = "";
|
||
if (sensorData.value === 2) {
|
||
color = "red";
|
||
} else if (sensorData.value === 1) {
|
||
color = "green";
|
||
} else if (sensorData.value === 0) {
|
||
color = "yellow";
|
||
} else {
|
||
console.warn(`Неожиданное значение sensorData.value: ${sensorData.value}`);
|
||
return;
|
||
}
|
||
|
||
// Получаем корневой узел сцены
|
||
const scene = this.node.scene;
|
||
if (!scene) {
|
||
console.warn('Сцена не найдена!');
|
||
return;
|
||
}
|
||
|
||
// Ищем узел с таким же именем, как у датчика
|
||
const targetNode = this._findNodeByName(scene, this.itemData.name);
|
||
if (!targetNode) {
|
||
console.warn(`Узел с именем ${this.itemData.name} не найден в сцене.`);
|
||
return;
|
||
}
|
||
|
||
// Если у найденного узла есть компонент PulsatingHighlighter, запускаем подсветку
|
||
const highlighter = targetNode.getComponent(PulsatingHighlighter);
|
||
if (highlighter) {
|
||
highlighter.switchToColor(color);
|
||
} else {
|
||
console.warn(`Узел ${this.itemData.name} не содержит PulsatingHighlighter.`);
|
||
}
|
||
|
||
// Находим в сцене компонент CameraMover и перемещаем камеру к выбранному узлу
|
||
const cameraMover = scene.getComponentInChildren(CameraMover);
|
||
if (cameraMover) {
|
||
cameraMover.moveToTarget(targetNode);
|
||
} else {
|
||
console.warn('CameraMover не найден в сцене!');
|
||
}
|
||
}
|
||
}
|