PuzzleSDK
TextInputTest.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 "TextInputTest.h"
26
28
29#define FONT_NAME "fonts/Thonburi.ttf"
30#define FONT_SIZE 36
31
32TextInputTests::TextInputTests()
33{
38}
39
40static Rect getRect(Node * node)
41{
42 Rect rc;
43 rc.origin = node->getPosition();
44 rc.size = node->getContentSize();
45 rc.origin.x -= rc.size.width / 2;
46 rc.origin.y -= rc.size.height / 2;
47 return rc;
48}
49
51{
52 return "text input test";
53}
54
56// implement KeyboardNotificationLayer
58
60: _trackNode(0)
61{
62 // Register Touch Event
63 auto listener = EventListenerTouchOneByOne::create();
64 listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this);
65 listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this);
66 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
67}
68
69void KeyboardNotificationLayer::keyboardWillShow(IMEKeyboardNotificationInfo& info)
70{
71 CCLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)",
72 info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height);
73
74 if (! _trackNode)
75 {
76 return;
77 }
78
79 auto rectTracked = getRect(_trackNode);
80 CCLOG("TextInputTest:trackingNodeAt(origin:%f,%f, size:%f,%f)",
81 rectTracked.origin.x, rectTracked.origin.y, rectTracked.size.width, rectTracked.size.height);
82
83 // if the keyboard area doesn't intersect with the tracking node area, nothing need to do.
84 if (! rectTracked.intersectsRect(info.end))
85 {
86 return;
87 }
88
89 // assume keyboard at the bottom of screen, calculate the vertical adjustment.
90 float adjustVert = info.end.getMaxY() - rectTracked.getMinY();
91 CCLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert);
92
93 // move all the children node of KeyboardNotificationLayer
94 auto& children = getChildren();
95 Node * node = 0;
96 ssize_t count = children.size();
97 Vec2 pos;
98 for (int i = 0; i < count; ++i)
99 {
100 node = children.at(i);
101 pos = node->getPosition();
102 pos.y += adjustVert;
103 node->setPosition(pos);
104 }
105}
106
107// Layer function
108
109bool KeyboardNotificationLayer::onTouchBegan(Touch *touch, Event *event)
110{
111 CCLOG("++++++++++++++++++++++++++++++++++++++++++++");
112 _beginPos = touch->getLocation();
113 return true;
114}
115
116void KeyboardNotificationLayer::onTouchEnded(Touch *touch, Event *event)
117{
118 if (! _trackNode)
119 {
120 return;
121 }
122
123 auto endPos = touch->getLocation();
124
125 float delta = 5.0f;
126 if (std::abs(endPos.x - _beginPos.x) > delta
127 || std::abs(endPos.y - _beginPos.y) > delta)
128 {
129 // not click
130 _beginPos.x = _beginPos.y = -1;
131 return;
132 }
133
134 // decide the trackNode is clicked.
135 Rect rect;
136 rect.size = _trackNode->getContentSize();
137 auto clicked = isScreenPointInRect(endPos, Camera::getVisitingCamera(), _trackNode->getWorldToNodeTransform(), rect, nullptr);
138 this->onClickTrackNode(clicked, endPos);
139 CCLOG("----------------------------------");
140}
141
143// implement TextFieldTTFDefaultTest
145
147{
148 return "TextFieldTTF with default behavior test";
149}
150
151void TextFieldTTFDefaultTest::onClickTrackNode(bool bClicked, const Vec2& touchPos)
152{
153 auto pTextField = (TextFieldTTF*)_trackNode;
154 if (bClicked)
155 {
156 // TextFieldTTFTest be clicked
157 CCLOG("TextFieldTTFDefaultTest:TextFieldTTF attachWithIME");
158 pTextField->attachWithIME();
159 }
160 else
161 {
162 // TextFieldTTFTest not be clicked
163 CCLOG("TextFieldTTFDefaultTest:TextFieldTTF detachWithIME");
164 pTextField->detachWithIME();
165 }
166}
167
169{
171
172 // add TextFieldTTF
173 auto s = Director::getInstance()->getWinSize();
174
175 auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
176 FONT_NAME,
177 FONT_SIZE);
178 addChild(pTextField);
179
180#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
181 // on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
182 // so we had to set a higher position to make it visible
183 pTextField->setPosition(Vec2(s.width / 2, s.height/2 + 50));
184#else
185 pTextField->setPosition(Vec2(s.width / 2, s.height / 2));
186#endif
187
188 _trackNode = pTextField;
189}
190
192// implement TextFieldTTFActionTest
194
196{
197 return "CCTextFieldTTF with action and char limit test";
198}
199
200void TextFieldTTFActionTest::onClickTrackNode(bool bClicked, const Vec2& touchPos)
201{
202 auto pTextField = (TextFieldTTF*)_trackNode;
203 if (bClicked)
204 {
205 // TextFieldTTFTest be clicked
206 CCLOG("TextFieldTTFActionTest:TextFieldTTF attachWithIME");
207 pTextField->attachWithIME();
208 }
209 else
210 {
211 // TextFieldTTFTest not be clicked
212 CCLOG("TextFieldTTFActionTest:TextFieldTTF detachWithIME");
213 pTextField->detachWithIME();
214 }
215}
216
218{
220
221 _charLimit = 12;
222
223 _textFieldAction = RepeatForever::create(
224 Sequence::create(
225 FadeOut::create(0.25),
226 FadeIn::create(0.25),
227 nullptr
228 ));
229 _textFieldAction->retain();
230 _action = false;
231
232 // add TextFieldTTF
233 auto s = Director::getInstance()->getWinSize();
234
235 _textField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
236 FONT_NAME,
237 FONT_SIZE);
238 addChild(_textField);
239
240 _textField->setDelegate(this);
241
242#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
243 // on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
244 // so we had to set a higher position
245 _textField->setPosition(Vec2(s.width / 2, s.height/2 + 50));
246#else
247 _textField->setPosition(Vec2(s.width / 2, s.height / 2));
248#endif
249
251}
252
254{
255 KeyboardNotificationLayer::onExit();
256 _textFieldAction->release();
257}
258
259// TextFieldDelegate protocol
261{
262 if (! _action)
263 {
264 _textField->runAction(_textFieldAction);
265 _action = true;
266 }
267 return false;
268}
269
271{
272 if (_action)
273 {
274 _textField->stopAction(_textFieldAction);
275 _textField->setOpacity(255);
276 _action = false;
277 }
278 return false;
279}
280
281bool TextFieldTTFActionTest::onTextFieldInsertText(TextFieldTTF * sender, const char * text, size_t nLen)
282{
283 // if insert enter, treat as default to detach with ime
284 if ('\n' == *text)
285 {
286 return false;
287 }
288
289 // if the textfield's char count more than _charLimit, doesn't insert text anymore.
290 if (sender->getCharCount() >= _charLimit)
291 {
292 return true;
293 }
294
295 // create a insert text sprite and do some action
296 auto label = Label::createWithSystemFont(text, FONT_NAME, FONT_SIZE);
297 this->addChild(label);
298 Color3B color(226, 121, 7);
299 label->setColor(color);
300
301 // move the sprite from top to position
302 auto endPos = sender->getPosition();
303 if (sender->getCharCount())
304 {
305 endPos.x += sender->getContentSize().width / 2;
306 }
307 auto inputTextSize = label->getContentSize();
308 Vec2 beginPos(endPos.x, Director::getInstance()->getWinSize().height - inputTextSize.height * 2);
309
310 float duration = 0.5;
311 label->setPosition(beginPos);
312 label->setScale(8);
313
314 auto seq = Sequence::create(
315 Spawn::create(
316 MoveTo::create(duration, endPos),
317 ScaleTo::create(duration, 1),
318 FadeOut::create(duration),
319 nullptr),
320 CallFuncN::create(CC_CALLBACK_1(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction, this)),
321 nullptr);
322 label->runAction(seq);
323 return false;
324}
325
326bool TextFieldTTFActionTest::onTextFieldDeleteBackward(TextFieldTTF * sender, const char * delText, size_t nLen)
327{
328 // create a delete text sprite and do some action
329 auto label = Label::createWithSystemFont(delText, FONT_NAME, FONT_SIZE);
330 this->addChild(label);
331
332 // move the sprite to fly out
333 auto beginPos = sender->getPosition();
334 auto textfieldSize = sender->getContentSize();
335 auto labelSize = label->getContentSize();
336 beginPos.x += (textfieldSize.width - labelSize.width) / 2.0f;
337
338 auto winSize = Director::getInstance()->getWinSize();
339 Vec2 endPos(- winSize.width / 4.0f, winSize.height * (0.5 + (float)rand() / (2.0f * RAND_MAX)));
340
341 float duration = 1;
342 float rotateDuration = 0.2f;
343 int repeatTime = 5;
344 label->setPosition(beginPos);
345
346 auto seq = Sequence::create(
347 Spawn::create(
348 MoveTo::create(duration, endPos),
349 Repeat::create(
350 RotateBy::create(rotateDuration, (rand()%2) ? 360 : -360),
351 repeatTime),
352 FadeOut::create(duration),
353 nullptr),
354 CallFuncN::create(CC_CALLBACK_1(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction, this)),
355 nullptr);
356 label->runAction(seq);
357 return false;
358}
359
360bool TextFieldTTFActionTest::onDraw(TextFieldTTF * sender)
361{
362 return false;
363}
364
366{
367 this->removeChild(node, true);
368}
369
370
372// implement TextFieldTTFSecureTextEntryTest
374
376{
377 return "TextFieldTTF with SecureTextEntry test";
378}
379
381{
383
384 // add TextFieldTTF
385 auto s = Director::getInstance()->getWinSize();
386
387 auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
388 FONT_NAME,
389 FONT_SIZE);
390 addChild(pTextField);
391
392#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
393 // on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
394 // so we had to set a higher position to make it visible
395 pTextField->setPosition(Vec2(s.width / 2, s.height/2 + 50));
396#else
397 pTextField->setPosition(Vec2(s.width / 2, s.height / 2));
398#endif
399 pTextField->setSecureTextEntry(true);
400
401 _trackNode = pTextField;
402}
403
405// implement TextFieldTTSetCursorFromPoint
407
409{
410 return "TextFieldTTF with setCursorFromPoint test";
411}
412
413void TextFieldTTSetCursorFromPoint::onClickTrackNode(bool bClicked, const Vec2& touchPos)
414{
415 auto pTextField = (TextFieldTTF*)_trackNode;
416 if (bClicked)
417 {
418 // TextFieldTTFTest be clicked
419 CCLOG("TextFieldTTSetCursorFromPoint:TextFieldTTF attachWithIME");
420 pTextField->attachWithIME();
421
422 // Set new position cursor
423 pTextField->setCursorFromPoint(touchPos, Camera::getVisitingCamera());
424 }
425 else
426 {
427 // TextFieldTTFTest not be clicked
428 CCLOG("TextFieldTTSetCursorFromPoint:TextFieldTTF detachWithIME");
429 pTextField->detachWithIME();
430 }
431}
432
434{
436
437 // add TextFieldTTF
438 auto s = Director::getInstance()->getWinSize();
439
440 auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
441 FONT_NAME,
442 FONT_SIZE);
443 addChild(pTextField);
444
445#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
446 // on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
447 // so we had to set a higher position to make it visible
448 pTextField->setPosition(Vec2(s.width / 2, s.height / 2 + 50));
449#else
450 pTextField->setPosition(Vec2(s.width / 2, s.height / 2));
451#endif
452
453 _trackNode = pTextField;
454}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
static Rect getRect(Node *node)
#define FONT_NAME
#define FONT_SIZE
USING_NS_CC
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
virtual void onClickTrackNode(bool bClicked, const cocos2d::Vec2 &touchPos)=0
void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
virtual void keyboardWillShow(cocos2d::IMEKeyboardNotificationInfo &info) override
virtual std::string title() const override
cocos2d::Node * _trackNode
Definition: TextInputTest.h:51
virtual void onEnter() override
Definition: BaseTest.cpp:430
void callbackRemoveNodeWhenDidAction(Node *node)
virtual void onClickTrackNode(bool bClicked, const cocos2d::Vec2 &touchPos) override
cocos2d::TextFieldTTF * _textField
Definition: TextInputTest.h:77
virtual bool onTextFieldAttachWithIME(cocos2d::TextFieldTTF *sender) override
virtual bool onTextFieldDetachWithIME(cocos2d::TextFieldTTF *sender) override
virtual bool onTextFieldDeleteBackward(cocos2d::TextFieldTTF *sender, const char *delText, size_t nLen) override
virtual bool onTextFieldInsertText(cocos2d::TextFieldTTF *sender, const char *text, size_t nLen) override
virtual std::string subtitle() const override
virtual void onExit() override
virtual bool onDraw(cocos2d::TextFieldTTF *sender)
virtual void onEnter() override
cocos2d::Action * _textFieldAction
Definition: TextInputTest.h:78
virtual std::string subtitle() const override
virtual void onClickTrackNode(bool bClicked, const cocos2d::Vec2 &touchPos) override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onEnter() override
virtual void onClickTrackNode(bool bClicked, const cocos2d::Vec2 &touchPos) override
virtual std::string subtitle() const override