2347d589-b8b2-4049-804f-ff6c83dc7d5b.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. "use strict";
  2. cc._RF.push(module, '2347dWJuLJASYBP/2yD3H1b', 'SuperScrollView');
  3. // Script/view/SuperScrollview/SuperScrollView.ts
  4. "use strict";
  5. var __extends = (this && this.__extends) || (function () {
  6. var extendStatics = function (d, b) {
  7. extendStatics = Object.setPrototypeOf ||
  8. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  9. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  10. return extendStatics(d, b);
  11. };
  12. return function (d, b) {
  13. extendStatics(d, b);
  14. function __() { this.constructor = d; }
  15. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  16. };
  17. })();
  18. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  19. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  20. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  21. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  22. return c > 3 && r && Object.defineProperty(target, key, r), r;
  23. };
  24. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  25. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  26. return new (P || (P = Promise))(function (resolve, reject) {
  27. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  28. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  29. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  30. step((generator = generator.apply(thisArg, _arguments || [])).next());
  31. });
  32. };
  33. var __generator = (this && this.__generator) || function (thisArg, body) {
  34. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  35. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  36. function verb(n) { return function (v) { return step([n, v]); }; }
  37. function step(op) {
  38. if (f) throw new TypeError("Generator is already executing.");
  39. while (_) try {
  40. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  41. if (y = 0, t) op = [op[0] & 2, t.value];
  42. switch (op[0]) {
  43. case 0: case 1: t = op; break;
  44. case 4: _.label++; return { value: op[1], done: false };
  45. case 5: _.label++; y = op[1]; op = [0]; continue;
  46. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  47. default:
  48. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  49. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  50. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  51. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  52. if (t[2]) _.ops.pop();
  53. _.trys.pop(); continue;
  54. }
  55. op = body.call(thisArg, _);
  56. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  57. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  58. }
  59. };
  60. Object.defineProperty(exports, "__esModule", { value: true });
  61. var SuperListItem_1 = require("./SuperListItem");
  62. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  63. var SuperScrollView = /** @class */ (function (_super) {
  64. __extends(SuperScrollView, _super);
  65. function SuperScrollView() {
  66. var _this = _super !== null && _super.apply(this, arguments) || this;
  67. _this.pfItemTemplate = null;
  68. _this.itemSize = null;
  69. _this.itemNodePool = null;
  70. _this.duration = 1;
  71. // @property({
  72. // tooltip: '最多可以显示多少个Item',
  73. // })
  74. _this.visibleNum = 12;
  75. _this.curIndex = 0;
  76. _this.itemInfoList = [];
  77. _this.isFrameLoading = false;
  78. _this.isLoadingFinished = true;
  79. _this.cbAfterSetData = null;
  80. _this.lastTime = 0;
  81. return _this;
  82. // update (dt) {}
  83. }
  84. SuperScrollView.prototype.onLoad = function () {
  85. this.elastic = true;
  86. this.content.on(cc.Node.EventType.CHILD_ADDED, this.childAdded.bind(this));
  87. this.node.on("touchend", this.onTouchEnd, this);
  88. this.node.on("scroll-ended", this.onScrollEnded, this);
  89. this.node.on("scrolling", this.onScrolling, this);
  90. this.node.on("bounce-top", this.onBounceTop, this);
  91. this.node.on("bounce-bottom", this.onBounceBottom, this);
  92. this.node.on("bounce-left", this.onBounceLeft, this);
  93. this.node.on("bounce-right", this.onBounceRight, this);
  94. this.initNodePool();
  95. };
  96. SuperScrollView.prototype.onDestroy = function () {
  97. this.content.off(cc.Node.EventType.CHILD_ADDED, this.childAdded);
  98. this.node.off("touchend", this.onTouchEnd, this);
  99. this.node.off("scroll-ended-with-threshold", this.onScrollEnded, this);
  100. this.node.off("scrolling", this.onScrolling, this);
  101. this.node.off("bounce-top", this.onBounceTop, this);
  102. this.node.off("bounce-bottom", this.onBounceBottom, this);
  103. this.node.off("bounce-left", this.onBounceLeft, this);
  104. this.node.off("bounce-right", this.onBounceRight, this);
  105. };
  106. SuperScrollView.prototype.initNodePool = function () {
  107. var itemNode = cc.instantiate(this.pfItemTemplate);
  108. var contentSize = itemNode.getContentSize();
  109. this.itemSize = contentSize;
  110. var parentSize = this.content.parent.getContentSize();
  111. var layoutComp = this.content.getComponent(cc.Layout);
  112. var num = 0;
  113. if (layoutComp) {
  114. if (layoutComp.type === cc.Layout.Type.VERTICAL && this.vertical && !this.horizontal) {
  115. num = Math.ceil(parentSize.height / contentSize.height);
  116. }
  117. else if (layoutComp.type === cc.Layout.Type.HORIZONTAL && this.horizontal && this.vertical) {
  118. num = Math.ceil(parentSize.width / contentSize.width);
  119. }
  120. else if (layoutComp.type === cc.Layout.Type.GRID) {
  121. var rowEleCount = Math.floor((parentSize.width - layoutComp.paddingLeft - layoutComp.paddingRight + layoutComp.spacingX) / (contentSize.width + layoutComp.spacingX));
  122. var colEleCount = Math.floor((parentSize.height - layoutComp.paddingTop - layoutComp.paddingBottom + layoutComp.spacingY) / (contentSize.height + layoutComp.spacingY));
  123. num = rowEleCount * colEleCount;
  124. // console.log('最多显示个数:GRID', rowEleCount, colEleCount);
  125. }
  126. }
  127. this.visibleNum = Math.floor(num * 2) + 2;
  128. // console.log('最多显示个数', this.visibleNum);
  129. if (this.itemNodePool) {
  130. //清空
  131. this.itemNodePool.clear();
  132. }
  133. else {
  134. this.itemNodePool = new cc.NodePool();
  135. }
  136. //多放3个
  137. for (var i = 0; i < this.visibleNum + 3; i++) {
  138. var itemNode_1 = cc.instantiate(this.pfItemTemplate);
  139. this.itemNodePool.put(itemNode_1);
  140. }
  141. };
  142. //添加节点
  143. SuperScrollView.prototype.childAdded = function (itemNode) {
  144. this.curIndex++;
  145. itemNode.name = "item" + this.curIndex;
  146. };
  147. SuperScrollView.prototype.scrollToIndex = function (index, seconds) {
  148. if (seconds === void 0) { seconds = 0.2; }
  149. // cc.log("index:", index);
  150. var childCount = this.content.childrenCount;
  151. // cc.log("childCount:", childCount);
  152. if (index < 1) {
  153. // cc.log("index 过小");
  154. index = 1;
  155. }
  156. else if (index > childCount) {
  157. // cc.log("index 过大");
  158. index = childCount;
  159. }
  160. var item = this.content.getChildByName('item' + index);
  161. if (item) {
  162. var layoutComp = this.content.getComponent(cc.Layout);
  163. if (layoutComp) {
  164. if (layoutComp.type === cc.Layout.Type.VERTICAL && this.vertical && !this.horizontal) {
  165. this.scrollToPercentVertical((childCount - index) / childCount, seconds);
  166. }
  167. else if (layoutComp.type === cc.Layout.Type.HORIZONTAL && !this.vertical && this.horizontal) {
  168. this.scrollToPercentHorizontal((childCount - index) / childCount, seconds);
  169. }
  170. else if (layoutComp.type === cc.Layout.Type.GRID) {
  171. if (layoutComp.startAxis === cc.Layout.AxisDirection.HORIZONTAL) {
  172. var contentSize = this.content.getContentSize();
  173. var itemContentSize = item.getContentSize();
  174. // contentSize.width = layoutComp.paddingLeft + layoutComp.paddingRight + itemContentSize.width * n + layoutComp.spacingX *(n-1)
  175. var rowEleCount = Math.floor((contentSize.width - layoutComp.paddingLeft - layoutComp.paddingRight + layoutComp.spacingX) / (itemContentSize.width + layoutComp.spacingX));
  176. // cc.log("每行多少个:", rowEleCount);
  177. var hang = Math.ceil(index / rowEleCount);
  178. var totalHang = Math.ceil(childCount / rowEleCount);
  179. this.scrollToPercentVertical((totalHang - hang) / totalHang, seconds);
  180. }
  181. else {
  182. var contentSize = this.content.getContentSize();
  183. var itemContentSize = item.getContentSize();
  184. var colEleCount = Math.floor((contentSize.height - layoutComp.paddingTop - layoutComp.paddingBottom + layoutComp.spacingY) / (itemContentSize.height + layoutComp.spacingY));
  185. // cc.log("每列多少个:", colEleCount);
  186. var lie = Math.ceil(index / colEleCount);
  187. var totalLie = Math.ceil(childCount / colEleCount);
  188. this.scrollToPercentHorizontal((totalLie - lie) / totalLie, seconds);
  189. }
  190. }
  191. else {
  192. // cc.log("cc.Layout.Type不对");
  193. }
  194. }
  195. }
  196. };
  197. SuperScrollView.prototype.onTouchEnd = function () {
  198. return;
  199. this.improveDC();
  200. };
  201. SuperScrollView.prototype.onScrollEnded = function () {
  202. // cc.log("onScrollEnded");
  203. this.improveDC();
  204. };
  205. SuperScrollView.prototype.onScrolling = function () {
  206. var now = Date.now();
  207. if (now - this.lastTime < 200) {
  208. return;
  209. }
  210. // cc.log("scolling");
  211. this.lastTime = now;
  212. var scrollOffset = this.getScrollOffset();
  213. var offsetX = scrollOffset.x;
  214. var offsetY = scrollOffset.y;
  215. this.improveDC();
  216. };
  217. SuperScrollView.prototype.onBounceTop = function () {
  218. // cc.log("onBounceTop")
  219. };
  220. SuperScrollView.prototype.onBounceBottom = function () {
  221. // cc.log("onBounceBottom")
  222. };
  223. SuperScrollView.prototype.onBounceLeft = function () {
  224. // cc.log("onBounceLeft")
  225. };
  226. SuperScrollView.prototype.onBounceRight = function () {
  227. // cc.log("onBounceLeft")
  228. };
  229. SuperScrollView.prototype.newItemNode = function () {
  230. // let itemNode = this.itemNodePool.get();
  231. // if (!itemNode) {
  232. // cc.log("instantiate");
  233. // itemNode = cc.instantiate(this.pfItemTemplate);
  234. // }
  235. var itemNode = cc.instantiate(this.pfItemTemplate);
  236. return itemNode;
  237. };
  238. // 优化DrawCall
  239. SuperScrollView.prototype.improveDC = function () {
  240. if (this.content.childrenCount == 0) {
  241. return;
  242. }
  243. var svLeftBottomPoint = this.node.parent.convertToWorldSpaceAR(
  244. // cc.v2(0,0)
  245. cc.v2(this.node.x - this.node.anchorX * this.node.width, this.node.y - this.node.anchorY * this.node.height));
  246. // 求出 ScrollView 可视区域在世界坐标系中的矩形(碰撞盒)
  247. var svBBoxRect = cc.rect(svLeftBottomPoint.x, svLeftBottomPoint.y, this.node.width, this.node.height);
  248. // 遍历 ScrollView Content 内容节点的子节点,对每个子节点的包围盒做和 ScrollView 可视区域包围盒做碰撞判断
  249. this.content.children.forEach(function (childNode) {
  250. // 如果相交了,那么就显示,否则就隐藏
  251. var childNodeBBox = childNode.getBoundingBoxToWorld();
  252. if (childNodeBBox.intersects(svBBoxRect)) {
  253. if (childNode.opacity === 0) {
  254. childNode.opacity = 255;
  255. }
  256. }
  257. else {
  258. if (childNode.opacity !== 0) {
  259. childNode.opacity = 0;
  260. }
  261. }
  262. });
  263. };
  264. SuperScrollView.prototype.setData = function (itemInfoList, isFrameLoading, cb) {
  265. if (isFrameLoading === void 0) { isFrameLoading = false; }
  266. return __awaiter(this, void 0, void 0, function () {
  267. var i, item;
  268. var _this = this;
  269. return __generator(this, function (_a) {
  270. switch (_a.label) {
  271. case 0:
  272. if (!this.isLoadingFinished) {
  273. return [2 /*return*/];
  274. }
  275. this.curIndex = 0;
  276. this.isLoadingFinished = false;
  277. this.isFrameLoading = isFrameLoading;
  278. this.itemInfoList = itemInfoList;
  279. this.cbAfterSetData = cb;
  280. this.content.destroyAllChildren();
  281. if (!this.isFrameLoading) return [3 /*break*/, 2];
  282. return [4 /*yield*/, this.executePreFrame(this.getItemGenerator(this.itemInfoList.length), this.duration)];
  283. case 1:
  284. _a.sent();
  285. return [3 /*break*/, 3];
  286. case 2:
  287. for (i = 0; i < this.itemInfoList.length; i++) {
  288. item = this.newItemNode();
  289. item.parent = this.content;
  290. item.getComponent(SuperListItem_1.default).setData(itemInfoList[i]);
  291. }
  292. this.isLoadingFinished = true;
  293. this.scheduleOnce(function () {
  294. _this.cbAfterSetData && _this.cbAfterSetData();
  295. _this.improveDC();
  296. });
  297. _a.label = 3;
  298. case 3: return [2 /*return*/];
  299. }
  300. });
  301. });
  302. };
  303. //@ts-ignore
  304. SuperScrollView.prototype.executePreFrame = function (generator, duration) {
  305. var _this = this;
  306. return new Promise(function (resolve, reject) {
  307. var gen = generator;
  308. // 创建执行函数
  309. var execute = function () {
  310. // 执行之前,先记录开始时间
  311. var startTime = new Date().getTime();
  312. // 然后一直从 Generator 中获取已经拆分好的代码段出来执行
  313. for (var iter = gen.next();; iter = gen.next()) {
  314. // 判断是否已经执行完所有 Generator 的小代码段,如果是的话,那么就表示任务完成
  315. if (iter == null || iter.done) {
  316. //@ts-ignore
  317. resolve();
  318. return;
  319. }
  320. // 每执行完一段小代码段,都检查一下是否已经超过我们分配的本帧,这些小代码端的最大可执行时间
  321. if (new Date().getTime() - startTime > duration) {
  322. // 如果超过了,那么本帧就不在执行,开定时器,让下一帧再执行
  323. _this.scheduleOnce(function () {
  324. execute();
  325. });
  326. return;
  327. }
  328. }
  329. };
  330. // 运行执行函数
  331. execute();
  332. });
  333. };
  334. SuperScrollView.prototype.initItem = function (itemInfo) {
  335. var itemNode = this.newItemNode();
  336. itemNode.parent = this.content;
  337. itemNode.getComponent(SuperListItem_1.default).setData(itemInfo);
  338. };
  339. SuperScrollView.prototype.getItemGenerator = function (length) {
  340. var i;
  341. var _this = this;
  342. return __generator(this, function (_a) {
  343. switch (_a.label) {
  344. case 0:
  345. i = 0;
  346. _a.label = 1;
  347. case 1:
  348. if (!(i < length)) return [3 /*break*/, 4];
  349. return [4 /*yield*/, this.initItem(this.itemInfoList[i])];
  350. case 2:
  351. _a.sent();
  352. _a.label = 3;
  353. case 3:
  354. i++;
  355. return [3 /*break*/, 1];
  356. case 4:
  357. this.isLoadingFinished = true;
  358. this.scheduleOnce(function () {
  359. _this.cbAfterSetData && _this.cbAfterSetData();
  360. _this.improveDC();
  361. });
  362. return [2 /*return*/];
  363. }
  364. });
  365. };
  366. SuperScrollView.prototype.canInputData = function () {
  367. return this.isLoadingFinished;
  368. };
  369. //通过index去获取节点
  370. SuperScrollView.prototype.getItem = function (index) {
  371. var item = this.content.getChildByName('item' + index);
  372. return item || null;
  373. };
  374. __decorate([
  375. property({
  376. type: cc.Node,
  377. tooltip: 'item模板',
  378. })
  379. ], SuperScrollView.prototype, "pfItemTemplate", void 0);
  380. __decorate([
  381. property({
  382. tooltip: '分帧加载时间间隔',
  383. })
  384. ], SuperScrollView.prototype, "duration", void 0);
  385. SuperScrollView = __decorate([
  386. ccclass
  387. ], SuperScrollView);
  388. return SuperScrollView;
  389. }(cc.ScrollView));
  390. exports.default = SuperScrollView;
  391. cc._RF.pop();