yantar/assets/Scripts/ui/VirtualList.ts

132 lines
4.5 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, Prefab, ScrollView, instantiate, UITransform, Node, MeshRenderer } from 'cc';
import { ItemButton } from '../ux/ItemButton';
import { apiSensors } from '../stores/apiSensors';
import { EquipmentManager } from '../stores/EquipmentManager';
const { ccclass, property } = _decorator;
@ccclass("LazyLoadList")
export class LazyLoadList extends Component {
@property(Prefab)
public itemPrefab: Prefab = null!;
@property(ScrollView)
public scrollView: ScrollView = null!;
@property(Node)
public equipmentNode: Node = null!;
@property
public spacing: number = 10;
private _data: apiSensors[] = [];
private _totalCount: number = 0;
private _loadedCount: number = 0;
private _itemHeight: number = 0;
private _content: Node = null!;
private _contentUI: UITransform = null!;
onLoad() {
if (!this.scrollView.content) {
console.error("ScrollView.content равен null!");
return;
}
this._content = this.scrollView.content;
this._contentUI = this._content.getComponent(UITransform)!;
const tempNode: Node = instantiate(this.itemPrefab);
const tempUI = tempNode.getComponent(UITransform);
if (!tempUI) {
console.error("В префабе отсутствует компонент UITransform!");
tempNode.destroy();
return;
}
this._itemHeight = tempUI.height;
tempNode.destroy();
this.waitForEquipmentManager();
// Добавляем обработчик прокрутки по аналогии с рабочим компонентом
this.scrollView.node.on('scrolling', this.onScrolling, this);
}
private onScrolling() {
const offsetY = this.scrollView.getScrollOffset().y;
const viewHeight = this.scrollView.node.getComponent(UITransform)!.height;
const contentHeight = this._contentUI.height;
if (offsetY + viewHeight + (this._itemHeight * 2) >= contentHeight) {
this.loadNextBatch(20);
}
}
private waitForEquipmentManager() {
if (!EquipmentManager.instance) {
this.scheduleOnce(() => { this.waitForEquipmentManager(); }, 0.1);
return;
}
EquipmentManager.instance.initialDataPromise.then(() => {
EquipmentManager.instance.node.on('data-updated', this.onDataUpdated, this);
this.onDataUpdated();
}).catch((error) => {
console.error("Ошибка загрузки данных в EquipmentManager:", error);
});
}
private async onDataUpdated() {
if (!EquipmentManager.instance) return;
const allData: apiSensors[] = await EquipmentManager.instance.getDataList();
const validNames: string[] = [];
if (this.equipmentNode) {
this.equipmentNode.children.forEach(child => {
if (child.getComponent(MeshRenderer)) {
validNames.push(child.name);
}
});
}
this._data = allData.filter(item => validNames.indexOf(item.name) !== -1);
this._totalCount = this._data.length;
this._loadedCount = 0;
this._content.removeAllChildren();
this.loadNextBatch(20);
}
loadNextBatch(batchSize: number) {
if (this._loadedCount >= this._totalCount) return;
const endIndex = Math.min(this._loadedCount + batchSize, this._totalCount);
for (let i = this._loadedCount; i < endIndex; i++) {
const item: Node = instantiate(this.itemPrefab);
const posY = -(this._loadedCount * (this._itemHeight + this.spacing) + this._itemHeight / 2);
item.setPosition(0, posY, 0);
this._content.addChild(item);
const itemButton = item.getComponent(ItemButton);
if (itemButton) {
itemButton.initialize(this._data[i]);
itemButton.itemIndex = i;
} else {
console.error("Компонент ItemButton не найден на ноде", item);
}
this._loadedCount++;
}
this._contentUI.height = this._loadedCount * (this._itemHeight + this.spacing);
}
onDestroy() {
if (EquipmentManager.instance) {
EquipmentManager.instance.node.off('data-updated', this.onDataUpdated, this);
}
this.scrollView.node.off('scrolling', this.onScrolling, this);
}
}