2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
#include "UIButtonTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
using namespace axis::ui;
|
2015-04-09 12:23:47 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
UIButtonTests::UIButtonTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UIButtonTest);
|
|
|
|
ADD_TEST_CASE(UIButtonTest_Scale9);
|
|
|
|
ADD_TEST_CASE(UIButtonTest_Scale9_State_Change);
|
|
|
|
ADD_TEST_CASE(UIButtonTest_PressedAction);
|
|
|
|
ADD_TEST_CASE(UIButtonTest_Title);
|
|
|
|
ADD_TEST_CASE(UIButtonTestRemoveSelf);
|
|
|
|
ADD_TEST_CASE(UIButtonTestSwitchScale9);
|
|
|
|
ADD_TEST_CASE(UIButtonTestZoomScale);
|
|
|
|
ADD_TEST_CASE(UIButtonTextOnly);
|
|
|
|
ADD_TEST_CASE(UIButtonIgnoreContentSizeTest);
|
|
|
|
ADD_TEST_CASE(UIButtonTitleEffectTest);
|
|
|
|
ADD_TEST_CASE(UIButtonFlipTest);
|
|
|
|
ADD_TEST_CASE(UIButtonNormalDefaultTest);
|
|
|
|
ADD_TEST_CASE(UIButtonDisableDefaultTest);
|
2015-04-16 14:38:00 +08:00
|
|
|
ADD_TEST_CASE(UIButtonCloneTest);
|
2015-07-23 17:27:04 +08:00
|
|
|
ADD_TEST_CASE(Issue12249);
|
2017-01-10 15:28:47 +08:00
|
|
|
ADD_TEST_CASE(Issue17116);
|
2017-01-20 09:59:30 +08:00
|
|
|
ADD_TEST_CASE(UIButtonWithPolygonInfo);
|
2017-07-17 15:52:01 +08:00
|
|
|
ADD_TEST_CASE(UIButtonScale9ChangeSpriteFrame);
|
2015-04-03 14:31:03 +08:00
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// UIButtonTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest::UIButtonTest() : _displayValueLabel(nullptr) {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest::~UIButtonTest() {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
bool UIButtonTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2015-04-16 14:38:00 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("Button", "fonts/Marker Felt.ttf", 30);
|
2015-04-16 14:38:00 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
|
2014-10-10 11:01:51 +08:00
|
|
|
button->getContentSize().height);
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTest::touchEvent, this));
|
2014-10-09 17:19:43 +08:00
|
|
|
button->setZoomScale(0.4f);
|
2014-09-12 14:24:27 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2014-04-17 14:08:25 +08:00
|
|
|
_uiLayer->addChild(button);
|
2014-08-11 15:04:00 +08:00
|
|
|
button->setOpacity(100);
|
2014-07-08 11:30:27 +08:00
|
|
|
// Create the imageview
|
|
|
|
ImageView* imageView = ImageView::create();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
imageView->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f + 50 + button->getContentSize().width / 2, widgetSize.height / 2.0f));
|
2014-07-08 11:30:27 +08:00
|
|
|
imageView->setTag(12);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-08 11:30:27 +08:00
|
|
|
_uiLayer->addChild(imageView);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2015-11-30 16:17:13 +08:00
|
|
|
_button = button;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2015-11-30 16:17:13 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(UIButtonTest::printWidgetResources, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
item1->setPosition(
|
|
|
|
Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
|
2015-11-30 16:17:13 +08:00
|
|
|
auto pMenu1 = Menu::create(item1, nullptr);
|
2019-09-24 11:31:35 +08:00
|
|
|
pMenu1->setPosition(Vec2(0.0f, 0.0f));
|
2015-11-30 16:17:13 +08:00
|
|
|
this->addChild(pMenu1, 10);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTest::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
|
|
|
ImageView* imageView = (ImageView*)_uiLayer->getChildByTag(12);
|
|
|
|
imageView->setVisible(false);
|
|
|
|
imageView->loadTexture("cocosui/ccicon.png");
|
|
|
|
imageView->setOpacity(0);
|
|
|
|
imageView->setVisible(true);
|
|
|
|
imageView->runAction(
|
|
|
|
Sequence::create(FadeIn::create(0.5), DelayTime::create(1.0), FadeOut::create(0.5), nullptr));
|
|
|
|
Button* btn = (Button*)pSender;
|
|
|
|
btn->loadTextureNormal("cocosui/animationbuttonnormal.png");
|
|
|
|
}
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIButtonTest::printWidgetResources(axis::Ref* sender)
|
2015-11-30 16:17:13 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ResourceData normalFileName = _button->getNormalFile();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("normalFileName Name : %s, Type: %d", normalFileName.file.c_str(), normalFileName.type);
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ResourceData clickedFileName = _button->getPressedFile();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("clickedFileName Name : %s, Type: %d", clickedFileName.file.c_str(), clickedFileName.type);
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ResourceData disabledFileName = _button->getDisabledFile();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("disabledFileName Name : %s, Type: %d", disabledFileName.file.c_str(), disabledFileName.type);
|
2015-11-30 16:17:13 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// UIButtonTest_Scale9
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_Scale9::UIButtonTest_Scale9() : _displayValueLabel(nullptr) {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_Scale9::~UIButtonTest_Scale9() {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
bool UIButtonTest_Scale9::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2014-04-03 10:31:10 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("Button scale9 render", "fonts/Marker Felt.ttf", 30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Create the button
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
2013-09-16 20:54:13 +08:00
|
|
|
// open scale9 render
|
|
|
|
button->setScale9Enabled(true);
|
2019-09-24 11:31:35 +08:00
|
|
|
button->setContentSize(Size(150.0f, 70.0f));
|
2015-12-03 17:53:02 +08:00
|
|
|
button->setPosition(Vec2(-button->getContentSize().width - 10, widgetSize.height / 2.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
auto moveBy = MoveBy::create(1.0f, Vec2(widgetSize.width / 2, 0.0f));
|
2015-12-03 17:53:02 +08:00
|
|
|
auto moveByReverse = moveBy->reverse()->clone();
|
2021-12-28 16:06:23 +08:00
|
|
|
button->runAction(RepeatForever::create(Sequence::create(moveBy, moveByReverse, NULL)));
|
2014-09-12 14:24:27 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTest_Scale9::touchEvent, this));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-08 11:30:27 +08:00
|
|
|
// Create the imageview
|
|
|
|
Button* button2 = Button::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
button2->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f + button->getContentSize().width + 20, widgetSize.height / 2.0f));
|
2014-07-08 11:30:27 +08:00
|
|
|
button2->setName("normal");
|
|
|
|
_uiLayer->addChild(button2);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Sprite* sprite = Sprite::create("cocosui/animationbuttonnormal.png");
|
2014-07-08 11:30:27 +08:00
|
|
|
button2->addChild(sprite);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTest_Scale9::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
|
|
|
Button* btn = (Button*)_uiLayer->getChildByName("normal");
|
|
|
|
btn->loadTextureNormal("cocosui/animationbuttonnormal.png");
|
|
|
|
btn->loadTexturePressed("cocosui/animationbuttonpressed.png");
|
|
|
|
btn->runAction(Sequence::create(FadeIn::create(0.5), DelayTime::create(1.0), FadeOut::create(0.5), nullptr));
|
|
|
|
}
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 20:57:54 +08:00
|
|
|
// UIButtonTest_Scale9_State_Change
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_Scale9_State_Change::UIButtonTest_Scale9_State_Change() : _displayValueLabel(nullptr) {}
|
2015-01-20 20:57:54 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_Scale9_State_Change::~UIButtonTest_Scale9_State_Change() {}
|
2015-01-20 20:57:54 +08:00
|
|
|
|
|
|
|
bool UIButtonTest_Scale9_State_Change::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
|
|
|
|
|
|
|
// Add a label in which the button events will be displayed
|
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
|
|
|
Text* alert = Text::create("Button scale9 render", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-01-20 20:57:54 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
// Create the button
|
2015-01-21 14:54:37 +08:00
|
|
|
Button* button = Button::create("cocosui/button.png");
|
2015-01-20 20:57:54 +08:00
|
|
|
// open scale9 render
|
|
|
|
button->ignoreContentAdaptWithSize(false);
|
|
|
|
button->setScale9Enabled(true);
|
2015-01-21 14:54:37 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f - 100, widgetSize.height / 2.0f));
|
2019-09-24 11:31:35 +08:00
|
|
|
button->setContentSize(Size(180.0f, 60.0f));
|
2015-01-21 14:54:37 +08:00
|
|
|
button->setTitleText("Hello Scale9");
|
2015-01-20 20:57:54 +08:00
|
|
|
button->setPressedActionEnabled(false);
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTest_Scale9_State_Change::touchEvent, this));
|
2015-01-20 20:57:54 +08:00
|
|
|
_uiLayer->addChild(button);
|
|
|
|
|
2015-01-21 14:54:37 +08:00
|
|
|
Button* button2 = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
// open scale9 render
|
|
|
|
button2->ignoreContentAdaptWithSize(false);
|
|
|
|
button2->setScale9Enabled(true);
|
|
|
|
button2->setTitleText("Hello scale9");
|
|
|
|
button2->setPosition(Vec2(widgetSize.width / 2.0f + 100, widgetSize.height / 2.0f));
|
2019-09-24 11:31:35 +08:00
|
|
|
button2->setContentSize(Size(180.0f, 60.0f));
|
2015-01-21 14:54:37 +08:00
|
|
|
button2->setPressedActionEnabled(true);
|
2022-07-15 19:17:01 +08:00
|
|
|
button2->addTouchEventListener(AX_CALLBACK_2(UIButtonTest_Scale9_State_Change::touchEvent, this));
|
2015-01-21 14:54:37 +08:00
|
|
|
_uiLayer->addChild(button2);
|
2015-01-20 20:57:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTest_Scale9_State_Change::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2015-01-20 20:57:54 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Widget::TouchEventType::BEGAN:
|
2015-07-15 12:04:48 +08:00
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
2015-01-20 20:57:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Widget::TouchEventType::MOVED:
|
2015-07-15 12:04:48 +08:00
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
2015-01-20 20:57:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
2015-07-15 12:04:48 +08:00
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
2021-12-28 16:06:23 +08:00
|
|
|
Button* btn = (Button*)pSender;
|
2015-01-20 20:57:54 +08:00
|
|
|
if (btn->isScale9Enabled())
|
|
|
|
{
|
|
|
|
btn->setScale9Enabled(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
btn->setScale9Enabled(true);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
break;
|
2015-01-20 20:57:54 +08:00
|
|
|
|
|
|
|
case Widget::TouchEventType::CANCELED:
|
2015-07-15 12:04:48 +08:00
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
2015-01-20 20:57:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// UIButtonTest_PressAction
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_PressedAction::UIButtonTest_PressedAction() : _displayValueLabel(nullptr) {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_PressedAction::~UIButtonTest_PressedAction() {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
bool UIButtonTest_PressedAction::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add the alert
|
2014-04-03 10:31:10 +08:00
|
|
|
Text* alert = Text::create("Button Pressed Action", "fonts/Marker Felt.ttf", 30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Create the button
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2013-09-16 20:54:13 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-07-08 11:30:27 +08:00
|
|
|
button->setColor(Color3B::GREEN);
|
|
|
|
button->setOpacity(30);
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTest_PressedAction::touchEvent, this));
|
2014-07-08 11:30:27 +08:00
|
|
|
button->setName("button");
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-08-05 08:11:37 +08:00
|
|
|
Button* button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
button2->setPosition(button->getPosition() + Vec2(100.0f, 0.0f));
|
2014-08-05 08:11:37 +08:00
|
|
|
button2->setName("button2");
|
|
|
|
_uiLayer->addChild(button2);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTest_PressedAction::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
|
|
|
Button* btn = (Button*)_uiLayer->getChildByName("button");
|
|
|
|
btn->loadTextureNormal("cocosui/animationbuttonnormal.png");
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Button* btn2 = (Button*)_uiLayer->getChildByName("button2");
|
|
|
|
btn2->setAnchorPoint(Vec2(0, 0.5));
|
|
|
|
}
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2013-12-23 15:35:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonTest_Title
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_Title::UIButtonTest_Title() : _displayValueLabel(nullptr) {}
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTest_Title::~UIButtonTest_Title() {}
|
2013-12-23 15:35:35 +08:00
|
|
|
|
|
|
|
bool UIButtonTest_Title::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-06-20 11:18:53 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
// Add a label in which the text button events will be displayed
|
2014-04-03 10:31:10 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2019-09-24 11:31:35 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2014-05-15 01:07:09 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
// Add the alert
|
2014-11-06 09:23:58 +08:00
|
|
|
Text* alert = Text::create("Button with title, title should be flipped!", "fonts/Marker Felt.ttf", 30);
|
2013-12-23 15:35:35 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
// Create the button with title
|
2014-04-03 10:31:10 +08:00
|
|
|
Button* button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png");
|
2014-09-12 14:24:27 +08:00
|
|
|
button->setTitleText("Title Button!");
|
2014-05-15 01:07:09 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-06-25 13:47:29 +08:00
|
|
|
button->setTitleColor(Color3B::YELLOW);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColor & getTitleColor not match!");
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTest_Title::touchEvent, this));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(button);
|
2014-11-06 09:23:58 +08:00
|
|
|
button->setFlippedX(true);
|
|
|
|
auto label = button->getTitleRenderer();
|
|
|
|
label->setScale(4.0);
|
2021-12-28 16:06:23 +08:00
|
|
|
button->runAction(
|
|
|
|
RepeatForever::create(Sequence::create(ScaleTo::create(1.0f, 1.2f), ScaleTo::create(1.0f, 1.0f), nullptr)));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
TextBMFont* text = TextBMFont::create("BMFont", "cocosui/bitmapFontTest2.fnt");
|
|
|
|
text->setPosition(button->getPosition() + Vec2(button->getContentSize().width / 2 + 50, 0.0f));
|
2014-07-08 11:30:27 +08:00
|
|
|
text->setColor(Color3B::YELLOW);
|
|
|
|
text->setOpacity(50);
|
|
|
|
text->setName("text");
|
|
|
|
|
2015-05-27 16:58:52 +08:00
|
|
|
auto button2 = Button::create("cocosui/backtotopnormal.png", "cocosui/backtotoppressed.png");
|
|
|
|
button2->setTitleFontName("fonts/helvetica-32.fnt");
|
|
|
|
button2->setTitleText("BMFont");
|
2021-12-28 16:06:23 +08:00
|
|
|
// the change of font size will take no effect.
|
2015-05-27 16:58:52 +08:00
|
|
|
button2->setTitleFontSize(30);
|
2021-12-28 16:06:23 +08:00
|
|
|
button2->setPosition(Vec2(button->getPosition() + Vec2(50.0f, -80.0f)));
|
2015-05-27 16:58:52 +08:00
|
|
|
this->addChild(button2);
|
2014-07-08 11:30:27 +08:00
|
|
|
|
|
|
|
_uiLayer->addChild(text);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2013-12-23 15:35:35 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTest_Title::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2013-12-23 15:35:35 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
|
|
|
TextBMFont* text = (TextBMFont*)_uiLayer->getChildByName("text");
|
|
|
|
text->setFntFile("cocosui/bitmapFontTest2.fnt");
|
|
|
|
if (text->getString() == "BMFont")
|
|
|
|
{
|
|
|
|
text->setString("Hello");
|
|
|
|
}
|
|
|
|
else
|
2014-07-08 11:30:27 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
text->setString("BMFont");
|
2014-07-08 11:30:27 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-07-23 18:52:03 +08:00
|
|
|
|
|
|
|
// UIButtonTest_RemoveSelf
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTestRemoveSelf::UIButtonTestRemoveSelf() : _displayValueLabel(nullptr) {}
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTestRemoveSelf::~UIButtonTestRemoveSelf() {}
|
2014-07-23 18:52:03 +08:00
|
|
|
|
2014-07-25 14:40:58 +08:00
|
|
|
bool UIButtonTestRemoveSelf::init()
|
2014-07-23 18:52:03 +08:00
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-23 18:52:03 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-07-23 18:52:03 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-23 18:52:03 +08:00
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert =
|
|
|
|
Text::create("Remove Self in the Button's Callback shouldn't cause crash!", "fonts/Marker Felt.ttf", 10);
|
2014-07-23 18:52:03 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-23 18:52:03 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Layout* layout = Layout::create();
|
2014-10-09 17:19:43 +08:00
|
|
|
layout->setContentSize(widgetSize * 0.6f);
|
2014-07-31 11:07:20 +08:00
|
|
|
layout->setBackGroundColor(Color3B::GREEN);
|
|
|
|
layout->setBackGroundColorType(Layout::BackGroundColorType::SOLID);
|
|
|
|
layout->setBackGroundColorOpacity(100);
|
2021-12-28 16:06:23 +08:00
|
|
|
layout->setPosition(Size(widgetSize.width / 2, widgetSize.height / 2));
|
2014-07-31 11:07:20 +08:00
|
|
|
layout->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
|
|
|
|
layout->setTag(12);
|
|
|
|
_uiLayer->addChild(layout);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-23 18:52:03 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-07-31 11:07:20 +08:00
|
|
|
button->setPosition(Vec2(layout->getContentSize().width / 2.0f, layout->getContentSize().height / 2.0f));
|
2014-07-23 18:52:03 +08:00
|
|
|
// button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTestRemoveSelf::touchEvent, this));
|
2014-07-31 11:07:20 +08:00
|
|
|
layout->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-23 18:52:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTestRemoveSelf::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2014-07-23 18:52:03 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
|
|
|
auto layout = _uiLayer->getChildByTag(12);
|
|
|
|
layout->removeFromParentAndCleanup(true);
|
|
|
|
}
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2014-07-31 11:07:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonTestSwitchScale9
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTestSwitchScale9::UIButtonTestSwitchScale9() : _displayValueLabel(nullptr) {}
|
2014-07-31 11:07:20 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTestSwitchScale9::~UIButtonTestSwitchScale9() {}
|
2014-07-31 11:07:20 +08:00
|
|
|
|
|
|
|
bool UIButtonTestSwitchScale9::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-31 11:07:20 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2014-07-31 11:07:20 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-31 11:07:20 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-07-31 11:07:20 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-15 19:17:01 +08:00
|
|
|
button->addTouchEventListener(AX_CALLBACK_2(UIButtonTestSwitchScale9::touchEvent, this));
|
2014-09-15 09:28:56 +08:00
|
|
|
button->setTitleText("Button Title");
|
2014-07-31 11:07:20 +08:00
|
|
|
button->ignoreContentAdaptWithSize(false);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-31 11:07:20 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-07-31 11:07:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTestSwitchScale9::touchEvent(Ref* pSender, Widget::TouchEventType type)
|
2014-07-31 11:07:20 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::BEGAN:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Down"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::MOVED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Move"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::ENDED:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Up"));
|
|
|
|
auto btn = ((Button*)pSender);
|
|
|
|
btn->setScale9Enabled(!btn->isScale9Enabled());
|
|
|
|
btn->setContentSize(Size(200.0f, 100.0f));
|
|
|
|
}
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
case Widget::TouchEventType::CANCELED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Touch Cancelled"));
|
|
|
|
break;
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
default:
|
|
|
|
break;
|
2014-07-23 18:52:03 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-14 11:14:24 +08:00
|
|
|
|
|
|
|
// UIButtonTestZoomScale
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTestZoomScale::UIButtonTestZoomScale() : _displayValueLabel(nullptr) {}
|
2014-08-14 11:14:24 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTestZoomScale::~UIButtonTestZoomScale() {}
|
2014-08-14 11:14:24 +08:00
|
|
|
|
|
|
|
bool UIButtonTestZoomScale::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("Zoom Scale: -0.5", "fonts/Marker Felt.ttf", 32);
|
2014-08-14 11:14:24 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-08-14 11:14:24 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
|
|
|
|
button->setPressedActionEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
button->addClickEventListener([=](Ref* sender) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Button clicked, position = (%f, %f)", button->getPosition().x, button->getPosition().y);
|
2014-08-21 10:21:07 +08:00
|
|
|
});
|
2014-08-14 11:14:24 +08:00
|
|
|
button->setName("button");
|
|
|
|
_uiLayer->addChild(button);
|
|
|
|
button->setZoomScale(-0.5);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-08-14 11:14:24 +08:00
|
|
|
Slider* slider = Slider::create();
|
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 20));
|
2022-07-15 19:17:01 +08:00
|
|
|
slider->addEventListener(AX_CALLBACK_2(UIButtonTestZoomScale::sliderEvent, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
slider->setPercent(button->getZoomScale() * 100);
|
2014-08-14 11:14:24 +08:00
|
|
|
_uiLayer->addChild(slider);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UIButtonTestZoomScale::sliderEvent(Ref* pSender, Slider::EventType type)
|
2014-08-14 11:14:24 +08:00
|
|
|
{
|
|
|
|
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
Slider* slider = dynamic_cast<Slider*>(pSender);
|
|
|
|
int percent = slider->getPercent();
|
|
|
|
Button* btn = (Button*)_uiLayer->getChildByName("button");
|
2014-08-14 11:14:24 +08:00
|
|
|
float zoomScale = percent * 0.01;
|
|
|
|
btn->setZoomScale(zoomScale);
|
2015-07-15 12:04:48 +08:00
|
|
|
_displayValueLabel->setString(StringUtils::format("Zoom Scale: %f", zoomScale));
|
2014-08-14 11:14:24 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-12 13:50:13 +08:00
|
|
|
|
2014-09-12 14:24:27 +08:00
|
|
|
// UIButtonTextOnly
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTextOnly::UIButtonTextOnly() : _displayValueLabel(nullptr) {}
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTextOnly::~UIButtonTextOnly() {}
|
2014-09-12 13:50:13 +08:00
|
|
|
|
|
|
|
bool UIButtonTextOnly::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 13:50:13 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("Text Only Button", "fonts/Marker Felt.ttf", 32);
|
2014-09-12 13:50:13 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 13:50:13 +08:00
|
|
|
// Create the button
|
|
|
|
auto button = Button::create();
|
2016-10-27 09:45:40 +08:00
|
|
|
button->setPositionNormalized(Vec2(0.5f, 0.5f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 13:50:13 +08:00
|
|
|
button->setTitleText("PLAY GAME");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width,
|
2014-10-10 11:01:51 +08:00
|
|
|
button->getContentSize().height);
|
2014-10-09 17:19:43 +08:00
|
|
|
button->setZoomScale(0.3f);
|
2014-09-12 14:24:27 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2022-07-16 10:43:05 +08:00
|
|
|
button->addClickEventListener([](Ref* sender) { AXLOG("clicked!"); });
|
2014-09-12 14:24:27 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 14:24:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonIgnoreContentSizeTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonIgnoreContentSizeTest::UIButtonIgnoreContentSizeTest() : _displayValueLabel(nullptr) {}
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonIgnoreContentSizeTest::~UIButtonIgnoreContentSizeTest() {}
|
2014-09-12 14:24:27 +08:00
|
|
|
|
|
|
|
bool UIButtonIgnoreContentSizeTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 14:24:27 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("Button IgnoreContent Size Test", "fonts/Marker Felt.ttf", 32);
|
2014-09-12 14:24:27 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 14:24:27 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-09-12 14:24:27 +08:00
|
|
|
button->ignoreContentAdaptWithSize(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
button->setContentSize(Size(200.0f, 100.0f));
|
2016-10-27 09:45:40 +08:00
|
|
|
button->setPositionNormalized(Vec2(0.3f, 0.5f));
|
2014-09-12 14:24:27 +08:00
|
|
|
button->setTitleText("PLAY GAME");
|
2014-10-09 17:19:43 +08:00
|
|
|
button->setZoomScale(0.3f);
|
2014-09-12 13:50:13 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2014-09-13 22:08:02 +08:00
|
|
|
button->addClickEventListener([=](Ref* sender) {
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("clicked!");
|
2014-10-09 17:19:43 +08:00
|
|
|
button->setScale(1.2f);
|
2014-09-12 13:50:13 +08:00
|
|
|
});
|
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-13 22:08:02 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2014-09-13 22:08:02 +08:00
|
|
|
button2->ignoreContentAdaptWithSize(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
button2->setContentSize(Size(200.0f, 100.0f));
|
2016-10-27 09:45:40 +08:00
|
|
|
button2->setPositionNormalized(Vec2(0.8f, 0.5f));
|
2014-09-13 22:08:02 +08:00
|
|
|
button2->setTitleText("PLAY GAME");
|
2014-10-09 17:19:43 +08:00
|
|
|
button2->setZoomScale(0.3f);
|
2014-09-13 22:08:02 +08:00
|
|
|
button2->setPressedActionEnabled(true);
|
|
|
|
button2->addClickEventListener([=](Ref* sender) {
|
2014-10-09 17:19:43 +08:00
|
|
|
button2->runAction(ScaleTo::create(1.0f, 1.2f));
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("clicked!");
|
2014-09-13 22:08:02 +08:00
|
|
|
});
|
|
|
|
_uiLayer->addChild(button2);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-12 13:50:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
// UIButtonTitleEffectTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTitleEffectTest::UIButtonTitleEffectTest() : _displayValueLabel(nullptr) {}
|
2014-09-17 10:57:40 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonTitleEffectTest::~UIButtonTitleEffectTest() {}
|
2014-09-17 10:57:40 +08:00
|
|
|
|
|
|
|
bool UIButtonTitleEffectTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("Button Title Effect", "fonts/Marker Felt.ttf", 32);
|
2014-09-17 10:57:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2016-10-27 09:45:40 +08:00
|
|
|
button->setPositionNormalized(Vec2(0.3f, 0.5f));
|
2014-09-17 10:57:40 +08:00
|
|
|
button->setTitleText("PLAY GAME");
|
|
|
|
button->setTitleFontName("fonts/Marker Felt.ttf");
|
2014-10-09 17:19:43 +08:00
|
|
|
button->setZoomScale(0.3f);
|
|
|
|
button->setScale(2.0f);
|
2014-09-17 10:57:40 +08:00
|
|
|
button->setPressedActionEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
Label* title = button->getTitleRenderer();
|
2014-09-17 10:57:40 +08:00
|
|
|
button->setTitleColor(Color3B::RED);
|
2021-12-28 16:06:23 +08:00
|
|
|
title->enableShadow(Color4B::BLACK, Size(2, -2));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2016-10-27 09:45:40 +08:00
|
|
|
button2->setPositionNormalized(Vec2(0.8f, 0.5f));
|
2014-09-17 10:57:40 +08:00
|
|
|
button2->setTitleText("PLAY GAME");
|
|
|
|
auto title2 = button2->getTitleRenderer();
|
|
|
|
title2->enableOutline(Color4B::GREEN, 3);
|
|
|
|
_uiLayer->addChild(button2);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-09-17 10:57:40 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-05 20:50:13 +08:00
|
|
|
// UIButtonFlipTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonFlipTest::UIButtonFlipTest() : _displayValueLabel(nullptr) {}
|
2014-09-17 10:57:40 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonFlipTest::~UIButtonFlipTest() {}
|
2014-11-05 20:50:13 +08:00
|
|
|
|
|
|
|
bool UIButtonFlipTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-11-05 20:50:13 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("Button X Flipped", "fonts/Marker Felt.ttf", 20);
|
2016-10-27 09:45:40 +08:00
|
|
|
_displayValueLabel->setPositionNormalized(Vec2(0.3f, 0.7f));
|
2014-11-05 20:50:13 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-11-05 20:50:13 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2016-10-27 09:45:40 +08:00
|
|
|
button->setPositionNormalized(Vec2(0.3f, 0.5f));
|
2014-11-05 20:50:13 +08:00
|
|
|
button->setTitleText("PLAY GAME");
|
|
|
|
button->setTitleFontName("fonts/Marker Felt.ttf");
|
|
|
|
button->setZoomScale(0.3f);
|
|
|
|
button->setScale(2.0f);
|
|
|
|
button->setFlippedX(true);
|
|
|
|
button->setPressedActionEnabled(true);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-11-05 20:50:13 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-11-05 20:50:13 +08:00
|
|
|
// Create the button
|
2021-12-28 16:06:23 +08:00
|
|
|
auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
|
2016-10-27 09:45:40 +08:00
|
|
|
button2->setPositionNormalized(Vec2(0.8f, 0.5f));
|
2014-11-05 20:50:13 +08:00
|
|
|
button2->setTitleText("PLAY GAME");
|
|
|
|
button2->setFlippedY(true);
|
|
|
|
_uiLayer->addChild(button2);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-11-05 20:50:13 +08:00
|
|
|
auto titleLabel = Text::create("Button Y flipped", "Arial", 20);
|
2016-10-27 09:45:40 +08:00
|
|
|
titleLabel->setPositionNormalized(Vec2(0.8f, 0.7f));
|
2014-11-05 20:50:13 +08:00
|
|
|
this->addChild(titleLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonNormalDefaultTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonNormalDefaultTest::UIButtonNormalDefaultTest() : _displayValueLabel(nullptr) {}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonNormalDefaultTest::~UIButtonNormalDefaultTest() {}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
|
|
|
bool UIButtonNormalDefaultTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("", "fonts/Marker Felt.ttf", 32);
|
2014-12-23 15:03:31 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("Button should scale when clicked", "fonts/Marker Felt.ttf", 20);
|
2014-12-23 15:03:31 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the button
|
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png");
|
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f + 40));
|
|
|
|
button->setZoomScale(0.4f);
|
|
|
|
button->setPressedActionEnabled(true);
|
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the button
|
|
|
|
Button* buttonScale9 = Button::create("cocosui/button.png");
|
|
|
|
// open scale9 render
|
|
|
|
buttonScale9->setScale9Enabled(true);
|
|
|
|
buttonScale9->setPosition(Vec2(widgetSize.width / 2.0f + 50, widgetSize.height / 2.0f + 40));
|
2019-09-24 11:31:35 +08:00
|
|
|
buttonScale9->setContentSize(Size(150.0f, 70.0f));
|
2014-12-23 15:03:31 +08:00
|
|
|
buttonScale9->setPressedActionEnabled(true);
|
|
|
|
_uiLayer->addChild(buttonScale9);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonDisableDefaultTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonDisableDefaultTest::UIButtonDisableDefaultTest() : _displayValueLabel(nullptr) {}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonDisableDefaultTest::~UIButtonDisableDefaultTest() {}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
|
|
|
bool UIButtonDisableDefaultTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("", "fonts/Marker Felt.ttf", 32);
|
2014-12-23 15:03:31 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("Left button will turn normal when clicked", "fonts/Marker Felt.ttf", 20);
|
2014-12-23 15:03:31 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the button
|
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png");
|
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f + 40));
|
|
|
|
button->setZoomScale(0.4f);
|
|
|
|
button->setPressedActionEnabled(true);
|
|
|
|
button->setBright(false);
|
2015-07-15 14:35:13 +08:00
|
|
|
button->setScale9Enabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
button->setCapInsets(Rect(3, 3, 5, 5));
|
|
|
|
button->addClickEventListener([=](Ref*) { button->setBright(true); });
|
2014-12-23 15:03:31 +08:00
|
|
|
_uiLayer->addChild(button);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the button
|
|
|
|
Button* buttonScale9 = Button::create("cocosui/button.png");
|
|
|
|
// open scale9 render
|
|
|
|
buttonScale9->setScale9Enabled(true);
|
|
|
|
buttonScale9->setPosition(Vec2(widgetSize.width / 2.0f + 50, widgetSize.height / 2.0f + 40));
|
2019-09-24 11:31:35 +08:00
|
|
|
buttonScale9->setContentSize(Size(150.0f, 70.0f));
|
2014-12-23 15:03:31 +08:00
|
|
|
buttonScale9->setPressedActionEnabled(true);
|
|
|
|
buttonScale9->setEnabled(false);
|
|
|
|
buttonScale9->setBright(false);
|
|
|
|
_uiLayer->addChild(buttonScale9);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UIButtonCloneTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonCloneTest::UIButtonCloneTest() : _displayValueLabel(nullptr) {}
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonCloneTest::~UIButtonCloneTest() {}
|
2015-04-16 14:38:00 +08:00
|
|
|
|
|
|
|
bool UIButtonCloneTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
|
|
|
|
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("", "fonts/Marker Felt.ttf", 32);
|
2015-04-16 14:38:00 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2015-04-16 14:38:00 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("This test case shouldn't trigger the Assertion!", "fonts/Marker Felt.ttf", 20);
|
2015-04-16 14:38:00 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
|
2015-04-16 14:38:00 +08:00
|
|
|
|
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
// Create the button
|
|
|
|
Button* button = Button::create("cocosui/animationbuttonnormal.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f + 40));
|
2015-04-16 14:38:00 +08:00
|
|
|
_uiLayer->addChild(button);
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(button->getTitleRenderer() == nullptr, "Button title render must be nullptr ");
|
2015-04-16 14:38:00 +08:00
|
|
|
|
|
|
|
auto buttonCopy = (Button*)button->clone();
|
2021-12-28 16:06:23 +08:00
|
|
|
buttonCopy->setPosition(Vec2(widgetSize.width / 2.0f + 80, widgetSize.height / 2.0f + 40));
|
2015-04-16 14:38:00 +08:00
|
|
|
this->addChild(buttonCopy);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-12-09 14:21:26 +08:00
|
|
|
auto buttonScale9Copy = (Button*)button->clone();
|
2019-09-24 11:31:35 +08:00
|
|
|
buttonScale9Copy->setPosition(button->getPosition() + Vec2(0.0f, -60.0f));
|
2015-12-09 14:21:26 +08:00
|
|
|
buttonScale9Copy->setScale9Enabled(true);
|
2019-09-24 11:31:35 +08:00
|
|
|
buttonScale9Copy->setContentSize(button->getContentSize() * 1.5f);
|
2015-12-09 14:21:26 +08:00
|
|
|
this->addChild(buttonScale9Copy);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-12-09 14:21:26 +08:00
|
|
|
auto buttonScale9Copy2 = (Button*)buttonScale9Copy->clone();
|
2019-09-24 11:31:35 +08:00
|
|
|
buttonScale9Copy2->setPosition(buttonCopy->getPosition() + Vec2(0.0f, -60.0f));
|
2015-12-09 14:21:26 +08:00
|
|
|
buttonScale9Copy2->setScale9Enabled(true);
|
2019-09-24 11:31:35 +08:00
|
|
|
buttonScale9Copy2->setContentSize(buttonCopy->getContentSize() * 1.5f);
|
2015-12-09 14:21:26 +08:00
|
|
|
this->addChild(buttonScale9Copy2);
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(button->getTitleRenderer() == nullptr, "Original Button title render must be nullptr ");
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(buttonCopy->getTitleRenderer() == nullptr, "Copied Button title render must be nullptr ");
|
2015-04-16 14:38:00 +08:00
|
|
|
|
2014-11-05 20:50:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-23 17:27:04 +08:00
|
|
|
|
|
|
|
// https://github.com/cocos2d/cocos2d-x/issues/12249
|
2021-12-28 16:06:23 +08:00
|
|
|
Issue12249::Issue12249() : _displayValueLabel(nullptr) {}
|
2015-07-23 17:27:04 +08:00
|
|
|
|
|
|
|
bool Issue12249::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-23 17:27:04 +08:00
|
|
|
// Add a label in which the button events will be displayed
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel = Text::create("Test Issue 12249", "fonts/Marker Felt.ttf", 32);
|
2015-07-23 17:27:04 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2015-07-23 17:27:04 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-23 17:27:04 +08:00
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("This test case two buttons should seem to be same!", "fonts/Marker Felt.ttf", 20);
|
2015-07-23 17:27:04 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.0f));
|
|
|
|
|
2015-07-23 17:27:04 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-23 17:27:04 +08:00
|
|
|
// Create the button 1
|
2021-12-28 16:06:23 +08:00
|
|
|
Button *button = nullptr, *button2 = nullptr;
|
2015-07-23 17:27:04 +08:00
|
|
|
float btnWidth = 100;
|
2021-12-28 16:06:23 +08:00
|
|
|
button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
2015-07-23 17:27:04 +08:00
|
|
|
button->setScale9Enabled(true);
|
|
|
|
button->setContentSize(Size(btnWidth, button->getContentSize().height));
|
|
|
|
button->setTitleText("Scale9 Button 1");
|
|
|
|
button->setPosition(Vec2(widgetSize.width / 2.0f - btnWidth, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(button);
|
|
|
|
|
|
|
|
// create button 2, load texture after button creation
|
|
|
|
button2 = Button::create();
|
|
|
|
button2->setScale9Enabled(true);
|
|
|
|
button2->loadTextures("cocosui/button.png", "cocosui/buttonHighlighted.png", "");
|
|
|
|
button2->setContentSize(Size(btnWidth, button2->getContentSize().height));
|
|
|
|
button2->setTitleText("Scale9 Button 2");
|
|
|
|
button2->setPosition(Vec2(widgetSize.width / 2.0f + btnWidth, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(button2);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-23 17:27:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-10 15:28:47 +08:00
|
|
|
|
|
|
|
// https://github.com/cocos2d/cocos2d-x/issues/17116
|
2021-12-28 16:06:23 +08:00
|
|
|
Issue17116::Issue17116() {}
|
2017-01-10 15:28:47 +08:00
|
|
|
|
|
|
|
bool Issue17116::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
auto visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/issue_17116.plist");
|
|
|
|
auto button = ui::Button::create();
|
2021-08-19 13:43:18 +08:00
|
|
|
button->loadTextureNormal("issue_17116/buttons/play-big", ui::Widget::TextureResType::PLIST);
|
2021-12-28 16:06:23 +08:00
|
|
|
button->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
|
2017-01-10 15:28:47 +08:00
|
|
|
button->setOpacity(100);
|
|
|
|
addChild(button);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-20 09:59:30 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonWithPolygonInfo::UIButtonWithPolygonInfo() {}
|
2017-01-20 09:59:30 +08:00
|
|
|
|
|
|
|
bool UIButtonWithPolygonInfo::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
auto visibleSize = Director::getInstance()->getVisibleSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-01-20 09:59:30 +08:00
|
|
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/poly_test_textures.plist");
|
|
|
|
auto button = ui::Button::create();
|
|
|
|
button->loadTextureNormal("poly_test/wheel_disc_back.png", ui::Widget::TextureResType::PLIST);
|
2021-12-28 16:06:23 +08:00
|
|
|
button->setPosition(Vec2(visibleSize.width / 2 - 100, visibleSize.height / 2));
|
2017-01-20 09:59:30 +08:00
|
|
|
button->setScale(0.5);
|
|
|
|
button->setScale9Enabled(true);
|
|
|
|
addChild(button);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-01-20 09:59:30 +08:00
|
|
|
auto buttonCopy = button->clone();
|
2021-12-28 16:06:23 +08:00
|
|
|
button->setPosition(Vec2(visibleSize.width / 2 + 100, visibleSize.height / 2));
|
2017-01-20 09:59:30 +08:00
|
|
|
addChild(buttonCopy);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-01-20 09:59:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-17 15:52:01 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIButtonScale9ChangeSpriteFrame::UIButtonScale9ChangeSpriteFrame() {}
|
2017-07-17 15:52:01 +08:00
|
|
|
|
|
|
|
bool UIButtonScale9ChangeSpriteFrame::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
ImageView* sprite = ImageView::create("Images/blocks.png");
|
|
|
|
sprite->setScale9Enabled(true);
|
|
|
|
sprite->setCapInsets(Rect(32, 32, 32, 32));
|
|
|
|
sprite->setContentSize(Size(96 * 1.5, 96));
|
2019-09-24 11:31:35 +08:00
|
|
|
sprite->setNormalizedPosition(Vec2(0.2f, 0.5f));
|
2017-07-17 15:52:01 +08:00
|
|
|
this->addChild(sprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-07-17 15:52:01 +08:00
|
|
|
auto button = Button::create("cocosui/button.png", "cocosui/buttonHighlighted.png");
|
|
|
|
button->setScale9Enabled(true);
|
2019-09-24 11:31:35 +08:00
|
|
|
button->setContentSize(Size(100.0f, 50.0f));
|
|
|
|
button->setNormalizedPosition(Vec2(0.6f, 0.5f));
|
2017-07-17 15:52:01 +08:00
|
|
|
button->setTitleText("Press me");
|
|
|
|
addChild(button);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
button->addClickEventListener([sprite](Ref* button) { sprite->loadTexture("Images/blocks9.png"); });
|
|
|
|
|
2017-07-17 15:52:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|