138 lines
4.6 KiB
TypeScript
138 lines
4.6 KiB
TypeScript
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';
|
||
import { ErrorLazyLoadList } from '../ui/ErrorLazyLoadList';
|
||
|
||
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('ItemButton')
|
||
export class ItemButton extends Component {
|
||
@property(Label)
|
||
public nameLabel: Label | null = null;
|
||
|
||
@property(Button)
|
||
public button: Button | null = null;
|
||
|
||
// Кнопка для запуска фильтрации ошибок
|
||
@property(Button)
|
||
public ErrorButton: Button | null = null;
|
||
|
||
// Свойство для вывода количества ошибок на оборудовании
|
||
@property(Label)
|
||
public ErrorNumber: Label | 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;
|
||
}
|
||
}
|
||
|
||
onLoad() {
|
||
if (this.button) {
|
||
this.button.node.on('click', this.onClick, this);
|
||
} else {
|
||
console.warn('Компонент Button не назначен в ItemButton.');
|
||
}
|
||
|
||
if (this.ErrorButton) {
|
||
this.ErrorButton.node.on('click', this.onErrorClick, this);
|
||
} else {
|
||
console.warn('Компонент ErrorButton не назначен в ItemButton.');
|
||
}
|
||
}
|
||
|
||
onDestroy() {
|
||
if (this.button) {
|
||
this.button.node.off('click', this.onClick, this);
|
||
}
|
||
if (this.ErrorButton) {
|
||
this.ErrorButton.node.off('click', this.onErrorClick, this);
|
||
}
|
||
}
|
||
|
||
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;
|
||
|
||
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;
|
||
}
|
||
|
||
const highlighter = targetNode.getComponent(PulsatingHighlighter);
|
||
if (highlighter) {
|
||
highlighter.switchToColor(color);
|
||
} else {
|
||
console.warn(`Узел ${this.itemData.name} не содержит PulsatingHighlighter.`);
|
||
}
|
||
|
||
const cameraMover = scene.getComponentInChildren(CameraMover);
|
||
if (cameraMover) {
|
||
cameraMover.moveToTarget(targetNode);
|
||
} else {
|
||
console.warn('CameraMover не найден в сцене!');
|
||
}
|
||
}
|
||
|
||
public onErrorClick(event?: Event) {
|
||
console.log('Нажата кнопка Error для элемента:', this.itemData);
|
||
if (!this.itemData) return;
|
||
|
||
// Используем статическую переменную ErrorLazyLoadList.instance
|
||
if (!ErrorLazyLoadList.instance) {
|
||
console.warn('ErrorLazyLoadList.instance не найден!');
|
||
return;
|
||
}
|
||
ErrorLazyLoadList.instance.filterBySensor(this.itemData.name);
|
||
}
|
||
}
|