Merge pull request #8923 from liamcindy/v3

update ui button size changed logic
This commit is contained in:
minggo 2014-10-28 21:59:49 +08:00
commit e3d813795d
2 changed files with 98 additions and 34 deletions

View File

@ -194,16 +194,7 @@ void Button::ignoreContentAdaptWithSize(bool ignore)
{ {
if (_unifySize) if (_unifySize)
{ {
if (_scale9Enabled) this->updateContentSize();
{
ProtectedNode::setContentSize(_customSize);
}
else
{
Size s = getVirtualRendererSize();
ProtectedNode::setContentSize(s);
}
onSizeChanged();
return; return;
} }
if (!_scale9Enabled || (_scale9Enabled && !ignore)) if (!_scale9Enabled || (_scale9Enabled && !ignore))
@ -249,7 +240,17 @@ void Button::loadTextureNormal(const std::string& normal,TextureResType texType)
updateFlippedY(); updateFlippedY();
this->updateChildrenDisplayedRGBA(); this->updateChildrenDisplayedRGBA();
updateContentSizeWithTextureSize(_normalTextureSize); if (_unifySize )
{
if (!_scale9Enabled)
{
updateContentSizeWithTextureSize(this->getNormalSize());
}
}
else
{
updateContentSizeWithTextureSize(_normalTextureSize);
}
_normalTextureLoaded = true; _normalTextureLoaded = true;
_normalTextureAdaptDirty = true; _normalTextureAdaptDirty = true;
} }
@ -434,7 +435,15 @@ void Button::onPressStateChangedToNormal()
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize); _buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
_titleRenderer->stopAllActions(); _titleRenderer->stopAllActions();
_titleRenderer->runAction(zoomAction->clone()); if (_unifySize)
{
Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1, 1);
_titleRenderer->runAction(zoomTitleAction);
}
else
{
_titleRenderer->runAction(zoomAction->clone());
}
} }
} }
else else
@ -449,8 +458,16 @@ void Button::onPressStateChangedToNormal()
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize); _buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
_titleRenderer->stopAllActions(); _titleRenderer->stopAllActions();
_titleRenderer->setScaleX(_normalTextureScaleXInSize); if (_unifySize)
_titleRenderer->setScaleY(_normalTextureScaleYInSize); {
_titleRenderer->setScaleX(1.0f);
_titleRenderer->setScaleY(1.0f);
}
else
{
_titleRenderer->setScaleX(_normalTextureScaleXInSize);
_titleRenderer->setScaleY(_normalTextureScaleYInSize);
}
} }
} }
} }
@ -474,6 +491,15 @@ void Button::onPressStateChangedToPressed()
_titleRenderer->stopAllActions(); _titleRenderer->stopAllActions();
//we must call zoomAction->clone here //we must call zoomAction->clone here
_titleRenderer->runAction(zoomAction->clone()); _titleRenderer->runAction(zoomAction->clone());
if (_unifySize)
{
Action *zoomTitleAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1 + _zoomScale, 1 + _zoomScale);
_titleRenderer->runAction(zoomTitleAction);
}
else
{
_titleRenderer->runAction(zoomAction->clone());
}
} }
} }
else else
@ -491,8 +517,16 @@ void Button::onPressStateChangedToPressed()
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale); _buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale);
_titleRenderer->stopAllActions(); _titleRenderer->stopAllActions();
_titleRenderer->setScaleX(_normalTextureScaleXInSize + _zoomScale); if (_unifySize)
_titleRenderer->setScaleY(_normalTextureScaleYInSize + _zoomScale); {
_titleRenderer->setScaleX(1.0f + _zoomScale);
_titleRenderer->setScaleY(1.0f + _zoomScale);
}
else
{
_titleRenderer->setScaleX(_normalTextureScaleXInSize + _zoomScale);
_titleRenderer->setScaleY(_normalTextureScaleYInSize + _zoomScale);
}
} }
} }
} }
@ -532,9 +566,23 @@ void Button::updateTitleLocation()
void Button::updateContentSize() void Button::updateContentSize()
{ {
if (_ignoreSize) { if (_unifySize)
this->setContentSize(getVirtualRendererSize()); {
} if (_scale9Enabled)
{
ProtectedNode::setContentSize(_customSize);
}
else
{
Size s = getNormalSize();
ProtectedNode::setContentSize(s);
}
onSizeChanged();
return;
}
if (_ignoreSize) {
this->setContentSize(getVirtualRendererSize());
}
} }
void Button::onSizeChanged() void Button::onSizeChanged()
@ -567,6 +615,10 @@ void Button::adaptRenderers()
Size Button::getVirtualRendererSize() const Size Button::getVirtualRendererSize() const
{ {
if (_unifySize)
{
return this->getNormalSize();
}
Size titleSize = _titleRenderer->getContentSize(); Size titleSize = _titleRenderer->getContentSize();
if (!_normalTextureLoaded && _titleRenderer->getString().size() > 0) { if (!_normalTextureLoaded && _titleRenderer->getString().size() > 0) {
return titleSize; return titleSize;
@ -596,11 +648,8 @@ Node* Button::getVirtualRenderer()
void Button::normalTextureScaleChangedWithSize() void Button::normalTextureScaleChangedWithSize()
{ {
if (_unifySize)
{ if (_ignoreSize && !_unifySize)
_buttonNormalRenderer->setPreferredSize(_contentSize);
}
else if (_ignoreSize)
{ {
if (!_scale9Enabled) if (!_scale9Enabled)
{ {
@ -638,11 +687,8 @@ void Button::normalTextureScaleChangedWithSize()
void Button::pressedTextureScaleChangedWithSize() void Button::pressedTextureScaleChangedWithSize()
{ {
if (_unifySize)
{ if (_ignoreSize && !_unifySize)
_buttonClickedRenderer->setPreferredSize(_contentSize);
}
else if (_ignoreSize)
{ {
if (!_scale9Enabled) if (!_scale9Enabled)
{ {
@ -679,11 +725,8 @@ void Button::pressedTextureScaleChangedWithSize()
void Button::disabledTextureScaleChangedWithSize() void Button::disabledTextureScaleChangedWithSize()
{ {
if (_unifySize)
{ if (_ignoreSize && !_unifySize)
_buttonDisableRenderer->setPreferredSize(_contentSize);
}
else if (_ignoreSize)
{ {
if (!_scale9Enabled) if (!_scale9Enabled)
{ {
@ -784,9 +827,11 @@ void Button::setTitleFontName(const std::string& fontName)
{ {
_titleRenderer->requestSystemFontRefresh(); _titleRenderer->requestSystemFontRefresh();
} }
_titleRenderer->setSystemFontSize(_fontSize);
_type = FontType::SYSTEM; _type = FontType::SYSTEM;
} }
_fontName = fontName; _fontName = fontName;
this->updateContentSize();
} }
Label* Button::getTitleRenderer()const Label* Button::getTitleRenderer()const
@ -829,6 +874,24 @@ void Button::copySpecialProperties(Widget *widget)
setPressedActionEnabled(button->_pressedActionEnabled); setPressedActionEnabled(button->_pressedActionEnabled);
setZoomScale(button->_zoomScale); setZoomScale(button->_zoomScale);
} }
}
Size Button::getNormalSize() const
{
Size titleSize;
if (_titleRenderer != nullptr)
{
titleSize = _titleRenderer->getContentSize();
}
Size imageSize;
if (_buttonNormalRenderer != nullptr)
{
imageSize = _buttonNormalRenderer->getContentSize();
}
float width = titleSize.width > imageSize.width ? titleSize.width : imageSize.width;
float height = titleSize.height > imageSize.height ? titleSize.height : imageSize.height;
return Size(width,height);
} }
} }

View File

@ -234,6 +234,7 @@ protected:
virtual Widget* createCloneInstance() override; virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override; virtual void copySpecialProperties(Widget* model) override;
virtual Size getNormalSize() const;
protected: protected:
Scale9Sprite* _buttonNormalRenderer; Scale9Sprite* _buttonNormalRenderer;
Scale9Sprite* _buttonClickedRenderer; Scale9Sprite* _buttonClickedRenderer;