From ab77e8c5207813da7a8eeb7806e080d97412fdcf Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Tue, 22 Dec 2015 19:53:24 +0800 Subject: [PATCH] Revert "loading bar use new scale9 feature" This reverts commit a671bf7ce9aea6c94a532740f9df470eeaaa7507. --- cocos/ui/UILoadingBar.cpp | 51 ++++++++++++++++++++++++++++++++------- cocos/ui/UILoadingBar.h | 1 + 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/cocos/ui/UILoadingBar.cpp b/cocos/ui/UILoadingBar.cpp index b232697d92..4b47a0a578 100644 --- a/cocos/ui/UILoadingBar.cpp +++ b/cocos/ui/UILoadingBar.cpp @@ -92,7 +92,7 @@ LoadingBar* LoadingBar::create(const std::string &textureName, void LoadingBar::initRenderer() { _barRenderer = Scale9Sprite::create(); - _barRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); + _barRenderer->setScale9Enabled(false); addProtectedChild(_barRenderer, BAR_RENDERER_Z, -1); _barRenderer->setAnchorPoint(Vec2(0.0,0.5)); } @@ -190,11 +190,7 @@ void LoadingBar::setScale9Enabled(bool enabled) return; } _scale9Enabled = enabled; - if (_scale9Enabled) { - _barRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE); - }else{ - _barRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); - } + _barRenderer->setScale9Enabled(_scale9Enabled); if (_scale9Enabled) { @@ -257,8 +253,21 @@ void LoadingBar::setPercent(float percent) void LoadingBar::updateProgressBar() { - float width = (float)(_percent) / 100.0f * _totalLength; - _barRenderer->setPreferredSize(Size(width, _contentSize.height)); + 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 LoadingBar::getPercent() const @@ -319,7 +328,25 @@ void LoadingBar::barRendererScaleChangedWithSize() else { _totalLength = _contentSize.width; - this->updateProgressBar(); + 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); + } } switch (_direction) { @@ -334,6 +361,12 @@ 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"; diff --git a/cocos/ui/UILoadingBar.h b/cocos/ui/UILoadingBar.h index c87249aef6..5d02229e9e 100644 --- a/cocos/ui/UILoadingBar.h +++ b/cocos/ui/UILoadingBar.h @@ -180,6 +180,7 @@ protected: virtual void initRenderer() override; virtual void onSizeChanged() override; + void setScale9Scale(); void updateProgressBar(); void barRendererScaleChangedWithSize();