axmol/cocos/2d/CCTextFieldTTF.cpp

375 lines
9.6 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
#include "CCTextFieldTTF.h"
#include "CCDirector.h"
2014-01-31 08:51:43 +08:00
#include "CCGLView.h"
NS_CC_BEGIN
static int _calcCharCount(const char * text)
{
int n = 0;
char ch = 0;
while ((ch = *text))
{
CC_BREAK_IF(! ch);
2013-04-19 09:55:29 +08:00
if (0x80 != (0xC0 & ch))
{
++n;
}
++text;
}
return n;
}
//////////////////////////////////////////////////////////////////////////
// constructor and destructor
//////////////////////////////////////////////////////////////////////////
TextFieldTTF::TextFieldTTF()
: _delegate(0)
, _charCount(0)
, _inputText("")
, _placeHolder("") // prevent Label initWithString assertion
, _secureTextEntry(false)
, _colorText(Color4B::WHITE)
{
_colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127;
_colorSpaceHolder.a = 255;
}
TextFieldTTF::~TextFieldTTF()
{
}
//////////////////////////////////////////////////////////////////////////
// static constructor
//////////////////////////////////////////////////////////////////////////
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
{
TextFieldTTF *ret = new TextFieldTTF();
if(ret && ret->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
{
ret->autorelease();
if (placeholder.size()>0)
{
ret->setPlaceHolder(placeholder);
}
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
TextFieldTTF * TextFieldTTF::textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
{
TextFieldTTF *ret = new TextFieldTTF();
if(ret && ret->initWithPlaceHolder("", fontName, fontSize))
{
ret->autorelease();
if (placeholder.size()>0)
{
ret->setPlaceHolder(placeholder);
}
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
//////////////////////////////////////////////////////////////////////////
// initialize
//////////////////////////////////////////////////////////////////////////
bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize)
{
_placeHolder = placeholder;
setDimensions(dimensions.width,dimensions.height);
setSystemFontName(fontName);
setSystemFontSize(fontSize);
setAlignment(alignment,TextVAlignment::CENTER);
Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder);
return true;
}
bool TextFieldTTF::initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize)
{
_placeHolder = std::string(placeholder);
setSystemFontName(fontName);
setSystemFontSize(fontSize);
Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder);
return true;
}
//////////////////////////////////////////////////////////////////////////
// IMEDelegate
//////////////////////////////////////////////////////////////////////////
bool TextFieldTTF::attachWithIME()
{
bool ret = IMEDelegate::attachWithIME();
if (ret)
{
// open keyboard
2014-01-31 07:40:56 +08:00
GLView * pGlView = Director::getInstance()->getOpenGLView();
if (pGlView)
{
pGlView->setIMEKeyboardState(true);
}
}
return ret;
}
bool TextFieldTTF::detachWithIME()
{
bool ret = IMEDelegate::detachWithIME();
if (ret)
{
// close keyboard
2014-01-31 07:40:56 +08:00
GLView * glView = Director::getInstance()->getOpenGLView();
if (glView)
{
glView->setIMEKeyboardState(false);
}
}
return ret;
}
bool TextFieldTTF::canAttachWithIME()
{
return (_delegate) ? (! _delegate->onTextFieldAttachWithIME(this)) : true;
}
bool TextFieldTTF::canDetachWithIME()
{
return (_delegate) ? (! _delegate->onTextFieldDetachWithIME(this)) : true;
}
2014-03-20 10:58:04 +08:00
void TextFieldTTF::insertText(const char * text, size_t len)
{
std::string insert(text, len);
// insert \n means input end
int pos = static_cast<int>(insert.find('\n'));
if ((int)insert.npos != pos)
{
len = pos;
insert.erase(pos);
}
2013-04-19 09:55:29 +08:00
if (len > 0)
{
if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
{
// delegate doesn't want to insert text
return;
}
2013-04-19 09:55:29 +08:00
_charCount += _calcCharCount(insert.c_str());
std::string sText(_inputText);
sText.append(insert);
setString(sText);
}
if ((int)insert.npos == pos) {
return;
}
2013-04-19 09:55:29 +08:00
// '\n' inserted, let delegate process first
if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
{
return;
}
2013-04-19 09:55:29 +08:00
// if delegate hasn't processed, detach from IME by default
detachWithIME();
}
void TextFieldTTF::deleteBackward()
{
size_t len = _inputText.length();
if (! len)
{
// 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
while(0x80 == (0xC0 & _inputText.at(len - deleteLen)))
{
++deleteLen;
}
2014-01-06 20:01:28 +08:00
if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + len - deleteLen, static_cast<int>(deleteLen)))
{
// delegate doesn't wan't to delete backwards
return;
}
// if all text deleted, show placeholder string
if (len <= deleteLen)
{
_inputText = "";
_charCount = 0;
Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder);
return;
}
// set new input text
std::string text(_inputText.c_str(), len - deleteLen);
setString(text);
}
const std::string& TextFieldTTF::getContentText()
{
return _inputText;
}
void TextFieldTTF::setTextColor(const Color4B &color)
{
_colorText = color;
Label::setTextColor(_colorText);
}
2014-04-08 21:45:54 +08:00
void TextFieldTTF::visit(Renderer *renderer, const Matrix &parentTransform, bool parentTransformUpdated)
{
if (_delegate && _delegate->onVisit(this,renderer,parentTransform,parentTransformUpdated))
{
return;
}
Label::visit(renderer,parentTransform,parentTransformUpdated);
}
const Color4B& TextFieldTTF::getColorSpaceHolder()
{
return _colorSpaceHolder;
}
void TextFieldTTF::setColorSpaceHolder(const Color3B& color)
{
_colorSpaceHolder.r = color.r;
_colorSpaceHolder.g = color.g;
_colorSpaceHolder.b = color.b;
_colorSpaceHolder.a = 255;
}
void TextFieldTTF::setColorSpaceHolder(const Color4B& color)
{
_colorSpaceHolder = color;
}
//////////////////////////////////////////////////////////////////////////
// properties
//////////////////////////////////////////////////////////////////////////
// input text property
void TextFieldTTF::setString(const std::string &text)
{
static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
2013-04-19 09:55:29 +08:00
std::string displayText;
2013-12-06 16:32:06 +08:00
size_t length;
2013-04-19 09:55:29 +08:00
if (text.length()>0)
{
_inputText = text;
displayText = _inputText;
if (_secureTextEntry)
2013-04-19 09:55:29 +08:00
{
displayText = "";
length = _inputText.length();
2013-04-19 09:55:29 +08:00
while (length)
{
displayText.append(bulletString);
--length;
}
}
}
else
{
_inputText = "";
}
// if there is no input text, display placeholder instead
if (! _inputText.length())
{
Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder);
}
else
{
Label::setTextColor(_colorText);
Label::setString(displayText);
}
_charCount = _calcCharCount(_inputText.c_str());
}
const std::string& TextFieldTTF::getString() const
{
return _inputText;
}
// place holder text property
void TextFieldTTF::setPlaceHolder(const std::string& text)
{
_placeHolder = text;
if (! _inputText.length())
{
Label::setTextColor(_colorSpaceHolder);
Label::setString(_placeHolder);
}
}
const std::string& TextFieldTTF::getPlaceHolder() const
{
return _placeHolder;
}
// secureTextEntry
void TextFieldTTF::setSecureTextEntry(bool value)
{
if (_secureTextEntry != value)
2013-04-19 09:55:29 +08:00
{
_secureTextEntry = value;
setString(_inputText);
2013-04-19 09:55:29 +08:00
}
}
bool TextFieldTTF::isSecureTextEntry()
{
return _secureTextEntry;
}
NS_CC_END