loading bar use new scale9 feature

This commit is contained in:
zilongshanren 2015-12-09 10:41:31 +08:00
parent ea5dacedd0
commit a671bf7ce9
2 changed files with 9 additions and 43 deletions

View File

@ -92,7 +92,7 @@ LoadingBar* LoadingBar::create(const std::string &textureName,
void LoadingBar::initRenderer()
{
_barRenderer = Scale9Sprite::create();
_barRenderer->setScale9Enabled(false);
_barRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
addProtectedChild(_barRenderer, BAR_RENDERER_Z, -1);
_barRenderer->setAnchorPoint(Vec2(0.0,0.5));
}
@ -216,7 +216,11 @@ void LoadingBar::setScale9Enabled(bool enabled)
return;
}
_scale9Enabled = enabled;
_barRenderer->setScale9Enabled(_scale9Enabled);
if (_scale9Enabled) {
_barRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE);
}else{
_barRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
}
if (_scale9Enabled)
{
@ -279,21 +283,8 @@ void LoadingBar::setPercent(float percent)
void LoadingBar::updateProgressBar()
{
if (_scale9Enabled)
{
setScale9Scale();
}
else
{
Sprite* innerSprite = _barRenderer->getSprite();
if (nullptr != innerSprite)
{
float res = _percent / 100.0f;
Rect rect = innerSprite->getTextureRect();
rect.size.width = _barRendererTextureSize.width * res;
innerSprite->setTextureRect(rect, innerSprite->isTextureRectRotated(), rect.size);
}
}
float width = (float)(_percent) / 100.0f * _totalLength;
_barRenderer->setPreferredSize(Size(width, _contentSize.height));
}
float LoadingBar::getPercent() const
@ -354,25 +345,7 @@ void LoadingBar::barRendererScaleChangedWithSize()
else
{
_totalLength = _contentSize.width;
if (_scale9Enabled)
{
this->setScale9Scale();
_barRenderer->setScale(1.0f);
}
else
{
Size textureSize = _barRendererTextureSize;
if (textureSize.width <= 0.0f || textureSize.height <= 0.0f)
{
_barRenderer->setScale(1.0f);
return;
}
float scaleX = _contentSize.width / textureSize.width;
float scaleY = _contentSize.height / textureSize.height;
_barRenderer->setScaleX(scaleX);
_barRenderer->setScaleY(scaleY);
}
this->updateProgressBar();
}
switch (_direction)
{
@ -387,12 +360,6 @@ void LoadingBar::barRendererScaleChangedWithSize()
}
}
void LoadingBar::setScale9Scale()
{
float width = (float)(_percent) / 100.0f * _totalLength;
_barRenderer->setPreferredSize(Size(width, _contentSize.height));
}
std::string LoadingBar::getDescription() const
{
return "LoadingBar";

View File

@ -180,7 +180,6 @@ protected:
virtual void initRenderer() override;
virtual void onSizeChanged() override;
void setScale9Scale();
void updateProgressBar();
void barRendererScaleChangedWithSize();