61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import { _decorator, Component, Label, Node } from 'cc'; // <-- добавили Node
|
||
import { SensorDescriptionField } from '../stores/SensorDescriptionField';
|
||
const { ccclass, property } = _decorator;
|
||
|
||
@ccclass('SensorFieldItem')
|
||
export class SensorFieldItem extends Component {
|
||
@property(Label)
|
||
public nameLabel: Label | null = null;
|
||
|
||
@property(Label)
|
||
public valueLabel: Label | null = null;
|
||
|
||
@property(Label)
|
||
public unitLabel: Label | null = null;
|
||
|
||
@property(Label)
|
||
public descriptionLabel: Label | null = null;
|
||
|
||
/** Нода-контейнер для анимации (спрайт + Label) */
|
||
@property({ type: Node })
|
||
public descriptionContainer: Node | null = null;
|
||
|
||
/** Нода Morph, которую нужно скрывать при наличии описания */
|
||
@property({ type: Node })
|
||
public Morph: Node | null = null;
|
||
|
||
protected onLoad(): void {
|
||
// Сразу прячем контейнер, теперь active доступен
|
||
if (this.descriptionContainer) {
|
||
this.descriptionContainer.active = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Установить данные для отображения поля датчика.
|
||
* @param field Объект поля датчика (name, value, unit, description)
|
||
*/
|
||
public setData(field: SensorDescriptionField): void {
|
||
if (this.nameLabel) this.nameLabel.string = field.name;
|
||
if (this.valueLabel) this.valueLabel.string = String(field.value);
|
||
if (this.unitLabel) this.unitLabel.string = field.unit ?? "????";
|
||
|
||
const hasDesc = !!field.description?.trim();
|
||
|
||
// Работаем с описанием
|
||
if (this.descriptionLabel) {
|
||
this.descriptionLabel.string = hasDesc ? field.description! : "";
|
||
}
|
||
if (this.descriptionContainer) {
|
||
this.descriptionContainer.active = hasDesc;
|
||
}
|
||
|
||
// Morph: теперь показываем его только при наличии описания,
|
||
// и скрываем, когда описания нет
|
||
if (this.Morph) {
|
||
this.Morph.active = hasDesc;
|
||
}
|
||
}
|
||
|
||
}
|