refactor UIButton

This commit is contained in:
andyque 2014-07-30 15:19:39 +08:00
parent 4a40604955
commit a6e376f93c
4 changed files with 150 additions and 195 deletions

View File

@ -135,9 +135,13 @@ bool Button::init()
void Button::initRenderer()
{
_buttonNormalRenderer = Sprite::create();
_buttonClickedRenderer = Sprite::create();
_buttonDisableRenderer = Sprite::create();
_buttonNormalRenderer = Scale9Sprite::create();
_buttonClickedRenderer = Scale9Sprite::create();
_buttonDisableRenderer = Scale9Sprite::create();
_buttonClickedRenderer->setScale9Enabled(false);
_buttonNormalRenderer->setScale9Enabled(false);
_buttonDisableRenderer->setScale9Enabled(false);
_titleRenderer = Label::create();
_titleRenderer->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
@ -153,33 +157,13 @@ void Button::setScale9Enabled(bool able)
{
return;
}
_brightStyle = BrightStyle::NONE;
_scale9Enabled = able;
removeProtectedChild(_buttonNormalRenderer);
removeProtectedChild(_buttonClickedRenderer);
removeProtectedChild(_buttonDisableRenderer);
_buttonNormalRenderer = nullptr;
_buttonClickedRenderer = nullptr;
_buttonDisableRenderer = nullptr;
if (_scale9Enabled)
{
_buttonNormalRenderer = Scale9Sprite::create();
_buttonClickedRenderer = Scale9Sprite::create();
_buttonDisableRenderer = Scale9Sprite::create();
}
else
{
_buttonNormalRenderer = Sprite::create();
_buttonClickedRenderer = Sprite::create();
_buttonDisableRenderer = Sprite::create();
}
loadTextureNormal(_normalFileName, _normalTexType);
loadTexturePressed(_clickedFileName, _pressedTexType);
loadTextureDisabled(_disabledFileName, _disabledTexType);
addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
addProtectedChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1);
_buttonNormalRenderer->setScale9Enabled(_scale9Enabled);
_buttonClickedRenderer->setScale9Enabled(_scale9Enabled);
_buttonDisableRenderer->setScale9Enabled(_scale9Enabled);
if (_scale9Enabled)
{
bool ignoreBefore = _ignoreSize;
@ -190,9 +174,12 @@ void Button::setScale9Enabled(bool able)
{
ignoreContentAdaptWithSize(_prevIgnoreSize);
}
setCapInsetsNormalRenderer(_capInsetsNormal);
setCapInsetsPressedRenderer(_capInsetsPressed);
setCapInsetsDisabledRenderer(_capInsetsDisabled);
_brightStyle = BrightStyle::NONE;
setBright(_bright);
}
@ -228,37 +215,19 @@ void Button::loadTextureNormal(const std::string& normal,TextureResType texType)
}
_normalFileName = normal;
_normalTexType = texType;
if (_scale9Enabled)
switch (_normalTexType)
{
Scale9Sprite* normalRendererScale9 = static_cast<Scale9Sprite*>(_buttonNormalRenderer);
switch (_normalTexType)
{
case TextureResType::LOCAL:
normalRendererScale9->initWithFile(normal);
break;
case TextureResType::PLIST:
normalRendererScale9->initWithSpriteFrameName(normal);
break;
default:
break;
}
normalRendererScale9->setCapInsets(_capInsetsNormal);
}
else
{
Sprite* normalRenderer = static_cast<Sprite*>(_buttonNormalRenderer);
switch (_normalTexType)
{
case TextureResType::LOCAL:
normalRenderer->setTexture(normal);
break;
case TextureResType::PLIST:
normalRenderer->setSpriteFrame(normal);
break;
default:
break;
}
case TextureResType::LOCAL:
_buttonNormalRenderer->initWithFile(normal);
break;
case TextureResType::PLIST:
_buttonNormalRenderer->initWithSpriteFrameName(normal);
break;
default:
break;
}
_normalTextureSize = _buttonNormalRenderer->getContentSize();
updateFlippedX();
updateFlippedY();
@ -276,38 +245,21 @@ void Button::loadTexturePressed(const std::string& selected,TextureResType texTy
}
_clickedFileName = selected;
_pressedTexType = texType;
if (_scale9Enabled)
switch (_pressedTexType)
{
Scale9Sprite* clickedRendererScale9 = static_cast<Scale9Sprite*>(_buttonClickedRenderer);
switch (_pressedTexType)
{
case TextureResType::LOCAL:
clickedRendererScale9->initWithFile(selected);
break;
case TextureResType::PLIST:
clickedRendererScale9->initWithSpriteFrameName(selected);
break;
default:
break;
}
clickedRendererScale9->setCapInsets(_capInsetsPressed);
}
else
{
Sprite* clickedRenderer = static_cast<Sprite*>(_buttonClickedRenderer);
switch (_pressedTexType)
{
case TextureResType::LOCAL:
clickedRenderer->setTexture(selected);
break;
case TextureResType::PLIST:
clickedRenderer->setSpriteFrame(selected);
break;
default:
break;
}
case TextureResType::LOCAL:
_buttonClickedRenderer->initWithFile(selected);
break;
case TextureResType::PLIST:
_buttonClickedRenderer->initWithSpriteFrameName(selected);
break;
default:
break;
}
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
//TODO: mark as dirty
updateFlippedX();
updateFlippedY();
@ -323,37 +275,19 @@ void Button::loadTextureDisabled(const std::string& disabled,TextureResType texT
}
_disabledFileName = disabled;
_disabledTexType = texType;
if (_scale9Enabled)
switch (_disabledTexType)
{
Scale9Sprite* disabledScale9 = static_cast<Scale9Sprite*>(_buttonDisableRenderer);
switch (_disabledTexType)
{
case TextureResType::LOCAL:
disabledScale9->initWithFile(disabled);
break;
case TextureResType::PLIST:
disabledScale9->initWithSpriteFrameName(disabled);
break;
default:
break;
}
disabledScale9->setCapInsets(_capInsetsDisabled);
}
else
{
Sprite* disabledRenderer = static_cast<Sprite*>(_buttonDisableRenderer);
switch (_disabledTexType)
{
case TextureResType::LOCAL:
disabledRenderer->setTexture(disabled);
break;
case TextureResType::PLIST:
disabledRenderer->setSpriteFrame(disabled);
break;
default:
break;
}
case TextureResType::LOCAL:
_buttonDisableRenderer->initWithFile(disabled);
break;
case TextureResType::PLIST:
_buttonDisableRenderer->initWithSpriteFrameName(disabled);
break;
default:
break;
}
_disabledTextureSize = _buttonDisableRenderer->getContentSize();
updateFlippedX();
updateFlippedY();
@ -376,7 +310,7 @@ void Button::setCapInsetsNormalRenderer(const Rect &capInsets)
{
return;
}
static_cast<Scale9Sprite*>(_buttonNormalRenderer)->setCapInsets(capInsets);
_buttonNormalRenderer->setCapInsets(capInsets);
}
const Rect& Button::getCapInsetsNormalRenderer()const
@ -391,7 +325,7 @@ void Button::setCapInsetsPressedRenderer(const Rect &capInsets)
{
return;
}
static_cast<Scale9Sprite*>(_buttonClickedRenderer)->setCapInsets(capInsets);
_buttonClickedRenderer->setCapInsets(capInsets);
}
const Rect& Button::getCapInsetsPressedRenderer()const
@ -406,7 +340,7 @@ void Button::setCapInsetsDisabledRenderer(const Rect &capInsets)
{
return;
}
static_cast<Scale9Sprite*>(_buttonDisableRenderer)->setCapInsets(capInsets);
_buttonDisableRenderer->setCapInsets(capInsets);
}
const Rect& Button::getCapInsetsDisabledRenderer()const
@ -432,28 +366,10 @@ void Button::onPressStateChangedToNormal()
}
else
{
if (_scale9Enabled)
{
updateTexturesRGBA();
}
else
{
_buttonNormalRenderer->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
}
_buttonNormalRenderer->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
}
}
void Button::updateTexturesRGBA()
{
_buttonNormalRenderer->setColor(this->getColor());
_buttonClickedRenderer->setColor(this->getColor());
_buttonDisableRenderer->setColor(this->getColor());
_buttonNormalRenderer->setOpacity(this->getOpacity());
_buttonClickedRenderer->setOpacity(this->getOpacity());
_buttonDisableRenderer->setOpacity(this->getOpacity());
}
void Button::onPressStateChangedToPressed()
{
@ -462,6 +378,7 @@ void Button::onPressStateChangedToPressed()
_buttonNormalRenderer->setVisible(false);
_buttonClickedRenderer->setVisible(true);
_buttonDisableRenderer->setVisible(false);
if (_pressedActionEnabled)
{
_buttonNormalRenderer->stopAllActions();
@ -501,36 +418,19 @@ void Button::updateFlippedX()
{
float flip = _flippedX ? -1.0f : 1.0f;
_titleRenderer->setScaleX(flip);
if (_scale9Enabled)
{
_buttonNormalRenderer->setScaleX(flip);
_buttonClickedRenderer->setScaleX(flip);
_buttonDisableRenderer->setScaleX(flip);
}
else
{
static_cast<Sprite*>(_buttonNormalRenderer)->setFlippedX(_flippedX);
static_cast<Sprite*>(_buttonClickedRenderer)->setFlippedX(_flippedX);
static_cast<Sprite*>(_buttonDisableRenderer)->setFlippedX(_flippedX);
}
_buttonNormalRenderer->setFlippedX(_flippedX);
_buttonClickedRenderer->setFlippedX(_flippedX);
_buttonDisableRenderer->setFlippedX(_flippedX);
}
void Button::updateFlippedY()
{
float flip = _flippedY ? -1.0f : 1.0f;
_titleRenderer->setScaleY(flip);
if (_scale9Enabled)
{
_buttonNormalRenderer->setScaleY(flip);
_buttonClickedRenderer->setScaleY(flip);
_buttonDisableRenderer->setScaleY(flip);
}
else
{
static_cast<Sprite*>(_buttonNormalRenderer)->setFlippedY(_flippedY);
static_cast<Sprite*>(_buttonClickedRenderer)->setFlippedY(_flippedY);
static_cast<Sprite*>(_buttonDisableRenderer)->setFlippedY(_flippedY);
}
_buttonNormalRenderer->setFlippedY(_flippedY);
_buttonClickedRenderer->setFlippedY(_flippedY);
_buttonDisableRenderer->setFlippedY(_flippedY);
}
void Button::updateTitleLocation()

View File

@ -33,7 +33,8 @@ NS_CC_BEGIN
class Label;
namespace ui{
class Scale9Sprite;
/**
* @js NA
* @lua NA
@ -205,9 +206,7 @@ protected:
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
void updateTexturesRGBA();
void normalTextureScaleChangedWithSize();
void pressedTextureScaleChangedWithSize();
void disabledTextureScaleChangedWithSize();
@ -219,10 +218,11 @@ protected:
virtual void copySpecialProperties(Widget* model) override;
protected:
Node* _buttonNormalRenderer;
Node* _buttonClickedRenderer;
Node* _buttonDisableRenderer;
Scale9Sprite* _buttonNormalRenderer;
Scale9Sprite* _buttonClickedRenderer;
Scale9Sprite* _buttonDisableRenderer;
Label* _titleRenderer;
std::string _normalFileName;
std::string _clickedFileName;
std::string _disabledFileName;

View File

@ -62,6 +62,49 @@ namespace ui {
void Scale9Sprite::cleanupSlicedSprites()
{
if (_topLeft)
{
_topLeft->onExit();
}
if (_top)
{
_top->onExit();
}
if (_topRight)
{
_topRight->onExit();
}
if (_left)
{
_left->onExit();
}
if (_centre)
{
_centre->onExit();
}
if (_right)
{
_right->onExit();
}
if (_bottomLeft)
{
_bottomLeft->onExit();
}
if (_bottomRight)
{
_bottomRight->onExit();
}
if (_bottom)
{
_bottom->onExit();
}
CC_SAFE_RELEASE(_topLeft);
CC_SAFE_RELEASE(_top);
CC_SAFE_RELEASE(_topRight);
@ -722,10 +765,6 @@ y+=ytranslate; \
}
}
//
// draw self
//
@ -744,7 +783,6 @@ y+=ytranslate; \
}
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
(*it)->visit(renderer, _modelViewTransform, flags);
@ -882,21 +920,22 @@ y+=ytranslate; \
_displayedColor.b = _realColor.b * parentColor.b/255.0;
updateColor();
if (_scale9Image) {
if (_scale9Image)
{
_scale9Image->updateDisplayedColor(_displayedColor);
}
for(const auto &child : _protectedChildren){
for(const auto &child : _protectedChildren)
{
child->updateDisplayedColor(_displayedColor);
}
if (_cascadeColorEnabled)
{
for(const auto &child : _children){
for(const auto &child : _children)
{
child->updateDisplayedColor(_displayedColor);
}
}
}
@ -905,33 +944,37 @@ y+=ytranslate; \
_displayedOpacity = _realOpacity * parentOpacity/255.0;
updateColor();
if (_scale9Image) {
if (_scale9Image)
{
_scale9Image->updateDisplayedOpacity(_displayedOpacity);
}
for(auto child : _protectedChildren){
for(auto child : _protectedChildren)
{
child->updateDisplayedOpacity(_displayedOpacity);
}
if (_cascadeOpacityEnabled)
{
for(auto child : _children){
for(auto child : _children)
{
child->updateDisplayedOpacity(_displayedOpacity);
}
}
}
void Scale9Sprite::disableCascadeColor()
{
for(auto child : _children){
for(auto child : _children)
{
child->updateDisplayedColor(Color3B::WHITE);
}
for(auto child : _protectedChildren){
for(auto child : _protectedChildren)
{
child->updateDisplayedColor(Color3B::WHITE);
}
if (_scale9Image) {
if (_scale9Image)
{
_scale9Image->updateDisplayedColor(Color3B::WHITE);
}
}
@ -944,20 +987,32 @@ y+=ytranslate; \
void Scale9Sprite::setFlippedX(bool flippedX)
{
_flippedX = flippedX;
if (_scale9Enabled) {
if (_scale9Enabled)
{
this->setScaleX(-1);
}else{
_scale9Image->setFlippedX(flippedX);
}
else
{
if (_scale9Image)
{
_scale9Image->setFlippedX(flippedX);
}
}
}
void Scale9Sprite::setFlippedY(bool flippedY)
{
_flippedY = flippedY;
if (_scale9Enabled) {
if (_scale9Enabled)
{
this->setScaleY(-1);
}else{
_scale9Image->setFlippedY(flippedY);
}
else
{
if (_scale9Image)
{
_scale9Image->setFlippedY(flippedY);
}
}
}

View File

@ -246,7 +246,7 @@ namespace ui {
// overrides
virtual void setContentSize(const Size & size) override;
Size getOriginalSize() const;
void setPreferredSize(const Size& size);