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

View File

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