08ba4751e7c5800eec3c9b3663921ed7b92e974b.js 35 KB

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