refactor loadingBar

This commit is contained in:
andyque 2014-07-30 16:08:29 +08:00
parent c5d2585049
commit 7bcb31f225
2 changed files with 21 additions and 50 deletions

View File

@ -81,7 +81,8 @@ LoadingBar* LoadingBar::create(const std::string &textureName, float percentage)
void LoadingBar::initRenderer() void LoadingBar::initRenderer()
{ {
_barRenderer = Sprite::create(); _barRenderer = Scale9Sprite::create();
_barRenderer->setScale9Enabled(false);
addProtectedChild(_barRenderer, BAR_RENDERER_Z, -1); addProtectedChild(_barRenderer, BAR_RENDERER_Z, -1);
_barRenderer->setAnchorPoint(Vec2(0.0,0.5)); _barRenderer->setAnchorPoint(Vec2(0.0,0.5));
} }
@ -100,17 +101,15 @@ void LoadingBar::setDirection(cocos2d::ui::LoadingBar::Direction direction)
case Direction::LEFT: case Direction::LEFT:
_barRenderer->setAnchorPoint(Vec2(0.0f,0.5f)); _barRenderer->setAnchorPoint(Vec2(0.0f,0.5f));
_barRenderer->setPosition(Vec2(-_totalLength*0.5f,0.0f)); _barRenderer->setPosition(Vec2(-_totalLength*0.5f,0.0f));
if (!_scale9Enabled) if (!_scale9Enabled) {
{ _barRenderer->setFlippedX(false);
static_cast<Sprite*>(_barRenderer)->setFlippedX(false);
} }
break; break;
case Direction::RIGHT: case Direction::RIGHT:
_barRenderer->setAnchorPoint(Vec2(1.0f,0.5f)); _barRenderer->setAnchorPoint(Vec2(1.0f,0.5f));
_barRenderer->setPosition(Vec2(_totalLength*0.5f,0.0f)); _barRenderer->setPosition(Vec2(_totalLength*0.5f,0.0f));
if (!_scale9Enabled) if (!_scale9Enabled) {
{ _barRenderer->setFlippedX(true);
static_cast<Sprite*>(_barRenderer)->setFlippedX(true);
} }
break; break;
} }
@ -134,28 +133,10 @@ void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
switch (_renderBarTexType) switch (_renderBarTexType)
{ {
case TextureResType::LOCAL: case TextureResType::LOCAL:
if (_scale9Enabled) _barRenderer->initWithFile(texture);
{
Scale9Sprite* barRendererScale9 = static_cast<Scale9Sprite*>(_barRenderer);
barRendererScale9->initWithFile(texture);
barRendererScale9->setCapInsets(_capInsets);
}
else
{
static_cast<Sprite*>(_barRenderer)->setTexture(texture);
}
break; break;
case TextureResType::PLIST: case TextureResType::PLIST:
if (_scale9Enabled) _barRenderer->initWithSpriteFrameName(texture);
{
Scale9Sprite* barRendererScale9 = static_cast<Scale9Sprite*>(_barRenderer);
barRendererScale9->initWithSpriteFrameName(texture);
barRendererScale9->setCapInsets(_capInsets);
}
else
{
static_cast<Sprite*>(_barRenderer)->setSpriteFrame(texture);
}
break; break;
default: default:
break; break;
@ -167,19 +148,19 @@ void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
{ {
case Direction::LEFT: case Direction::LEFT:
_barRenderer->setAnchorPoint(Vec2(0.0f,0.5f)); _barRenderer->setAnchorPoint(Vec2(0.0f,0.5f));
if (!_scale9Enabled) if (!_scale9Enabled) {
{ _barRenderer->setFlippedX(false);
static_cast<Sprite*>(_barRenderer)->setFlippedX(false);
} }
break; break;
case Direction::RIGHT: case Direction::RIGHT:
_barRenderer->setAnchorPoint(Vec2(1.0f,0.5f)); _barRenderer->setAnchorPoint(Vec2(1.0f,0.5f));
if (!_scale9Enabled) if (!_scale9Enabled) {
{ _barRenderer->setFlippedX(true);
static_cast<Sprite*>(_barRenderer)->setFlippedX(true);
} }
break; break;
} }
_barRenderer->setCapInsets(_capInsets);
barRendererScaleChangedWithSize(); barRendererScaleChangedWithSize();
updateContentSizeWithTextureSize(_barRendererTextureSize); updateContentSizeWithTextureSize(_barRendererTextureSize);
_barRendererAdaptDirty = true; _barRendererAdaptDirty = true;
@ -192,18 +173,8 @@ void LoadingBar::setScale9Enabled(bool enabled)
return; return;
} }
_scale9Enabled = enabled; _scale9Enabled = enabled;
removeProtectedChild(_barRenderer); _barRenderer->setScale9Enabled(_scale9Enabled);
_barRenderer = nullptr;
if (_scale9Enabled)
{
_barRenderer = Scale9Sprite::create();
}
else
{
_barRenderer = Sprite::create();
}
loadTexture(_textureFile,_renderBarTexType);
addProtectedChild(_barRenderer, BAR_RENDERER_Z, -1);
if (_scale9Enabled) if (_scale9Enabled)
{ {
bool ignoreBefore = _ignoreSize; bool ignoreBefore = _ignoreSize;
@ -230,7 +201,7 @@ void LoadingBar::setCapInsets(const Rect &capInsets)
{ {
return; return;
} }
static_cast<Scale9Sprite*>(_barRenderer)->setCapInsets(capInsets); _barRenderer->setCapInsets(capInsets);
} }
const Rect& LoadingBar::getCapInsets()const const Rect& LoadingBar::getCapInsets()const
@ -257,7 +228,7 @@ void LoadingBar::setPercent(float percent)
} }
else else
{ {
Sprite* spriteRenderer = static_cast<Sprite*>(_barRenderer); Sprite* spriteRenderer = _barRenderer->getSprite();
Rect rect = spriteRenderer->getTextureRect(); Rect rect = spriteRenderer->getTextureRect();
rect.size.width = _barRendererTextureSize.width * res; rect.size.width = _barRendererTextureSize.width * res;
spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size); spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size);
@ -351,7 +322,7 @@ void LoadingBar::barRendererScaleChangedWithSize()
void LoadingBar::setScale9Scale() void LoadingBar::setScale9Scale()
{ {
float width = (float)(_percent) / 100.0f * _totalLength; float width = (float)(_percent) / 100.0f * _totalLength;
static_cast<Scale9Sprite*>(_barRenderer)->setPreferredSize(Size(width, _contentSize.height)); _barRenderer->setPreferredSize(Size(width, _contentSize.height));
} }
std::string LoadingBar::getDescription() const std::string LoadingBar::getDescription() const

View File

@ -31,7 +31,7 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
namespace ui { namespace ui {
class Scale9Sprite;
/** /**
* @js NA * @js NA
* @lua NA * @lua NA
@ -154,7 +154,7 @@ protected:
Direction _direction; Direction _direction;
float _percent; float _percent;
float _totalLength; float _totalLength;
Node* _barRenderer; Scale9Sprite* _barRenderer;
TextureResType _renderBarTexType; TextureResType _renderBarTexType;
Size _barRendererTextureSize; Size _barRendererTextureSize;
bool _scale9Enabled; bool _scale9Enabled;