123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- import { Component, instantiate, Node, _decorator } from 'cc';
- import { BaseLayer } from '../../common/BaseLayer';
- const { ccclass, property } = _decorator;
- @ccclass('ItemLayer')
- export class ItemLayer extends Component {
- itemUI: Node;
- itemUIArr: any;
- itemArr: any;
- idItemUIObj: any = {};
- layerJS: BaseLayer;
- callback: Function;
- onLoad() {
- this.itemUI = this.node.children[0];
- this.itemUI.active = false;
- // 缓存还未被使用的itemUI
- this.itemUIArr = [this.itemUI];
- // 存储每个itemUI的数据
- this.itemArr = [];
- this.idItemUIObj = {};
- }
- hideAllItems() {
- let chs = this.node.children;
- this.itemUIArr = [];
- this.idItemUIObj = {};
- for (let i in chs) {
- let node = chs[i];
- node.active = false;
- this.itemUIArr.push(node);
- }
- }
- initUI(layerJS: BaseLayer, arr: any, callback: Function) {
- this.layerJS = layerJS;
- this.callback = callback;
- // 先全部回收隐藏
- this.hideAllItems();
- this.itemArr = [];
- this.idItemUIObj = {};
- for (let i in arr) {
- let item = arr[i];
- this.addItem(item);
- }
- }
- addItem(item: any, cb?: Function) {
- let itemUI = this.itemUIArr.shift();
- if (!itemUI) {
- itemUI = instantiate(this.itemUI);
- itemUI.parent = this.node;
- }
- itemUI.active = true;
- itemUI.item = item;
- let index = this.itemArr.length;
- itemUI.index = index;
- if (typeof (item) == "object" && item && item.id != undefined) {
- this.idItemUIObj[item.id] = itemUI;
- }
- if (cb) {
- cb(itemUI, item, index);
- } else if (this.callback) {
- this.callback(itemUI, item, index);
- }
- if (this.layerJS && this.layerJS.addButtonListener) {
- this.layerJS.addButtonListener(itemUI);
- }
- this.itemArr.push(item);
- return itemUI;
- }
- getItemUIById(id: number) {
- let itemUI = this.idItemUIObj[id];
- return itemUI;
- }
- loadAndRefreshItemUIByItem(item: any, cb?: Function) {
- if (!item || item.id == undefined) {
- return;
- }
- let itemUI = this.getItemUIById(item.id);
- if (!itemUI) {
- this.addItem(item, cb);
- return;
- }
- this.refreshItemUIByItem(item);
- }
- refreshItemUIByItem(item: any) {
- if (!item || item.id == undefined) {
- return;
- }
- let itemUI = this.getItemUIById(item.id);
- if (!itemUI) {
- return;
- }
- itemUI.item = item;
- if (this.callback) {
- this.callback(itemUI, item, itemUI.index);
- }
- }
- refreshItemUIById(id: number) {
- let itemUI = this.getItemUIById(id);
- if (!itemUI) {
- return;
- }
- this.refreshItemUIByItem(itemUI.item);
- }
- removeItemByIndex(index) {
- if (index < 0 || index >= this.itemArr.length) {
- return;
- }
- let chs = this.node.children;
- let itemUI = null;
- for (let i in chs) {
- let tmpItemUI = chs[i];
- if (!tmpItemUI.active || tmpItemUI["index"] != index) {
- continue;
- }
- itemUI = tmpItemUI;
- break;
- }
- this.removeItemUIByItemUI(itemUI);
- }
- removeItemUIById(id: number) {
- if (id == undefined) {
- return;
- }
- let itemUI = this.idItemUIObj[id];
- this.removeItemUIByItemUI(itemUI);
- }
- removeItemUIByItemUI(itemUI: Node) {
- if (!itemUI) {
- return;
- }
- itemUI.active = false;
- if (this.itemUIArr.indexOf(itemUI) == -1) {
- itemUI["index"] = -1;
- this.itemUIArr.push(itemUI);
- }
- let item = itemUI["item"];
- let index = this.itemArr.indexOf(item);
- if (index != -1) {
- this.itemArr.splice(index, 1);
- }
- if (typeof (item) == "object" && item && item.id) {
- delete this.idItemUIObj[item.id];
- }
- }
- forShowItemUI(cb: Function) {
- let chs = this.node.children;
- for (let i in chs) {
- let node = chs[i];
- if (!node.active) {
- continue;
- }
- if (cb) {
- cb(node, node["item"]);
- }
- }
- }
- refreshUI() {
- if (!this.callback) {
- return;
- }
- let chs = this.node.children;
- for (let i in chs) {
- let node = chs[i];
- if (!node.active) {
- continue;
- }
- this.callback(node, node["item"], node["index"]);
- }
- }
- }
|