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-02-24 18:45:25 +08:00
|
|
|
https://adxeproject.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 "UICheckBoxTest.h"
|
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
using namespace cocos2d::ui;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
UICheckBoxTests::UICheckBoxTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UICheckBoxTest);
|
|
|
|
ADD_TEST_CASE(UICheckBoxDefaultBehaviorTest);
|
2015-06-04 10:09:17 +08:00
|
|
|
ADD_TEST_CASE(UICheckBoxCopyTest);
|
2015-04-03 14:31:03 +08:00
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// UICheckBoxTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UICheckBoxTest::UICheckBoxTest() : _displayValueLabel(nullptr) {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UICheckBoxTest::~UICheckBoxTest() {}
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
bool UICheckBoxTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2016-09-12 09:45:34 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Add a label in which the checkbox events will be displayed
|
2014-04-03 10:34:20 +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);
|
2021-12-28 16:06:23 +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("CheckBox", "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));
|
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
// Create the checkbox
|
2021-12-28 16:06:23 +08:00
|
|
|
_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");
|
2015-11-30 16:17:13 +08:00
|
|
|
_checkBox->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-11-30 16:17:13 +08:00
|
|
|
_checkBox->addEventListener(CC_CALLBACK_2(UICheckBoxTest::selectedEvent, this));
|
|
|
|
_uiLayer->addChild(_checkBox);
|
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
|
2021-12-28 16:06:23 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UICheckBoxTest::printWidgetResources, this));
|
|
|
|
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);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void UICheckBoxTest::selectedEvent(Ref* pSender, CheckBox::EventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
case CheckBox::EventType::SELECTED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Selected"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CheckBox::EventType::UNSELECTED:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("Unselected"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
2015-12-01 11:07:02 +08:00
|
|
|
void UICheckBoxTest::printWidgetResources(cocos2d::Ref* sender)
|
2015-11-30 16:17:13 +08:00
|
|
|
{
|
2015-12-10 14:27:39 +08:00
|
|
|
cocos2d::ResourceData backGroundFileName = _checkBox->getBackNormalFile();
|
2021-12-28 16:06:23 +08:00
|
|
|
CCLOG("backGroundFile Name : %s, Type: %d", backGroundFileName.file.c_str(), backGroundFileName.type);
|
2015-12-10 14:27:39 +08:00
|
|
|
cocos2d::ResourceData backGroundSelectedFileName = _checkBox->getBackPressedFile();
|
2021-12-28 16:06:23 +08:00
|
|
|
CCLOG("backGroundSelectedFile Name : %s, Type: %d", backGroundSelectedFileName.file.c_str(),
|
|
|
|
backGroundSelectedFileName.type);
|
2015-12-10 14:27:39 +08:00
|
|
|
cocos2d::ResourceData backGroundDisabledFileName = _checkBox->getBackDisabledFile();
|
2021-12-28 16:06:23 +08:00
|
|
|
CCLOG("backGroundDisabledFile Name : %s, Type: %d", backGroundDisabledFileName.file.c_str(),
|
|
|
|
backGroundDisabledFileName.type);
|
2015-12-10 14:27:39 +08:00
|
|
|
cocos2d::ResourceData frontCrossFileName = _checkBox->getCrossNormalFile();
|
2015-11-30 16:17:13 +08:00
|
|
|
CCLOG("frontCrossFile Name : %s, Type: %d", frontCrossFileName.file.c_str(), frontCrossFileName.type);
|
2015-12-10 14:27:39 +08:00
|
|
|
cocos2d::ResourceData frontCrossDisabledFileName = _checkBox->getCrossDisabledFile();
|
2021-12-28 16:06:23 +08:00
|
|
|
CCLOG("frontCrossDisabledFile Name : %s, Type: %d", frontCrossDisabledFileName.file.c_str(),
|
|
|
|
frontCrossDisabledFileName.type);
|
2015-11-30 16:17:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// UICheckBoxDefaultBehaviorTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UICheckBoxDefaultBehaviorTest::UICheckBoxDefaultBehaviorTest() : _displayValueLabel(nullptr) {}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UICheckBoxDefaultBehaviorTest::~UICheckBoxDefaultBehaviorTest() {}
|
2014-12-23 15:03:31 +08:00
|
|
|
|
|
|
|
bool UICheckBoxDefaultBehaviorTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2016-09-12 09:45:34 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Add a label in which the checkbox events will be displayed
|
|
|
|
_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-12-23 15:03:31 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-28 16:06:23 +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("Only left two and the last checkbox can be clicked!", "fonts/Marker Felt.ttf", 20);
|
2014-12-23 15:03:31 +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));
|
2014-12-23 15:03:31 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the checkbox
|
2021-12-28 16:06:23 +08:00
|
|
|
CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
|
2014-12-23 15:03:31 +08:00
|
|
|
checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
_uiLayer->addChild(checkBox);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the checkbox
|
2021-12-28 16:06:23 +08:00
|
|
|
CheckBox* checkBox2 = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
|
2014-12-23 15:03:31 +08:00
|
|
|
checkBox2->setPosition(Vec2(widgetSize.width / 2.0f - 150, widgetSize.height / 2.0f));
|
|
|
|
checkBox2->ignoreContentAdaptWithSize(false);
|
2019-09-24 11:31:35 +08:00
|
|
|
checkBox2->setZoomScale(0.5f);
|
2021-12-28 16:06:23 +08:00
|
|
|
checkBox2->setContentSize(Size(80.0f, 80.0f));
|
2014-12-23 15:03:31 +08:00
|
|
|
checkBox2->setName("bigCheckBox");
|
|
|
|
_uiLayer->addChild(checkBox2);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-12-23 15:03:31 +08:00
|
|
|
// Create the checkbox
|
2021-12-28 16:06:23 +08:00
|
|
|
CheckBox* checkBoxDisabled = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
|
2014-12-23 15:03:31 +08:00
|
|
|
checkBoxDisabled->setPosition(Vec2(widgetSize.width / 2.0f + 20, widgetSize.height / 2.0f));
|
|
|
|
checkBoxDisabled->setEnabled(false);
|
|
|
|
checkBoxDisabled->setBright(false);
|
|
|
|
_uiLayer->addChild(checkBoxDisabled);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
CheckBox* checkBoxDisabled2 = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
|
2014-12-23 15:03:31 +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;
|
|
|
|
}
|
|
|
|
|
2015-06-04 10:09:17 +08:00
|
|
|
// UICheckBoxCopyTest
|
2021-12-28 16:06:23 +08:00
|
|
|
UICheckBoxCopyTest::UICheckBoxCopyTest() : _displayValueLabel(nullptr) {}
|
2015-06-04 10:09:17 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UICheckBoxCopyTest::~UICheckBoxCopyTest() {}
|
2015-06-04 10:09:17 +08:00
|
|
|
|
|
|
|
bool UICheckBoxCopyTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2016-09-12 09:45:34 +08:00
|
|
|
Size widgetSize = _widget->getContentSize();
|
2015-06-04 10:09:17 +08:00
|
|
|
|
|
|
|
// Add a label in which the checkbox events will be displayed
|
|
|
|
_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));
|
2015-06-04 10:09:17 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
2021-12-28 16:06:23 +08:00
|
|
|
Text* alert = Text::create("Two checkbox are identical.", "fonts/Marker Felt.ttf", 20);
|
2015-06-04 10:09:17 +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-06-04 10:09:17 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
// Create the checkbox
|
2021-12-28 16:06:23 +08:00
|
|
|
CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png", "cocosui/check_box_active.png");
|
2015-06-04 10:09:17 +08:00
|
|
|
checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
|
|
|
|
|
|
|
|
_uiLayer->addChild(checkBox);
|
|
|
|
|
|
|
|
auto checkboxCopy = checkBox->clone();
|
2021-12-28 16:06:23 +08:00
|
|
|
checkboxCopy->setPosition(checkBox->getPosition() + Vec2(50.0f, 0.0f));
|
2015-06-04 10:09:17 +08:00
|
|
|
_uiLayer->addChild(checkboxCopy);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|