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

301 lines
11 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-28 16:06:23 +08:00
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:
2021-12-28 16:06:23 +08:00
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.
2021-12-28 16:06:23 +08:00
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 "UITextTest.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;
UITextTests::UITextTests()
{
ADD_TEST_CASE(UITextTest);
ADD_TEST_CASE(UITextTest_LineWrap);
ADD_TEST_CASE(UILabelTest_Effect);
ADD_TEST_CASE(UITextTest_TTF);
ADD_TEST_CASE(UITextTest_IgnoreContentSize);
ADD_TEST_CASE(UITextTest_Clone);
ADD_TEST_CASE(Issue16073Test);
}
// UITextTest
bool UITextTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Text", "fonts/Marker Felt.ttf", 30);
2019-11-23 20:27:39 +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));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(alert);
// Create the text
Text* text = Text::create("Text", "AmericanTypewriter", 30);
2021-12-28 16:06:23 +08:00
text->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + text->getContentSize().height / 4.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(text);
return true;
}
return false;
}
// UITextTest_LineWrap
bool UITextTest_LineWrap::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Text line wrap", "fonts/Marker Felt.ttf", 30);
2019-11-23 20:27:39 +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));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(alert);
// Create the line wrap
2021-12-28 16:06:23 +08:00
Text* text = Text::create("TextArea Widget can line wrap", "AmericanTypewriter", 32);
2019-11-23 20:27:39 +08:00
text->ignoreContentAdaptWithSize(false);
text->setContentSize(Size(280.0f, 150.0f));
text->setTextHorizontalAlignment(TextHAlignment::CENTER);
text->setTouchScaleChangeEnabled(true);
text->setTouchEnabled(true);
2021-12-28 16:06:23 +08:00
text->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type) {
2019-11-23 20:27:39 +08:00
if (type == Widget::TouchEventType::ENDED)
{
if ((int)text->getContentSize().width == 280)
{
2021-12-28 16:06:23 +08:00
text->setContentSize(Size(380.0f, 100.0f));
}
else
2019-11-23 20:27:39 +08:00
{
text->setContentSize(Size(280.0f, 150.0f));
}
}
});
2021-12-28 16:06:23 +08:00
text->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - text->getContentSize().height / 8.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(text);
return true;
}
return false;
}
// UILabelTest_Effect
bool UILabelTest_Effect::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
Text* alert = Text::create();
alert->setString("Label Effect");
alert->setFontName("fonts/Marker Felt.ttf");
alert->setFontSize(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 * 3.05f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(alert);
// create the shadow only label
Text* shadow_label = Text::create();
shadow_label->enableShadow();
shadow_label->setString("Shadow");
shadow_label->setFontName("fonts/Marker Felt.ttf");
2021-12-28 16:06:23 +08:00
shadow_label->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + shadow_label->getContentSize().height + 20));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(shadow_label);
// create the stroke only label
Text* glow_label = Text::create();
glow_label->setFontName("fonts/Marker Felt.ttf");
glow_label->setString("Glow");
glow_label->enableGlow(Color4B::RED);
2021-12-28 16:06:23 +08:00
glow_label->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - 20));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(glow_label);
// create the label stroke and shadow
Text* outline_label = Text::create();
outline_label->setString("Outline");
2021-12-28 16:06:23 +08:00
CCLOG("content size without outline: %f %f", outline_label->getContentSize().width,
2019-11-23 20:27:39 +08:00
outline_label->getContentSize().height);
outline_label->enableOutline(Color4B::GREEN, 4);
2021-12-28 16:06:23 +08:00
outline_label->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - shadow_label->getContentSize().height - 50));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(outline_label);
2021-12-28 16:06:23 +08:00
CCLOG("content size after applying outline: %f %f", outline_label->getContentSize().width,
2019-11-23 20:27:39 +08:00
outline_label->getContentSize().height);
2021-12-28 16:06:23 +08:00
// create buttons to disable effect and add
auto disableOutlineBtn = Button::create();
2019-11-23 20:27:39 +08:00
disableOutlineBtn->setTitleText("Disable outline");
disableOutlineBtn->setTitleFontName("fonts/Marker Felt.ttf");
2021-12-28 16:06:23 +08:00
disableOutlineBtn->setPosition(Vec2(widgetSize.width * 0.3f, widgetSize.height * 0.7f));
2019-11-23 20:27:39 +08:00
disableOutlineBtn->setPressedActionEnabled(true);
2021-12-28 16:06:23 +08:00
disableOutlineBtn->addClickEventListener([=](Ref*) {
2019-11-23 20:27:39 +08:00
outline_label->disableEffect(LabelEffect::OUTLINE);
2021-12-28 16:06:23 +08:00
CCLOG("content size after disable outline: %f %f", outline_label->getContentSize().width,
2019-11-23 20:27:39 +08:00
outline_label->getContentSize().height);
});
this->addChild(disableOutlineBtn);
auto buttonWidth = disableOutlineBtn->getContentSize().width;
auto disableGlowBtn = (Button*)disableOutlineBtn->clone();
2021-12-28 16:06:23 +08:00
disableGlowBtn->setPosition(disableOutlineBtn->getPosition() + Vec2(buttonWidth + 40, 0.0f));
2019-11-23 20:27:39 +08:00
disableGlowBtn->setTitleText("Disable Glow");
2021-12-28 16:06:23 +08:00
disableGlowBtn->addClickEventListener([=](Ref*) { glow_label->disableEffect(LabelEffect::GLOW); });
2019-11-23 20:27:39 +08:00
this->addChild(disableGlowBtn);
auto disableShadowBtn = (Button*)disableGlowBtn->clone();
2021-12-28 16:06:23 +08:00
disableShadowBtn->setPosition(disableGlowBtn->getPosition() + Vec2(buttonWidth + 40, 0.0f));
2019-11-23 20:27:39 +08:00
disableShadowBtn->setTitleText("Disable Shadow");
2021-12-28 16:06:23 +08:00
disableShadowBtn->addClickEventListener([=](Ref*) { shadow_label->disableEffect(LabelEffect::SHADOW); });
2019-11-23 20:27:39 +08:00
this->addChild(disableShadowBtn);
return true;
}
return false;
}
// UITextTest_TTF
bool UITextTest_TTF::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("Text set TTF font", "fonts/Marker Felt.ttf", 30);
2019-11-23 20:27:39 +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));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(alert);
// Create the text, and set font with .ttf
2021-12-28 16:06:23 +08:00
Text* text = Text::create("Text", "fonts/A Damn Mess.ttf", 30);
text->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + text->getContentSize().height / 4.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(text);
return true;
}
return false;
}
// UITextTest_IgnoreContentSize
bool UITextTest_IgnoreContentSize::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
Text* leftText = Text::create("ignore content", "fonts/Marker Felt.ttf", 10);
leftText->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
leftText->ignoreContentAdaptWithSize(false);
2021-12-28 16:06:23 +08:00
leftText->setTextAreaSize(Size(60.0f, 60.0f));
2019-11-23 20:27:39 +08:00
leftText->setString("Text line with break\nText line with break\nText line with break\nText line with break\n");
leftText->setTouchScaleChangeEnabled(true);
leftText->setTouchEnabled(true);
_uiLayer->addChild(leftText);
2021-12-28 16:06:23 +08:00
Text* rightText = Text::create("ignore content", "fonts/Marker Felt.ttf", 10);
rightText->setPosition(Vec2(widgetSize.width / 2.0f + 50, widgetSize.height / 2.0f));
rightText->setString(
"Text line with break\nText line with break\nText line with break\nText line with break\n");
// note:setTextAreaSize must be used with ignoreContentAdaptWithSize(false)
rightText->setTextAreaSize(Size(100.0f, 30.0f));
2019-11-23 20:27:39 +08:00
rightText->ignoreContentAdaptWithSize(false);
_uiLayer->addChild(rightText);
auto halighButton = Button::create();
halighButton->setTitleText("Alignment Right");
2021-12-28 16:06:23 +08:00
halighButton->addClickEventListener([=](Ref*) {
2019-11-23 20:27:39 +08:00
leftText->setTextHorizontalAlignment(TextHAlignment::RIGHT);
rightText->setTextHorizontalAlignment(TextHAlignment::RIGHT);
});
2021-12-28 16:06:23 +08:00
halighButton->setPosition(Vec2(widgetSize.width / 2 - 50, widgetSize.height / 2 - 50));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(halighButton);
return true;
}
return false;
}
// UITextTest_IgnoreContentSize
bool UITextTest_Clone::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
Text* singleText = Text::create("ignore content", "fonts/Marker Felt.ttf", 30);
singleText->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
singleText->setString("CHUKONG");
singleText->setTouchScaleChangeEnabled(true);
singleText->setTouchEnabled(true);
2021-12-28 16:06:23 +08:00
singleText->enableOutline(Color4B(255, 0, 0, 100), 10);
singleText->enableShadow(Color4B::YELLOW, Size(2, -2), 0);
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(singleText);
auto cloneText = singleText->clone();
2021-12-28 16:06:23 +08:00
cloneText->setPosition(Vec2(widgetSize.width / 2.0f + 80, widgetSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(cloneText);
return true;
}
return false;
}
// Issue16073Test
bool Issue16073Test::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
Text* singleText = Text::create("mwhahaha\360", "Verdana", 40);
2021-12-28 16:06:23 +08:00
singleText->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
_uiLayer->addChild(singleText);
return true;
}
return false;
}