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

387 lines
17 KiB
C++
Raw Normal View History

2014-08-19 10:28:24 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
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
2014-08-19 10:28:24 +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
2014-08-19 10:28:24 +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
2014-08-19 10:28:24 +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 "UIEditBoxTest.h"
#include "testResource.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;
UIEditBoxTests::UIEditBoxTests()
2014-08-19 10:28:24 +08:00
{
ADD_TEST_CASE(UIEditBoxTest);
ADD_TEST_CASE(UIEditBoxTestToggleVisibility);
ADD_TEST_CASE(UIEditBoxTestTextHorizontalAlignment);
ADD_TEST_CASE(UIEditBoxTestPressedAndDisabled);
2014-08-19 10:28:24 +08:00
}
// UIEditBoxTest
2014-08-19 10:28:24 +08:00
bool UIEditBoxTest::init()
{
if (UIScene::init())
{
2021-12-28 16:06:23 +08:00
auto glview = Director::getInstance()->getOpenGLView();
2014-08-19 10:28:24 +08:00
auto visibleOrigin = glview->getVisibleOrigin();
2021-12-28 16:06:23 +08:00
auto visibleSize = glview->getVisibleSize();
2014-08-19 10:28:24 +08:00
auto pBg = Sprite::create("Images/HelloWorld.png");
2021-12-28 16:06:23 +08:00
pBg->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2));
2014-08-19 10:28:24 +08:00
addChild(pBg);
2021-12-28 16:06:23 +08:00
2014-08-19 10:28:24 +08:00
_TTFShowEditReturn = Label::createWithSystemFont("No edit control return!", "Arial", 30);
2021-12-28 16:06:23 +08:00
_TTFShowEditReturn->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height - 50));
2014-08-19 10:28:24 +08:00
addChild(_TTFShowEditReturn);
2021-12-28 16:06:23 +08:00
2015-09-02 11:04:50 +08:00
auto editBoxSize = Size(visibleSize.width - 100, visibleSize.height * 0.1);
2021-12-28 16:06:23 +08:00
2014-08-19 10:28:24 +08:00
// top
std::string pNormalSprite = "extensions/green_edit.png";
2021-12-28 16:06:23 +08:00
_editName = ui::EditBox::create(editBoxSize + Size(0, 40), ui::Scale9Sprite::create(pNormalSprite));
_editName->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, visibleOrigin.y + visibleSize.height * 3 / 4));
2014-08-19 10:28:24 +08:00
_editName->setFontColor(Color3B::RED);
_editName->setPlaceHolder("Name:");
_editName->setPlaceholderFontColor(Color3B::WHITE);
_editName->setMaxLength(8);
2021-12-28 16:06:23 +08:00
_editName->setFontSize((int)editBoxSize.height / 2);
_editName->setText("v👐👊💝");
2014-08-19 10:28:24 +08:00
_editName->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
_editName->setDelegate(this);
2016-02-25 11:05:18 +08:00
_editName->setVisible(true);
2014-08-19 10:28:24 +08:00
addChild(_editName);
2021-12-28 16:06:23 +08:00
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
auto buttonSize = button->getContentSize();
button->setTitleText("Single Line");
2021-12-28 16:06:23 +08:00
button->setPosition(_editName->getPosition() + Vec2(editBoxSize.width / 2 + buttonSize.width / 2, 0.0f));
button->addClickEventListener([=](Ref* ref) { _editName->setInputMode(ui::EditBox::InputMode::SINGLE_LINE); });
addChild(button);
2014-08-19 10:28:24 +08:00
// middle
2021-12-28 16:06:23 +08:00
_editPassword =
ui::EditBox::create(Size(editBoxSize.width, editBoxSize.height + 20), "extensions/orange_edit.png");
_editPassword->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, visibleOrigin.y + visibleSize.height / 2));
2014-08-19 10:28:24 +08:00
_editPassword->setFontColor(Color3B::GREEN);
_editPassword->setPlaceHolder("Password:");
_editPassword->setMaxLength(6);
_editPassword->setInputFlag(ui::EditBox::InputFlag::PASSWORD);
_editPassword->setInputMode(ui::EditBox::InputMode::SINGLE_LINE);
2021-12-28 16:06:23 +08:00
_editPassword->setFontSize((int)editBoxSize.height / 2);
2014-08-19 10:28:24 +08:00
_editPassword->setDelegate(this);
2016-02-25 11:05:18 +08:00
_editPassword->setVisible(true);
2014-08-19 10:28:24 +08:00
addChild(_editPassword);
2021-12-28 16:06:23 +08:00
auto buttonPassword = (ui::Button*)button->clone();
buttonPassword->setTitleText("Multiline");
2021-12-28 16:06:23 +08:00
buttonPassword->setPosition(_editPassword->getPosition() +
Vec2(editBoxSize.width / 2 + buttonSize.width / 2, 0.0f));
buttonPassword->addClickEventListener(
[=](Ref* ref) { _editPassword->setInputMode(ui::EditBox::InputMode::ANY); });
addChild(buttonPassword);
2021-12-28 16:06:23 +08:00
2014-08-19 10:28:24 +08:00
// bottom
// Add an intermediate Node to test scaling and content size relative to world
_editEmailParent = Node::create();
2021-12-28 16:06:23 +08:00
// _editEmailParent->setScale(0.5);
_editEmailParent->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, visibleOrigin.y + visibleSize.height / 4));
addChild(_editEmailParent);
2021-12-28 16:06:23 +08:00
auto bottomButtonSize = Size(editBoxSize.width, editBoxSize.height + 10);
2021-12-28 16:06:23 +08:00
_editEmail = ui::EditBox::create(bottomButtonSize, "extensions/yellow_edit.png");
2014-08-19 10:28:24 +08:00
_editEmail->setPlaceHolder("Email:");
_editEmail->setInputMode(ui::EditBox::InputMode::EMAIL_ADDRESS);
_editEmail->setDelegate(this);
2016-02-25 11:05:18 +08:00
_editEmail->setVisible(true);
_editEmailParent->addChild(_editEmail);
2021-12-28 16:06:23 +08:00
// It is required to use setFontSize and setContentSize after adding it to the hierarchy, so that native EditBox
// get the right size
_editEmail->setFontSize(30);
_editEmail->setContentSize(bottomButtonSize);
2021-12-28 16:06:23 +08:00
auto buttonEmail = (ui::Button*)button->clone();
buttonEmail->setTitleText("Multiline");
2021-12-28 16:06:23 +08:00
buttonEmail->setPosition(_editEmailParent->getPosition() +
Vec2(editBoxSize.width / 2 + buttonSize.width / 2, 0.0f));
buttonEmail->addClickEventListener([=](Ref* ref) { _editEmail->setInputMode(ui::EditBox::InputMode::ANY); });
addChild(buttonEmail);
2021-12-28 16:06:23 +08:00
2014-08-19 10:28:24 +08:00
return true;
}
return false;
}
void UIEditBoxTest::editBoxEditingDidBegin(cocos2d::ui::EditBox* editBox)
{
log("editBox %p DidBegin !", editBox);
}
2021-12-28 16:06:23 +08:00
void UIEditBoxTest::editBoxEditingDidEndWithAction(cocos2d::ui::EditBox* editBox,
cocos2d::ui::EditBoxDelegate::EditBoxEndAction action)
{
log("editBox %p DidEnd with action %d!", editBox, action);
}
2021-12-28 15:58:56 +08:00
void UIEditBoxTest::editBoxTextChanged(cocos2d::ui::EditBox* editBox, std::string_view text)
2014-08-19 10:28:24 +08:00
{
2021-12-28 15:58:56 +08:00
log("editBox %p TextChanged, text: %s ", editBox, text.data());
editBox->setText(text.data());
2014-08-19 10:28:24 +08:00
}
void UIEditBoxTest::editBoxReturn(ui::EditBox* editBox)
{
2021-12-28 16:06:23 +08:00
log("editBox %p was returned !", editBox);
2014-08-19 10:28:24 +08:00
if (_editName == editBox)
{
_TTFShowEditReturn->setString("Name EditBox return !");
}
else if (_editPassword == editBox)
{
_TTFShowEditReturn->setString("Password EditBox return !");
}
else if (_editEmail == editBox)
{
_TTFShowEditReturn->setString("Email EditBox return !");
}
}
// UIEditBoxTestToggleVisibility
bool UIEditBoxTestToggleVisibility::init()
{
if (UIScene::init())
{
2021-12-28 16:06:23 +08:00
auto glview = Director::getInstance()->getOpenGLView();
auto visibleOrigin = glview->getVisibleOrigin();
2021-12-28 16:06:23 +08:00
auto visibleSize = glview->getVisibleSize();
auto pBg = Sprite::create("Images/HelloWorld.png");
2021-12-28 16:06:23 +08:00
pBg->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2));
addChild(pBg);
2021-12-28 16:06:23 +08:00
_TTFShowEditReturn = Label::createWithSystemFont("No edit control return!", "Arial", 30);
2021-12-28 16:06:23 +08:00
_TTFShowEditReturn->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height - 50));
addChild(_TTFShowEditReturn);
2021-12-28 16:06:23 +08:00
2019-09-20 11:20:07 +08:00
auto editBoxSize = Size(visibleSize.width - 100, visibleSize.height * 0.1f);
2021-12-28 16:06:23 +08:00
// top
std::string pNormalSprite = "extensions/green_edit.png";
2021-12-28 16:06:23 +08:00
_editName = ui::EditBox::create(editBoxSize + Size(0, 40), ui::Scale9Sprite::create(pNormalSprite));
_editName->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, visibleOrigin.y + visibleSize.height * 3 / 4));
_editName->setFontColor(Color3B::RED);
_editName->setPlaceHolder("Name:");
_editName->setPlaceholderFontColor(Color3B::WHITE);
_editName->setMaxLength(8);
2021-12-28 16:06:23 +08:00
_editName->setFontSize((int)editBoxSize.height / 2);
_editName->setText("v👐👊💝");
_editName->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
_editName->setDelegate(this);
_editName->setVisible(true);
addChild(_editName);
2021-12-28 16:06:23 +08:00
Button* button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png");
auto buttonSize = button->getContentSize();
button->setTitleText("Toggle Visibility");
2021-12-28 16:06:23 +08:00
button->setPosition(_editName->getPosition() + Vec2(editBoxSize.width / 2 + buttonSize.width / 2, 0.0f));
button->addClickEventListener([=](Ref* ref) { _editName->setVisible(!_editName->isVisible()); });
addChild(button);
2021-12-28 16:06:23 +08:00
// middle
_editPassword = ui::EditBox::create(editBoxSize, "extensions/orange_edit.png");
2021-12-28 16:06:23 +08:00
_editPassword->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, visibleOrigin.y + visibleSize.height / 2));
_editPassword->setFontColor(Color3B::GREEN);
_editPassword->setPlaceHolder("Password:");
_editPassword->setMaxLength(6);
_editPassword->setInputFlag(ui::EditBox::InputFlag::PASSWORD);
_editPassword->setInputMode(ui::EditBox::InputMode::SINGLE_LINE);
2021-12-28 16:06:23 +08:00
_editPassword->setFontSize((float)editBoxSize.height / 2);
_editPassword->setDelegate(this);
_editPassword->setVisible(true);
addChild(_editPassword);
2021-12-28 16:06:23 +08:00
auto buttonPassword = (ui::Button*)button->clone();
buttonPassword->setTitleText("Toggle Visibility");
2021-12-28 16:06:23 +08:00
buttonPassword->setPosition(_editPassword->getPosition() +
Vec2(editBoxSize.width / 2 + buttonSize.width / 2, 0.0f));
buttonPassword->addClickEventListener(
[=](Ref* ref) { _editPassword->setVisible(!_editPassword->isVisible()); });
addChild(buttonPassword);
2021-12-28 16:06:23 +08:00
// bottom
// Add an intermediate Node to test scaling and content size relative to world
_editEmailParent = Node::create();
// _editEmailParent->setScale(0.5);
2021-12-28 16:06:23 +08:00
_editEmailParent->setPosition(
Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, visibleOrigin.y + visibleSize.height / 4));
addChild(_editEmailParent);
2021-12-28 16:06:23 +08:00
auto bottomButtonSize = Size(editBoxSize.width, editBoxSize.height + 10);
2021-12-28 16:06:23 +08:00
_editEmail = ui::EditBox::create(bottomButtonSize, "extensions/yellow_edit.png");
_editEmail->setPlaceHolder("Email:");
_editEmail->setInputMode(ui::EditBox::InputMode::EMAIL_ADDRESS);
_editEmail->setDelegate(this);
_editEmail->setVisible(true);
_editEmailParent->addChild(_editEmail);
2021-12-28 16:06:23 +08:00
// It is required to use setFontSize and setContentSize after adding it to the hierarchy, so that native EditBox
// get the right size
_editEmail->setFontSize(30);
_editEmail->setContentSize(bottomButtonSize);
2021-12-28 16:06:23 +08:00
auto buttonEmail = (ui::Button*)button->clone();
buttonEmail->setTitleText("Toggle Visibility");
2021-12-28 16:06:23 +08:00
buttonEmail->setPosition(_editEmailParent->getPosition() +
Vec2(editBoxSize.width / 2 + buttonSize.width / 2, 0.0f));
buttonEmail->addClickEventListener([=](Ref* ref) { _editEmail->setVisible(!_editEmail->isVisible()); });
addChild(buttonEmail);
2021-12-28 16:06:23 +08:00
return true;
}
return false;
}
void UIEditBoxTestToggleVisibility::editBoxEditingDidBegin(cocos2d::ui::EditBox* editBox)
{
log("editBox %p DidBegin !", editBox);
}
2021-12-28 15:58:56 +08:00
void UIEditBoxTestToggleVisibility::editBoxTextChanged(cocos2d::ui::EditBox* editBox, std::string_view text)
{
2021-12-28 15:58:56 +08:00
log("editBox %p TextChanged, text: %s ", editBox, text.data());
}
void UIEditBoxTestToggleVisibility::editBoxReturn(ui::EditBox* editBox)
{
2021-12-28 16:06:23 +08:00
log("editBox %p was returned !", editBox);
if (_editName == editBox)
{
_TTFShowEditReturn->setString("Name EditBox return !");
}
else if (_editPassword == editBox)
{
_TTFShowEditReturn->setString("Password EditBox return !");
}
else if (_editEmail == editBox)
{
_TTFShowEditReturn->setString("Email EditBox return !");
}
}
// UIEditBoxTestTextHorizontalAlignment
2021-12-28 16:06:23 +08:00
bool UIEditBoxTestTextHorizontalAlignment::init()
{
if (!UIScene::init())
{
return false;
}
2021-12-28 16:06:23 +08:00
const auto glview = Director::getInstance()->getOpenGLView();
const auto visibleOrigin = glview->getVisibleOrigin();
2021-12-28 16:06:23 +08:00
const auto visibleSize = glview->getVisibleSize();
const auto editBoxSize = Size(visibleSize.width - 100, visibleSize.height * 0.1f);
const auto createEditBox = [this, editBoxSize, visibleOrigin, visibleSize](
std::string_view text, const TextHAlignment alignment, const int position_y) {
ui::EditBox* editbox =
ui::EditBox::create(editBoxSize + Size(0, 40), ui::Scale9Sprite::create("extensions/green_edit.png"));
editbox->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2 - 50, (float)position_y));
editbox->setFontColor(Color3B::RED);
2021-12-28 15:58:56 +08:00
editbox->setPlaceHolder(text.data());
editbox->setPlaceholderFontColor(Color3B::WHITE);
2021-12-28 16:06:23 +08:00
editbox->setPlaceholderFontSize((int)editBoxSize.height / 2);
editbox->setFontSize((int)editBoxSize.height / 2);
2021-12-28 15:58:56 +08:00
editbox->setText(text.data());
editbox->setTextHorizontalAlignment(alignment);
editbox->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
addChild(editbox);
};
2021-12-28 16:06:23 +08:00
createEditBox("horizontal left text", TextHAlignment::LEFT, (int)(visibleOrigin.y + visibleSize.height * 3 / 4));
createEditBox("horizontal center text", TextHAlignment::CENTER, (int)(visibleOrigin.y + visibleSize.height / 2));
createEditBox("horizontal right text", TextHAlignment::RIGHT, (int)(visibleOrigin.y + visibleSize.height / 4));
return true;
}
// UIEditBoxTestPressedAndDisabled
2021-12-28 16:06:23 +08:00
bool UIEditBoxTestPressedAndDisabled::init()
{
if (!UIScene::init())
{
return false;
}
2021-12-28 16:06:23 +08:00
auto glview = Director::getInstance()->getOpenGLView();
auto visibleOrigin = glview->getVisibleOrigin();
auto visibleSize = glview->getVisibleSize();
2019-09-20 11:20:07 +08:00
const auto editBoxSize = Size(visibleSize.width - 100, visibleSize.height * 0.1f);
2021-12-28 16:06:23 +08:00
ui::EditBox* editbox =
ui::EditBox::create(editBoxSize + Size(0, 40), "extensions/yellow_edit.png", "extensions/orange_edit.png");
editbox->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height * 3 / 4));
editbox->setFontColor(Color3B::GREEN);
editbox->setPlaceHolder("Test Pressed Image");
editbox->setPlaceholderFontColor(Color3B::WHITE);
2021-12-28 16:06:23 +08:00
editbox->setPlaceholderFontSize((int)editBoxSize.height / 2);
editbox->setFontSize((int)editBoxSize.height / 2);
editbox->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
addChild(editbox);
2021-12-28 16:06:23 +08:00
editbox = ui::EditBox::create(editBoxSize + Size(0, 40), "extensions/yellow_edit.png", "extensions/orange_edit.png",
"extensions/green_edit.png");
editbox->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 2));
editbox->setFontColor(Color3B::GREEN);
editbox->setPlaceHolder("Test Disabled Image - green");
editbox->setPlaceholderFontColor(Color3B::WHITE);
2021-12-28 16:06:23 +08:00
editbox->setPlaceholderFontSize((int)editBoxSize.height / 2);
editbox->setFontSize((int)editBoxSize.height / 2);
editbox->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
addChild(editbox);
editbox->setEnabled(false);
2021-12-28 16:06:23 +08:00
editbox = ui::EditBox::create(editBoxSize + Size(0, 40), "extensions/yellow_edit.png");
editbox->setPosition(Vec2(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height / 4));
editbox->setFontColor(Color3B::GREEN);
editbox->setPlaceHolder("Disabled Gray effect");
editbox->setPlaceholderFontColor(Color3B::WHITE);
2021-12-28 16:06:23 +08:00
editbox->setPlaceholderFontSize((int)editBoxSize.height / 2);
editbox->setFontSize((int)editBoxSize.height / 2);
editbox->setReturnType(ui::EditBox::KeyboardReturnType::DONE);
addChild(editbox);
editbox->setEnabled(false);
return true;
}