Merge pull request #8030 from andyque/fixButtonScaleIssue

fix button scale issue
This commit is contained in:
minggo 2014-09-15 09:49:06 +08:00
commit 70f922d07a
2 changed files with 54 additions and 13 deletions

View File

@ -362,15 +362,23 @@ void Button::onPressStateChangedToNormal()
{
if (_pressedActionEnabled)
{
this->stopAllActions();
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0, 1.0);
this->runAction(zoomAction);
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
Action *zoomAction = ScaleTo::create(0.05f, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
_buttonNormalRenderer->runAction(zoomAction);
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
_titleRenderer->stopAllActions();
_titleRenderer->runAction(zoomAction->clone());
}
}
else
{
this->stopAllActions();
this->setScale(1.0, 1.0);
_buttonNormalRenderer->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
_titleRenderer->stopAllActions();
_titleRenderer->setScaleX(_normalTextureScaleXInSize);
_titleRenderer->setScaleY(_normalTextureScaleYInSize);
}
}
@ -384,9 +392,15 @@ void Button::onPressStateChangedToPressed()
if (_pressedActionEnabled)
{
this->stopAllActions();
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0 + _zoomScale, 1.0 + _zoomScale);
this->runAction(zoomAction);
_buttonNormalRenderer->stopAllActions();
_buttonClickedRenderer->stopAllActions();
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
_buttonClickedRenderer->runAction(zoomAction);
_buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
_titleRenderer->stopAllActions();
//we must call zoomAction->clone here
_titleRenderer->runAction(zoomAction->clone());
}
}
else
@ -400,8 +414,12 @@ void Button::onPressStateChangedToPressed()
}
else
{
this->stopAllActions();
this->setScale(1.0 +_zoomScale, 1.0 + _zoomScale);
_buttonNormalRenderer->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale);
_titleRenderer->stopAllActions();
_titleRenderer->setScaleX(_normalTextureScaleXInSize + _zoomScale);
_titleRenderer->setScaleY(_normalTextureScaleYInSize + _zoomScale);
}
}
}
@ -411,6 +429,8 @@ void Button::onPressStateChangedToDisabled()
_buttonNormalRenderer->setVisible(false);
_buttonClickedRenderer->setVisible(false);
_buttonDisableRenderer->setVisible(true);
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
}
void Button::updateFlippedX()
@ -528,6 +548,7 @@ void Button::normalTextureScaleChangedWithSize()
_normalTextureScaleYInSize = scaleY;
}
}
_buttonNormalRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
}

View File

@ -304,6 +304,9 @@ bool UIButtonTest_Title::init()
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Title::touchEvent, this));
_uiLayer->addChild(button);
button->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(1., 1.2),
ScaleTo::create(1.0, 1.0),nullptr)));
TextBMFont *text = TextBMFont::create("BMFont", "cocosui/bitmapFontTest2.fnt");
text->setPosition(button->getPosition() + Vec2(button->getContentSize().width/2 + 50,0));
@ -479,6 +482,7 @@ bool UIButtonTestSwitchScale9::init()
"cocosui/animationbuttonpressed.png");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTestSwitchScale9::touchEvent, this));
button->setTitleText("Button Title");
button->ignoreContentAdaptWithSize(false);
_uiLayer->addChild(button);
@ -678,15 +682,31 @@ bool UIButtonIgnoreContentSizeTest::init()
"cocosui/animationbuttonpressed.png");
button->ignoreContentAdaptWithSize(false);
button->setContentSize(Size(200,100));
button->setNormalizedPosition(Vec2(0.5, 0.5));
button->setNormalizedPosition(Vec2(0.3, 0.5));
button->setTitleText("PLAY GAME");
button->setZoomScale(0.3);
button->setPressedActionEnabled(true);
button->addClickEventListener([this](Ref* sender) {
CCLOG("touched!");
button->addClickEventListener([=](Ref* sender) {
CCLOG("clicked!");
button->setScale(1.2);
});
_uiLayer->addChild(button);
// Create the button
auto button2 = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
button2->ignoreContentAdaptWithSize(false);
button2->setContentSize(Size(200,100));
button2->setNormalizedPosition(Vec2(0.8, 0.5));
button2->setTitleText("PLAY GAME");
button2->setZoomScale(0.3);
button2->setPressedActionEnabled(true);
button2->addClickEventListener([=](Ref* sender) {
button2->runAction(ScaleTo::create(1.0, 1.2));
CCLOG("clicked!");
});
_uiLayer->addChild(button2);
return true;
}
return false;