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-07-09 22:23:34 +08:00
|
|
|
https://axis-project.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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
#include "TextInputTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-04-09 12:23:47 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
#define FONT_NAME "fonts/Thonburi.ttf"
|
|
|
|
#define FONT_SIZE 36
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
TextInputTests::TextInputTests()
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(TextFieldTTFDefaultTest);
|
|
|
|
ADD_TEST_CASE(TextFieldTTFActionTest);
|
2016-05-23 09:48:15 +08:00
|
|
|
ADD_TEST_CASE(TextFieldTTFSecureTextEntryTest);
|
2017-10-17 13:48:38 +08:00
|
|
|
ADD_TEST_CASE(TextFieldTTSetCursorFromPoint);
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
static Rect getRect(Node* node)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Rect rc;
|
2013-07-23 08:25:44 +08:00
|
|
|
rc.origin = node->getPosition();
|
2021-12-28 16:06:23 +08:00
|
|
|
rc.size = node->getContentSize();
|
2011-04-26 18:04:07 +08:00
|
|
|
rc.origin.x -= rc.size.width / 2;
|
|
|
|
rc.origin.y -= rc.size.height / 2;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
std::string KeyboardNotificationLayer::title() const
|
2011-04-28 14:19:12 +08:00
|
|
|
{
|
|
|
|
return "text input test";
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement KeyboardNotificationLayer
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
KeyboardNotificationLayer::KeyboardNotificationLayer() : _trackNode(0)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
// Register Touch Event
|
2021-12-28 16:06:23 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
listener->onTouchBegan = AX_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this);
|
|
|
|
listener->onTouchEnded = AX_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void KeyboardNotificationLayer::keyboardWillShow(IMEKeyboardNotificationInfo& info)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)", info.end.origin.x, info.end.origin.y,
|
2021-12-28 16:06:23 +08:00
|
|
|
info.end.size.width, info.end.size.height);
|
2011-04-27 17:01:27 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!_trackNode)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto rectTracked = getRect(_trackNode);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextInputTest:trackingNodeAt(origin:%f,%f, size:%f,%f)", rectTracked.origin.x, rectTracked.origin.y,
|
2021-12-28 16:06:23 +08:00
|
|
|
rectTracked.size.width, rectTracked.size.height);
|
2011-04-27 17:01:27 +08:00
|
|
|
|
|
|
|
// if the keyboard area doesn't intersect with the tracking node area, nothing need to do.
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!rectTracked.intersectsRect(info.end))
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-27 17:01:27 +08:00
|
|
|
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
|
2012-08-01 15:30:12 +08:00
|
|
|
float adjustVert = info.end.getMaxY() - rectTracked.getMinY();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert);
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2011-04-27 17:01:27 +08:00
|
|
|
// move all the children node of KeyboardNotificationLayer
|
2013-11-29 10:37:40 +08:00
|
|
|
auto& children = getChildren();
|
2021-12-28 16:06:23 +08:00
|
|
|
Node* node = 0;
|
|
|
|
ssize_t count = children.size();
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 pos;
|
2011-04-26 18:04:07 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
node = children.at(i);
|
2021-12-28 16:06:23 +08:00
|
|
|
pos = node->getPosition();
|
2011-04-26 18:04:07 +08:00
|
|
|
pos.y += adjustVert;
|
|
|
|
node->setPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// Layer function
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool KeyboardNotificationLayer::onTouchBegan(Touch* touch, Event* event)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("++++++++++++++++++++++++++++++++++++++++++++");
|
2021-12-28 16:06:23 +08:00
|
|
|
_beginPos = touch->getLocation();
|
2011-05-04 14:13:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void KeyboardNotificationLayer::onTouchEnded(Touch* touch, Event* event)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!_trackNode)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto endPos = touch->getLocation();
|
2011-05-04 14:13:15 +08:00
|
|
|
|
|
|
|
float delta = 5.0f;
|
2021-12-28 16:06:23 +08:00
|
|
|
if (std::abs(endPos.x - _beginPos.x) > delta || std::abs(endPos.y - _beginPos.y) > delta)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
// not click
|
2013-06-15 14:03:30 +08:00
|
|
|
_beginPos.x = _beginPos.y = -1;
|
2011-05-04 14:13:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide the trackNode is clicked.
|
2013-06-20 14:17:10 +08:00
|
|
|
Rect rect;
|
2015-04-14 18:04:35 +08:00
|
|
|
rect.size = _trackNode->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto clicked =
|
|
|
|
isScreenPointInRect(endPos, Camera::getVisitingCamera(), _trackNode->getWorldToNodeTransform(), rect, nullptr);
|
2017-10-17 13:48:38 +08:00
|
|
|
this->onClickTrackNode(clicked, endPos);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("----------------------------------");
|
2011-05-04 14:13:15 +08:00
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2011-05-04 14:13:15 +08:00
|
|
|
// implement TextFieldTTFDefaultTest
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TextFieldTTFDefaultTest::subtitle() const
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
return "TextFieldTTF with default behavior test";
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2017-10-17 13:48:38 +08:00
|
|
|
void TextFieldTTFDefaultTest::onClickTrackNode(bool bClicked, const Vec2& touchPos)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pTextField = (TextFieldTTF*)_trackNode;
|
2011-05-04 14:13:15 +08:00
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextFieldTTFDefaultTest:TextFieldTTF attachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->attachWithIME();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextFieldTTFDefaultTest:TextFieldTTF detachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFDefaultTest::onEnter()
|
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// add TextFieldTTF
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>", FONT_NAME, FONT_SIZE);
|
2011-05-04 14:13:15 +08:00
|
|
|
addChild(pTextField);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2013-06-20 14:17:10 +08:00
|
|
|
// on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
2017-03-17 10:44:38 +08:00
|
|
|
// so we had to set a higher position to make it visible
|
2021-12-28 16:06:23 +08:00
|
|
|
pTextField->setPosition(Vec2(s.width / 2, s.height / 2 + 50));
|
2011-05-12 21:32:57 +08:00
|
|
|
#else
|
2014-05-15 01:07:09 +08:00
|
|
|
pTextField->setPosition(Vec2(s.width / 2, s.height / 2));
|
2011-05-12 21:32:57 +08:00
|
|
|
#endif
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_trackNode = pTextField;
|
2011-05-04 14:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextFieldTTFActionTest
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TextFieldTTFActionTest::subtitle() const
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
return "CCTextFieldTTF with action and char limit test";
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:48:38 +08:00
|
|
|
void TextFieldTTFActionTest::onClickTrackNode(bool bClicked, const Vec2& touchPos)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pTextField = (TextFieldTTF*)_trackNode;
|
2011-05-04 14:13:15 +08:00
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextFieldTTFActionTest:TextFieldTTF attachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->attachWithIME();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextFieldTTFActionTest:TextFieldTTF detachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTFActionTest::onEnter()
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_charLimit = 12;
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_textFieldAction = RepeatForever::create(Sequence::create(FadeOut::create(0.25), FadeIn::create(0.25), nullptr));
|
2013-06-15 14:03:30 +08:00
|
|
|
_textFieldAction->retain();
|
|
|
|
_action = false;
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// add TextFieldTTF
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_textField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>", FONT_NAME, FONT_SIZE);
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_textField);
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textField->setDelegate(this);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2013-06-20 14:17:10 +08:00
|
|
|
// on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
2012-04-19 14:35:52 +08:00
|
|
|
// so we had to set a higher position
|
2021-12-28 16:06:23 +08:00
|
|
|
_textField->setPosition(Vec2(s.width / 2, s.height / 2 + 50));
|
2011-05-12 21:32:57 +08:00
|
|
|
#else
|
2014-05-15 01:07:09 +08:00
|
|
|
_textField->setPosition(Vec2(s.width / 2, s.height / 2));
|
2011-05-12 21:32:57 +08:00
|
|
|
#endif
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_trackNode = _textField;
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFActionTest::onExit()
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2011-05-11 18:45:53 +08:00
|
|
|
KeyboardNotificationLayer::onExit();
|
2013-06-15 14:03:30 +08:00
|
|
|
_textFieldAction->release();
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// TextFieldDelegate protocol
|
2021-12-28 16:06:23 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldAttachWithIME(TextFieldTTF* sender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!_action)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textField->runAction(_textFieldAction);
|
|
|
|
_action = true;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldDetachWithIME(TextFieldTTF* sender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_action)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textField->stopAction(_textFieldAction);
|
|
|
|
_textField->setOpacity(255);
|
|
|
|
_action = false;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldInsertText(TextFieldTTF* sender, const char* text, size_t nLen)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2011-05-11 18:22:55 +08:00
|
|
|
// if insert enter, treat as default to detach with ime
|
|
|
|
if ('\n' == *text)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// if the textfield's char count more than _charLimit, doesn't insert text anymore.
|
2013-07-26 06:53:24 +08:00
|
|
|
if (sender->getCharCount() >= _charLimit)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
|
|
|
return true;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a insert text sprite and do some action
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithSystemFont(text, FONT_NAME, FONT_SIZE);
|
2011-04-29 18:09:54 +08:00
|
|
|
this->addChild(label);
|
2013-07-07 21:08:14 +08:00
|
|
|
Color3B color(226, 121, 7);
|
2011-04-29 16:46:29 +08:00
|
|
|
label->setColor(color);
|
2011-04-29 18:09:54 +08:00
|
|
|
|
|
|
|
// move the sprite from top to position
|
2013-08-16 16:05:27 +08:00
|
|
|
auto endPos = sender->getPosition();
|
2013-07-26 06:53:24 +08:00
|
|
|
if (sender->getCharCount())
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
endPos.x += sender->getContentSize().width / 2;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
2013-08-16 16:05:27 +08:00
|
|
|
auto inputTextSize = label->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
Vec2 beginPos(endPos.x, Director::getInstance()->getWinSize().height - inputTextSize.height * 2);
|
2011-04-29 18:09:54 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
float duration = 0.5;
|
2011-04-29 18:09:54 +08:00
|
|
|
label->setPosition(beginPos);
|
|
|
|
label->setScale(8);
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto seq = Sequence::create(
|
2021-12-28 16:06:23 +08:00
|
|
|
Spawn::create(MoveTo::create(duration, endPos), ScaleTo::create(duration, 1), FadeOut::create(duration),
|
|
|
|
nullptr),
|
2022-07-15 19:17:01 +08:00
|
|
|
CallFuncN::create(AX_CALLBACK_1(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction, this)), nullptr);
|
2011-04-29 18:09:54 +08:00
|
|
|
label->runAction(seq);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldDeleteBackward(TextFieldTTF* sender, const char* delText, size_t nLen)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
|
|
|
// create a delete text sprite and do some action
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithSystemFont(delText, FONT_NAME, FONT_SIZE);
|
2011-04-29 18:09:54 +08:00
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
// move the sprite to fly out
|
2021-12-28 16:06:23 +08:00
|
|
|
auto beginPos = sender->getPosition();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto textfieldSize = sender->getContentSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto labelSize = label->getContentSize();
|
2011-04-29 18:09:54 +08:00
|
|
|
beginPos.x += (textfieldSize.width - labelSize.width) / 2.0f;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
Vec2 endPos(-winSize.width / 4.0f, winSize.height * (0.5 + (float)rand() / (2.0f * RAND_MAX)));
|
2011-04-29 18:09:54 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
float duration = 1;
|
2012-06-08 13:55:28 +08:00
|
|
|
float rotateDuration = 0.2f;
|
2021-12-28 16:06:23 +08:00
|
|
|
int repeatTime = 5;
|
2011-04-29 18:09:54 +08:00
|
|
|
label->setPosition(beginPos);
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto seq = Sequence::create(
|
2021-12-28 16:06:23 +08:00
|
|
|
Spawn::create(MoveTo::create(duration, endPos),
|
|
|
|
Repeat::create(RotateBy::create(rotateDuration, (rand() % 2) ? 360 : -360), repeatTime),
|
|
|
|
FadeOut::create(duration), nullptr),
|
2022-07-15 19:17:01 +08:00
|
|
|
CallFuncN::create(AX_CALLBACK_1(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction, this)), nullptr);
|
2011-04-29 18:09:54 +08:00
|
|
|
label->runAction(seq);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool TextFieldTTFActionTest::onDraw(TextFieldTTF* sender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction(Node* node)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
this->removeChild(node, true);
|
2011-04-29 16:46:29 +08:00
|
|
|
}
|
2016-05-23 09:48:15 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextFieldTTFSecureTextEntryTest
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
std::string TextFieldTTFSecureTextEntryTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "TextFieldTTF with SecureTextEntry test";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTFSecureTextEntryTest::onEnter()
|
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2016-05-23 09:48:15 +08:00
|
|
|
// add TextFieldTTF
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>", FONT_NAME, FONT_SIZE);
|
2016-05-23 09:48:15 +08:00
|
|
|
addChild(pTextField);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2016-05-23 09:48:15 +08:00
|
|
|
// on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
2017-03-17 10:44:38 +08:00
|
|
|
// so we had to set a higher position to make it visible
|
2021-12-28 16:06:23 +08:00
|
|
|
pTextField->setPosition(Vec2(s.width / 2, s.height / 2 + 50));
|
2016-05-23 09:48:15 +08:00
|
|
|
#else
|
|
|
|
pTextField->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
#endif
|
|
|
|
pTextField->setSecureTextEntry(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2017-10-17 13:48:38 +08:00
|
|
|
_trackNode = pTextField;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextFieldTTSetCursorFromPoint
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
std::string TextFieldTTSetCursorFromPoint::subtitle() const
|
|
|
|
{
|
|
|
|
return "TextFieldTTF with setCursorFromPoint test";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTSetCursorFromPoint::onClickTrackNode(bool bClicked, const Vec2& touchPos)
|
|
|
|
{
|
|
|
|
auto pTextField = (TextFieldTTF*)_trackNode;
|
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextFieldTTSetCursorFromPoint:TextFieldTTF attachWithIME");
|
2017-10-17 13:48:38 +08:00
|
|
|
pTextField->attachWithIME();
|
|
|
|
|
|
|
|
// Set new position cursor
|
|
|
|
pTextField->setCursorFromPoint(touchPos, Camera::getVisitingCamera());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("TextFieldTTSetCursorFromPoint:TextFieldTTF detachWithIME");
|
2017-10-17 13:48:38 +08:00
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTSetCursorFromPoint::onEnter()
|
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
|
|
|
// add TextFieldTTF
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>", FONT_NAME, FONT_SIZE);
|
2017-10-17 13:48:38 +08:00
|
|
|
addChild(pTextField);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID)
|
2017-10-17 13:48:38 +08:00
|
|
|
// on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
|
|
|
// so we had to set a higher position to make it visible
|
|
|
|
pTextField->setPosition(Vec2(s.width / 2, s.height / 2 + 50));
|
|
|
|
#else
|
|
|
|
pTextField->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
#endif
|
|
|
|
|
2016-05-23 09:48:15 +08:00
|
|
|
_trackNode = pTextField;
|
|
|
|
}
|