08ba4751e7c5800eec3c9b3663921ed7b92e974b.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Node, Component, instantiate, v3, v2, size, Tween, tween, UITransform, sys, UIRenderer, RenderAlternative, ShowNode, ItemCell, LoopList, _crd;
  4. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  5. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  6. /**
  7. * 获取节点的边缘
  8. * @param cell
  9. * @param needArr 顺序是上 下 左 右
  10. */
  11. function getNodeBorder(cell, needArr) {
  12. var borderData = {};
  13. var wpos = cell.node.worldPosition.clone(); //显示区域上边缘
  14. if (needArr[0]) {
  15. borderData.topSide = wpos.y + cell.uiTrans.height * (1 - cell.uiTrans.anchorY);
  16. } //显示区域下边缘
  17. if (needArr[1]) {
  18. borderData.bottomSide = wpos.y - cell.uiTrans.height * cell.uiTrans.anchorY;
  19. } //显示区域左边缘
  20. if (needArr[2]) {
  21. borderData.leftSide = wpos.x - cell.uiTrans.width * cell.uiTrans.anchorX;
  22. } //显示区域右边缘
  23. if (needArr[3]) {
  24. borderData.rightSide = wpos.x + cell.uiTrans.width * (1 - cell.uiTrans.anchorX);
  25. }
  26. borderData.size = size(cell.uiTrans.width, cell.uiTrans.height);
  27. return borderData;
  28. }
  29. function _reportPossibleCrUseOfRenderAlternative(extras) {
  30. _reporterNs.report("RenderAlternative", "./RenderAlternative", _context.meta, extras);
  31. }
  32. _export({
  33. ShowNode: void 0,
  34. ItemCell: void 0,
  35. default: void 0
  36. });
  37. return {
  38. setters: [function (_unresolved_) {
  39. _reporterNs = _unresolved_;
  40. }, function (_cc) {
  41. _cclegacy = _cc.cclegacy;
  42. __checkObsolete__ = _cc.__checkObsolete__;
  43. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  44. Node = _cc.Node;
  45. Component = _cc.Component;
  46. instantiate = _cc.instantiate;
  47. v3 = _cc.v3;
  48. v2 = _cc.v2;
  49. size = _cc.size;
  50. Tween = _cc.Tween;
  51. tween = _cc.tween;
  52. UITransform = _cc.UITransform;
  53. sys = _cc.sys;
  54. UIRenderer = _cc.UIRenderer;
  55. }, function (_unresolved_2) {
  56. RenderAlternative = _unresolved_2.default;
  57. }],
  58. execute: function () {
  59. _crd = true;
  60. _cclegacy._RF.push({}, "eb0f0XLY1pHG4T0p8KqFdV/", "LoopList", undefined);
  61. __checkObsolete__(['Node', 'Component', 'Vec2', 'Size', 'Prefab', 'instantiate', 'v3', 'ScrollView', 'v2', 'size', 'Tween', 'tween', 'UITransform', 'Vec3', 'sys', 'UIRenderer']);
  62. /**边缘 */
  63. _export("ShowNode", ShowNode = class ShowNode extends Component {
  64. constructor() {
  65. super(...arguments);
  66. this.hasInit = false;
  67. /**渲染分层开启的挂载节点 */
  68. this.renderLayer = void 0;
  69. /**上级的cell */
  70. this.cell = void 0;
  71. /**数据 */
  72. this.data = void 0;
  73. }
  74. /**初始化 */
  75. init(cell, data, renderLayer) {
  76. this.cell = cell;
  77. this.data = data;
  78. if (this.renderLayer != renderLayer) {
  79. this.renderLayer = renderLayer;
  80. renderLayer && this.initRenders();
  81. }
  82. this.initShow();
  83. }
  84. /**初始化操作 */
  85. initShow() {}
  86. /**被回收,应该做一些释放的操作 */
  87. onRecircle() {}
  88. /**
  89. * 在特定时间内进行大小变化
  90. * @param targetSize
  91. * @param time
  92. */
  93. resize(targetSize, time) {
  94. if (time === void 0) {
  95. time = 0.3;
  96. }
  97. this.cell.resize(targetSize, time);
  98. }
  99. /**
  100. * 处理分层代理渲染
  101. */
  102. initRenders() {
  103. if (!this.hasInit && this.renderLayer) {
  104. var propMap = new WeakMap();
  105. var keys = Object.keys(this);
  106. for (var i = 0, len = keys.length; i < len; ++i) {
  107. var value = this[keys[i]];
  108. var alternative = null;
  109. if (value instanceof Node) {
  110. alternative = value.getComponent(_crd && RenderAlternative === void 0 ? (_reportPossibleCrUseOfRenderAlternative({
  111. error: Error()
  112. }), RenderAlternative) : RenderAlternative);
  113. if (alternative) {
  114. alternative.init(this.renderLayer);
  115. }
  116. } else if (value instanceof UIRenderer) {
  117. alternative = value.getComponent(_crd && RenderAlternative === void 0 ? (_reportPossibleCrUseOfRenderAlternative({
  118. error: Error()
  119. }), RenderAlternative) : RenderAlternative);
  120. if (alternative) {
  121. this[keys[i]] = alternative.init(this.renderLayer);
  122. }
  123. }
  124. alternative && propMap.set(alternative, true);
  125. }
  126. this.dealChilrenAlternative(this.node, propMap, 0, 0);
  127. this.hasInit = true;
  128. }
  129. }
  130. /**
  131. * 处理子节点代理
  132. * @param node
  133. * @param propMap 记录脚本已经处理过的属性代理
  134. * @param parentSindex 父节点的所在的顺序
  135. * @param depth 深度
  136. */
  137. dealChilrenAlternative(node, propMap, parentSindex, depth) {
  138. for (var i = 0, len = node.children.length; i < len; i++) {
  139. var _children$getComponen;
  140. var children = node.children[i];
  141. var alternative = (_children$getComponen = children.getComponent(UIRenderer)) == null ? void 0 : _children$getComponen.getComponent(_crd && RenderAlternative === void 0 ? (_reportPossibleCrUseOfRenderAlternative({
  142. error: Error()
  143. }), RenderAlternative) : RenderAlternative);
  144. var sIndex = parentSindex + i * Math.pow(0.1, depth);
  145. if (alternative) {
  146. if (!propMap.has(alternative) || !propMap.get(alternative)) {
  147. alternative.init(this.renderLayer);
  148. }
  149. alternative.sIndex = sIndex;
  150. }
  151. this.dealChilrenAlternative(children, propMap, sIndex, depth + 1);
  152. }
  153. }
  154. });
  155. _export("ItemCell", ItemCell = class ItemCell {
  156. /**
  157. * 构造函数
  158. * @param node
  159. * @param looplist
  160. */
  161. constructor(node, looplist) {
  162. /**持有cell的list */
  163. this.looplist = null;
  164. /**大小处理 */
  165. this.uiTrans = null;
  166. /**位置节点 */
  167. this.node = null;
  168. /**显示节点 */
  169. this.showNode = null;
  170. /**数据下标 */
  171. this.dataIndex = null;
  172. /**cell下标 */
  173. this.cellIndex = null;
  174. /**尺寸 */
  175. this.size = null;
  176. /**数据 */
  177. this.data = null;
  178. this.node = node;
  179. this.looplist = looplist;
  180. }
  181. /**
  182. * 初始化
  183. * @param node 节点
  184. * @param data 数据
  185. * @param size 大小
  186. * @param dataIndex 数据下标
  187. * @param cellIndex 节点下标
  188. * @param looplist 挂载的列表
  189. */
  190. init(data, size, dataIndex, cellIndex) {
  191. Tween.stopAllByTarget(this.node);
  192. this.cellIndex = cellIndex;
  193. this.dataIndex = dataIndex;
  194. !this.uiTrans && (this.uiTrans = this.node.getComponent(UITransform));
  195. this.uiTrans.width = size.width;
  196. this.uiTrans.height = size.height;
  197. this.data = data;
  198. this.size = size;
  199. }
  200. /**
  201. * 重置大小
  202. * @param newSize
  203. * @param time
  204. */
  205. resize(newSize, time) {
  206. var nodeSides = getNodeBorder(this, [true, true, true, true]);
  207. var changeSize = size(newSize.width - this.size.width, newSize.height - this.size.height);
  208. this.size = newSize;
  209. this.looplist.dealChangeSize(this, nodeSides, changeSize, time).then(() => {
  210. this.uiTrans.width = newSize.width;
  211. this.uiTrans.height = newSize.height;
  212. this.looplist.updateNodes();
  213. });
  214. }
  215. /**
  216. *
  217. * 更新节点显示
  218. * @param isShow 是否显示
  219. * @param prefab 需要显示的节点的预制件(也可以是节点,或者预制件url,生成方式自由改动)
  220. * @param cacheNode 对象池中的显示节点
  221. * @param renderLayer 分层节点
  222. */
  223. updateShow(isShow, prefab, cacheNode, renderLayer) {
  224. if (isShow) {
  225. //当前没有显示节点
  226. if (!this.showNode) {
  227. //如果是临时对象池还有节点则重用
  228. if (cacheNode) {
  229. this.showNode = cacheNode;
  230. } else {
  231. //否则使用传入的预制件进行生成
  232. this.showNode = instantiate(prefab).getComponent(ShowNode);
  233. }
  234. this.showNode.node.setParent(this.node);
  235. this.showNode.node.position = v3();
  236. this.updateShowNode(renderLayer);
  237. }
  238. }
  239. }
  240. /**
  241. * 更新showNode节点显示
  242. * @param renderLayer
  243. */
  244. updateShowNode(renderLayer) {
  245. //当前有显示节点
  246. if (this.showNode) {
  247. this.showNode.init(this, this.data, renderLayer);
  248. }
  249. }
  250. /**是否有显示节点 */
  251. get hasNoNode() {
  252. return !this.showNode;
  253. }
  254. /**回收显示节点 */
  255. recircle() {
  256. if (this.showNode) {
  257. var node = this.showNode;
  258. this.showNode.cell = null;
  259. this.showNode.onRecircle();
  260. this.showNode.node.removeFromParent();
  261. this.showNode = null;
  262. return node;
  263. }
  264. }
  265. destroy() {
  266. this.node.destroy();
  267. this.node = null;
  268. this.uiTrans = null;
  269. this.looplist = null;
  270. }
  271. /**
  272. * 失去显示节点
  273. * @returns
  274. */
  275. lostShowNode() {
  276. if (this.showNode) {
  277. var node = this.showNode;
  278. this.showNode.cell = null;
  279. this.showNode.node.removeFromParent();
  280. this.showNode = null;
  281. return node;
  282. }
  283. }
  284. /**
  285. * 获取显示节点
  286. * @param showNode
  287. */
  288. getShowNode(showNode) {
  289. this.showNode = showNode;
  290. showNode.node.setParent(this.node);
  291. this.showNode.cell = this;
  292. }
  293. });
  294. /**
  295. * 目前列表注意事项
  296. * 1.开启cellMaxNum后不能一行多列或者一列多行,只能单行或者单列
  297. */
  298. _export("default", LoopList = class LoopList {
  299. constructor() {
  300. /**分层节点(只有在showNode中挂载了RenderAlternative才能起作用) */
  301. this.renderLayer = null;
  302. /**滚动组件 */
  303. this.scrollView = null;
  304. /**cell节点最大数量 */
  305. this.cellMaxNum = null;
  306. /**当前起点 */
  307. this.curStartIndex = null;
  308. /**容器的ui控件 */
  309. this.contentUiTrans = null;
  310. /**数据 */
  311. this.datas = [];
  312. /**item节点们 */
  313. this.itemCells = [];
  314. /**预制件 */
  315. this.prefab = null;
  316. /**显示节点的池子 */
  317. this.showNodePool = [];
  318. /**边缘数据 */
  319. this.viewSides = {};
  320. /**上一帧滚动容器的坐标 */
  321. this.lastPos = null;
  322. /**刷新显示的间隔次数 */
  323. this.updateInv = 0;
  324. /**循环切换的最后一次时间 */
  325. this.lastChange = 0;
  326. /**通用尺寸 */
  327. this.size = null;
  328. /**通用锚点 */
  329. this.anchor = null;
  330. }
  331. static ins(listName) {
  332. if (!this.insMap.has(listName)) {
  333. this.insMap.set(listName, new LoopList());
  334. }
  335. return this.insMap.get(listName);
  336. }
  337. /**
  338. * 更新边界
  339. */
  340. updateBorder() {
  341. this.contentUiTrans = this.scrollView.content.getComponent(UITransform);
  342. var view = this.scrollView.view;
  343. view.anchorX = this.contentUiTrans.anchorX;
  344. view.anchorY = this.contentUiTrans.anchorY; //修改锚点之后下一帧世界坐标才会更新,没办法只能等一帧了
  345. return new Promise((resolve, reject) => {
  346. requestAnimationFrame(() => {
  347. var wpos = view.node.worldPosition.clone(); //显示区域左边缘
  348. var leftSide = wpos.x - view.width * view.anchorX; //显示区域右边缘
  349. var rightSide = wpos.x + view.width * (1 - view.anchorX); //显示区域下边缘
  350. var bottomSide = wpos.y - view.height * view.anchorY; //显示区域上边缘
  351. var topSide = wpos.y + view.height * (1 - view.anchorY);
  352. this.viewSides = {
  353. leftSide,
  354. rightSide,
  355. bottomSide,
  356. topSide,
  357. size: size(view.width, view.height)
  358. };
  359. this.contentUiTrans.node.setPosition(v3(0, 0));
  360. resolve();
  361. });
  362. });
  363. }
  364. /**
  365. * 初始化滚动列表*
  366. * @param scrollView
  367. * @param renderLayer 渲染分层的父节点,如果传入则可以配合showNode上的renderAlternative来进行渲染分层
  368. * @param cellMaxNum cell节点的最大数量,设置成null则不启用,启用的话,要设置成(可视窗刚好能显示的最大数量*2)+2,假设最多能看到10个节点,那么设置成22
  369. */
  370. init(scrollView, renderLayer, cellMaxNum) {
  371. var _this = this;
  372. return _asyncToGenerator(function* () {
  373. cellMaxNum && (_this.cellMaxNum = cellMaxNum);
  374. renderLayer && (_this.renderLayer = renderLayer);
  375. _this.scrollView = scrollView;
  376. yield _this.updateBorder();
  377. _this.scrollView.node.on("scrolling", _this.scrollingListener, _this);
  378. })();
  379. }
  380. /**滚动监听 */
  381. scrollingListener() {
  382. this.updateInv--;
  383. if (this.updateInv <= 0) {
  384. this.updateInv = 2;
  385. var showCells = this.updateNodes();
  386. if (!this.cellMaxNum) {
  387. return;
  388. }
  389. if (!this.lastPos) {
  390. this.lastPos = this.scrollView.content.position.clone();
  391. } else {
  392. var offset = this.scrollView.content.position.clone().subtract(this.lastPos);
  393. this.lastPos = this.scrollView.content.position.clone();
  394. var now = sys.now();
  395. if (!this.lastChange || now - this.lastChange > 1) {
  396. this.lastChange = now;
  397. this.dealLoopList(offset, showCells);
  398. }
  399. }
  400. }
  401. }
  402. /**
  403. * 初始化
  404. * @param data 数据组
  405. * @param prefab 预制件
  406. * @param size 通用尺寸
  407. * @param anchor cell的锚点
  408. */
  409. initData(data, prefab, size, anchor) {
  410. if (anchor === void 0) {
  411. anchor = v2(0.5, 0.5);
  412. }
  413. this.datas = data;
  414. this.size = size;
  415. this.anchor = anchor;
  416. this.prefab = prefab;
  417. var len = this.cellMaxNum ? this.cellMaxNum : data.length;
  418. for (var i = 0; i < len; ++i) {
  419. var _data = this.datas[this.curStartIndex + i];
  420. var cellSize = _data.size ? _data.size : size;
  421. var itemCell = this.itemCells[i];
  422. if (!itemCell) {
  423. var node = new Node('LoopListCell');
  424. var uiTrans = node.addComponent(UITransform);
  425. itemCell = this.itemCells[i] = new ItemCell(node, this);
  426. uiTrans.setAnchorPoint(anchor);
  427. node.setParent(this.scrollView.content);
  428. }
  429. itemCell.init(_data, cellSize, this.curStartIndex + i, i);
  430. }
  431. for (var _i = data.length, _len = this.itemCells.length; _i < _len; ++_i) {
  432. this.recircleShowNode(this.itemCells[_i].recircle());
  433. this.itemCells[_i].destroy();
  434. this.itemCells[_i] = null;
  435. }
  436. requestAnimationFrame(() => {
  437. this.updateNodes();
  438. });
  439. }
  440. /**
  441. * 压入新的数据
  442. * @param datas
  443. */
  444. pushDatas(datas) {
  445. var length = this.datas.length;
  446. for (var i = 0, len = datas.length; i < len; ++i) {
  447. var data = datas[i];
  448. this.datas.push(data);
  449. var index = length + i;
  450. this.addNodeFromIndex(index);
  451. }
  452. }
  453. /**
  454. * 插入新的数据
  455. * @param data
  456. * @param insertIndex
  457. */
  458. insertData(data, insertIndex) {
  459. if (this.datas.length >= insertIndex) {
  460. this.datas.splice(insertIndex, 0, data);
  461. this.addNodeFromIndex(insertIndex);
  462. } else {
  463. this.pushDatas([data]);
  464. }
  465. }
  466. /**
  467. * 移除对应数据
  468. * @param removeIndex
  469. */
  470. removeData(removeIndex) {
  471. if (this.datas.length >= removeIndex) {
  472. this.datas.splice(removeIndex, 1);
  473. if (this.cellMaxNum) {
  474. var cellIndex = removeIndex - this.curStartIndex; //在使用范围内
  475. if (cellIndex >= 0 && cellIndex < this.cellMaxNum) {
  476. var itemCell = this.itemCells[cellIndex];
  477. var nodeSides = getNodeBorder(itemCell, [true, true, true, true]);
  478. var changeSize = size(-itemCell.size.width, -itemCell.size.height);
  479. itemCell.size = size(0, 0);
  480. this.dealChangeSize(itemCell, nodeSides, changeSize).then(() => {
  481. //更新数据
  482. for (var i = cellIndex; i < this.cellMaxNum; i++) {
  483. var curDataIndex = this.curStartIndex + i;
  484. var curData = this.datas[curDataIndex];
  485. var curCellSize = curData.size ? curData.size : this.size;
  486. this.itemCells[i].init(curData, curCellSize, curDataIndex, i);
  487. this.itemCells[i].updateShowNode(this.renderLayer);
  488. }
  489. });
  490. }
  491. } else {
  492. var _itemCell = this.itemCells.splice(removeIndex, 1)[0];
  493. var _nodeSides = getNodeBorder(_itemCell, [true, true, true, true]);
  494. var _changeSize = size(-_itemCell.size.width, -_itemCell.size.height);
  495. _itemCell.size = size(0, 0);
  496. this.dealChangeSize(_itemCell, _nodeSides, _changeSize).then(() => {
  497. this.recircleShowNode(_itemCell.recircle());
  498. _itemCell.node.destroy();
  499. });
  500. }
  501. }
  502. }
  503. /**
  504. * 从某个数据开始更新
  505. * @param dataIndex
  506. */
  507. addNodeFromIndex(dataIndex) {
  508. var _this2 = this;
  509. return _asyncToGenerator(function* () {
  510. var data = _this2.datas[dataIndex];
  511. var cellSize = data.size ? data.size : _this2.size; //是否开启cell循环
  512. if (_this2.cellMaxNum) {
  513. var cellIndex = dataIndex - _this2.curStartIndex; //在使用范围内
  514. if (cellIndex >= 0 && cellIndex < _this2.cellMaxNum) {
  515. //更新数据
  516. for (var i = cellIndex; i < _this2.cellMaxNum; i++) {
  517. var curDataIndex = _this2.curStartIndex + i;
  518. var curData = _this2.datas[curDataIndex];
  519. var curCellSize = curData.size ? curData.size : _this2.size;
  520. _this2.itemCells[i].init(curData, curCellSize, curDataIndex, i);
  521. _this2.itemCells[i].updateShowNode(_this2.renderLayer);
  522. } //获取当前的位置
  523. var nodeSides = null;
  524. var itemCell = _this2.itemCells[cellIndex];
  525. if (itemCell) {
  526. nodeSides = getNodeBorder(itemCell, [true, true, true, true]);
  527. itemCell.uiTrans.width = 0;
  528. itemCell.uiTrans.height = 0;
  529. _this2.dealChangeSize(itemCell, nodeSides, cellSize).then(() => {
  530. itemCell.uiTrans.width = cellSize.width;
  531. itemCell.uiTrans.height = cellSize.height;
  532. });
  533. }
  534. }
  535. } else {
  536. //获取当前的位置
  537. var _nodeSides2 = null;
  538. if (_this2.itemCells[dataIndex]) {
  539. _nodeSides2 = getNodeBorder(_this2.itemCells[dataIndex], [true, true, true, true]);
  540. }
  541. var node = new Node('LoopListCell');
  542. var uiTrans = node.addComponent(UITransform);
  543. uiTrans.setAnchorPoint(_this2.anchor);
  544. node.setParent(_this2.scrollView.content);
  545. node.setSiblingIndex(dataIndex);
  546. var _itemCell2 = new ItemCell(node, _this2);
  547. _this2.itemCells.splice(dataIndex, 0, _itemCell2);
  548. _itemCell2.init(data, cellSize, dataIndex, dataIndex);
  549. if (!_nodeSides2) {
  550. yield new Promise((resolve, reject) => {
  551. requestAnimationFrame(() => {
  552. _nodeSides2 = getNodeBorder(_itemCell2, [true, true, true, true]);
  553. resolve();
  554. });
  555. });
  556. }
  557. if (_nodeSides2) {
  558. _itemCell2.uiTrans.width = 0;
  559. _itemCell2.uiTrans.height = 0;
  560. _this2.dealChangeSize(_itemCell2, _nodeSides2, cellSize).then(() => {
  561. _itemCell2.uiTrans.width = cellSize.width;
  562. _itemCell2.uiTrans.height = cellSize.height;
  563. });
  564. }
  565. _this2.dealItemShow(_itemCell2);
  566. }
  567. })();
  568. }
  569. /**
  570. * 处理列表的循环利用
  571. * @param offset
  572. * @param dataIndexs
  573. */
  574. dealLoopList(offset, showCells) {
  575. var startCell = showCells[0];
  576. var endCell = showCells[showCells.length - 1];
  577. if (this.scrollView.vertical) {
  578. /**内容上移 */
  579. if (offset.y > 0) {
  580. //已经到达最后倒数第二个节点并且还有数据
  581. if (this.scrollView.content.position.y >= -this.itemCells[this.itemCells.length - 2].node.position.y - this.viewSides.size.height && endCell.dataIndex < this.datas.length - 2) {
  582. //显示开始数据下标(这里-1是想要顶部留一个itemcell做缓冲)
  583. var showStart = startCell.dataIndex - 1; //剩余可展示的数据量
  584. var leave = this.datas.length - showStart; //计算应该从那个位置开始重新赋值,因为如果直接移动到最上方,数据量不够覆盖所有的cell
  585. var cellStartIndex = Math.max(0, this.itemCells.length - leave); //计算第一个cell的数据下标
  586. this.curStartIndex = showStart - cellStartIndex;
  587. var cellTop = getNodeBorder(this.itemCells[startCell.cellIndex], [true]).topSide;
  588. var contentHeight = 0;
  589. var targetY = 0;
  590. var jumpFirst = true;
  591. var showIndex = 0;
  592. for (var i = 0, len = this.itemCells.length; i < len; ++i) {
  593. if (i >= cellStartIndex) {
  594. //将当前已经显示的showNode节点直接重新设置到移动后的节点上
  595. if (showIndex < showCells.length && !jumpFirst) {
  596. if (showIndex == 0) {
  597. targetY = contentHeight;
  598. }
  599. var showNode = showCells[showIndex].lostShowNode();
  600. showIndex++;
  601. this.itemCells[i].getShowNode(showNode);
  602. }
  603. jumpFirst = false;
  604. }
  605. var dataIndex = this.curStartIndex + i;
  606. var data = this.datas[dataIndex];
  607. var cellSize = data.size ? data.size : this.size;
  608. contentHeight += cellSize.height;
  609. this.itemCells[i].init(data, cellSize, dataIndex, i);
  610. } //将content的位置移动到最上方并对齐移动前的状态
  611. var posY = targetY + (cellTop - this.viewSides.topSide);
  612. this.scrollView.content.setPosition(v3(this.scrollView.content.position.x, posY));
  613. this.scrollView.scrollToOffset(v2(0, posY + endCell.size.height), 1);
  614. this.lastPos = null;
  615. }
  616. } //内容下移
  617. else if (offset.y < 0) {
  618. //已经到达第二个节点并且还有数据
  619. if (this.scrollView.content.position.y < -this.itemCells[2].node.position.y && startCell.dataIndex > 2) {
  620. //展示的最后一个数据下表,+1是为了在底部预留一部分空间
  621. var showEndIndex = endCell.dataIndex + 1; //计算应该从那个位置开始重新赋值,因为如果直接移动到最下方,数据量不够覆盖所有的cell
  622. var cellEndIndex = Math.min(this.itemCells.length - 2, showEndIndex - 1); //计算第一个cell的数据下标
  623. this.curStartIndex = Math.max(showEndIndex - (this.itemCells.length - 1), 0); //将content的位置移动到最下方并对齐移动前的状态
  624. var _cellTop = getNodeBorder(this.itemCells[Math.max(startCell.cellIndex - 1, 0)], [true]).topSide;
  625. var _contentHeight = 0;
  626. var _showIndex = showCells.length - 1;
  627. for (var _i2 = this.itemCells.length - 1; _i2 >= 0; --_i2) {
  628. if (_i2 <= cellEndIndex) {
  629. //将当前已经显示的showNode节点直接重新设置到移动后的节点上
  630. if (_showIndex >= 0) {
  631. var _showNode = showCells[_showIndex].lostShowNode();
  632. _showIndex--;
  633. this.itemCells[_i2].getShowNode(_showNode);
  634. } else {
  635. _showIndex--;
  636. }
  637. }
  638. var _dataIndex = this.curStartIndex + _i2;
  639. var _data2 = this.datas[_dataIndex];
  640. var _cellSize = _data2.size ? _data2.size : this.size;
  641. if (_showIndex < -1) {
  642. _contentHeight += _cellSize.height;
  643. }
  644. this.itemCells[_i2].init(_data2, _cellSize, _dataIndex, _i2);
  645. } //将content的位置移动到最下方并对齐移动前的状态
  646. var _posY = _contentHeight + (_cellTop - this.viewSides.topSide);
  647. this.scrollView.content.setPosition(v3(this.scrollView.content.position.x, _posY));
  648. this.scrollView.scrollToOffset(v2(0, _posY - startCell.size.height), 1);
  649. this.lastPos = null;
  650. }
  651. }
  652. } else if (this.scrollView.horizontal) {
  653. if (offset.x < 0) {
  654. //已经到达最后倒数第二个节点并且还有数据
  655. if (this.scrollView.content.position.x <= -(this.itemCells[this.itemCells.length - 2].node.position.x - this.viewSides.size.width) && endCell.dataIndex < this.datas.length - 2) {
  656. //显示开始数据下标(这里-1是想要左侧留一个itemcell做缓冲)
  657. var _showStart = startCell.dataIndex - 1; //剩余可展示的数据量
  658. var _leave = this.datas.length - _showStart; //计算应该从那个位置开始重新赋值,因为如果直接移动到最左侧,数据量不够覆盖所有的cell
  659. var _cellStartIndex = Math.max(0, this.itemCells.length - _leave); //计算第一个cell的数据下标
  660. this.curStartIndex = _showStart - _cellStartIndex;
  661. var cellLeft = getNodeBorder(this.itemCells[startCell.cellIndex], [false, false, true]).leftSide;
  662. var contentWidth = 0;
  663. var targetX = 0;
  664. var _jumpFirst = true;
  665. var _showIndex2 = 0;
  666. for (var _i3 = 0, _len2 = this.itemCells.length; _i3 < _len2; ++_i3) {
  667. if (_i3 >= _cellStartIndex) {
  668. //将当前已经显示的showNode节点直接重新设置到移动后的节点上
  669. if (_showIndex2 < showCells.length && !_jumpFirst) {
  670. if (_showIndex2 == 0) {
  671. targetX = contentWidth;
  672. }
  673. var _showNode2 = showCells[_showIndex2].lostShowNode();
  674. _showIndex2++;
  675. this.itemCells[_i3].getShowNode(_showNode2);
  676. }
  677. _jumpFirst = false;
  678. }
  679. var _dataIndex2 = this.curStartIndex + _i3;
  680. var _data3 = this.datas[_dataIndex2];
  681. var _cellSize2 = _data3.size ? _data3.size : this.size;
  682. contentWidth += _cellSize2.width;
  683. this.itemCells[_i3].init(_data3, _cellSize2, _dataIndex2, _i3);
  684. } //将content的位置移动到最左侧并对齐移动前的状态
  685. var posX = cellLeft - this.viewSides.leftSide - targetX;
  686. this.scrollView.content.setPosition(v3(posX, this.scrollView.content.position.y));
  687. this.scrollView.scrollToOffset(v2(-(posX - endCell.size.width), 0), 1);
  688. this.lastPos = null;
  689. }
  690. } else if (offset.x > 0) {
  691. //已经到达第二个节点并且还有数据
  692. if (this.scrollView.content.position.x > -this.itemCells[2].node.position.x && startCell.dataIndex > 2) {
  693. //展示的最后一个数据下表,+1是为了在右侧预留一部分空间
  694. var _showEndIndex = endCell.dataIndex + 1; //计算应该从那个位置开始重新赋值,因为如果直接移动到最右侧,数据量不够覆盖所有的cell
  695. var _cellEndIndex = Math.min(this.itemCells.length - 2, _showEndIndex - 1); //计算第一个cell的数据下标
  696. this.curStartIndex = Math.max(_showEndIndex - (this.itemCells.length - 1), 0); //将content的位置移动到最右侧并对齐移动前的状态
  697. var _cellLeft = getNodeBorder(this.itemCells[Math.max(startCell.cellIndex - 1, 0)], [false, false, true]).leftSide;
  698. var _contentWidth = 0;
  699. var _showIndex3 = showCells.length - 1;
  700. for (var _i4 = this.itemCells.length - 1; _i4 >= 0; --_i4) {
  701. if (_i4 <= _cellEndIndex) {
  702. //将当前已经显示的showNode节点直接重新设置到移动后的节点上
  703. if (_showIndex3 >= 0) {
  704. var _showNode3 = showCells[_showIndex3].lostShowNode();
  705. _showIndex3--;
  706. this.itemCells[_i4].getShowNode(_showNode3);
  707. } else {
  708. _showIndex3--;
  709. }
  710. }
  711. var _dataIndex3 = this.curStartIndex + _i4;
  712. var _data4 = this.datas[_dataIndex3];
  713. var _cellSize3 = _data4.size ? _data4.size : this.size;
  714. if (_showIndex3 < -1) {
  715. _contentWidth += _cellSize3.width;
  716. }
  717. this.itemCells[_i4].init(_data4, _cellSize3, _dataIndex3, _i4);
  718. } //将content的位置移动到最右侧并对齐移动前的状态
  719. var _posX = _cellLeft - this.viewSides.leftSide - _contentWidth;
  720. this.scrollView.content.setPosition(v3(_posX, this.scrollView.content.position.y));
  721. this.scrollView.scrollToOffset(v2(-(_posX + startCell.size.width), 0), 1);
  722. this.lastPos = null;
  723. }
  724. }
  725. }
  726. }
  727. /**更新节点的显示 */
  728. updateNodes() {
  729. var showCells = [];
  730. for (var i = 0, len = this.itemCells.length; i < len; ++i) {
  731. var itemCell = this.itemCells[i];
  732. var isShow = this.dealItemShow(itemCell);
  733. if (isShow) {
  734. showCells.push(itemCell);
  735. }
  736. }
  737. return showCells;
  738. }
  739. /**
  740. * 处理cell的显示隐藏
  741. * @param itemCell
  742. */
  743. dealItemShow(itemCell) {
  744. if (itemCell) {
  745. var isShow = this.checkInView(itemCell);
  746. if (isShow) {
  747. itemCell.hasNoNode && itemCell.updateShow(isShow, this.prefab, this.showNodePool.length > 0 ? this.showNodePool.pop() : null, this.renderLayer);
  748. } else {
  749. !itemCell.hasNoNode && this.recircleShowNode(itemCell.recircle());
  750. }
  751. return isShow;
  752. }
  753. }
  754. /**
  755. * 检查是否在屏幕内
  756. * @param node
  757. * @returns
  758. */
  759. checkInView(itemCell) {
  760. var nodeSides = getNodeBorder(itemCell, [true, true, true, true]); //节点右侧边缘在屏幕左侧外面或者节点左边缘在屏幕右侧外面 则不在显示区域
  761. var horizontalOutside = nodeSides.rightSide < this.viewSides.leftSide || nodeSides.leftSide > this.viewSides.rightSide;
  762. var verticalOutside = false;
  763. if (!horizontalOutside) {
  764. //节点上边缘在屏幕下方外面或者节点下边缘在屏幕上方外面 则不在显示区域
  765. verticalOutside = nodeSides.topSide < this.viewSides.bottomSide || nodeSides.bottomSide > this.viewSides.topSide;
  766. }
  767. return !horizontalOutside && !verticalOutside;
  768. }
  769. /**
  770. * 处理变化的大小
  771. * @param cell
  772. * @param nodeSides
  773. * @param changeSize
  774. */
  775. dealChangeSize(cell, nodeSides, changeSize, time) {
  776. var _this3 = this;
  777. return _asyncToGenerator(function* () {
  778. if (time === void 0) {
  779. time = 0.3;
  780. }
  781. var topToBottomFulfill = _this3.contentUiTrans.anchorY == 1 && nodeSides.bottomSide >= _this3.viewSides.topSide;
  782. var bottomToTopFulfill = _this3.contentUiTrans.anchorY == 0 && nodeSides.topSide <= _this3.viewSides.bottomSide;
  783. if (topToBottomFulfill || bottomToTopFulfill) {
  784. if (topToBottomFulfill) {
  785. _this3.scrollView.content.position.add(v3(0, changeSize.height));
  786. } else {
  787. _this3.scrollView.content.position.subtract(v3(0, changeSize.height));
  788. }
  789. }
  790. var leftToRightFulfill = _this3.contentUiTrans.anchorX == 0 && nodeSides.rightSide <= _this3.viewSides.leftSide;
  791. var RightToLeftFulfill = _this3.contentUiTrans.anchorX == 1 && nodeSides.leftSide >= _this3.viewSides.rightSide;
  792. if (leftToRightFulfill || RightToLeftFulfill) {
  793. if (RightToLeftFulfill) {
  794. _this3.scrollView.content.position.add(v3(changeSize.width, 0));
  795. } else {
  796. _this3.scrollView.content.position.subtract(v3(changeSize.width, 0));
  797. }
  798. }
  799. if (!(topToBottomFulfill || bottomToTopFulfill || leftToRightFulfill || RightToLeftFulfill)) {
  800. Tween.stopAllByTarget(cell.node);
  801. yield new Promise((resolve, reject) => {
  802. tween(cell.uiTrans).to(time, {
  803. width: cell.size.width,
  804. height: cell.size.height
  805. }).call(() => {
  806. resolve();
  807. }).start();
  808. });
  809. }
  810. _this3.updateNodes();
  811. return true;
  812. })();
  813. }
  814. /**
  815. * 回收显示节点
  816. * @param node
  817. */
  818. recircleShowNode(node) {
  819. node && this.showNodePool.push(node);
  820. }
  821. /**
  822. * 释放当前列表
  823. */
  824. freeList() {
  825. this.scrollView.node.off("scrolling", this.scrollingListener);
  826. for (var i = 0, len = this.itemCells.length; i < len; ++i) {
  827. this.recircleShowNode(this.itemCells[i].recircle());
  828. this.itemCells[i].destroy();
  829. }
  830. this.itemCells = [];
  831. for (var index = 0, _len3 = this.showNodePool.length; index < _len3; ++index) {
  832. this.showNodePool[index].destroy();
  833. }
  834. this.showNodePool = [];
  835. this.renderLayer = null;
  836. this.scrollView = null;
  837. this.prefab = null;
  838. }
  839. });
  840. LoopList.insMap = new Map();
  841. _cclegacy._RF.pop();
  842. _crd = false;
  843. }
  844. };
  845. });
  846. //# sourceMappingURL=08ba4751e7c5800eec3c9b3663921ed7b92e974b.js.map