From a0bf2591067b8e7aa5fd8265bf267e348053871e Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 17 Feb 2016 19:41:46 +0800 Subject: [PATCH] add radial mode to progressbar fix https://github.com/cocos-creator/fireball/issues/1888 --- cocos2d/core/components/CCProgressBar.js | 43 +++++++++++++++++++----- cocos2d/core/components/CCSprite.js | 10 ++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/components/CCProgressBar.js b/cocos2d/core/components/CCProgressBar.js index 80fc352c66..5a4376e794 100644 --- a/cocos2d/core/components/CCProgressBar.js +++ b/cocos2d/core/components/CCProgressBar.js @@ -36,7 +36,11 @@ var Mode = cc.Enum({ /** * @property {Number} VERTICAL */ - VERTICAL: 1 + VERTICAL: 1, + /** + * @property {Number} RADIAL + */ + RADIAL: 2, }); /** @@ -55,6 +59,7 @@ var ProgressBar = cc.Class({ _initBarSprite: function() { if (this.barSprite) { var entity = this.barSprite.node; + if (!entity) return; var nodeSize = this.node.getContentSize(); var nodeAnchor = this.node.getAnchorPoint(); @@ -65,12 +70,20 @@ var ProgressBar = cc.Class({ this.node.setContentSize(entitySize); } + if (this.barSprite.fillType === cc.Sprite.FillType.RADIAL) { + this.mode = Mode.RADIAL; + } + var barSpriteSize = entity.getContentSize(); if (this.mode === Mode.HORIZONTAL) { this.totalLength = barSpriteSize.width; - } else { + } + else if(this.mode === Mode.VERTICAL) { this.totalLength = barSpriteSize.height; } + else { + this.totalLength = this.barSprite.fillRange; + } if(entity.parent === this.node){ var x = - nodeSize.width * nodeAnchor.x; @@ -83,6 +96,9 @@ var ProgressBar = cc.Class({ _updateBarStatus: function() { if (this.barSprite) { var entity = this.barSprite.node; + + if (!entity) return; + var entityAnchorPoint = entity.getAnchorPoint(); var entitySize = entity.getContentSize(); var entityPosition = entity.getPosition(); @@ -110,17 +126,24 @@ var ProgressBar = cc.Class({ finalContentSize = cc.size(entitySize.width, actualLenth); totalWidth = entitySize.width; totalHeight = this.totalLength; + break; + case Mode.RADIAL: + this.barSprite.fillRange = actualLenth; + break; } - var anchorOffsetX = anchorPoint.x - entityAnchorPoint.x; - var anchorOffsetY = anchorPoint.y - entityAnchorPoint.y; + if (this.barSprite.type !== cc.Sprite.Type.FILLED) { - var finalPosition = cc.p(totalWidth * anchorOffsetX, totalHeight * anchorOffsetY); + var anchorOffsetX = anchorPoint.x - entityAnchorPoint.x; + var anchorOffsetY = anchorPoint.y - entityAnchorPoint.y; + var finalPosition = cc.p(totalWidth * anchorOffsetX, totalHeight * anchorOffsetY); - entity.setPosition(cc.pAdd(entityPosition, finalPosition)); + entity.setPosition(cc.pAdd(entityPosition, finalPosition)); + + entity.setAnchorPoint(anchorPoint); + entity.setContentSize(finalContentSize); + } - entity.setAnchorPoint(anchorPoint); - entity.setContentSize(finalContentSize); } }, @@ -151,11 +174,15 @@ var ProgressBar = cc.Class({ notify: function() { if (this.barSprite) { var entity = this.barSprite.node; + if (!entity) return; + var entitySize = entity.getContentSize(); if (this.mode === Mode.HORIZONTAL) { this.totalLength = entitySize.width; } else if (this.mode === Mode.VERTICAL) { this.totalLength = entitySize.height; + } else if (this.mode === Mode.RADIAL) { + this.totalLength = this.barSprite.fillRange; } } }, diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 75479284bb..280b12caaa 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -170,7 +170,7 @@ var Sprite = cc.Class({ return this._fillStart; }, set: function(value) { - this._fillStart = value; + this._fillStart = cc.clamp01(value); this._sgNode && this._sgNode.setFillStart(value); }, }, @@ -180,7 +180,7 @@ var Sprite = cc.Class({ return this._fillRange; }, set: function(value) { - this._fillRange = value; + this._fillRange = cc.clamp01(value); this._sgNode && this._sgNode.setFillRange(value); }, }, @@ -236,6 +236,12 @@ var Sprite = cc.Class({ } }, + statics: { + FillType: FillType, + Type: SpriteType, + SizeMode: SizeMode, + }, + /** * Sets whether the sprite is visible or not. * @method setVisible