2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCTextFieldTTF.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-03-07 19:22:03 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "2d/CCSprite.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
#define CURSOR_TIME_SHOW_HIDE 0.5f
|
|
|
|
#define CURSOR_DEFAULT_CHAR '|'
|
2016-05-23 09:48:15 +08:00
|
|
|
#define PASSWORD_STYLE_TEXT_DEFAULT "\xe2\x80\xa2"
|
2016-11-09 10:34:50 +08:00
|
|
|
static std::size_t _calcCharCount(const char * text)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
char ch = 0;
|
2013-12-18 17:47:20 +08:00
|
|
|
while ((ch = *text))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_BREAK_IF(! ch);
|
2013-04-19 09:55:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (0x80 != (0xC0 & ch))
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
++text;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool TextFieldDelegate::onTextFieldAttachWithIME(TextFieldTTF* /*sender*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldDetachWithIME(TextFieldTTF* /*sender*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldInsertText(TextFieldTTF* /*sender*/, const char* /*text*/, size_t /*nLen*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldDeleteBackward(TextFieldTTF* /*sender*/, const char* /*delText*/, size_t /*nLen*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onVisit(TextFieldTTF* /*sender*/, Renderer* /*renderer*/, const Mat4& /*transform*/, uint32_t /*flags*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// constructor and destructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextFieldTTF::TextFieldTTF()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _delegate(0)
|
|
|
|
, _charCount(0)
|
2013-11-08 03:58:48 +08:00
|
|
|
, _inputText("")
|
2014-03-14 21:06:40 +08:00
|
|
|
, _placeHolder("") // prevent Label initWithString assertion
|
2014-03-31 11:16:12 +08:00
|
|
|
, _colorText(Color4B::WHITE)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _secureTextEntry(false)
|
2016-05-23 09:48:15 +08:00
|
|
|
, _passwordStyleText(PASSWORD_STYLE_TEXT_DEFAULT)
|
2016-03-11 20:09:50 +08:00
|
|
|
, _cursorEnabled(false)
|
2016-03-07 19:22:03 +08:00
|
|
|
, _cursorPosition(0)
|
|
|
|
, _cursorChar(CURSOR_DEFAULT_CHAR)
|
|
|
|
, _cursorShowingTime(0.0f)
|
|
|
|
, _isAttachWithIME(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127;
|
2014-03-31 11:16:12 +08:00
|
|
|
_colorSpaceHolder.a = 255;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TextFieldTTF::~TextFieldTTF()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// static constructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-08 03:58:48 +08:00
|
|
|
TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize)
|
2013-04-19 09:55:29 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextFieldTTF *ret = new (std::nothrow) TextFieldTTF();
|
2013-11-08 03:58:48 +08:00
|
|
|
if(ret && ret->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
ret->autorelease();
|
|
|
|
if (placeholder.size()>0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
ret->setPlaceHolder(placeholder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-08 03:58:48 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-08 03:58:48 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 03:58:48 +08:00
|
|
|
TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextFieldTTF *ret = new (std::nothrow) TextFieldTTF();
|
2014-03-14 21:06:40 +08:00
|
|
|
if(ret && ret->initWithPlaceHolder("", fontName, fontSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
ret->autorelease();
|
|
|
|
if (placeholder.size()>0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
ret->setPlaceHolder(placeholder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-08 03:58:48 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-08 03:58:48 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// initialize
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-08 03:58:48 +08:00
|
|
|
bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-03-07 19:22:03 +08:00
|
|
|
setDimensions(dimensions.width, dimensions.height);
|
|
|
|
setAlignment(alignment, TextVAlignment::CENTER);
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
return initWithPlaceHolder(placeholder, fontName, fontSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-08 03:58:48 +08:00
|
|
|
bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2016-03-07 19:22:03 +08:00
|
|
|
_placeHolder = placeholder;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// If fontName is ttf file and it corrected, use TTFConfig
|
|
|
|
if (FileUtils::getInstance()->isFileExist(fontName))
|
|
|
|
{
|
2016-05-04 03:18:25 +08:00
|
|
|
TTFConfig ttfConfig(fontName, fontSize, GlyphCollection::DYNAMIC);
|
2016-03-07 19:22:03 +08:00
|
|
|
if (setTTFConfig(ttfConfig))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSystemFontName(fontName);
|
|
|
|
setSystemFontSize(fontSize);
|
|
|
|
|
|
|
|
} while (false);
|
2017-12-29 11:24:02 +08:00
|
|
|
|
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
2014-03-14 21:06:40 +08:00
|
|
|
Label::setString(_placeHolder);
|
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
|
|
|
// On desktop default enable cursor
|
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
setCursorEnabled(true);
|
2016-03-07 19:22:03 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-06-20 14:13:12 +08:00
|
|
|
// IMEDelegate
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TextFieldTTF::attachWithIME()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = IMEDelegate::attachWithIME();
|
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// open keyboard
|
2014-07-31 00:53:04 +08:00
|
|
|
auto pGlView = Director::getInstance()->getOpenGLView();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pGlView)
|
|
|
|
{
|
|
|
|
pGlView->setIMEKeyboardState(true);
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TextFieldTTF::detachWithIME()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = IMEDelegate::detachWithIME();
|
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// close keyboard
|
2014-07-31 00:53:04 +08:00
|
|
|
auto glView = Director::getInstance()->getOpenGLView();
|
2013-12-18 17:47:20 +08:00
|
|
|
if (glView)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
glView->setIMEKeyboardState(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
void TextFieldTTF::didAttachWithIME()
|
|
|
|
{
|
|
|
|
setAttachWithIME(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::didDetachWithIME()
|
|
|
|
{
|
|
|
|
setAttachWithIME(false);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TextFieldTTF::canAttachWithIME()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_delegate) ? (! _delegate->onTextFieldAttachWithIME(this)) : true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool TextFieldTTF::canDetachWithIME()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_delegate) ? (! _delegate->onTextFieldDetachWithIME(this)) : true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 10:58:04 +08:00
|
|
|
void TextFieldTTF::insertText(const char * text, size_t len)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string insert(text, len);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// insert \n means input end
|
2016-03-07 19:22:03 +08:00
|
|
|
int pos = static_cast<int>(insert.find((char)TextFormatter::NewLine));
|
2013-12-18 17:47:20 +08:00
|
|
|
if ((int)insert.npos != pos)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
len = pos;
|
|
|
|
insert.erase(pos);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-04-19 09:55:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// delegate doesn't want to insert text
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-04-19 09:55:29 +08:00
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
std::size_t countInsertChar = _calcCharCount(insert.c_str());
|
2016-03-07 19:22:03 +08:00
|
|
|
_charCount += countInsertChar;
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
stringUTF8.replace(_inputText);
|
2016-03-07 19:22:03 +08:00
|
|
|
stringUTF8.insert(_cursorPosition, insert);
|
|
|
|
|
2017-12-29 11:24:02 +08:00
|
|
|
setCursorPosition(_cursorPosition + countInsertChar);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
|
|
|
setString(stringUTF8.getAsCharSequence());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string sText(_inputText);
|
|
|
|
sText.append(insert);
|
|
|
|
setString(sText);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if ((int)insert.npos == pos) {
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-04-19 09:55:29 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// '\n' inserted, let delegate process first
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-19 09:55:29 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// if delegate hasn't processed, detach from IME by default
|
2012-04-19 14:35:52 +08:00
|
|
|
detachWithIME();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextFieldTTF::deleteBackward()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
size_t len = _inputText.length();
|
|
|
|
if (! len)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// there is no string
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the delete byte number
|
2013-12-27 15:06:16 +08:00
|
|
|
size_t deleteLen = 1; // default, erase 1 byte
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
while(0x80 == (0xC0 & _inputText.at(len - deleteLen)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
++deleteLen;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2014-01-06 20:01:28 +08:00
|
|
|
if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + len - deleteLen, static_cast<int>(deleteLen)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-11-26 09:53:52 +08:00
|
|
|
// delegate doesn't want to delete backwards
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// if all text deleted, show placeholder string
|
2013-12-18 17:47:20 +08:00
|
|
|
if (len <= deleteLen)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
_inputText = "";
|
2013-06-15 14:03:30 +08:00
|
|
|
_charCount = 0;
|
2016-03-07 19:22:03 +08:00
|
|
|
setCursorPosition(0);
|
|
|
|
setString(_inputText);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set new input text
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
if (_cursorPosition)
|
|
|
|
{
|
|
|
|
setCursorPosition(_cursorPosition - 1);
|
|
|
|
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
stringUTF8.replace(_inputText);
|
2016-03-07 19:22:03 +08:00
|
|
|
stringUTF8.deleteChar(_cursorPosition);
|
|
|
|
|
|
|
|
_charCount = stringUTF8.length();
|
|
|
|
setString(stringUTF8.getAsCharSequence());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string text(_inputText.c_str(), len - deleteLen);
|
|
|
|
setString(text);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 15:49:58 +08:00
|
|
|
const std::string& TextFieldTTF::getContentText()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-24 15:49:58 +08:00
|
|
|
return _inputText;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
void TextFieldTTF::setCursorPosition(std::size_t cursorPosition)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled && cursorPosition <= (std::size_t)_charCount)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
_cursorPosition = cursorPosition;
|
2016-07-04 22:12:45 +08:00
|
|
|
_cursorShowingTime = CURSOR_TIME_SHOW_HIDE * 2.0f;
|
2016-03-07 19:22:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setCursorFromPoint(const Vec2 &point, const Camera* camera)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
// Reset Label, no cursor
|
|
|
|
bool oldIsAttachWithIME = _isAttachWithIME;
|
|
|
|
_isAttachWithIME = false;
|
|
|
|
updateCursorDisplayText();
|
|
|
|
|
|
|
|
Rect rect;
|
|
|
|
rect.size = getContentSize();
|
|
|
|
if (isScreenPointInRect(point, camera, getWorldToNodeTransform(), rect, nullptr))
|
|
|
|
{
|
|
|
|
int latterPosition = 0;
|
|
|
|
for (; latterPosition < _lengthOfString; ++latterPosition)
|
|
|
|
{
|
2017-10-17 11:58:51 +08:00
|
|
|
if (_lettersInfo[latterPosition].valid && _lettersInfo[latterPosition].atlasIndex >= 0)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
auto sprite = getLetter(latterPosition);
|
2017-10-17 11:58:51 +08:00
|
|
|
if (sprite)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
2017-10-17 11:58:51 +08:00
|
|
|
rect.size = sprite->getContentSize();
|
|
|
|
if (isScreenPointInRect(point, camera, sprite->getWorldToNodeTransform(), rect, nullptr))
|
|
|
|
{
|
|
|
|
setCursorPosition(latterPosition);
|
|
|
|
break;
|
|
|
|
}
|
2016-03-07 19:22:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (latterPosition == _lengthOfString)
|
|
|
|
{
|
|
|
|
setCursorPosition(latterPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set cursor
|
|
|
|
_isAttachWithIME = oldIsAttachWithIME;
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setAttachWithIME(bool isAttachWithIME)
|
|
|
|
{
|
|
|
|
if (isAttachWithIME != _isAttachWithIME)
|
|
|
|
{
|
|
|
|
_isAttachWithIME = isAttachWithIME;
|
|
|
|
|
|
|
|
if (_isAttachWithIME)
|
|
|
|
{
|
|
|
|
setCursorPosition(_charCount);
|
|
|
|
}
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-29 11:24:02 +08:00
|
|
|
void TextFieldTTF::setTextColorInternally(const Color4B& color)
|
|
|
|
{
|
|
|
|
if (_currentLabelType == LabelType::BMFONT) {
|
|
|
|
Label::setColor(Color3B(color));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Label::setTextColor(color);
|
|
|
|
}
|
|
|
|
|
2014-03-31 11:16:12 +08:00
|
|
|
void TextFieldTTF::setTextColor(const Color4B &color)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-03-14 21:06:40 +08:00
|
|
|
_colorText = color;
|
2017-12-29 11:24:02 +08:00
|
|
|
if (!_inputText.empty())
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
setTextColorInternally(color);
|
2014-08-13 11:28:37 +08:00
|
|
|
}
|
2014-03-14 21:06:40 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void TextFieldTTF::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2014-03-14 21:06:40 +08:00
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
if (_delegate && _delegate->onVisit(this,renderer,parentTransform,parentFlags))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-05-31 07:42:05 +08:00
|
|
|
Label::visit(renderer,parentTransform,parentFlags);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
void TextFieldTTF::update(float delta)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled && _isAttachWithIME)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
_cursorShowingTime -= delta;
|
|
|
|
if (_cursorShowingTime < -CURSOR_TIME_SHOW_HIDE)
|
|
|
|
{
|
|
|
|
_cursorShowingTime = CURSOR_TIME_SHOW_HIDE;
|
|
|
|
}
|
|
|
|
// before cursor inserted '\b', need next letter
|
2016-04-01 09:31:18 +08:00
|
|
|
auto sprite = getLetter((int)_cursorPosition + 1);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
|
|
|
if (sprite)
|
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
if (_currentLabelType == LabelType::BMFONT) {
|
|
|
|
sprite->setColor(getColor());
|
|
|
|
}
|
2016-03-07 19:22:03 +08:00
|
|
|
if (_cursorShowingTime >= 0.0f)
|
|
|
|
{
|
|
|
|
sprite->setOpacity(255);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite->setOpacity(0);
|
|
|
|
}
|
|
|
|
sprite->setDirty(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 11:16:12 +08:00
|
|
|
const Color4B& TextFieldTTF::getColorSpaceHolder()
|
2012-11-20 16:34:55 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _colorSpaceHolder;
|
2012-11-20 16:34:55 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void TextFieldTTF::setColorSpaceHolder(const Color3B& color)
|
2014-03-31 11:16:12 +08:00
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
setColorSpaceHolder(Color4B(color));
|
2014-03-31 11:16:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setColorSpaceHolder(const Color4B& color)
|
2012-11-20 16:34:55 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorSpaceHolder = color;
|
2016-03-07 19:22:03 +08:00
|
|
|
if (_inputText.empty())
|
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
2014-08-13 11:28:37 +08:00
|
|
|
}
|
2012-11-20 16:34:55 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// properties
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// input text property
|
2013-11-08 03:58:48 +08:00
|
|
|
void TextFieldTTF::setString(const std::string &text)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-04-19 09:55:29 +08:00
|
|
|
std::string displayText;
|
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
std::size_t charCount = 0;
|
|
|
|
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!text.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
_inputText = text;
|
|
|
|
displayText = _inputText;
|
2016-03-07 19:22:03 +08:00
|
|
|
charCount = _calcCharCount(_inputText.c_str());
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_secureTextEntry)
|
2013-04-19 09:55:29 +08:00
|
|
|
{
|
|
|
|
displayText = "";
|
2016-03-07 19:22:03 +08:00
|
|
|
size_t length = charCount;
|
2013-04-19 09:55:29 +08:00
|
|
|
while (length)
|
|
|
|
{
|
2016-04-01 10:01:37 +08:00
|
|
|
displayText.append(_passwordStyleText);
|
2013-04-19 09:55:29 +08:00
|
|
|
--length;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
_inputText = "";
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled && charCount != _charCount)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
_cursorPosition = charCount;
|
|
|
|
}
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
// Need for recreate all letters in Label
|
|
|
|
Label::removeAllChildrenWithCleanup(false);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// if there is no input text, display placeholder instead
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_inputText.empty() && (!_cursorEnabled || !_isAttachWithIME))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
2014-03-14 21:06:40 +08:00
|
|
|
Label::setString(_placeHolder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-07 19:22:03 +08:00
|
|
|
makeStringSupportCursor(displayText);
|
2017-12-29 11:24:02 +08:00
|
|
|
setTextColorInternally(_colorText);
|
2014-03-14 21:06:40 +08:00
|
|
|
Label::setString(displayText);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2016-03-07 19:22:03 +08:00
|
|
|
_charCount = charCount;
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:52:55 +08:00
|
|
|
|
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
void TextFieldTTF::appendString(const std::string& text)
|
|
|
|
{
|
|
|
|
insertText(text.c_str(), text.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::makeStringSupportCursor(std::string& displayText)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled && _isAttachWithIME)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
if (displayText.empty())
|
|
|
|
{
|
|
|
|
// \b - Next char not change x position
|
2017-12-29 11:24:02 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
|
2017-12-19 08:56:30 +08:00
|
|
|
displayText.push_back((char) TextFormatter::NextCharNoChangeX);
|
2016-03-07 19:22:03 +08:00
|
|
|
displayText.push_back(_cursorChar);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
stringUTF8.replace(displayText);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
|
|
|
if (_cursorPosition > stringUTF8.length())
|
|
|
|
{
|
|
|
|
_cursorPosition = stringUTF8.length();
|
|
|
|
}
|
|
|
|
std::string cursorChar;
|
|
|
|
// \b - Next char not change x position
|
2017-12-29 11:24:02 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
|
2017-12-19 08:56:30 +08:00
|
|
|
cursorChar.push_back((char)TextFormatter::NextCharNoChangeX);
|
2016-03-07 19:22:03 +08:00
|
|
|
cursorChar.push_back(_cursorChar);
|
|
|
|
stringUTF8.insert(_cursorPosition, cursorChar);
|
|
|
|
|
|
|
|
displayText = stringUTF8.getAsCharSequence();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::updateCursorDisplayText()
|
|
|
|
{
|
|
|
|
// Update Label content
|
|
|
|
setString(_inputText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setCursorChar(char cursor)
|
|
|
|
{
|
|
|
|
if (_cursorChar != cursor)
|
|
|
|
{
|
|
|
|
_cursorChar = cursor;
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::controlKey(EventKeyboard::KeyCode keyCode)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
switch (keyCode)
|
|
|
|
{
|
|
|
|
case EventKeyboard::KeyCode::KEY_HOME:
|
|
|
|
case EventKeyboard::KeyCode::KEY_KP_HOME:
|
|
|
|
setCursorPosition(0);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_END:
|
|
|
|
setCursorPosition(_charCount);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_DELETE:
|
|
|
|
case EventKeyboard::KeyCode::KEY_KP_DELETE:
|
|
|
|
if (_cursorPosition < (std::size_t)_charCount)
|
|
|
|
{
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
stringUTF8.replace(_inputText);
|
2016-03-07 19:22:03 +08:00
|
|
|
stringUTF8.deleteChar(_cursorPosition);
|
|
|
|
setCursorPosition(_cursorPosition);
|
|
|
|
_charCount = stringUTF8.length();
|
|
|
|
setString(stringUTF8.getAsCharSequence());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
|
|
|
|
if (_cursorPosition)
|
|
|
|
{
|
|
|
|
setCursorPosition(_cursorPosition - 1);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
|
|
|
|
if (_cursorPosition < (std::size_t)_charCount)
|
|
|
|
{
|
|
|
|
setCursorPosition(_cursorPosition + 1);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_ESCAPE:
|
|
|
|
detachWithIME();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-08 04:42:16 +08:00
|
|
|
const std::string& TextFieldTTF::getString() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 04:42:16 +08:00
|
|
|
return _inputText;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// place holder text property
|
2013-11-08 03:58:48 +08:00
|
|
|
void TextFieldTTF::setPlaceHolder(const std::string& text)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
_placeHolder = text;
|
2017-10-23 11:37:07 +08:00
|
|
|
if (_inputText.empty() && !_isAttachWithIME)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
2014-03-14 21:06:40 +08:00
|
|
|
Label::setString(_placeHolder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-08 03:58:48 +08:00
|
|
|
const std::string& TextFieldTTF::getPlaceHolder() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-08 03:58:48 +08:00
|
|
|
return _placeHolder;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2016-03-11 20:09:50 +08:00
|
|
|
void TextFieldTTF::setCursorEnabled(bool enabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
if (_cursorEnabled == enabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cursorEnabled = enabled;
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
_cursorPosition = _charCount;
|
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT) {
|
|
|
|
scheduleUpdate();
|
2016-03-07 19:22:03 +08:00
|
|
|
}
|
2017-12-29 11:24:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cursorPosition = 0;
|
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT) {
|
|
|
|
unscheduleUpdate();
|
2016-03-07 19:22:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:08:33 +08:00
|
|
|
// secureTextEntry
|
2013-06-20 14:13:12 +08:00
|
|
|
void TextFieldTTF::setSecureTextEntry(bool value)
|
2013-03-28 22:08:33 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_secureTextEntry != value)
|
2013-04-19 09:55:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_secureTextEntry = value;
|
2014-03-31 11:16:12 +08:00
|
|
|
setString(_inputText);
|
2013-04-19 09:55:29 +08:00
|
|
|
}
|
2013-03-28 22:08:33 +08:00
|
|
|
}
|
|
|
|
|
2016-04-01 00:52:55 +08:00
|
|
|
void TextFieldTTF::setPasswordTextStyle(const std::string &text)
|
|
|
|
{
|
2016-04-01 09:31:18 +08:00
|
|
|
if (text.length() < 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-01 00:52:55 +08:00
|
|
|
if (text != _passwordStyleText) {
|
|
|
|
_passwordStyleText = text;
|
|
|
|
setString(_inputText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 09:43:41 +08:00
|
|
|
const std::string& TextFieldTTF::getPasswordTextStyle() const
|
2016-04-01 00:52:55 +08:00
|
|
|
{
|
|
|
|
return _passwordStyleText;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldTTF::isSecureTextEntry() const
|
2013-03-28 22:08:33 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _secureTextEntry;
|
2013-03-28 22:08:33 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
NS_CC_END
|
2017-12-29 11:24:02 +08:00
|
|
|
|