From df2ebfbc9c26c647291477edae9c1011052f5c70 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 9 Dec 2015 14:21:26 +0800 Subject: [PATCH] change uibutton nons9 to simple rendering mode --- cocos/ui/UIButton.cpp | 145 ++++-------------- cocos/ui/UIButton.h | 5 - cocos/ui/UIScale9Sprite.cpp | 17 +- cocos/ui/UIScale9Sprite.h | 1 - .../UIButtonTest/UIButtonTest.cpp | 18 ++- 5 files changed, 50 insertions(+), 136 deletions(-) diff --git a/cocos/ui/UIButton.cpp b/cocos/ui/UIButton.cpp index abc7648240..c877a70eac 100644 --- a/cocos/ui/UIButton.cpp +++ b/cocos/ui/UIButton.cpp @@ -66,10 +66,6 @@ _capInsetsDisabled(Rect::ZERO), _normalTextureSize(_contentSize), _pressedTextureSize(_contentSize), _disabledTextureSize(_contentSize), -_normalTextureScaleXInSize(1.0f), -_normalTextureScaleYInSize(1.0f), -_pressedTextureScaleXInSize(1.0f), -_pressedTextureScaleYInSize(1.0f), _normalTextureLoaded(false), _pressedTextureLoaded(false), _disabledTextureLoaded(false), @@ -146,9 +142,9 @@ void Button::initRenderer() _buttonNormalRenderer = Scale9Sprite::create(); _buttonClickedRenderer = Scale9Sprite::create(); _buttonDisabledRenderer = Scale9Sprite::create(); - _buttonClickedRenderer->setScale9Enabled(false); - _buttonNormalRenderer->setScale9Enabled(false); - _buttonDisabledRenderer->setScale9Enabled(false); + _buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); + _buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); + _buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1); addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1); @@ -171,9 +167,16 @@ void Button::setScale9Enabled(bool able) _scale9Enabled = able; - _buttonNormalRenderer->setScale9Enabled(_scale9Enabled); - _buttonClickedRenderer->setScale9Enabled(_scale9Enabled); - _buttonDisabledRenderer->setScale9Enabled(_scale9Enabled); + if (_scale9Enabled) { + _buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE); + _buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE); + _buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SLICE); + }else{ + _buttonNormalRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); + _buttonClickedRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); + _buttonDisabledRenderer->setRenderingType(Scale9Sprite::RenderingType::SIMPLE); + } + if (_scale9Enabled) { @@ -252,6 +255,10 @@ void Button::loadTextureNormal(const std::string& normal,TextureResType texType) break; } } + //FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249 + if (!_ignoreSize) { + _customSize = _buttonNormalRenderer->getContentSize(); + } this->setupNormalTexture(textureLoaded); } @@ -443,8 +450,8 @@ void Button::onPressStateChangedToNormal() // Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, _normalTextureScaleXInSize, _normalTextureScaleYInSize); //fixme: the zoomAction will run in the next frame which will cause the _buttonNormalRenderer to a wrong scale - _buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize); - _buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize); + _buttonNormalRenderer->setScale(1.0); + _buttonClickedRenderer->setScale(1.0); if(nullptr != _titleRenderer) { @@ -465,7 +472,7 @@ void Button::onPressStateChangedToNormal() else { _buttonNormalRenderer->stopAllActions(); - _buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize); + _buttonNormalRenderer->setScale(1.0); if(nullptr != _titleRenderer) { @@ -493,12 +500,12 @@ void Button::onPressStateChangedToPressed() _buttonClickedRenderer->stopAllActions(); Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, - _pressedTextureScaleXInSize + _zoomScale, - _pressedTextureScaleYInSize + _zoomScale); + 1.0 + _zoomScale, + 1.0 + _zoomScale); _buttonClickedRenderer->runAction(zoomAction); - _buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + _zoomScale, - _pressedTextureScaleYInSize + _zoomScale); + _buttonNormalRenderer->setScale(1.0 + _zoomScale, + 1.0 + _zoomScale); if(nullptr != _titleRenderer) { @@ -516,7 +523,7 @@ void Button::onPressStateChangedToPressed() _buttonDisabledRenderer->setVisible(false); _buttonNormalRenderer->stopAllActions(); - _buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale); + _buttonNormalRenderer->setScale(1.0 +_zoomScale, 1.0 + _zoomScale); if(nullptr != _titleRenderer) { @@ -544,8 +551,8 @@ void Button::onPressStateChangedToDisabled() } _buttonClickedRenderer->setVisible(false); - _buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize); - _buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize); + _buttonNormalRenderer->setScale(1.0); + _buttonClickedRenderer->setScale(1.0); } void Button::updateTitleLocation() @@ -649,112 +656,22 @@ Node* Button::getVirtualRenderer() void Button::normalTextureScaleChangedWithSize() { - - if (_ignoreSize && !_unifySize) - { - if (!_scale9Enabled) - { - _buttonNormalRenderer->setScale(1.0f); - _normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f; - } - } - else - { - if (_scale9Enabled) - { - _buttonNormalRenderer->setPreferredSize(_contentSize); - _normalTextureScaleXInSize = _normalTextureScaleYInSize = 1.0f; - _buttonNormalRenderer->setScale(_normalTextureScaleXInSize,_normalTextureScaleYInSize); - } - else - { - Size textureSize = _normalTextureSize; - if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) - { - _buttonNormalRenderer->setScale(1.0f); - return; - } - float scaleX = _contentSize.width / textureSize.width; - float scaleY = _contentSize.height / textureSize.height; - _buttonNormalRenderer->setScaleX(scaleX); - _buttonNormalRenderer->setScaleY(scaleY); - _normalTextureScaleXInSize = scaleX; - _normalTextureScaleYInSize = scaleY; - } - } + _buttonNormalRenderer->setPreferredSize(_contentSize); _buttonNormalRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f); } void Button::pressedTextureScaleChangedWithSize() { + _buttonClickedRenderer->setPreferredSize(_contentSize); - if (_ignoreSize && !_unifySize) - { - if (!_scale9Enabled) - { - _buttonClickedRenderer->setScale(1.0f); - _pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f; - } - } - else - { - if (_scale9Enabled) - { - _buttonClickedRenderer->setPreferredSize(_contentSize); - _pressedTextureScaleXInSize = _pressedTextureScaleYInSize = 1.0f; - _buttonClickedRenderer->setScale(_pressedTextureScaleXInSize,_pressedTextureScaleYInSize); - } - else - { - Size textureSize = _pressedTextureSize; - if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) - { - _buttonClickedRenderer->setScale(1.0f); - return; - } - float scaleX = _contentSize.width / _pressedTextureSize.width; - float scaleY = _contentSize.height / _pressedTextureSize.height; - _buttonClickedRenderer->setScaleX(scaleX); - _buttonClickedRenderer->setScaleY(scaleY); - _pressedTextureScaleXInSize = scaleX; - _pressedTextureScaleYInSize = scaleY; - } - } _buttonClickedRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f); } void Button::disabledTextureScaleChangedWithSize() { - - if (_ignoreSize && !_unifySize) - { - if (!_scale9Enabled) - { - _buttonDisabledRenderer->setScale(1.0f); - } - } - else - { - if (_scale9Enabled) - { - _buttonDisabledRenderer->setScale(1.0); - _buttonDisabledRenderer->setPreferredSize(_contentSize); - } - else - { - Size textureSize = _disabledTextureSize; - if (textureSize.width <= 0.0f || textureSize.height <= 0.0f) - { - _buttonDisabledRenderer->setScale(1.0f); - return; - } - float scaleX = _contentSize.width / _disabledTextureSize.width; - float scaleY = _contentSize.height / _disabledTextureSize.height; - _buttonDisabledRenderer->setScaleX(scaleX); - _buttonDisabledRenderer->setScaleY(scaleY); - } - } + _buttonDisabledRenderer->setPreferredSize(_contentSize); + _buttonDisabledRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f); } diff --git a/cocos/ui/UIButton.h b/cocos/ui/UIButton.h index 396b64d9e7..5034b1dba8 100644 --- a/cocos/ui/UIButton.h +++ b/cocos/ui/UIButton.h @@ -360,11 +360,6 @@ protected: Size _pressedTextureSize; Size _disabledTextureSize; - float _normalTextureScaleXInSize; - float _normalTextureScaleYInSize; - float _pressedTextureScaleXInSize; - float _pressedTextureScaleYInSize; - bool _normalTextureLoaded; bool _pressedTextureLoaded; bool _disabledTextureLoaded; diff --git a/cocos/ui/UIScale9Sprite.cpp b/cocos/ui/UIScale9Sprite.cpp index 0dbab02b8e..91454b8058 100644 --- a/cocos/ui/UIScale9Sprite.cpp +++ b/cocos/ui/UIScale9Sprite.cpp @@ -39,8 +39,7 @@ NS_CC_BEGIN namespace ui { Scale9Sprite::Scale9Sprite() - : _spritesGenerated(false) - , _spriteFrameRotated(false) + : _spriteFrameRotated(false) , _scale9Image(nullptr) , _scale9Enabled(true) , _insetLeft(0) @@ -401,10 +400,6 @@ namespace ui { const Size &originalSize, const Rect& capInsets) { - - GLubyte opacity = getOpacity(); - Color3B color = getColor(); - // Release old sprites this->cleanupSlicedSprites(); @@ -477,15 +472,7 @@ namespace ui { size.height = size.height - 2; } this->setContentSize(size); - - if (_spritesGenerated) - { - // Restore color and opacity - this->setOpacity(opacity); - this->setColor(color); - } - _spritesGenerated = true; - + return true; } diff --git a/cocos/ui/UIScale9Sprite.h b/cocos/ui/UIScale9Sprite.h index 29b6c8602f..7a36a5f1de 100644 --- a/cocos/ui/UIScale9Sprite.h +++ b/cocos/ui/UIScale9Sprite.h @@ -687,7 +687,6 @@ namespace ui { TrianglesCommand::Triangles calculateTriangles(const std::vector& uv, const std::vector& vertices); - bool _spritesGenerated; Rect _spriteRect; bool _spriteFrameRotated; Rect _capInsetsInternal; diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp index 3e170f4716..8a32353e17 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest.cpp @@ -1089,7 +1089,23 @@ bool UIButtonCloneTest::init() buttonCopy->setPosition(Vec2(widgetSize.width / 2.0f + 80, widgetSize.height / 2.0f + 40)); this->addChild(buttonCopy); - + + + + auto buttonScale9Copy = (Button*)button->clone(); + buttonScale9Copy->setPosition(button->getPosition() + Vec2(0, -60)); + buttonScale9Copy->setScale9Enabled(true); + buttonScale9Copy->setContentSize(button->getContentSize() * 1.5); + this->addChild(buttonScale9Copy); + + + auto buttonScale9Copy2 = (Button*)buttonScale9Copy->clone(); + buttonScale9Copy2->setPosition(buttonCopy->getPosition() + Vec2(0, -60)); + buttonScale9Copy2->setScale9Enabled(true); + buttonScale9Copy2->setContentSize(buttonCopy->getContentSize() * 1.5); + this->addChild(buttonScale9Copy2); + + CCASSERT(button->getTitleRenderer() == nullptr, "Original Button title render must be nullptr ");