From 99765ac2f4e86e20be3d3d5e2c19e94757c0549f Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Mon, 12 Sep 2016 10:37:15 +0800 Subject: [PATCH] add pageview to componet for fireball/issues/1461 --- cocos2d/core/components/CCPageView.js | 434 ++++++++++++++++++ .../core/components/CCPageViewIndicator.js | 199 ++++++++ cocos2d/core/components/CCScrollView.js | 22 +- cocos2d/core/components/index.js | 2 + modules.json | 12 + 5 files changed, 660 insertions(+), 9 deletions(-) create mode 100644 cocos2d/core/components/CCPageView.js create mode 100644 cocos2d/core/components/CCPageViewIndicator.js diff --git a/cocos2d/core/components/CCPageView.js b/cocos2d/core/components/CCPageView.js new file mode 100644 index 0000000000..5b30fe5cae --- /dev/null +++ b/cocos2d/core/components/CCPageView.js @@ -0,0 +1,434 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +// 快速滑动的临界值 +var _CUSTOMSCROLLTHRESHOLD = 200; + +/** + * !#en The Page View Direction + * !#zh 页面视图滚动类型 + * @enum PageView.Direction + */ +var Direction = cc.Enum({ + /** + * !#en Horizontal scroll. + * !#zh 水平滚动 + * @property {Number} Horizontal + */ + Horizontal: 0, + /** + * !#en Vertical scroll. + * !#zh 垂直滚动 + * @property {Number} Horizontal + */ + Vertical: 1 +}); + +/** + * !#en Enum for ScrollView event type. + * !#zh 滚动视图事件类型 + * @enum PageView.EventType + */ +var EventType = cc.Enum({ + /** + * !#en The page turning event + * !#zh 翻页事件 + * @property {Number} PAGE_TURNING + */ + PAGE_TURNING: 0 + +}); + +/** + * !#en The PageView control + * !#zh 页面视图组件 + * @class PageView + * @extends ScrollView + */ +var PageView = cc.Class({ + name: 'cc.PageView', + extends: cc.ScrollView, + + editor: CC_EDITOR && { + menu: 'i18n:MAIN_MENU.component.ui/PageView', + help: 'i18n:COMPONENT.help_url.pageview', + inspector: 'packages://inspector/inspectors/comps/ccpageview.js', + executeInEditMode: false + }, + + ctor: function () { + this._curPageIdx = 0; + this._lastPageIdx = 0; + this._pages = []; + }, + + properties: { + /** + * !#en The page view direction + * !#zh 页面视图滚动类型 + * @property {PageView.Direction} direction + */ + direction: { + default: Direction.Horizontal, + type: Direction, + tooltip: 'i18n:COMPONENT.pageview.direction', + notify: function() { + this.horizontal = this.direction === Direction.Horizontal; + this.vertical = this.direction === Direction.Vertical; + } + }, + + /** + * !#en + * The scroll threshold value, when drag exceeds this value, + * release the next page will automatically scroll, less than the restore + * !#zh 滚动临界值,默认单位百分比,当拖拽超出该数值时,松开会自动滚动下一页,小于时则还原。 + * @property {Number} scrollThreshold + */ + scrollThreshold: { + default: 0.5, + type: cc.Float, + slide: true, + range: [0, 1, 0.1], + tooltip: 'i18n:COMPONENT.pageview.scrollThreshold' + }, + + /** + * !#en The Page View Indicator + * !#zh 页面视图指示器组件 + * @property {PageViewIndicator} indicator + */ + indicator: { + default: null, + type: cc.PageViewIndicator, + tooltip: 'i18n:COMPONENT.pageview.indicator', + notify: function() { + if (this.indicator) { + this.indicator.setPageView(this); + } + } + }, + + /** + * !#en PageView events callback + * !#zh 滚动视图的事件回调函数 + * @property {Component.EventHandler[]} pageEvents + */ + pageEvents: { + default: [], + type: cc.Component.EventHandler, + tooltip: 'i18n:COMPONENT.pageview.pageEvents' + } + }, + + statics: { + Direction: Direction, + EventType: EventType + }, + + __preload: function () { + this._super(); + this.node.on('size-changed', this._updateAllPagesSize, this); + + var pageTurningEvent = new cc.Component.EventHandler(); + pageTurningEvent.target = this.node; + pageTurningEvent.component = "cc.PageView"; + pageTurningEvent.handler = "_dispatchPageTurningEvent"; + this.scrollEvents.push(pageTurningEvent); + }, + + onLoad: function () { + if (this.content) { + this._contentInitPos = this.content.position; + } + this._initPages(); + if (this.indicator) { + this.indicator.setPageView(this); + } + }, + + onDestroy: function() { + this._super(); + this.node.off('size-changed', this._updateAllPagesSize, this); + this.node.off(cc.ScrollView.EventType.AUTOSCROLL_ENDED, this._dispatchPageTurningEvent, this); + }, + + /** + * !#en Returns current page index + * !#zh 返回当前页面索引 + * @method getCurrentPageIndex + * @returns {Number} + */ + getCurrentPageIndex: function () { + return this._curPageIdx; + }, + + /** + * !#en Returns current page index + * !#zh 返回当前页面索引 + * @method setCurrentPaegIndex + * @param {Number} index + */ + setCurrentPageIndex: function (index) { + this.scrollToPage(index, true); + }, + + /** + * !#en Returns all pages of pageview + * !#zh 返回视图中的所有页面 + * @method getPages + * @returns {Node[]} + */ + getPages: function () { + return this._pages; + }, + + /** + * !#en Adds a page to PageView. + * !#zh 添加页面到视图中 + * @method addPage + * @param {Node} page + */ + addPage: function (page) { + if (!page || this._pages.indexOf(page) !== -1 || !this.content) + return; + this.content.addChild(page); + this._pages.push(page); + this._updatePageView(); + }, + + /** + * !#en Inserts a page in the specified location. + * !#zh 将页面插入指定位置中 + * @method insertPage + * @param {Node} page + * @param {Number} index + */ + insertPage: function (page, index) { + if (index < 0 || !page || this._pages.indexOf(page) !== -1 || !this.content) + return; + var pageCount = this._pages.length; + if (index >= pageCount) + this.addPage(page); + else { + this._pages.splice(index, 0, page); + this.content.addChild(page); + this._updatePageView(); + } + }, + + /** + * !#en Removes a page from PageView. + * !#zh 移除指定页面 + * @method removePage + * @param {Node} page + */ + removePage: function (page) { + if (!page || !this.content) return; + var index = this._pages.indexOf(page); + if (index === -1) { + cc.warn('can not found the %s page.', page.name); + return; + } + this.removePageAtIndex(index); + }, + + /** + * !#en Removes a page at index of PageView. + * !#zh 移除指定下标的页面 + * @method removePageAtIndex + * @param {Number} index + */ + removePageAtIndex: function (index) { + var pageList = this._pages; + if (index < 0 || index >= pageList.length) return; + var page = pageList[index]; + if (!page) return; + this.content.removeChild(page); + pageList.splice(index, 1); + this._updatePageView(); + }, + + /** + * !#en Removes all pages from PageView + * !#zh 移除所有页面 + * @method removeAllPages + */ + removeAllPages: function () { + if (!this.content) { return; } + var locPages = this._pages; + for (var i = 0, len = locPages.length; i < len; i++) + this.content.removeChild(locPages[i]); + this._pages.length = 0; + this._updatePageView(); + }, + + /** + * !#en scroll PageView to index. + * !#zh 滚动到指定页面 + * @method scrollToPage + * @param {Number} idx index of page. + * @param {Boolean} immediately immediately jumps + */ + scrollToPage: function (idx, immediately) { + if (idx < 0 || idx > this._pages.length) + return; + immediately = immediately || false; + var timeInSecond = immediately ? 0 : 0.3; + this._curPageIdx = idx; + this.scrollToOffset(this._moveOffsetValue(idx), timeInSecond, true); + if (this.indicator) { + this.indicator._changedState(); + } + }, + + // 刷新页面视图 + _updatePageView: function () { + var pageCount = this._pages.length; + + if (this._curPageIdx >= pageCount) { + // 当页面数组变化时修改 content 大小 + var layout = this.content.getComponent(cc.Layout); + if(layout && layout.enabledInHierarchy) { + layout._updateLayout(); + } + this._curPageIdx = pageCount === 0 ? 0 : pageCount - 1; + this._lastPageIdx = this._curPageIdx; + } + // 进行排序 + for (var i = 0; i < pageCount; ++i) { + this._pages[i].setSiblingIndex(i); + } + // 刷新 indicator 信息与状态 + if (this.indicator) { + this.indicator._refresh(); + } + }, + + // 刷新所有页面的大小 + _updateAllPagesSize: function () { + var locPages = CC_EDITOR ? this.content.children : this._pages; + var selfSize = this.node.getContentSize(); + for (var i = 0, len = locPages.length; i < len; i++) { + locPages[i].setContentSize(selfSize); + } + }, + + // 初始化页面 + _initPages: function () { + if (!this.content) { return; } + var children = this.content.children; + for (var i = 0; i < children.length; ++i) { + var page = children[i]; + if (this._pages.indexOf(page) >= 0) { continue; } + this._pages.push(page); + } + }, + + _dispatchPageTurningEvent: function (sender, event) { + if (event === cc.ScrollView.EventType.AUTOSCROLL_ENDED) { + if (this._lastPageIdx === this._curPageIdx) return; + this._lastPageIdx = this._curPageIdx; + cc.Component.EventHandler.emitEvents(this.pageEvents, this, EventType.PAGE_TURNING); + } + }, + + // 是否超过自动滚动临界值 + _isScrollable: function (offset) { + if (this.direction === Direction.Horizontal) { + return Math.abs(offset.x) >= this.node.width * this.scrollThreshold; + } + else if (this.direction === Direction.Vertical) { + return Math.abs(offset.y) >= this.node.height * this.scrollThreshold; + } + }, + + // 通过 idx 获取偏移值数值 + _moveOffsetValue: function (idx) { + var offset = cc.p(0, 0); + if (this.direction === Direction.Horizontal) { + offset.x = idx * this.node.width; + } + else if (this.direction === Direction.Vertical) { + offset.y = idx * this.node.height; + } + return offset; + }, + + _updatePageIndex: function (moveOffset) { + var idx = 0; + if (this.direction === Direction.Horizontal) { + idx += (moveOffset.x > 0 ? 1 : -1); + } + else if (this.direction === Direction.Vertical) { + idx += (moveOffset.y > 0 ? 1 : -1); + } + return this._curPageIdx + idx; + }, + + _handleReleaseLogic: function(touch) { + var bounceBackStarted = this._startBounceBackIfNeeded(); + if (!bounceBackStarted) { + var idx = this._curPageIdx; + if (idx === 0 && this._pages.length === 1) { + return; + } + var moveOffset = cc.pSub(this._touchBeganPosition, this._touchEndPosition); + if (this._isScrollable(moveOffset)) { + idx = this._updatePageIndex(moveOffset); + } + else { + var touchMoveVelocity = this._calculateTouchMoveVelocity(); + if (Math.abs(touchMoveVelocity.x) > _CUSTOMSCROLLTHRESHOLD) { + idx = this._updatePageIndex(moveOffset); + } + } + this.scrollToPage(idx); + } + }, + + _onTouchBegan: function (event) { + this._touchBeganPosition = event.touch.getLocation(); + this._super(event); + }, + + _onTouchMoved: function (event) { + this._super(event); + }, + + _onTouchEnded: function (event) { + this._touchEndPosition = event.touch.getLocation(); + this._super(event); + }, + + _onTouchCancelled: function (event) { + this._touchEndPosition = event.touch.getLocation(); + this._super(event); + }, + + _onMouseWheel: function () { } +}); + +cc.PageView = module.exports = PageView; diff --git a/cocos2d/core/components/CCPageViewIndicator.js b/cocos2d/core/components/CCPageViewIndicator.js new file mode 100644 index 0000000000..407d52214d --- /dev/null +++ b/cocos2d/core/components/CCPageViewIndicator.js @@ -0,0 +1,199 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Chukong Aipu reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +/** + * !#en Enum for PageView Indicator direction + * !#zh 页面视图指示器的摆放方向 + * @enum PageViewIndicator.Direction + */ +var Direction = cc.Enum({ + /** + * !#en The horizontal direction. + * !#zh 水平方向 + * @property {Number} HORIZONTAL + */ + HORIZONTAL: 0, + + /** + * !#en The vertical direction. + * !#zh 垂直方向 + * @property {Number} VERTICAL + */ + VERTICAL: 1 +}); + + +/** + * !#en The Page View Indicator Component + * !#zh 页面视图每页标记组件 + * @class PageViewIndicator + * @extends Component + */ +var PageViewIndicator = cc.Class({ + name: 'cc.PageViewIndicator', + extends: require('./CCComponent'), + + editor: CC_EDITOR && { + menu: 'i18n:MAIN_MENU.component.ui/PageViewIndicator', + help: 'i18n:COMPONENT.help_url.pageviewIndicator' + }, + + properties: { + _layout: null, + _pageView: null, + _indicators: [], + + /** + * !#en The spriteFrame for each element. + * !#zh 每个页面标记显示的图片 + * @property {SpriteFrame} spriteFrame + */ + spriteFrame: { + default: null, + type: cc.SpriteFrame, + tooltip: 'i18n:COMPONENT.pageview_indicator.spriteFrame' + }, + + /** + * !#en The location direction of PageViewIndicator. + * !#zh 页面标记摆放方向 + *@property {PageViewIndicator.Direction} direction + */ + direction: { + default: Direction.HORIZONTAL, + type: Direction, + tooltip: 'i18n:COMPONENT.pageview_indicator.direction' + }, + + /** + * !#en The cellSize for each element. + * !#zh 每个页面标记的大小 + * @property {Size} cellSize + */ + cellSize: { + default: cc.size(20, 20), + tooltip: 'i18n:COMPONENT.layout.cell_size' + }, + + /** + * !#en The distance between each element. + * !#zh 每个页面标记之间的边距 + * @property {Number} spacing + */ + spacing: { + default: 0, + tooltip: 'i18n:COMPONENT.pageview_indicator.spacing' + } + }, + + statics: { + Direction: Direction + }, + + onLoad: function () { + this._updateLayout(); + }, + + /** + * !#en Set Page View + * !#zh 设置页面视图 + * @method setPageView + * @param {PageView} target + */ + setPageView: function (target) { + this._pageView = target; + this._refresh(); + }, + + _updateLayout: function () { + this._layout = this.getComponent(cc.Layout); + if (!this._layout) { + this._layout = this.addComponent(cc.Layout); + } + if (this.direction === Direction.HORIZONTAL) { + this._layout.type = cc.Layout.Type.HORIZONTAL; + this._layout.spacingX = this.spacing; + } + else if (this.direction === Direction.VERTICAL) { + this._layout.type = cc.Layout.Type.VERTICAL; + this._layout.spacingY = this.spacing; + } + this._layout.resizeMode = cc.Layout.ResizeMode.CONTAINER; + }, + + _createIndicator: function () { + var node = new cc.Node(); + var sprite = node.addComponent(cc.Sprite); + sprite.spriteFrame = this.spriteFrame; + node.parent = this.node; + node.width = this.cellSize.width; + node.height = this.cellSize.height; + return node; + }, + + _changedState: function () { + var indicators = this._indicators; + if (indicators.length === 0) return; + var idx = this._pageView._curPageIdx; + if (idx >= indicators.length) return; + for (var i = 0; i < indicators.length; ++i) { + var node = indicators[i]; + node.opacity = 255 / 2; + } + indicators[idx].opacity = 255; + }, + + _refresh: function () { + if (!this._pageView) { return; } + var indicators = this._indicators; + var pages = this._pageView.getPages(); + if (pages.length === indicators.length) { + return; + } + var i = 0; + if (pages.length > indicators.length) { + for (i = 0; i < pages.length; ++i) { + if (!indicators[i]) { + indicators[i] = this._createIndicator(); + } + } + } + else { + var count = indicators.length - pages.length; + for (i = count; i > 0; --i) { + var node = indicators[i - 1]; + this.node.removeChild(node); + indicators.splice(i - 1, 1); + } + } + if(this._layout && this._layout.enabledInHierarchy) { + this._layout._updateLayout(); + } + this._changedState(); + } +}); + + +cc.PageViewIndicator = module.exports = PageViewIndicator; diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index 0a535109cf..6ddc514ec1 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -216,7 +216,8 @@ var ScrollView = cc.Class({ default: 0.5, type: 'Float', range: [0, 1, 0.1], - animatable: false + animatable: false, + tooltip: 'i18n:COMPONENT.scrollview.brake', }, /** @@ -226,7 +227,8 @@ var ScrollView = cc.Class({ */ elastic: { default: true, - animatable: false + animatable: false, + tooltip: 'i18n:COMPONENT.scrollview.elastic', }, /** @@ -237,7 +239,8 @@ var ScrollView = cc.Class({ bounceDuration: { default: 1, range: [0, 10], - animatable: false + animatable: false, + tooltip: 'i18n:COMPONENT.scrollview.bounceDuration', }, /** @@ -283,19 +286,21 @@ var ScrollView = cc.Class({ */ scrollEvents: { default: [], - type: cc.Component.EventHandler + type: cc.Component.EventHandler, + tooltip: 'i18n:COMPONENT.scrollview.scrollEvents' }, /** - * !#en If cancelInnerEvents is set to true, the scroll behavior will cancel touch events on inner content nodes of the scroll view + * !#en If cancelInnerEvents is set to true, the scroll behavior will cancel touch events on inner content nodes * It's set to true by default. - * !#zh 如果这个属性被设置为 true,那么滚动行为会取消 ScrollView 的子节点上注册的触摸事件,默认被设置为 true。 + * !#zh 如果这个属性被设置为 true,那么滚动行为会取消子节点上注册的触摸事件,默认被设置为 true。 * 注意,子节点上的 touchstart 事件仍然会触发,触点移动距离非常短的情况下 touchmove 和 touchend 也不会受影响。 * @property {Boolean} cancelInnerEvents */ cancelInnerEvents: { default: true, animatable: false, + tooltip: 'i18n:COMPONENT.scrollview.cancelInnerEvents' } }, @@ -1055,8 +1060,7 @@ var ScrollView = cc.Class({ _handleReleaseLogic: function(touch) { var delta = touch.getDelta(); this._gatherTouchMove(delta); - - this._processInertiaScroll(); + this._processInertiaScroll(); }, _isOutOfBoundary: function() { @@ -1292,7 +1296,7 @@ var ScrollView = cc.Class({ this._outOfBoundaryAmountDirty = false; } - outOfBoundaryAmount = this._clampDelta(outOfBoundaryAmount); + //outOfBoundaryAmount = this._clampDelta(outOfBoundaryAmount); return outOfBoundaryAmount; }, diff --git a/cocos2d/core/components/index.js b/cocos2d/core/components/index.js index 49d5614db4..dd2e1b06ff 100644 --- a/cocos2d/core/components/index.js +++ b/cocos2d/core/components/index.js @@ -41,6 +41,8 @@ module.exports = [ require('./CCMask'), require('./CCScrollBar'), require('./CCScrollView'), + require('./CCPageViewIndicator'), + require('./CCPageView'), require('./CCLayout'), require('./CCEditBox'), require('./CCVideoPlayer'), diff --git a/modules.json b/modules.json index 2beba53acc..d121aaea47 100644 --- a/modules.json +++ b/modules.json @@ -90,6 +90,18 @@ "./cocos2d/core/components/CCScrollView.js" ] }, + { + "name": "PageView", + "entries": [ + "./cocos2d/core/components/CCPageView.js" + ] + }, + { + "name": "PageViewIndicator", + "entries": [ + "./cocos2d/core/components/CCPageViewIndicator.js" + ] + }, { "name": "Layout", "entries": [