fix text doesn't scale with button issue

This commit is contained in:
andyque 2014-09-12 14:24:27 +08:00
parent ed22792a72
commit c81cfc1ecf
7 changed files with 91 additions and 26 deletions

View File

@ -37,6 +37,7 @@ static const int NORMAL_RENDERER_Z = (-2);
static const int PRESSED_RENDERER_Z = (-2); static const int PRESSED_RENDERER_Z = (-2);
static const int DISABLED_RENDERER_Z = (-2); static const int DISABLED_RENDERER_Z = (-2);
static const int TITLE_RENDERER_Z = (-1); static const int TITLE_RENDERER_Z = (-1);
static const float ZOOM_ACTION_TIME_STEP = 0.05;
IMPLEMENT_CLASS_GUI_INFO(Button) IMPLEMENT_CLASS_GUI_INFO(Button)
@ -361,17 +362,15 @@ void Button::onPressStateChangedToNormal()
{ {
if (_pressedActionEnabled) if (_pressedActionEnabled)
{ {
_buttonNormalRenderer->stopAllActions(); this->stopAllActions();
_buttonClickedRenderer->stopAllActions(); Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0, 1.0);
Action *zoomAction = ScaleTo::create(0.05f, _normalTextureScaleXInSize, _normalTextureScaleYInSize); this->runAction(zoomAction);
_buttonNormalRenderer->runAction(zoomAction);
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
} }
} }
else else
{ {
_buttonNormalRenderer->stopAllActions(); this->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize); this->setScale(1.0, 1.0);
} }
} }
@ -385,11 +384,9 @@ void Button::onPressStateChangedToPressed()
if (_pressedActionEnabled) if (_pressedActionEnabled)
{ {
_buttonNormalRenderer->stopAllActions(); this->stopAllActions();
_buttonClickedRenderer->stopAllActions(); Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0 + _zoomScale, 1.0 + _zoomScale);
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale); this->runAction(zoomAction);
_buttonClickedRenderer->runAction(zoomAction);
_buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
} }
} }
else else
@ -403,8 +400,8 @@ void Button::onPressStateChangedToPressed()
} }
else else
{ {
_buttonNormalRenderer->stopAllActions(); this->stopAllActions();
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale); this->setScale(1.0 +_zoomScale, 1.0 + _zoomScale);
} }
} }
} }
@ -414,8 +411,6 @@ void Button::onPressStateChangedToDisabled()
_buttonNormalRenderer->setVisible(false); _buttonNormalRenderer->setVisible(false);
_buttonClickedRenderer->setVisible(false); _buttonClickedRenderer->setVisible(false);
_buttonDisableRenderer->setVisible(true); _buttonDisableRenderer->setVisible(true);
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
} }
void Button::updateFlippedX() void Button::updateFlippedX()
@ -473,7 +468,7 @@ void Button::adaptRenderers()
const Size Button::getVirtualRendererSize() const const Size Button::getVirtualRendererSize() const
{ {
Size titleSize = _titleRenderer->getContentSize(); Size titleSize = _titleRenderer->getContentSize();
if (titleSize.width > _normalTextureSize.width) { if (!_normalTextureLoaded && _titleRenderer->getString().size() > 0) {
return titleSize; return titleSize;
} }
return _normalTextureSize; return _normalTextureSize;

View File

@ -450,7 +450,7 @@ void CheckBox::adaptRenderers()
} }
} }
const Size& CheckBox::getVirtualRendererSize() const const Size CheckBox::getVirtualRendererSize() const
{ {
return _backGroundBoxRenderer->getContentSize(); return _backGroundBoxRenderer->getContentSize();
} }

View File

@ -89,7 +89,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager(); UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUIButtonTest); sceneManager->setCurrentUISceneId(kUIButtonTest);
sceneManager->setMinUISceneId(kUIButtonTest); sceneManager->setMinUISceneId(kUIButtonTest);
sceneManager->setMaxUISceneId(kUIButtonTextOnly); sceneManager->setMaxUISceneId(kUIButtonIgnoreContentSizeTest);
Scene* scene = sceneManager->currentUIScene(); Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene); Director::getInstance()->replaceScene(scene);
} }

View File

@ -36,13 +36,12 @@ bool UIButtonTest::init()
_uiLayer->addChild(alert); _uiLayer->addChild(alert);
// Create the button // Create the button
Button* button = Button::create("cocosui/animationbuttonnormal.png", Button* button = Button::create("cocosui/animationbuttonnormal.png");
"cocosui/animationbuttonpressed.png");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
// button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this)); button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest::touchEvent, this));
button->setZoomScale(0.4);
button->setPressedActionEnabled(true);
_uiLayer->addChild(button); _uiLayer->addChild(button);
// button->setColor(Color3B::RED);
button->setOpacity(100); button->setOpacity(100);
// Create the imageview // Create the imageview
ImageView* imageView = ImageView::create(); ImageView* imageView = ImageView::create();
@ -133,6 +132,7 @@ bool UIButtonTest_Scale9::init()
button->setScale9Enabled(true); button->setScale9Enabled(true);
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->setContentSize(Size(150, 70)); button->setContentSize(Size(150, 70));
button->setPressedActionEnabled(true);
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this)); button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this));
_uiLayer->addChild(button); _uiLayer->addChild(button);
@ -297,7 +297,7 @@ bool UIButtonTest_Title::init()
// Create the button with title // Create the button with title
Button* button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png"); Button* button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png");
button->setTitleText("Title Button Large than button!"); button->setTitleText("Title Button!");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
button->setTitleColor(Color3B::YELLOW); button->setTitleColor(Color3B::YELLOW);
CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!"); CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!");
@ -539,7 +539,7 @@ bool UIButtonTestZoomScale::init()
Size widgetSize = _widget->getContentSize(); Size widgetSize = _widget->getContentSize();
// Add a label in which the button events will be displayed // Add a label in which the button events will be displayed
_displayValueLabel = Text::create("Zoom Scale: 0.1", "fonts/Marker Felt.ttf",32); _displayValueLabel = Text::create("Zoom Scale: -0.5", "fonts/Marker Felt.ttf",32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel); _uiLayer->addChild(_displayValueLabel);
@ -593,7 +593,7 @@ void UIButtonTestZoomScale::sliderEvent(Ref *pSender, Slider::EventType type)
} }
} }
// UIButtonTestZoomScale // UIButtonTextOnly
UIButtonTextOnly::UIButtonTextOnly() UIButtonTextOnly::UIButtonTextOnly()
: _displayValueLabel(nullptr) : _displayValueLabel(nullptr)
{ {
@ -629,6 +629,58 @@ bool UIButtonTextOnly::init()
auto button = Button::create(); auto button = Button::create();
button->setNormalizedPosition(Vec2(0.5, 0.5)); button->setNormalizedPosition(Vec2(0.5, 0.5));
button->setTitleText("PLAY GAME"); button->setTitleText("PLAY GAME");
button->setZoomScale(0.3);
button->setPressedActionEnabled(true);
button->addClickEventListener([this](Ref* sender) {
CCLOG("clicked!");
});
_uiLayer->addChild(button);
return true;
}
return false;
}
// UIButtonIgnoreContentSizeTest
UIButtonIgnoreContentSizeTest::UIButtonIgnoreContentSizeTest()
: _displayValueLabel(nullptr)
{
}
UIButtonIgnoreContentSizeTest::~UIButtonIgnoreContentSizeTest()
{
}
bool UIButtonIgnoreContentSizeTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the button events will be displayed
_displayValueLabel = Text::create("Button IgnoreContent Size Test", "fonts/Marker Felt.ttf",32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","fonts/Marker Felt.ttf",30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
_uiLayer->addChild(alert);
// Create the button
auto button = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
button->ignoreContentAdaptWithSize(false);
button->setContentSize(Size(200,100));
button->setNormalizedPosition(Vec2(0.5, 0.5));
button->setTitleText("PLAY GAME");
button->setZoomScale(0.3);
button->setPressedActionEnabled(true); button->setPressedActionEnabled(true);
button->addClickEventListener([this](Ref* sender) { button->addClickEventListener([this](Ref* sender) {
CCLOG("touched!"); CCLOG("touched!");

View File

@ -129,4 +129,16 @@ protected:
UI_SCENE_CREATE_FUNC(UIButtonTextOnly) UI_SCENE_CREATE_FUNC(UIButtonTextOnly)
Text* _displayValueLabel; Text* _displayValueLabel;
}; };
class UIButtonIgnoreContentSizeTest : public UIScene
{
public:
UIButtonIgnoreContentSizeTest();
~UIButtonIgnoreContentSizeTest();
bool init();
protected:
UI_SCENE_CREATE_FUNC(UIButtonIgnoreContentSizeTest)
Text* _displayValueLabel;
};
#endif /* defined(__TestCpp__UIButtonTest__) */ #endif /* defined(__TestCpp__UIButtonTest__) */

View File

@ -40,6 +40,8 @@ static const char* s_testArray[] =
"UIButtonTestSwitchScale9", "UIButtonTestSwitchScale9",
"UIButtonTestZoomScale", "UIButtonTestZoomScale",
"UIButtonTextOnly", "UIButtonTextOnly",
"UIButtonIgnoreContentSizeTest",
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
"UIEditBoxTest", "UIEditBoxTest",
#endif #endif
@ -198,6 +200,9 @@ Scene *UISceneManager::currentUIScene()
return UIButtonTestZoomScale::sceneWithTitle(s_testArray[_currentUISceneId]); return UIButtonTestZoomScale::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIButtonTextOnly: case kUIButtonTextOnly:
return UIButtonTextOnly::sceneWithTitle(s_testArray[_currentUISceneId]); return UIButtonTextOnly::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIButtonIgnoreContentSizeTest:
return UIButtonIgnoreContentSizeTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUICheckBoxTest: case kUICheckBoxTest:
return UICheckBoxTest::sceneWithTitle(s_testArray[_currentUISceneId]); return UICheckBoxTest::sceneWithTitle(s_testArray[_currentUISceneId]);

View File

@ -39,6 +39,7 @@ enum
kUIButtonTestSwitchScale9, kUIButtonTestSwitchScale9,
kUIButtonTestZoomScale, kUIButtonTestZoomScale,
kUIButtonTextOnly, kUIButtonTextOnly,
kUIButtonIgnoreContentSizeTest,
kUIEditBoxTest, kUIEditBoxTest,
kUICheckBoxTest, kUICheckBoxTest,
kUISliderTest, kUISliderTest,