2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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"
|
|
|
|
#include "CCEGLView.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
static int _calcCharCount(const char * pszText)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
char ch = 0;
|
|
|
|
while ((ch = *pszText))
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
++pszText;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// constructor and destructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCTextFieldTTF::CCTextFieldTTF()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _delegate(0)
|
|
|
|
, _charCount(0)
|
|
|
|
, _inputText(new std::string)
|
|
|
|
, _placeHolder(new std::string) // prevent CCLabelTTF initWithString assertion
|
|
|
|
, _secureTextEntry(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTextFieldTTF::~CCTextFieldTTF()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_inputText);
|
|
|
|
CC_SAFE_DELETE(_placeHolder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// static constructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
2013-04-19 09:55:29 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTextFieldTTF *pRet = new CCTextFieldTTF();
|
|
|
|
if(pRet && pRet->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
if (placeholder)
|
|
|
|
{
|
|
|
|
pRet->setPlaceHolder(placeholder);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
|
|
|
|
{
|
|
|
|
CCTextFieldTTF *pRet = new CCTextFieldTTF();
|
|
|
|
if(pRet && pRet->initWithString("", fontName, fontSize))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
if (placeholder)
|
|
|
|
{
|
|
|
|
pRet->setPlaceHolder(placeholder);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// initialize
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
|
|
|
|
{
|
|
|
|
if (placeholder)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_placeHolder);
|
|
|
|
_placeHolder = new std::string(placeholder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return CCLabelTTF::initWithString(_placeHolder->c_str(), fontName, fontSize, dimensions, alignment);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
|
|
|
|
{
|
|
|
|
if (placeholder)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_placeHolder);
|
|
|
|
_placeHolder = new std::string(placeholder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return CCLabelTTF::initWithString(_placeHolder->c_str(), fontName, fontSize);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// CCIMEDelegate
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool CCTextFieldTTF::attachWithIME()
|
|
|
|
{
|
|
|
|
bool bRet = CCIMEDelegate::attachWithIME();
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
// open keyboard
|
|
|
|
CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
|
|
|
|
if (pGlView)
|
|
|
|
{
|
|
|
|
pGlView->setIMEKeyboardState(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCTextFieldTTF::detachWithIME()
|
|
|
|
{
|
|
|
|
bool bRet = CCIMEDelegate::detachWithIME();
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
// close keyboard
|
|
|
|
CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
|
|
|
|
if (pGlView)
|
|
|
|
{
|
|
|
|
pGlView->setIMEKeyboardState(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCTextFieldTTF::canAttachWithIME()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_delegate) ? (! _delegate->onTextFieldAttachWithIME(this)) : true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCTextFieldTTF::canDetachWithIME()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_delegate) ? (! _delegate->onTextFieldDetachWithIME(this)) : true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextFieldTTF::insertText(const char * text, int len)
|
|
|
|
{
|
|
|
|
std::string sInsert(text, len);
|
|
|
|
|
|
|
|
// insert \n means input end
|
|
|
|
int nPos = sInsert.find('\n');
|
|
|
|
if ((int)sInsert.npos != nPos)
|
|
|
|
{
|
|
|
|
len = nPos;
|
|
|
|
sInsert.erase(nPos);
|
|
|
|
}
|
2013-04-19 09:55:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate && _delegate->onTextFieldInsertText(this, sInsert.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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_charCount += _calcCharCount(sInsert.c_str());
|
|
|
|
std::string sText(*_inputText);
|
2012-04-19 14:35:52 +08:00
|
|
|
sText.append(sInsert);
|
|
|
|
setString(sText.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)sInsert.npos == nPos) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextFieldTTF::deleteBackward()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int nStrLen = _inputText->length();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! nStrLen)
|
|
|
|
{
|
|
|
|
// there is no string
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the delete byte number
|
|
|
|
int nDeleteLen = 1; // default, erase 1 byte
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
while(0x80 == (0xC0 & _inputText->at(nStrLen - nDeleteLen)))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
++nDeleteLen;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText->c_str() + nStrLen - nDeleteLen, nDeleteLen))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// delegate doesn't wan't 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
|
2012-04-19 14:35:52 +08:00
|
|
|
if (nStrLen <= nDeleteLen)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_inputText);
|
|
|
|
_inputText = new std::string;
|
|
|
|
_charCount = 0;
|
|
|
|
CCLabelTTF::setString(_placeHolder->c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set new input text
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string sText(_inputText->c_str(), nStrLen - nDeleteLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
setString(sText.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * CCTextFieldTTF::getContentText()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _inputText->c_str();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextFieldTTF::draw()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate && _delegate->onDraw(this))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_inputText->length())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCLabelTTF::draw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw placeholder
|
|
|
|
ccColor3B color = getColor();
|
2013-06-15 14:03:30 +08:00
|
|
|
setColor(_colorSpaceHolder);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLabelTTF::draw();
|
|
|
|
setColor(color);
|
|
|
|
}
|
|
|
|
|
2013-01-14 15:51:53 +08:00
|
|
|
const ccColor3B& CCTextFieldTTF::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
|
|
|
}
|
|
|
|
|
|
|
|
void CCTextFieldTTF::setColorSpaceHolder(const ccColor3B& color)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorSpaceHolder = color;
|
2012-11-20 16:34:55 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// properties
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// input text property
|
|
|
|
void CCTextFieldTTF::setString(const char *text)
|
|
|
|
{
|
2013-04-19 16:09:26 +08:00
|
|
|
static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
|
2013-04-19 09:55:29 +08:00
|
|
|
std::string displayText;
|
|
|
|
int length;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_inputText);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (text)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_inputText = new std::string(text);
|
|
|
|
displayText = *_inputText;
|
|
|
|
if (_secureTextEntry)
|
2013-04-19 09:55:29 +08:00
|
|
|
{
|
|
|
|
displayText = "";
|
2013-06-15 14:03:30 +08:00
|
|
|
length = _inputText->length();
|
2013-04-19 09:55:29 +08:00
|
|
|
while (length)
|
|
|
|
{
|
|
|
|
displayText.append(bulletString);
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_inputText = new std::string;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if there is no input text, display placeholder instead
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _inputText->length())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCLabelTTF::setString(_placeHolder->c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-28 22:08:33 +08:00
|
|
|
CCLabelTTF::setString(displayText.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_charCount = _calcCharCount(_inputText->c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCTextFieldTTF::getString(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _inputText->c_str();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// place holder text property
|
|
|
|
void CCTextFieldTTF::setPlaceHolder(const char * text)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_placeHolder);
|
|
|
|
_placeHolder = (text) ? new std::string(text) : new std::string;
|
|
|
|
if (! _inputText->length())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCLabelTTF::setString(_placeHolder->c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * CCTextFieldTTF::getPlaceHolder(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _placeHolder->c_str();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-03-28 22:08:33 +08:00
|
|
|
// secureTextEntry
|
|
|
|
void CCTextFieldTTF::setSecureTextEntry(bool value)
|
|
|
|
{
|
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;
|
2013-04-19 09:55:29 +08:00
|
|
|
setString(getString());
|
|
|
|
}
|
2013-03-28 22:08:33 +08:00
|
|
|
}
|
|
|
|
|
2013-04-15 15:11:04 +08:00
|
|
|
bool CCTextFieldTTF::isSecureTextEntry()
|
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
|