mirror of https://github.com/axmolengine/axmol.git
fix text doesn't scale with button issue
This commit is contained in:
parent
ed22792a72
commit
c81cfc1ecf
|
@ -37,6 +37,7 @@ static const int NORMAL_RENDERER_Z = (-2);
|
|||
static const int PRESSED_RENDERER_Z = (-2);
|
||||
static const int DISABLED_RENDERER_Z = (-2);
|
||||
static const int TITLE_RENDERER_Z = (-1);
|
||||
static const float ZOOM_ACTION_TIME_STEP = 0.05;
|
||||
|
||||
IMPLEMENT_CLASS_GUI_INFO(Button)
|
||||
|
||||
|
@ -361,17 +362,15 @@ void Button::onPressStateChangedToNormal()
|
|||
{
|
||||
if (_pressedActionEnabled)
|
||||
{
|
||||
_buttonNormalRenderer->stopAllActions();
|
||||
_buttonClickedRenderer->stopAllActions();
|
||||
Action *zoomAction = ScaleTo::create(0.05f, _normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
||||
_buttonNormalRenderer->runAction(zoomAction);
|
||||
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
|
||||
this->stopAllActions();
|
||||
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0, 1.0);
|
||||
this->runAction(zoomAction);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_buttonNormalRenderer->stopAllActions();
|
||||
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
||||
this->stopAllActions();
|
||||
this->setScale(1.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -385,11 +384,9 @@ void Button::onPressStateChangedToPressed()
|
|||
|
||||
if (_pressedActionEnabled)
|
||||
{
|
||||
_buttonNormalRenderer->stopAllActions();
|
||||
_buttonClickedRenderer->stopAllActions();
|
||||
Action *zoomAction = ScaleTo::create(0.05f, _pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
|
||||
_buttonClickedRenderer->runAction(zoomAction);
|
||||
_buttonNormalRenderer->setScale(_pressedTextureScaleXInSize + _zoomScale, _pressedTextureScaleYInSize + _zoomScale);
|
||||
this->stopAllActions();
|
||||
Action *zoomAction = ScaleTo::create(ZOOM_ACTION_TIME_STEP, 1.0 + _zoomScale, 1.0 + _zoomScale);
|
||||
this->runAction(zoomAction);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -403,8 +400,8 @@ void Button::onPressStateChangedToPressed()
|
|||
}
|
||||
else
|
||||
{
|
||||
_buttonNormalRenderer->stopAllActions();
|
||||
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize +_zoomScale, _normalTextureScaleYInSize + _zoomScale);
|
||||
this->stopAllActions();
|
||||
this->setScale(1.0 +_zoomScale, 1.0 + _zoomScale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -414,8 +411,6 @@ void Button::onPressStateChangedToDisabled()
|
|||
_buttonNormalRenderer->setVisible(false);
|
||||
_buttonClickedRenderer->setVisible(false);
|
||||
_buttonDisableRenderer->setVisible(true);
|
||||
_buttonNormalRenderer->setScale(_normalTextureScaleXInSize, _normalTextureScaleYInSize);
|
||||
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
|
||||
}
|
||||
|
||||
void Button::updateFlippedX()
|
||||
|
@ -473,7 +468,7 @@ void Button::adaptRenderers()
|
|||
const Size Button::getVirtualRendererSize() const
|
||||
{
|
||||
Size titleSize = _titleRenderer->getContentSize();
|
||||
if (titleSize.width > _normalTextureSize.width) {
|
||||
if (!_normalTextureLoaded && _titleRenderer->getString().size() > 0) {
|
||||
return titleSize;
|
||||
}
|
||||
return _normalTextureSize;
|
||||
|
|
|
@ -450,7 +450,7 @@ void CheckBox::adaptRenderers()
|
|||
}
|
||||
}
|
||||
|
||||
const Size& CheckBox::getVirtualRendererSize() const
|
||||
const Size CheckBox::getVirtualRendererSize() const
|
||||
{
|
||||
return _backGroundBoxRenderer->getContentSize();
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ g_guisTests[] =
|
|||
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
|
||||
sceneManager->setCurrentUISceneId(kUIButtonTest);
|
||||
sceneManager->setMinUISceneId(kUIButtonTest);
|
||||
sceneManager->setMaxUISceneId(kUIButtonTextOnly);
|
||||
sceneManager->setMaxUISceneId(kUIButtonIgnoreContentSizeTest);
|
||||
Scene* scene = sceneManager->currentUIScene();
|
||||
Director::getInstance()->replaceScene(scene);
|
||||
}
|
||||
|
|
|
@ -36,13 +36,12 @@ bool UIButtonTest::init()
|
|||
_uiLayer->addChild(alert);
|
||||
|
||||
// Create the button
|
||||
Button* button = Button::create("cocosui/animationbuttonnormal.png",
|
||||
"cocosui/animationbuttonpressed.png");
|
||||
Button* button = Button::create("cocosui/animationbuttonnormal.png");
|
||||
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->setZoomScale(0.4);
|
||||
button->setPressedActionEnabled(true);
|
||||
_uiLayer->addChild(button);
|
||||
// button->setColor(Color3B::RED);
|
||||
button->setOpacity(100);
|
||||
// Create the imageview
|
||||
ImageView* imageView = ImageView::create();
|
||||
|
@ -133,6 +132,7 @@ bool UIButtonTest_Scale9::init()
|
|||
button->setScale9Enabled(true);
|
||||
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
||||
button->setContentSize(Size(150, 70));
|
||||
button->setPressedActionEnabled(true);
|
||||
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this));
|
||||
_uiLayer->addChild(button);
|
||||
|
||||
|
@ -297,7 +297,7 @@ bool UIButtonTest_Title::init()
|
|||
|
||||
// Create the button with title
|
||||
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->setTitleColor(Color3B::YELLOW);
|
||||
CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!");
|
||||
|
@ -539,7 +539,7 @@ bool UIButtonTestZoomScale::init()
|
|||
Size widgetSize = _widget->getContentSize();
|
||||
|
||||
// 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->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
|
||||
_uiLayer->addChild(_displayValueLabel);
|
||||
|
@ -593,7 +593,7 @@ void UIButtonTestZoomScale::sliderEvent(Ref *pSender, Slider::EventType type)
|
|||
}
|
||||
}
|
||||
|
||||
// UIButtonTestZoomScale
|
||||
// UIButtonTextOnly
|
||||
UIButtonTextOnly::UIButtonTextOnly()
|
||||
: _displayValueLabel(nullptr)
|
||||
{
|
||||
|
@ -629,6 +629,58 @@ bool UIButtonTextOnly::init()
|
|||
auto button = Button::create();
|
||||
button->setNormalizedPosition(Vec2(0.5, 0.5));
|
||||
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->addClickEventListener([this](Ref* sender) {
|
||||
CCLOG("touched!");
|
||||
|
|
|
@ -129,4 +129,16 @@ protected:
|
|||
UI_SCENE_CREATE_FUNC(UIButtonTextOnly)
|
||||
Text* _displayValueLabel;
|
||||
};
|
||||
|
||||
class UIButtonIgnoreContentSizeTest : public UIScene
|
||||
{
|
||||
public:
|
||||
UIButtonIgnoreContentSizeTest();
|
||||
~UIButtonIgnoreContentSizeTest();
|
||||
bool init();
|
||||
|
||||
protected:
|
||||
UI_SCENE_CREATE_FUNC(UIButtonIgnoreContentSizeTest)
|
||||
Text* _displayValueLabel;
|
||||
};
|
||||
#endif /* defined(__TestCpp__UIButtonTest__) */
|
||||
|
|
|
@ -40,6 +40,8 @@ static const char* s_testArray[] =
|
|||
"UIButtonTestSwitchScale9",
|
||||
"UIButtonTestZoomScale",
|
||||
"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)
|
||||
"UIEditBoxTest",
|
||||
#endif
|
||||
|
@ -198,6 +200,9 @@ Scene *UISceneManager::currentUIScene()
|
|||
return UIButtonTestZoomScale::sceneWithTitle(s_testArray[_currentUISceneId]);
|
||||
case kUIButtonTextOnly:
|
||||
return UIButtonTextOnly::sceneWithTitle(s_testArray[_currentUISceneId]);
|
||||
case kUIButtonIgnoreContentSizeTest:
|
||||
return UIButtonIgnoreContentSizeTest::sceneWithTitle(s_testArray[_currentUISceneId]);
|
||||
|
||||
case kUICheckBoxTest:
|
||||
return UICheckBoxTest::sceneWithTitle(s_testArray[_currentUISceneId]);
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ enum
|
|||
kUIButtonTestSwitchScale9,
|
||||
kUIButtonTestZoomScale,
|
||||
kUIButtonTextOnly,
|
||||
kUIButtonIgnoreContentSizeTest,
|
||||
kUIEditBoxTest,
|
||||
kUICheckBoxTest,
|
||||
kUISliderTest,
|
||||
|
|
Loading…
Reference in New Issue