From b672b8d0fe7c230df539a33ad27c66bc1b735012 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Fri, 4 Nov 2016 18:02:14 +0800 Subject: [PATCH 1/4] improve button cancel behavior --- cocos2d/core/components/CCButton.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 19414c0af2..5123a02a2f 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -480,7 +480,7 @@ var Button = cc.Class({ }, _onTouchMove: function (event) { - if (!this.interactable || !this.enabledInHierarchy) return; + if (!this.interactable || !this.enabledInHierarchy || !this._pressed) return; // mobile phone will not emit _onMouseMoveOut, // so we have to do hit test when touch moving var touch = event.touch; @@ -530,7 +530,7 @@ var Button = cc.Class({ }, _zoomBack: function () { - this._fromScale = this._originalScale * this.zoomScale; + this._fromScale = this.node.scale; this._toScale = this._originalScale; this.time = 0; this._transitionFinished = false; @@ -540,8 +540,11 @@ var Button = cc.Class({ if (!this.interactable || !this.enabledInHierarchy) return; this._pressed = false; - - this._updateState(); + if(this.transition === Transition.SCALE) { + this._zoomBack(); + } else { + this._updateState(); + } }, _onMouseMoveIn: function () { From 362b8b131f74aa45dab5930cc79fda4d7f24c0b1 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 8 Nov 2016 16:52:38 +0800 Subject: [PATCH 2/4] refactoring button --- cocos2d/core/components/CCButton.js | 123 +++++++++++++++------------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 5123a02a2f..d95e5ef2d4 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -384,6 +384,8 @@ var Button = cc.Class({ if (!this.target) { this.target = this.node; } + this._applyTarget(); + this._updateState(); }, onEnable: function () { @@ -412,15 +414,11 @@ var Button = cc.Class({ } }, - onLoad: function () { - this._applyTarget(); - this._updateState(); - }, - update: function (dt) { var target = this.target; - if ((this.transition !== Transition.COLOR && this.transition !== Transition.SCALE) - || !target || this._transitionFinished) return; + if(!target) return; + if(this._transitionFinished) return; + if (this.transition !== Transition.COLOR && this.transition !== Transition.SCALE) return; this.time += dt; var ratio = 1.0; @@ -471,11 +469,7 @@ var Button = cc.Class({ if (!this.interactable || !this.enabledInHierarchy) return; this._pressed = true; - if(this.transition === Transition.SCALE) { - this._zoomUp(); - } else { - this._updateState(); - } + this._updateState(); event.stopPropagation(); }, @@ -499,9 +493,7 @@ var Button = cc.Class({ } else { state = 'normal'; } - var color = this[state + 'Color']; - var sprite = this[state + 'Sprite']; - this._applyTransition(color, sprite); + this._applyTransition(state); } event.stopPropagation(); }, @@ -514,11 +506,7 @@ var Button = cc.Class({ this.node.emit('click', this); } this._pressed = false; - if(this.transition === Transition.SCALE) { - this._zoomBack(); - } else { - this._updateState(); - } + this._updateState(); event.stopPropagation(); }, @@ -530,7 +518,7 @@ var Button = cc.Class({ }, _zoomBack: function () { - this._fromScale = this.node.scale; + this._fromScale = this.target.scale; this._toScale = this._originalScale; this.time = 0; this._transitionFinished = false; @@ -540,11 +528,7 @@ var Button = cc.Class({ if (!this.interactable || !this.enabledInHierarchy) return; this._pressed = false; - if(this.transition === Transition.SCALE) { - this._zoomBack(); - } else { - this._updateState(); - } + this._updateState(); }, _onMouseMoveIn: function () { @@ -566,23 +550,8 @@ var Button = cc.Class({ // state handler _updateState: function () { - var state; - if (!this.interactable) { - state = 'disabled'; - } - else if (this._pressed) { - state = 'pressed'; - } - else if (this._hovered) { - state = 'hover'; - } - else { - state = 'normal'; - } - var color = this[state + 'Color']; - var sprite = this[state + 'Sprite']; - - this._applyTransition(color, sprite); + var state = this._getButtonState(); + this._applyTransition(state); this._updateDisabledState(); }, @@ -603,24 +572,64 @@ var Button = cc.Class({ } }, - _applyTransition: function (color, sprite) { + _getButtonState: function () { + var state; + if (!this.interactable) { + state = 'disabled'; + } + else if (this._pressed) { + state = 'pressed'; + } + else if (this._hovered) { + state = 'hover'; + } + else { + state = 'normal'; + } + return state; + }, + + _updateColorTransition: function (state) { + var color = this[state + 'Color']; + var target = this.target; + + if (CC_EDITOR) { + target.color = color; + } + else { + this._fromColor = target.color.clone(); + this._toColor = color; + this.time = 0; + this._transitionFinished = false; + } + }, + + _updateSpriteTransition: function (state) { + var sprite = this[state + 'Sprite']; + if(this._sprite && sprite) { + this._sprite.spriteFrame = sprite; + } + }, + + _updateScaleTransition: function (state) { + if(state === 'pressed') { + this._zoomUp(); + } else { + this._zoomBack(); + } + }, + + _applyTransition: function (state) { + var transition = this.transition; if (transition === Transition.COLOR) { - var target = this.target; - - if (CC_EDITOR) { - target.color = color; - } - else { - this._fromColor = target.color.clone(); - this._toColor = color; - this.time = 0; - this._transitionFinished = false; - } + this._updateColorTransition(state); } - else if (transition === Transition.SPRITE && this._sprite && sprite) { - this._sprite.spriteFrame = sprite; + else if (transition === Transition.SPRITE) { + this._updateSpriteTransition(state); + } else if(transition === Transition.SCALE) { + this._updateScaleTransition(state); } }, From 4b7066f76c8d4427fa9b8c2e1ac26953fea7804b Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 8 Nov 2016 17:41:12 +0800 Subject: [PATCH 3/4] improve scale transition touch move --- cocos2d/core/components/CCButton.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index d95e5ef2d4..6a207010b0 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -482,8 +482,12 @@ var Button = cc.Class({ if(this.transition === Transition.SCALE && this.target) { if(hit) { - this.target.scale = this._originalScale * this.zoomScale; + this._fromScale = this._originalScale; + this._toScale = this._originalScale * this.zoomScale; + this._transitionFinished = false; } else { + this.time = 0; + this._transitionFinished = true; this.target.scale = this._originalScale; } } else { @@ -625,8 +629,7 @@ var Button = cc.Class({ if (transition === Transition.COLOR) { this._updateColorTransition(state); - } - else if (transition === Transition.SPRITE) { + } else if (transition === Transition.SPRITE) { this._updateSpriteTransition(state); } else if(transition === Transition.SCALE) { this._updateScaleTransition(state); From a22b669d021abfc7bf5280197a3ccd4fe2bf80ea Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 8 Nov 2016 17:43:54 +0800 Subject: [PATCH 4/4] remove unused logic --- cocos2d/core/components/CCButton.js | 1 - 1 file changed, 1 deletion(-) diff --git a/cocos2d/core/components/CCButton.js b/cocos2d/core/components/CCButton.js index 6a207010b0..1b0309d1a8 100644 --- a/cocos2d/core/components/CCButton.js +++ b/cocos2d/core/components/CCButton.js @@ -416,7 +416,6 @@ var Button = cc.Class({ update: function (dt) { var target = this.target; - if(!target) return; if(this._transitionFinished) return; if (this.transition !== Transition.COLOR && this.transition !== Transition.SCALE) return;