diff --git a/cocos/ui/UIImageView.cpp b/cocos/ui/UIImageView.cpp index e35c1d355f..f9c16ccc48 100644 --- a/cocos/ui/UIImageView.cpp +++ b/cocos/ui/UIImageView.cpp @@ -105,6 +105,8 @@ bool ImageView::init(const std::string &imageFileName, TextureResType texType) void ImageView::initRenderer() { _imageRenderer = Scale9Sprite::create(); + _imageRenderer->setScale9Enabled(false); + addProtectedChild(_imageRenderer, IMAGE_RENDERER_Z, -1); } diff --git a/cocos/ui/UIScale9Sprite.cpp b/cocos/ui/UIScale9Sprite.cpp index 042a33b3da..518b710c8c 100644 --- a/cocos/ui/UIScale9Sprite.cpp +++ b/cocos/ui/UIScale9Sprite.cpp @@ -390,6 +390,25 @@ y+=ytranslate; \ this->_positionsAreDirty = true; } + void Scale9Sprite::setAnchorPoint(const cocos2d::Vec2 &anchorPoint) + { + Node::setAnchorPoint(anchorPoint); + + if (_scale9Enabled) { + for(const auto& node : _protectedChildren) + { + node->setAnchorPoint(anchorPoint); + } + } + else + { + if (_scale9Image) { + _scale9Image->setAnchorPoint(anchorPoint); + } + + } + } + void Scale9Sprite::updatePositions() { // Check that instances are non-NULL @@ -866,7 +885,8 @@ y+=ytranslate; \ void Scale9Sprite::adjustScale9ImagePosition() { if (_scale9Image) { - _scale9Image->setPosition(_scale9Image->getPosition() + Vec2(_originalSize.width/2, _originalSize.height/2)); + _scale9Image->setPosition(_scale9Image->getPosition() + Vec2(_originalSize.width * _anchorPoint.x, + _originalSize.height * _anchorPoint.y)); } } diff --git a/cocos/ui/UIScale9Sprite.h b/cocos/ui/UIScale9Sprite.h index 2080137672..8a616a35c6 100644 --- a/cocos/ui/UIScale9Sprite.h +++ b/cocos/ui/UIScale9Sprite.h @@ -246,7 +246,7 @@ namespace ui { // overrides virtual void setContentSize(const Size & size) override; - + virtual void setAnchorPoint(const Vec2& anchorPoint) override; Size getOriginalSize() const; void setPreferredSize(const Size& size); diff --git a/cocos/ui/UISlider.cpp b/cocos/ui/UISlider.cpp index 0ed75696c2..963f32e74f 100644 --- a/cocos/ui/UISlider.cpp +++ b/cocos/ui/UISlider.cpp @@ -98,20 +98,28 @@ bool Slider::init() void Slider::initRenderer() { - _barRenderer = Sprite::create(); - _progressBarRenderer = Sprite::create(); + _barRenderer = Scale9Sprite::create(); + _progressBarRenderer = Scale9Sprite::create(); + _barRenderer->setScale9Enabled(false); + _progressBarRenderer->setScale9Enabled(false); + _progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f)); + addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1); addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1); + _slidBallNormalRenderer = Sprite::create(); _slidBallPressedRenderer = Sprite::create(); _slidBallPressedRenderer->setVisible(false); _slidBallDisabledRenderer = Sprite::create(); _slidBallDisabledRenderer->setVisible(false); + _slidBallRenderer = Node::create(); + _slidBallRenderer->addChild(_slidBallNormalRenderer); _slidBallRenderer->addChild(_slidBallPressedRenderer); _slidBallRenderer->addChild(_slidBallDisabledRenderer); + addProtectedChild(_slidBallRenderer, SLIDBALL_RENDERER_Z, -1); } @@ -126,24 +134,10 @@ void Slider::loadBarTexture(const std::string& fileName, TextureResType texType) switch (_barTexType) { case TextureResType::LOCAL: - if (_scale9Enabled) - { - static_cast(_barRenderer)->initWithFile(fileName); - } - else - { - static_cast(_barRenderer)->setTexture(fileName); - } + _barRenderer->initWithFile(fileName); break; case TextureResType::PLIST: - if (_scale9Enabled) - { - static_cast(_barRenderer)->initWithSpriteFrameName(fileName); - } - else - { - static_cast(_barRenderer)->setSpriteFrame(fileName); - } + _barRenderer->initWithSpriteFrameName(fileName); break; default: break; @@ -165,24 +159,10 @@ void Slider::loadProgressBarTexture(const std::string& fileName, TextureResType switch (_progressBarTexType) { case TextureResType::LOCAL: - if (_scale9Enabled) - { - static_cast(_progressBarRenderer)->initWithFile(fileName); - } - else - { - static_cast(_progressBarRenderer)->setTexture(fileName); - } + _progressBarRenderer->initWithFile(fileName); break; case TextureResType::PLIST: - if (_scale9Enabled) - { - static_cast(_progressBarRenderer)->initWithSpriteFrameName(fileName); - } - else - { - static_cast(_progressBarRenderer)->setSpriteFrame(fileName); - } + _progressBarRenderer->initWithSpriteFrameName(fileName); break; default: break; @@ -201,24 +181,9 @@ void Slider::setScale9Enabled(bool able) } _scale9Enabled = able; - removeProtectedChild(_barRenderer); - removeProtectedChild(_progressBarRenderer); - _barRenderer = nullptr; - _progressBarRenderer = nullptr; - if (_scale9Enabled) - { - _barRenderer = Scale9Sprite::create(); - _progressBarRenderer = Scale9Sprite::create(); - } - else - { - _barRenderer = Sprite::create(); - _progressBarRenderer = Sprite::create(); - } - loadBarTexture(_textureFile, _barTexType); - loadProgressBarTexture(_progressBarTextureFile, _progressBarTexType); - addProtectedChild(_barRenderer, BASEBAR_RENDERER_Z, -1); - addProtectedChild(_progressBarRenderer, PROGRESSBAR_RENDERER_Z, -1); + _barRenderer->setScale9Enabled(_scale9Enabled); + _progressBarRenderer->setScale9Enabled(_scale9Enabled); + if (_scale9Enabled) { bool ignoreBefore = _ignoreSize; @@ -260,7 +225,7 @@ void Slider::setCapInsetsBarRenderer(const Rect &capInsets) { return; } - static_cast(_barRenderer)->setCapInsets(capInsets); + _barRenderer->setCapInsets(capInsets); } const Rect& Slider::getCapInsetsBarRenderer()const @@ -275,7 +240,7 @@ void Slider::setCapInsetProgressBarRebderer(const Rect &capInsets) { return; } - static_cast(_progressBarRenderer)->setCapInsets(capInsets); + _progressBarRenderer->setCapInsets(capInsets); } const Rect& Slider::getCapInsetsProgressBarRebderer()const @@ -369,11 +334,11 @@ void Slider::setPercent(int percent) _slidBallRenderer->setPosition(Vec2(dis, _contentSize.height / 2.0f)); if (_scale9Enabled) { - static_cast(_progressBarRenderer)->setPreferredSize(Size(dis,_progressBarTextureSize.height)); + _progressBarRenderer->setPreferredSize(Size(dis,_progressBarTextureSize.height)); } else { - Sprite* spriteRenderer = static_cast(_progressBarRenderer); + Sprite* spriteRenderer = _progressBarRenderer->getSprite(); Rect rect = spriteRenderer->getTextureRect(); rect.size.width = _progressBarTextureSize.width * res; spriteRenderer->setTextureRect(rect, spriteRenderer->isTextureRectRotated(), rect.size); @@ -497,7 +462,7 @@ void Slider::barRendererScaleChangedWithSize() _barLength = _contentSize.width; if (_scale9Enabled) { - static_cast(_barRenderer)->setPreferredSize(_contentSize); + _barRenderer->setPreferredSize(_contentSize); } else { @@ -534,7 +499,7 @@ void Slider::progressBarRendererScaleChangedWithSize() { if (_scale9Enabled) { - static_cast(_progressBarRenderer)->setPreferredSize(_contentSize); + _progressBarRenderer->setPreferredSize(_contentSize); _progressBarTextureSize = _progressBarRenderer->getContentSize(); } else diff --git a/cocos/ui/UISlider.h b/cocos/ui/UISlider.h index 8212dd9026..5c64aa32b9 100644 --- a/cocos/ui/UISlider.h +++ b/cocos/ui/UISlider.h @@ -33,7 +33,8 @@ NS_CC_BEGIN class Sprite; namespace ui { - + class Scale9Sprite; + typedef enum { SLIDER_PERCENTCHANGED @@ -226,8 +227,8 @@ protected: virtual void copySpecialProperties(Widget* model) override; virtual void adaptRenderers() override; protected: - Node* _barRenderer; - Node* _progressBarRenderer; + Scale9Sprite* _barRenderer; + Scale9Sprite* _progressBarRenderer; Size _progressBarTextureSize; Sprite* _slidBallNormalRenderer;