From 04389e27dd9b1ec738e04779b07ce1a573ff8cad Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 10 Oct 2016 14:02:53 +0800 Subject: [PATCH 1/6] add setAutoScrollStopEpsilon API --- cocos2d/core/components/CCPageView.js | 20 ++++++++++++++++++++ cocos2d/core/components/CCScrollView.js | 10 +++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCPageView.js b/cocos2d/core/components/CCPageView.js index 2ec2ac0c5b..9527ebdcd1 100644 --- a/cocos2d/core/components/CCPageView.js +++ b/cocos2d/core/components/CCPageView.js @@ -81,6 +81,7 @@ var PageView = cc.Class({ ctor: function () { this._curPageIdx = 0; this._lastPageIdx = 0; + this._autoScrollStopEpsilon = 0.001; this._pages = []; }, @@ -300,6 +301,25 @@ var PageView = cc.Class({ } }, + /** + * !#en Change the AutoScroll stop epsilon value of PageView, change this value could adjust the PageView's + * event triggerring time. + * !#zh 设置 PageView 页面自动滚动动画结束的阈值,修改此值可以调整 PageView 事件的发送时机。 + * 默认值是 0.001,增大此值,可以让 PageView 的事件更快发送出来,比如设置 0.1。 + * @param {Number} value AutoScroll Stop epsilon value, should be a value in 0-1 + */ + setAutoScrollStopEpsilon: function (value) { + this._autoScrollStopEpsilon = value; + }, + + /** + * !#en Query the current AutoScroll stop epsilon value of PageView. + * !#zh 查询 PageView 页面自动滚动动画结束的阈值 + */ + getAutoScrollStopEpsilon: function () { + return this._autoScrollStopEpsilon; + }, + // 刷新页面视图 _updatePageView: function () { var pageCount = this._pages.length; diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index c3dafe2a4d..5b093e4b99 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -1088,6 +1088,10 @@ var ScrollView = cc.Class({ return false; }, + getAutoScrollStopEpsilon: function () { + return EPSILON; + }, + _processAutoScrolling: function(dt) { var isAutoScrollBrake = this._isNecessaryAutoScrollBrake(); @@ -1100,7 +1104,11 @@ var ScrollView = cc.Class({ } var newPosition = cc.pAdd(this._autoScrollStartPosition, cc.pMult(this._autoScrollTargetDelta, percentage)); - var reachedEnd = (percentage === 1); + var reachedEnd = Math.abs(percentage - 1) <= this.getAutoScrollStopEpsilon(); + + if(reachedEnd) { + newPosition = cc.pAdd(this._autoScrollStartPosition, this._autoScrollTargetDelta); + } if (this.elastic) { var brakeOffsetPosition = cc.pSub(newPosition, this._autoScrollBrakingStartPosition); From a0e418ec2d807ab7004a7602050a2309e196501e Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 10 Oct 2016 14:06:10 +0800 Subject: [PATCH 2/6] fixed animation may emit wrong event when set time --- cocos2d/animation/animation-animator.js | 2 +- cocos2d/animation/animation-curves.js | 23 +++++++++++-- cocos2d/animation/animation-state.js | 3 +- test/qunit/unit/test-animation.js | 45 +++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/cocos2d/animation/animation-animator.js b/cocos2d/animation/animation-animator.js index 234b7a92bd..af4c23d98e 100644 --- a/cocos2d/animation/animation-animator.js +++ b/cocos2d/animation/animation-animator.js @@ -31,7 +31,7 @@ p.playState = function (state, startTime) { state.play(); if (typeof startTime === 'number') { - state.time = startTime; + state.setTime(startTime); } this.play(); diff --git a/cocos2d/animation/animation-curves.js b/cocos2d/animation/animation-curves.js index 5dce0a2796..331ca9309f 100644 --- a/cocos2d/animation/animation-curves.js +++ b/cocos2d/animation/animation-curves.js @@ -318,6 +318,10 @@ var EventAnimCurve = cc.Class({ if (direction < 0) currentIndex += 1; } + if (this._ignoreIndex !== currentIndex) { + this._ignoreIndex = null; + } + var lastWrappedInfo = this._lastWrappedInfo; currentWrappedInfo.frameIndex = currentIndex; @@ -377,7 +381,7 @@ var EventAnimCurve = cc.Class({ }, _fireEvent: function (index) { - if (index < 0 || index >= this.events.length) return; + if (index < 0 || index >= this.events.length || this._ignoreIndex === index) return; var eventInfo = this.events[index]; var events = eventInfo.events; @@ -396,8 +400,23 @@ var EventAnimCurve = cc.Class({ } }, - onTimeChangedManually: function () { + onTimeChangedManually: function (time, animationNode) { this._lastWrappedInfo = null; + this._ignoreIndex = null; + + var info = animationNode.getWrappedInfo(time); + var direction = info.direction; + var frameIndex = binarySearch(this.ratios, info.ratio); + + // only ignore when time not on a frame index + if (frameIndex < 0) { + frameIndex = ~frameIndex - 1; + + // if direction is inverse, then increase index + if (direction < 0) frameIndex += 1; + + this._ignoreIndex = frameIndex; + } } }); diff --git a/cocos2d/animation/animation-state.js b/cocos2d/animation/animation-state.js index 7e2d90c3c7..5f28adaf9c 100644 --- a/cocos2d/animation/animation-state.js +++ b/cocos2d/animation/animation-state.js @@ -88,8 +88,9 @@ state.onStop = function () { state.setTime = function (time) { this.time = time || 0; + var self = this; this.curves.forEach(function (curve) { - curve.onTimeChangedManually(); + curve.onTimeChangedManually(time, self); }); }; diff --git a/test/qunit/unit/test-animation.js b/test/qunit/unit/test-animation.js index 51f1afc1ca..21477f4de8 100644 --- a/test/qunit/unit/test-animation.js +++ b/test/qunit/unit/test-animation.js @@ -882,6 +882,51 @@ test('EventAnimCurve', function () { ], 'should triggered frame 5 event once if speed is -1'); + state.speed = 1; + state.wrapMode = cc.WrapMode.Normal; + animation.stop('test'); + animation.play('test', 1.1); + calls = []; + manager.update(0); + manager.update(1); + deepEqual(calls, [ + { + func: 'func1', + args: ['Frame 3 Event triggered'] + }, + { + func: 'func1', + args: ['Frame 4 Event triggered'] + }, + { + func: 'func1', + args: ['Frame 5 Event triggered'] + } + ], 'should triggered from frame 3 to frame 5'); + + + state.wrapMode = cc.WrapMode.Reverse; + animation.stop('test'); + animation.play('test', 1.1); + calls = []; + manager.update(0); + manager.update(1); + deepEqual(calls, [ + { + func: 'func2', + args: [1,2] + }, + { + func: 'func3', + args: ['Second event on frame 0.4'] + }, + { + func: 'func1', + args: ['Frame 0 Event triggered'] + } + ], 'should triggered from frame 2 to frame 0'); + + // new clip clip = new cc.AnimationClip(); clip._duration = 2; From 885f9283cc7ca1097b71dcd6a5ec7226dbc75239 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 10 Oct 2016 14:07:18 +0800 Subject: [PATCH 3/6] add method anotation --- cocos2d/core/components/CCPageView.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cocos2d/core/components/CCPageView.js b/cocos2d/core/components/CCPageView.js index 9527ebdcd1..2098710653 100644 --- a/cocos2d/core/components/CCPageView.js +++ b/cocos2d/core/components/CCPageView.js @@ -305,6 +305,7 @@ var PageView = cc.Class({ * !#en Change the AutoScroll stop epsilon value of PageView, change this value could adjust the PageView's * event triggerring time. * !#zh 设置 PageView 页面自动滚动动画结束的阈值,修改此值可以调整 PageView 事件的发送时机。 + * @method setAutoScrollStopEpsilon * 默认值是 0.001,增大此值,可以让 PageView 的事件更快发送出来,比如设置 0.1。 * @param {Number} value AutoScroll Stop epsilon value, should be a value in 0-1 */ @@ -315,6 +316,7 @@ var PageView = cc.Class({ /** * !#en Query the current AutoScroll stop epsilon value of PageView. * !#zh 查询 PageView 页面自动滚动动画结束的阈值 + * @method getAutoScrollStopEpsilon */ getAutoScrollStopEpsilon: function () { return this._autoScrollStopEpsilon; From d05aa4874cb6a0b56e9fa14901491d6dc79e4bba Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 10 Oct 2016 14:30:29 +0800 Subject: [PATCH 4/6] change method to property --- cocos2d/core/components/CCPageView.js | 34 ++++++++++++--------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/cocos2d/core/components/CCPageView.js b/cocos2d/core/components/CCPageView.js index 2098710653..1954a46679 100644 --- a/cocos2d/core/components/CCPageView.js +++ b/cocos2d/core/components/CCPageView.js @@ -81,7 +81,6 @@ var PageView = cc.Class({ ctor: function () { this._curPageIdx = 0; this._lastPageIdx = 0; - this._autoScrollStopEpsilon = 0.001; this._pages = []; }, @@ -116,6 +115,19 @@ var PageView = cc.Class({ tooltip: 'i18n:COMPONENT.pageview.scrollThreshold' }, + /** + * !#en Change the AutoScroll stop epsilon value of PageView, change this value could adjust the PageView's + * event triggerring timing. + * !#zh 设置 PageView 页面自动滚动动画结束的阈值,修改此值可以调整 PageView 事件的发送时机。 + * @property {Number} autoScrollStopEpsilon + */ + autoScrollStopEpsilon: { + default: 0.1, + type: cc.Float, + range: [0, 1, 0.1], + tooltip: 'i18n:COMPONENT.pageview.autoScrollStopEpsilon' + }, + /** * !#en The Page View Indicator * !#zh 页面视图指示器组件 @@ -301,25 +313,9 @@ var PageView = cc.Class({ } }, - /** - * !#en Change the AutoScroll stop epsilon value of PageView, change this value could adjust the PageView's - * event triggerring time. - * !#zh 设置 PageView 页面自动滚动动画结束的阈值,修改此值可以调整 PageView 事件的发送时机。 - * @method setAutoScrollStopEpsilon - * 默认值是 0.001,增大此值,可以让 PageView 的事件更快发送出来,比如设置 0.1。 - * @param {Number} value AutoScroll Stop epsilon value, should be a value in 0-1 - */ - setAutoScrollStopEpsilon: function (value) { - this._autoScrollStopEpsilon = value; - }, - - /** - * !#en Query the current AutoScroll stop epsilon value of PageView. - * !#zh 查询 PageView 页面自动滚动动画结束的阈值 - * @method getAutoScrollStopEpsilon - */ + //override the method of ScrollView getAutoScrollStopEpsilon: function () { - return this._autoScrollStopEpsilon; + return this.autoScrollStopEpsilon; }, // 刷新页面视图 From 602cf64ca2d49963b3f2ea869fbfe0f750f5bbe6 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 10 Oct 2016 14:34:10 +0800 Subject: [PATCH 5/6] refine code --- cocos2d/animation/animation-curves.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cocos2d/animation/animation-curves.js b/cocos2d/animation/animation-curves.js index 331ca9309f..abc51e9011 100644 --- a/cocos2d/animation/animation-curves.js +++ b/cocos2d/animation/animation-curves.js @@ -297,7 +297,15 @@ var EventAnimCurve = cc.Class({ * @type {Object} * @default null */ - _lastWrappedInfo: null + _lastWrappedInfo: null, + + /** + * Ignore event index + * @property _ignoreIndex + * @type {number} + * @default NaN + */ + _ignoreIndex: NaN }, _wrapIterations: function (iterations) { @@ -319,7 +327,7 @@ var EventAnimCurve = cc.Class({ } if (this._ignoreIndex !== currentIndex) { - this._ignoreIndex = null; + this._ignoreIndex = NaN; } var lastWrappedInfo = this._lastWrappedInfo; @@ -402,7 +410,7 @@ var EventAnimCurve = cc.Class({ onTimeChangedManually: function (time, animationNode) { this._lastWrappedInfo = null; - this._ignoreIndex = null; + this._ignoreIndex = NaN; var info = animationNode.getWrappedInfo(time); var direction = info.direction; From 24829bd068f87573608e0d1fe961ce06a9fbc9b7 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 10 Oct 2016 14:35:11 +0800 Subject: [PATCH 6/6] change range precision to 0.01 --- cocos2d/core/components/CCPageView.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCPageView.js b/cocos2d/core/components/CCPageView.js index 1954a46679..afa56b5be8 100644 --- a/cocos2d/core/components/CCPageView.js +++ b/cocos2d/core/components/CCPageView.js @@ -111,7 +111,7 @@ var PageView = cc.Class({ default: 0.5, type: cc.Float, slide: true, - range: [0, 1, 0.1], + range: [0, 1, 0.01], tooltip: 'i18n:COMPONENT.pageview.scrollThreshold' }, @@ -124,7 +124,7 @@ var PageView = cc.Class({ autoScrollStopEpsilon: { default: 0.1, type: cc.Float, - range: [0, 1, 0.1], + range: [0, 1, 0.01], tooltip: 'i18n:COMPONENT.pageview.autoScrollStopEpsilon' },