— так компоненты не упадут, даже если кнопка или её node к моменту onDestroy уже не существует сделал ноды менеджеров списка оборудовани, деталей, и аварий Persistent Node, чтобы при смене сцены не сбрасывалось их состояние.
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
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() {
|
||
this.actionButton?.node?.on('click', this.onActionClick, this);
|
||
}
|
||
|
||
onDestroy() {
|
||
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;
|
||
}
|
||
|
||
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!');
|
||
}
|
||
}
|
||
|
||
private findNodeByName(root: Node, name: string): Node | null {
|
||
if (root.name === name) return root;
|
||
for (const child of root.children) {
|
||
const found = this.findNodeByName(child, name);
|
||
if (found) return found;
|
||
}
|
||
return null;
|
||
}
|
||
}
|