2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
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 '|'
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
static int _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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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-04-01 09:31:18 +08:00
|
|
|
,_passwordStyleText("\u25CF")
|
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);
|
|
|
|
|
|
|
|
|
2014-03-31 11:16:12 +08:00
|
|
|
Label::setTextColor(_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-03-07 19:22:03 +08:00
|
|
|
int countInsertChar = _calcCharCount(insert.c_str());
|
|
|
|
_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);
|
|
|
|
|
|
|
|
setCursorPosition(_cursorPosition + countInsertChar);
|
|
|
|
|
|
|
|
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;
|
|
|
|
_cursorShowingTime = CURSOR_TIME_SHOW_HIDE*2.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (_lettersInfo[latterPosition].valid)
|
|
|
|
{
|
|
|
|
auto sprite = getLetter(latterPosition);
|
|
|
|
rect.size = sprite->getContentSize();
|
|
|
|
if (isScreenPointInRect(point, camera, sprite->getWorldToNodeTransform(), rect, nullptr))
|
|
|
|
{
|
|
|
|
setCursorPosition(latterPosition);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2016-03-07 19:22:03 +08:00
|
|
|
if (!_inputText.empty())
|
|
|
|
{
|
2014-08-13 11:28:37 +08:00
|
|
|
Label::setTextColor(_colorText);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
_colorSpaceHolder.r = color.r;
|
|
|
|
_colorSpaceHolder.g = color.g;
|
|
|
|
_colorSpaceHolder.b = color.b;
|
|
|
|
_colorSpaceHolder.a = 255;
|
2015-09-01 11:26:09 +08:00
|
|
|
if (_inputText.empty())
|
2014-08-13 11:28:37 +08:00
|
|
|
{
|
|
|
|
Label::setTextColor(_colorSpaceHolder);
|
|
|
|
}
|
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())
|
|
|
|
{
|
2014-08-13 11:28:37 +08:00
|
|
|
Label::setTextColor(_colorSpaceHolder);
|
|
|
|
}
|
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-03-07 19:22:03 +08:00
|
|
|
int 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
|
|
|
{
|
2014-03-31 11:16:12 +08:00
|
|
|
Label::setTextColor(_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);
|
|
|
|
|
2014-03-31 11:16:12 +08:00
|
|
|
Label::setTextColor(_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
|
|
|
|
displayText.push_back((char)TextFormatter::NextCharNoChangeX);
|
|
|
|
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
|
|
|
|
cursorChar.push_back((char)TextFormatter::NextCharNoChangeX);
|
|
|
|
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;
|
2015-09-01 11:26:09 +08:00
|
|
|
if (_inputText.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-03-31 11:16:12 +08:00
|
|
|
Label::setTextColor(_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
|
|
|
{
|
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
if (_cursorEnabled != enabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
2016-03-11 20:09:50 +08:00
|
|
|
_cursorEnabled = enabled;
|
|
|
|
if (_cursorEnabled)
|
2016-03-07 19:22:03 +08:00
|
|
|
{
|
|
|
|
_cursorPosition = _charCount;
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_cursorPosition = 0;
|
|
|
|
|
|
|
|
unscheduleUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("TextFieldTTF cursor worked only LabelType::TTF");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextFieldTTF::getPasswordTextStyle()const
|
|
|
|
{
|
|
|
|
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
|