var t = require; var e = module; var i = exports; e = (this && this.__decorate) || function(t, e, i, s) { var o, a = arguments.length, n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s; if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s); else for (var r = t.length - 1; 0 <= r; r--) (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n); return 3 < a && n && Object.defineProperty(e, i, n), n; }; Object.defineProperty(i, "__esModule", { value: !0 }); const s = t("ItemRenderer"), { ccclass: o, property: a, menu: n } = cc._decorator; t = class extends cc.ScrollView { constructor() { super(...arguments), (this.itemRender = null), (this._curPageIdx = 0), (this._lastPageIdx = 0), (this.pageTurningSpeed = 0.3), (this.scrollThreshold = 0.5), (this.autoPageTurningThreshold = 100), (this.pages = null), (this.scaleCenterDist = 10), (this.scaleMin = 0.7), (this.alphaMin = 127.5), (this.items = []), (this.itemsPool = []); } setData(t) { var i = this; (this.pages = t), (this.itemRendererClass = this.itemRendererClass || s.default), this.items.map(function(t) { t.removeFromParent(!1), i.itemsPool.push(t); }), (this.items = []), this.pages.map(function(t) { var e = null; 0 < i.itemsPool.length ? ((e = i.itemsPool[0]), i.itemsPool.splice(0, 1)) : (e = cc.instantiate(i.itemRender)), i.items.push(e), (e.y = 0), i.content.addChild(e), e.getComponent(i.itemRendererClass).setData(t); }), (this.node.width = this.itemRender.width), this.updatePageView(); } updatePageView() { var t = this.content.getComponent(cc.Layout); t && t.enabled && t.updateLayout(); t = this.pages.length; this._curPageIdx >= t && ((this._curPageIdx = 0 === t ? 0 : t - 1), (this._lastPageIdx = this._curPageIdx)); } setCurrentPageIndex(t) { this.scrollToPage(t, void 0); } scrollToPage(t, e) { t < 0 || t >= this.pages.length || ((e = void 0 !== e ? e : 0.3), (this._curPageIdx = t), this.scrollToOffset(this._moveOffsetValue(t), e, !0)); } getCurrentPage() { return this._curPageIdx; } getCurrentPageData() { return this.pages[this._curPageIdx]; } onEnable() { this.registerEvent(), super.onEnable(); } onDisable() { this.unregisterEvent(), super.onDisable(); } _moveOffsetValue(t) { var e = cc.v2(0, 0), i = this.content.getComponent(cc.Layout); return (e.x = t * (this.node.width + i.spacingX)), e; } registerEvent() { this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this, !0), this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, !0), this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, !0), this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancelled, this, !0), this.node.on("scroll-ended-with-threshold", this.dispatchPageTurningEvent, this), this.node.on("scrolling", this.onScrolling, this); } unregisterEvent() { this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this, !0), this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this, !0), this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this, !0), this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancelled, this, !0), this.node.off("scroll-ended-with-threshold", this.dispatchPageTurningEvent, this), this.node.off("scrolling", this.onScrolling, this); } getDragDirection(t) { return 0 === t.x ? 0 : 0 < t.x ? 1 : -1; } dispatchPageTurningEvent() { this._lastPageIdx !== this._curPageIdx && ((this._lastPageIdx = this._curPageIdx), this.node.emit("page-turning", this)); } isScrollable(t, e, i) { return Math.abs(t.x) >= this.node.width * this.scrollThreshold; } isQuicklyScrollable(t) { return Math.abs(t.x) > this.autoPageTurningThreshold; } autoScrollToPage() { var t = this._startBounceBackIfNeeded(), e = this._touchBeganPosition.sub(this._touchEndPosition); if (t) { var i = this.getDragDirection(e); 0 !== i && (this._curPageIdx = 0 < i ? this.pages.length - 1 : 0); } else { var s = this._curPageIdx, t = s + this.getDragDirection(e), i = this.pageTurningSpeed * Math.abs(s - t); if (t < this.pages.length) { if (this.isScrollable(e, s, t)) return void this.scrollToPage(t, i); e = this._calculateTouchMoveVelocity(); if (this.isQuicklyScrollable(e)) return void this.scrollToPage(t, i); } this.scrollToPage(s, i); } } _handleReleaseLogic(t) { this.autoScrollToPage(), this._scrolling && ((this._scrolling = !1), this._autoScrolling || this._dispatchEvent("scroll-ended")); } _onTouchBegan(t, e) { (this._touchBeganPosition = t.touch.getLocation()), super._onTouchBegan(t, e), this.node.emit("startScroll", this); } _onTouchMoved(t, e) { super._onTouchMoved(t, e); } _onTouchEnded(t, e) { (this._touchEndPosition = t.touch.getLocation()), super._onTouchEnded(t, e); } _onTouchCancelled(t, e) { (this._touchEndPosition = t.touch.getLocation()), super._onTouchCancelled(t, e); } emitItems(e, i, s, o, a, n) { this.items.map(function(t) { t.emit(e, i, s, o, a, n); }); } onScrolling() { var n = this; this.items.map(function(t) { var e, i, s, o, a; t.isValid && ((a = t.getComponent(n.itemRendererClass)), (e = n.node.convertToNodeSpaceAR(t.convertToWorldSpaceAR(t.position))), (o = n.content.getComponent(cc.Layout)), (s = (a.data.id - 1) * (n.node.width + o.spacingX) - n.content.width / 2 + n.node.width / 2), (i = 1), (a = 255), (s = Math.abs(e.x - s)), (o = n.node.width + o.spacingX), (a = s <= n.scaleCenterDist ? ((i = 1), 255) : o <= s ? ((i = n.scaleMin), n.alphaMin) : ((i = n.scaleMin + ((1 - n.scaleMin) / o) * (o - s)), n.alphaMin + ((255 - n.alphaMin) / o) * (o - s))), (t.scale = i), (t.opacity = a)); }); } }; e([a(cc.Node)], t.prototype, "itemRender", void 0), (t = e([o], t)), (i.default = t);