yantar/assets/Scripts/ux/ErrorItemButton.ts
2025-02-21 12:30:16 +07:00

135 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 { ErrorManager } from '../stores/ErrorManager';
const { ccclass, property } = _decorator;
@ccclass('ErrorItemButton')
export class ErrorItemButton extends Component {
@property(Label)
public nameLabel: Label | null = null;
@property(Label)
public timeLabel: Label | null = null;
// Кнопка для стандартного действия (подсветка и перемещение камеры)
@property(Button)
public actionButton: Button | null = null;
// Кнопка для отключения звукового оповещения
@property(Button)
public disableAlertButton: Button | null = null;
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;
}
console.log('Инициализирован ErrorItemButton с данными:', data);
}
onLoad() {
if (this.actionButton) {
this.actionButton.node.on('click', this.onActionClick, this);
} else {
console.warn('Кнопка действия не назначена.');
}
if (this.disableAlertButton) {
this.disableAlertButton.node.on('click', this.onDisableAlertClick, this);
} else {
console.warn('Кнопка отключения оповещения не назначена.');
}
}
onDestroy() {
if (this.actionButton) {
this.actionButton.node.off('click', this.onActionClick, this);
}
if (this.disableAlertButton) {
this.disableAlertButton.node.off('click', this.onDisableAlertClick, this);
}
}
// Обработка клика по кнопке стандартного действия
onActionClick(event?: Event) {
console.log('Нажата кнопка действия для:', this.itemData);
if (!this.itemData) return;
// Ищем датчик в списке оборудования по имени
const sensors = EquipmentManager.instance.dataList;
const sensorData = sensors.find(s => s.name === this.itemData!.name);
if (!sensorData) {
console.warn(`Датчик ${this.itemData.name} не найден!`);
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.`);
}
// Перемещаем камеру к найденному узлу
const cameraMover = scene.getComponentInChildren(CameraMover);
if (cameraMover) {
cameraMover.moveToTarget(targetNode);
} else {
console.warn('CameraMover не найден!');
}
}
// Обработка клика по кнопке отключения звукового оповещения
onDisableAlertClick(event?: Event) {
console.log('Нажата кнопка отключения оповещения для:', this.itemData);
if (!this.itemData) return;
// Вызываем метод из ErrorManager для отключения оповещения по тегу
ErrorManager.instance.disableAlertForError(this.itemData.tag);
}
// Рекурсивный поиск узла по имени
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;
}
}