ItemLayer.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { Component, instantiate, Node, _decorator } from 'cc';
  2. import { BaseLayer } from '../../common/BaseLayer';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('ItemLayer')
  5. export class ItemLayer extends Component {
  6. itemUI: Node;
  7. itemUIArr: any;
  8. itemArr: any;
  9. idItemUIObj: any = {};
  10. layerJS: BaseLayer;
  11. callback: Function;
  12. onLoad() {
  13. this.itemUI = this.node.children[0];
  14. this.itemUI.active = false;
  15. // 缓存还未被使用的itemUI
  16. this.itemUIArr = [this.itemUI];
  17. // 存储每个itemUI的数据
  18. this.itemArr = [];
  19. this.idItemUIObj = {};
  20. }
  21. hideAllItems() {
  22. let chs = this.node.children;
  23. this.itemUIArr = [];
  24. this.idItemUIObj = {};
  25. for (let i in chs) {
  26. let node = chs[i];
  27. node.active = false;
  28. this.itemUIArr.push(node);
  29. }
  30. }
  31. initUI(layerJS: BaseLayer, arr: any, callback: Function) {
  32. this.layerJS = layerJS;
  33. this.callback = callback;
  34. // 先全部回收隐藏
  35. this.hideAllItems();
  36. this.itemArr = [];
  37. this.idItemUIObj = {};
  38. for (let i in arr) {
  39. let item = arr[i];
  40. this.addItem(item);
  41. }
  42. }
  43. addItem(item: any, cb?: Function) {
  44. let itemUI = this.itemUIArr.shift();
  45. if (!itemUI) {
  46. itemUI = instantiate(this.itemUI);
  47. itemUI.parent = this.node;
  48. }
  49. itemUI.active = true;
  50. itemUI.item = item;
  51. let index = this.itemArr.length;
  52. itemUI.index = index;
  53. if (typeof (item) == "object" && item && item.id != undefined) {
  54. this.idItemUIObj[item.id] = itemUI;
  55. }
  56. if (cb) {
  57. cb(itemUI, item, index);
  58. } else if (this.callback) {
  59. this.callback(itemUI, item, index);
  60. }
  61. if (this.layerJS && this.layerJS.addButtonListener) {
  62. this.layerJS.addButtonListener(itemUI);
  63. }
  64. this.itemArr.push(item);
  65. return itemUI;
  66. }
  67. getItemUIById(id: number) {
  68. let itemUI = this.idItemUIObj[id];
  69. return itemUI;
  70. }
  71. loadAndRefreshItemUIByItem(item: any, cb?: Function) {
  72. if (!item || item.id == undefined) {
  73. return;
  74. }
  75. let itemUI = this.getItemUIById(item.id);
  76. if (!itemUI) {
  77. this.addItem(item, cb);
  78. return;
  79. }
  80. this.refreshItemUIByItem(item);
  81. }
  82. refreshItemUIByItem(item: any) {
  83. if (!item || item.id == undefined) {
  84. return;
  85. }
  86. let itemUI = this.getItemUIById(item.id);
  87. if (!itemUI) {
  88. return;
  89. }
  90. itemUI.item = item;
  91. if (this.callback) {
  92. this.callback(itemUI, item, itemUI.index);
  93. }
  94. }
  95. refreshItemUIById(id: number) {
  96. let itemUI = this.getItemUIById(id);
  97. if (!itemUI) {
  98. return;
  99. }
  100. this.refreshItemUIByItem(itemUI.item);
  101. }
  102. removeItemByIndex(index) {
  103. if (index < 0 || index >= this.itemArr.length) {
  104. return;
  105. }
  106. let chs = this.node.children;
  107. let itemUI = null;
  108. for (let i in chs) {
  109. let tmpItemUI = chs[i];
  110. if (!tmpItemUI.active || tmpItemUI["index"] != index) {
  111. continue;
  112. }
  113. itemUI = tmpItemUI;
  114. break;
  115. }
  116. this.removeItemUIByItemUI(itemUI);
  117. }
  118. removeItemUIById(id: number) {
  119. if (id == undefined) {
  120. return;
  121. }
  122. let itemUI = this.idItemUIObj[id];
  123. this.removeItemUIByItemUI(itemUI);
  124. }
  125. removeItemUIByItemUI(itemUI: Node) {
  126. if (!itemUI) {
  127. return;
  128. }
  129. itemUI.active = false;
  130. if (this.itemUIArr.indexOf(itemUI) == -1) {
  131. itemUI["index"] = -1;
  132. this.itemUIArr.push(itemUI);
  133. }
  134. let item = itemUI["item"];
  135. let index = this.itemArr.indexOf(item);
  136. if (index != -1) {
  137. this.itemArr.splice(index, 1);
  138. }
  139. if (typeof (item) == "object" && item && item.id) {
  140. delete this.idItemUIObj[item.id];
  141. }
  142. }
  143. forShowItemUI(cb: Function) {
  144. let chs = this.node.children;
  145. for (let i in chs) {
  146. let node = chs[i];
  147. if (!node.active) {
  148. continue;
  149. }
  150. if (cb) {
  151. cb(node, node["item"]);
  152. }
  153. }
  154. }
  155. refreshUI() {
  156. if (!this.callback) {
  157. return;
  158. }
  159. let chs = this.node.children;
  160. for (let i in chs) {
  161. let node = chs[i];
  162. if (!node.active) {
  163. continue;
  164. }
  165. this.callback(node, node["item"], node["index"]);
  166. }
  167. }
  168. }