yantar/assets/Scripts/ux/ErrorItemNamedButton.ts

121 lines
3.9 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 { EquipmentManager } from '../stores/EquipmentManager';
import { PulsatingHighlighter } from '../PulsatingHighlighter';
import { CameraMover } from '../CameraMover';
import { ErrorItemData } from '../stores/ErrorItemData';
const { ccclass, property } = _decorator;
@ccclass('ErrorItemNamedButton')
export class ErrorItemNamedButton extends Component {
@property(Label)
public nameLabel: Label | null = null;
@property(Label)
public timeLabel: Label | null = null;
@property(Button)
public actionButton: Button | null = null; // Первая кнопка – выбирает оборудование
private itemData: ErrorItemData | null = null;
// Инициализация данных для элемента ошибки
public initialize(data: ErrorItemData) {
this.itemData = data;
if (this.nameLabel) {
this.nameLabel.string = data.description;
}
if (this.timeLabel) {
this.timeLabel.string = data.timestamp;
}
console.log('Initialized ErrorItemButton with data:', data);
}
onLoad() {
if (this.actionButton) {
this.actionButton.node.on('click', this.onActionClick, this);
} else {
console.warn('Action button not assigned.');
}
}
onDestroy() {
if (this.actionButton) {
this.actionButton.node.off('click', this.onActionClick, this);
}
}
// Обработка клика по кнопке стандартного действия (без изменений)
onActionClick(event?: Event) {
console.log('Action clicked for error:', this.itemData);
if (!this.itemData) return;
const sensors = EquipmentManager.instance.dataList;
const sensorData = sensors.find(s => s.name === this.itemData!.sensorName);
if (!sensorData) {
console.warn(`Sensor ${this.itemData.sensorName} not found!`);
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(`Unexpected sensor value: ${sensorData.value}`);
return;
}
const scene = this.node.scene;
if (!scene) {
console.warn('Scene not found!');
return;
}
// Рекурсивно ищем узел с именем, соответствующим sensorName
const targetNode = this.findNodeByName(scene, this.itemData.sensorName);
if (!targetNode) {
console.warn(`Node with name ${this.itemData.sensorName} not found!`);
return;
}
const highlighter = targetNode.getComponent(PulsatingHighlighter);
if (highlighter) {
highlighter.switchToColor(color);
} else {
console.warn(`Node ${this.itemData.sensorName} does not have PulsatingHighlighter.`);
}
const cameraMover = scene.getComponentInChildren(CameraMover);
if (cameraMover) {
cameraMover.moveToTarget(targetNode);
} else {
console.warn('CameraMover not found!');
}
}
// Обработка клика по кнопке отключения оповещения (отправляет запрос на API)
// Рекурсивный поиск узла по имени
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;
}
}