PuzzleSDK
UITextTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "UITextTest.h"
26
29using namespace cocos2d::ui;
30
31UITextTests::UITextTests()
32{
40}
41
42// UITextTest
43
45{
46 if (UIScene::init())
47 {
48 Size widgetSize = _widget->getContentSize();
49
50 Text* alert = Text::create("Text","fonts/Marker Felt.ttf", 30);
51 alert->setColor(Color3B(159, 168, 176));
52 alert->setPosition(Vec2(widgetSize.width / 2.0f,
53 widgetSize.height / 2.0f
54 - alert->getContentSize().height * 1.75f));
55 _uiLayer->addChild(alert);
56
57 // Create the text
58 Text* text = Text::create("Text", "AmericanTypewriter", 30);
59 text->setPosition(Vec2(widgetSize.width / 2.0f,
60 widgetSize.height / 2.0f
61 + text->getContentSize().height / 4.0f));
62 _uiLayer->addChild(text);
63
64 return true;
65 }
66 return false;
67}
68
69// UITextTest_LineWrap
70
72{
73 if (UIScene::init())
74 {
75 Size widgetSize = _widget->getContentSize();
76
77 Text* alert = Text::create("Text line wrap","fonts/Marker Felt.ttf",30);
78 alert->setColor(Color3B(159, 168, 176));
79 alert->setPosition(Vec2(widgetSize.width / 2.0f,
80 widgetSize.height / 2.0f
81 - alert->getContentSize().height * 1.75f));
82 _uiLayer->addChild(alert);
83
84 // Create the line wrap
85 Text* text = Text::create("TextArea Widget can line wrap",
86 "AmericanTypewriter",32);
87 text->ignoreContentAdaptWithSize(false);
88 text->setContentSize(Size(280.0f, 150.0f));
89 text->setTextHorizontalAlignment(TextHAlignment::CENTER);
90 text->setTouchScaleChangeEnabled(true);
91 text->setTouchEnabled(true);
92 text->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type)
93 {
94 if (type == Widget::TouchEventType::ENDED)
95 {
96 if ((int)text->getContentSize().width == 280)
97 {
98 text->setContentSize(Size(380.0f,100.0f));
99 }else
100 {
101 text->setContentSize(Size(280.0f, 150.0f));
102 }
103 }
104 });
105 text->setPosition(Vec2(widgetSize.width / 2.0f,
106 widgetSize.height / 2.0f
107 - text->getContentSize().height / 8.0f));
108 _uiLayer->addChild(text);
109
110 return true;
111 }
112 return false;
113}
114
115// UILabelTest_Effect
116
117
119{
120 if (UIScene::init())
121 {
122 Size widgetSize = _widget->getContentSize();
123
124 Text* alert = Text::create();
125 alert->setString("Label Effect");
126 alert->setFontName("fonts/Marker Felt.ttf");
127 alert->setFontSize(30);
128 alert->setColor(Color3B(159, 168, 176));
129 alert->setPosition(Vec2(widgetSize.width / 2.0f,
130 widgetSize.height / 2.0f
131 - alert->getContentSize().height * 3.05f));
132 _uiLayer->addChild(alert);
133
134
135 // create the shadow only label
136 Text* shadow_label = Text::create();
137
138 shadow_label->enableShadow();
139 shadow_label->setString("Shadow");
140 shadow_label->setFontName("fonts/Marker Felt.ttf");
141 shadow_label->setPosition(Vec2(widgetSize.width / 2.0f,
142 widgetSize.height / 2.0f
143 + shadow_label->getContentSize().height + 20));
144
145 _uiLayer->addChild(shadow_label);
146
147
148 // create the stroke only label
149 Text* glow_label = Text::create();
150 glow_label->setFontName("fonts/Marker Felt.ttf");
151 glow_label->setString("Glow");
152 glow_label->enableGlow(Color4B::RED);
153
154
155 glow_label->setPosition(Vec2(widgetSize.width / 2.0f,
156 widgetSize.height / 2.0f - 20));
157
158 _uiLayer->addChild(glow_label);
159
160
161 // create the label stroke and shadow
162 Text* outline_label = Text::create();
163 outline_label->setString("Outline");
164 CCLOG("content size without outline: %f %f",
165 outline_label->getContentSize().width,
166 outline_label->getContentSize().height);
167 outline_label->enableOutline(Color4B::GREEN, 4);
168 outline_label->setPosition(Vec2(widgetSize.width / 2.0f,
169 widgetSize.height / 2.0f
170 - shadow_label->getContentSize().height - 50));
171
172 _uiLayer->addChild(outline_label);
173 CCLOG("content size after applying outline: %f %f",
174 outline_label->getContentSize().width,
175 outline_label->getContentSize().height);
176
177 //create buttons to disable effect and add
178 auto disableOutlineBtn= Button::create();
179 disableOutlineBtn->setTitleText("Disable outline");
180 disableOutlineBtn->setTitleFontName("fonts/Marker Felt.ttf");
181 disableOutlineBtn->setPosition(Vec2(widgetSize.width * 0.3f,
182 widgetSize.height * 0.7f));
183 disableOutlineBtn->setPressedActionEnabled(true);
184 disableOutlineBtn->addClickEventListener([=](Ref*){
185 outline_label->disableEffect(LabelEffect::OUTLINE);
186 CCLOG("content size after disable outline: %f %f",
187 outline_label->getContentSize().width,
188 outline_label->getContentSize().height);
189 });
190 this->addChild(disableOutlineBtn);
191
192 auto buttonWidth = disableOutlineBtn->getContentSize().width;
193
194 auto disableGlowBtn = (Button*)disableOutlineBtn->clone();
195 disableGlowBtn->setPosition(disableOutlineBtn->getPosition()
196 + Vec2(buttonWidth + 40,0.0f));
197 disableGlowBtn->setTitleText("Disable Glow");
198 disableGlowBtn->addClickEventListener([=](Ref*){
199 glow_label->disableEffect(LabelEffect::GLOW);
200 });
201 this->addChild(disableGlowBtn);
202
203 auto disableShadowBtn = (Button*)disableGlowBtn->clone();
204 disableShadowBtn->setPosition(disableGlowBtn->getPosition()
205 + Vec2(buttonWidth + 40,0.0f));
206 disableShadowBtn->setTitleText("Disable Shadow");
207 disableShadowBtn->addClickEventListener([=](Ref*){
208 shadow_label->disableEffect(LabelEffect::SHADOW);
209 });
210 this->addChild(disableShadowBtn);
211
212
213 return true;
214 }
215 return false;
216}
217
218
219// UITextTest_TTF
220
222{
223 if (UIScene::init())
224 {
225 Size widgetSize = _widget->getContentSize();
226
227 Text* alert = Text::create("Text set TTF font",
228 "fonts/Marker Felt.ttf",30);
229 alert->setColor(Color3B(159, 168, 176));
230 alert->setPosition(Vec2(widgetSize.width / 2.0f,
231 widgetSize.height / 2.0f
232 - alert->getContentSize().height * 1.75f));
233 _uiLayer->addChild(alert);
234
235 // Create the text, and set font with .ttf
236 Text* text = Text::create("Text","fonts/A Damn Mess.ttf",30);
237 text->setPosition(Vec2(widgetSize.width / 2.0f,
238 widgetSize.height / 2.0f
239 + text->getContentSize().height / 4.0f));
240 _uiLayer->addChild(text);
241
242 return true;
243 }
244 return false;
245}
246
247// UITextTest_IgnoreContentSize
248
250{
251 if (UIScene::init())
252 {
253 Size widgetSize = _widget->getContentSize();
254
255 Text* leftText = Text::create("ignore content",
256 "fonts/Marker Felt.ttf",10);
257 leftText->setPosition(Vec2(widgetSize.width / 2.0f - 50,
258 widgetSize.height / 2.0f));
259 leftText->ignoreContentAdaptWithSize(false);
260 leftText->setTextAreaSize(Size(60.0f,60.0f));
261 leftText->setString("Text line with break\nText line with break\nText line with break\nText line with break\n");
262 leftText->setTouchScaleChangeEnabled(true);
263 leftText->setTouchEnabled(true);
264 _uiLayer->addChild(leftText);
265
266
267 Text* rightText = Text::create("ignore content",
268 "fonts/Marker Felt.ttf",10);
269 rightText->setPosition(Vec2(widgetSize.width / 2.0f + 50,
270 widgetSize.height / 2.0f));
271 rightText->setString("Text line with break\nText line with break\nText line with break\nText line with break\n");
272 //note:setTextAreaSize must be used with ignoreContentAdaptWithSize(false)
273 rightText->setTextAreaSize(Size(100.0f,30.0f));
274 rightText->ignoreContentAdaptWithSize(false);
275 _uiLayer->addChild(rightText);
276
277
278 auto halighButton = Button::create();
279 halighButton->setTitleText("Alignment Right");
280 halighButton->addClickEventListener([=](Ref*){
281 leftText->setTextHorizontalAlignment(TextHAlignment::RIGHT);
282 rightText->setTextHorizontalAlignment(TextHAlignment::RIGHT);
283 });
284 halighButton->setPosition(Vec2(widgetSize.width/2 - 50,
285 widgetSize.height/2 - 50));
286 _uiLayer->addChild(halighButton);
287
288 return true;
289 }
290 return false;
291}
292
293// UITextTest_IgnoreContentSize
294
296{
297 if (UIScene::init())
298 {
299 Size widgetSize = _widget->getContentSize();
300
301 Text* singleText = Text::create("ignore content",
302 "fonts/Marker Felt.ttf", 30);
303 singleText->setPosition(Vec2(widgetSize.width / 2.0f - 80,
304 widgetSize.height / 2.0f));
305 singleText->setString("CHUKONG");
306 singleText->setTouchScaleChangeEnabled(true);
307 singleText->setTouchEnabled(true);
308 singleText->enableOutline(Color4B(255,0,0,100), 10);
309 singleText->enableShadow(Color4B::YELLOW, Size(2,-2), 0);
310 _uiLayer->addChild(singleText);
311
312 auto cloneText = singleText->clone();
313 cloneText->setPosition(Vec2(widgetSize.width / 2.0f + 80,
314 widgetSize.height / 2.0f));
315 _uiLayer->addChild(cloneText);
316
317 return true;
318 }
319 return false;
320}
321
322// Issue16073Test
323
325{
326 if (UIScene::init())
327 {
328 Size widgetSize = _widget->getContentSize();
329
330 Text* singleText = Text::create("mwhahaha\360", "Verdana", 40);
331
332 singleText->setPosition(Vec2(widgetSize.width / 2.0f - 80,
333 widgetSize.height / 2.0f));
334 _uiLayer->addChild(singleText);
335
336 return true;
337 }
338 return false;
339}
340
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC_EXT
Definition: UITextTest.cpp:28
USING_NS_CC
Definition: UITextTest.cpp:27
virtual bool init() override
Definition: UITextTest.cpp:324
virtual bool init() override
Definition: UITextTest.cpp:118
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45
virtual bool init() override
Definition: UITextTest.cpp:295
virtual bool init() override
Definition: UITextTest.cpp:249
virtual bool init() override
Definition: UITextTest.cpp:71
virtual bool init() override
Definition: UITextTest.cpp:221
virtual bool init() override
Definition: UITextTest.cpp:44