axmol/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest.cpp

212 lines
8.9 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2019-11-23 20:27:39 +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:
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2019-11-23 20:27:39 +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.
****************************************************************************/
#include "UICheckBoxTest.h"
USING_NS_AX;
using namespace ax::ui;
2019-11-23 20:27:39 +08:00
UICheckBoxTests::UICheckBoxTests()
{
ADD_TEST_CASE(UICheckBoxTest);
ADD_TEST_CASE(UICheckBoxDefaultBehaviorTest);
ADD_TEST_CASE(UICheckBoxCopyTest);
}
// UICheckBoxTest
UICheckBoxTest::UICheckBoxTest() : _displayValueLabel(nullptr) {}
2019-11-23 20:27:39 +08:00
UICheckBoxTest::~UICheckBoxTest() {}
2019-11-23 20:27:39 +08:00
bool UICheckBoxTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2019-11-23 20:27:39 +08:00
// Add a label in which the checkbox 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);
2019-11-23 20:27:39 +08:00
// Add the alert
Text* alert = Text::create("CheckBox", "fonts/Marker Felt.ttf", 30);
2019-11-23 20:27:39 +08:00
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);
2019-11-23 20:27:39 +08:00
// Create the checkbox
_checkBox = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_normal_press.png",
"cocosui/check_box_active.png", "cocosui/check_box_normal_disable.png",
"cocosui/check_box_active_disable.png");
2019-11-23 20:27:39 +08:00
_checkBox->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
2022-07-16 10:43:05 +08:00
_checkBox->addEventListener(AX_CALLBACK_2(UICheckBoxTest::selectedEvent, this));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(_checkBox);
TTFConfig ttfConfig("fonts/arial.ttf", 15);
auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
2022-07-16 10:43:05 +08:00
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(UICheckBoxTest::printWidgetResources, this));
item1->setPosition(
Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
2019-11-23 20:27:39 +08:00
auto pMenu1 = Menu::create(item1, nullptr);
pMenu1->setPosition(Vec2(0.0f, 0.0f));
this->addChild(pMenu1, 10);
2019-11-23 20:27:39 +08:00
return true;
}
return false;
}
void UICheckBoxTest::selectedEvent(Ref* pSender, CheckBox::EventType type)
2019-11-23 20:27:39 +08:00
{
switch (type)
{
case CheckBox::EventType::SELECTED:
_displayValueLabel->setString(StringUtils::format("Selected"));
break;
case CheckBox::EventType::UNSELECTED:
_displayValueLabel->setString(StringUtils::format("Unselected"));
break;
default:
break;
2019-11-23 20:27:39 +08:00
}
}
void UICheckBoxTest::printWidgetResources(axis::Ref* sender)
2019-11-23 20:27:39 +08:00
{
axis::ResourceData backGroundFileName = _checkBox->getBackNormalFile();
2022-07-16 10:43:05 +08:00
AXLOG("backGroundFile Name : %s, Type: %d", backGroundFileName.file.c_str(), backGroundFileName.type);
axis::ResourceData backGroundSelectedFileName = _checkBox->getBackPressedFile();
2022-07-16 10:43:05 +08:00
AXLOG("backGroundSelectedFile Name : %s, Type: %d", backGroundSelectedFileName.file.c_str(),
backGroundSelectedFileName.type);
axis::ResourceData backGroundDisabledFileName = _checkBox->getBackDisabledFile();
2022-07-16 10:43:05 +08:00
AXLOG("backGroundDisabledFile Name : %s, Type: %d", backGroundDisabledFileName.file.c_str(),
backGroundDisabledFileName.type);
axis::ResourceData frontCrossFileName = _checkBox->getCrossNormalFile();
2022-07-16 10:43:05 +08:00
AXLOG("frontCrossFile Name : %s, Type: %d", frontCrossFileName.file.c_str(), frontCrossFileName.type);
axis::ResourceData frontCrossDisabledFileName = _checkBox->getCrossDisabledFile();
2022-07-16 10:43:05 +08:00
AXLOG("frontCrossDisabledFile Name : %s, Type: %d", frontCrossDisabledFileName.file.c_str(),
frontCrossDisabledFileName.type);
2019-11-23 20:27:39 +08:00
}
// UICheckBoxDefaultBehaviorTest
UICheckBoxDefaultBehaviorTest::UICheckBoxDefaultBehaviorTest() : _displayValueLabel(nullptr) {}
2019-11-23 20:27:39 +08:00
UICheckBoxDefaultBehaviorTest::~UICheckBoxDefaultBehaviorTest() {}
2019-11-23 20:27:39 +08:00
bool UICheckBoxDefaultBehaviorTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2019-11-23 20:27:39 +08:00
// Add a label in which the checkbox 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);
2019-11-23 20:27:39 +08:00
// Add the alert
Text* alert = Text::create("Only left two and the last checkbox can be clicked!", "fonts/Marker Felt.ttf", 20);
2019-11-23 20:27:39 +08:00
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(alert);
2019-11-23 20:27:39 +08:00
// Create the checkbox
CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
2019-11-23 20:27:39 +08:00
checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(checkBox);
2019-11-23 20:27:39 +08:00
// Create the checkbox
CheckBox* checkBox2 = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
2019-11-23 20:27:39 +08:00
checkBox2->setPosition(Vec2(widgetSize.width / 2.0f - 150, widgetSize.height / 2.0f));
checkBox2->ignoreContentAdaptWithSize(false);
checkBox2->setZoomScale(0.5f);
checkBox2->setContentSize(Size(80.0f, 80.0f));
2019-11-23 20:27:39 +08:00
checkBox2->setName("bigCheckBox");
_uiLayer->addChild(checkBox2);
2019-11-23 20:27:39 +08:00
// Create the checkbox
CheckBox* checkBoxDisabled = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
2019-11-23 20:27:39 +08:00
checkBoxDisabled->setPosition(Vec2(widgetSize.width / 2.0f + 20, widgetSize.height / 2.0f));
checkBoxDisabled->setEnabled(false);
checkBoxDisabled->setBright(false);
_uiLayer->addChild(checkBoxDisabled);
CheckBox* checkBoxDisabled2 = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
2019-11-23 20:27:39 +08:00
checkBoxDisabled2->setPosition(Vec2(widgetSize.width / 2.0f + 70, widgetSize.height / 2.0f));
checkBoxDisabled2->setBright(false);
checkBoxDisabled2->setSelected(true);
_uiLayer->addChild(checkBoxDisabled2);
return true;
}
return false;
}
// UICheckBoxCopyTest
UICheckBoxCopyTest::UICheckBoxCopyTest() : _displayValueLabel(nullptr) {}
2019-11-23 20:27:39 +08:00
UICheckBoxCopyTest::~UICheckBoxCopyTest() {}
2019-11-23 20:27:39 +08:00
bool UICheckBoxCopyTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the checkbox 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("Two checkbox are identical.", "fonts/Marker Felt.ttf", 20);
2019-11-23 20:27:39 +08:00
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(alert);
// Create the checkbox
CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
2019-11-23 20:27:39 +08:00
checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
_uiLayer->addChild(checkBox);
auto checkboxCopy = checkBox->clone();
checkboxCopy->setPosition(checkBox->getPosition() + Vec2(50.0f, 0.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(checkboxCopy);
return true;
}
return false;
}