axmol/cocos2dx/text_input_node/CCTextFieldTTF.cpp

325 lines
8.8 KiB
C++
Raw Normal View History

2011-04-19 09:32:33 +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;
2011-04-28 18:01:29 +08:00
static int _calcCharCount(const char * pszText)
{
int n = 0;
char ch = 0;
while (ch = *pszText)
{
CC_BREAK_IF(! ch);
if (0x80 != (0xC0 & ch))
{
++n;
}
++pszText;
}
return n;
}
2011-04-19 09:32:33 +08:00
//////////////////////////////////////////////////////////////////////////
// constructor and destructor
//////////////////////////////////////////////////////////////////////////
CCTextFieldTTF::CCTextFieldTTF()
2011-04-28 18:01:29 +08:00
: m_pDelegate(0)
, m_nCharCount(0)
, m_pInputText(new std::string)
2011-04-19 09:32:33 +08:00
, m_pPlaceHolder(new std::string) // prevent CCLabelTTF initWithString assertion
{
2011-04-28 18:01:29 +08:00
m_ColorSpaceHolder.r = m_ColorSpaceHolder.g = m_ColorSpaceHolder.b = 127;
2011-04-19 09:32:33 +08:00
}
CCTextFieldTTF::~CCTextFieldTTF()
{
CC_SAFE_DELETE(m_pInputText);
CC_SAFE_DELETE(m_pPlaceHolder);
}
//////////////////////////////////////////////////////////////////////////
// static constructor
//////////////////////////////////////////////////////////////////////////
CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
2011-04-29 18:09:54 +08:00
{
CCTextFieldTTF *pRet = new CCTextFieldTTF();
if(pRet && pRet->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
{
pRet->autorelease();
2011-04-19 09:32:33 +08:00
if (placeholder)
{
pRet->setPlaceHolder(placeholder);
}
2011-04-29 18:09:54 +08:00
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
2011-04-19 09:32:33 +08:00
}
CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
{
2011-04-29 18:09:54 +08:00
CCTextFieldTTF *pRet = new CCTextFieldTTF();
if(pRet && pRet->initWithString("", fontName, fontSize))
{
pRet->autorelease();
2011-04-19 09:32:33 +08:00
if (placeholder)
{
pRet->setPlaceHolder(placeholder);
}
2011-04-29 18:09:54 +08:00
return pRet;
}
CC_SAFE_DELETE(pRet);
2011-04-19 09:32:33 +08:00
return NULL;
}
//////////////////////////////////////////////////////////////////////////
// initialize
//////////////////////////////////////////////////////////////////////////
2011-04-29 18:09:54 +08:00
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
{
2011-04-19 09:32:33 +08:00
if (placeholder)
{
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = new std::string(placeholder);
}
2011-04-29 18:09:54 +08:00
return CCLabelTTF::initWithString(m_pPlaceHolder->c_str(), dimensions, alignment, fontName, fontSize);
}
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
{
2011-04-19 09:32:33 +08:00
if (placeholder)
{
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = new std::string(placeholder);
}
2011-04-29 18:09:54 +08:00
return CCLabelTTF::initWithString(m_pPlaceHolder->c_str(), fontName, fontSize);
2011-04-19 09:32:33 +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()
2011-04-19 09:32:33 +08:00
{
bool bRet = CCIMEDelegate::detachWithIME();
if (bRet)
{
// close keyboard
CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
if (pGlView)
{
pGlView->setIMEKeyboardState(false);
}
}
return bRet;
2011-04-19 09:32:33 +08:00
}
bool CCTextFieldTTF::canAttachWithIME()
2011-04-19 09:32:33 +08:00
{
2011-04-28 18:01:29 +08:00
return (m_pDelegate) ? (! m_pDelegate->onTextFieldAttachWithIME(this)) : true;
2011-04-19 09:32:33 +08:00
}
bool CCTextFieldTTF::canDetachWithIME()
2011-04-19 09:32:33 +08:00
{
2011-04-29 16:46:29 +08:00
return (m_pDelegate) ? (! m_pDelegate->onTextFieldDetachWithIME(this)) : true;
2011-04-19 09:32:33 +08:00
}
void CCTextFieldTTF::insertText(const char * text, int len)
{
2011-04-22 16:33:41 +08:00
std::string sInsert(text, len);
// insert \n means input end
int nPos = sInsert.find('\n');
if (sInsert.npos != nPos)
{
len = nPos;
sInsert.erase(nPos);
}
if (len > 0)
2011-04-22 16:33:41 +08:00
{
if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
2011-04-22 16:33:41 +08:00
{
// delegate doesn't want insert text
return;
2011-04-22 16:33:41 +08:00
}
m_nCharCount += _calcCharCount(sInsert.c_str());
std::string sText(*m_pInputText);
sText.append(sInsert);
setString(sText.c_str());
2011-04-22 16:33:41 +08:00
}
if (sInsert.npos == nPos) {
return;
}
// '\n' has inserted, let delegate process first
if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
2011-04-28 18:01:29 +08:00
{
return;
}
// if delegate hasn't process, detach with ime as default
detachWithIME();
2011-04-19 09:32:33 +08:00
}
void CCTextFieldTTF::deleteBackward()
{
int nStrLen = m_pInputText->length();
if (! nStrLen)
{
// there is no string
return;
}
// get the delete byte number
int nDeleteLen = 1; // default, erase 1 byte
while(0x80 == (0xC0 & m_pInputText->at(nStrLen - nDeleteLen)))
2011-04-19 09:32:33 +08:00
{
++nDeleteLen;
2011-04-19 09:32:33 +08:00
}
2011-04-28 18:01:29 +08:00
if (m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_pInputText->c_str() + nStrLen - nDeleteLen, nDeleteLen))
{
// delegate don't wan't delete backward
return;
}
2011-04-19 09:32:33 +08:00
// if delete all text, show space holder string
if (nStrLen <= nDeleteLen)
2011-04-19 09:32:33 +08:00
{
CC_SAFE_DELETE(m_pInputText);
m_pInputText = new std::string;
2011-04-29 18:09:54 +08:00
m_nCharCount = 0;
2011-04-19 09:32:33 +08:00
CCLabelTTF::setString(m_pPlaceHolder->c_str());
return;
}
// set new input text
std::string sText(m_pInputText->c_str(), nStrLen - nDeleteLen);
2011-04-19 09:32:33 +08:00
setString(sText.c_str());
2011-04-28 18:01:29 +08:00
}
const char * CCTextFieldTTF::getContentText()
{
return m_pInputText->c_str();
}
2011-04-28 18:01:29 +08:00
void CCTextFieldTTF::draw()
{
if (m_pDelegate && m_pDelegate->onDraw(this))
{
return;
}
if (m_pInputText->length())
{
CCLabelTTF::draw();
return;
}
// draw placeholder
ccColor3B color = getColor();
setColor(m_ColorSpaceHolder);
CCLabelTTF::draw();
setColor(color);
2011-04-19 09:32:33 +08:00
}
//////////////////////////////////////////////////////////////////////////
// properties
//////////////////////////////////////////////////////////////////////////
2011-04-29 18:09:54 +08:00
// input text property
void CCTextFieldTTF::setString(const char *text)
{
2011-04-19 09:32:33 +08:00
CC_SAFE_DELETE(m_pInputText);
2011-04-19 09:32:33 +08:00
if (text)
{
m_pInputText = new std::string(text);
}
else
{
m_pInputText = new std::string;
}
2011-04-29 18:09:54 +08:00
// if there is no input text, display placeholder instead
if (! m_pInputText->length())
{
CCLabelTTF::setString(m_pPlaceHolder->c_str());
}
else
2011-04-19 09:32:33 +08:00
{
CCLabelTTF::setString(m_pInputText->c_str());
}
2011-04-28 18:01:29 +08:00
m_nCharCount = _calcCharCount(m_pInputText->c_str());
2011-04-29 18:09:54 +08:00
}
const char* CCTextFieldTTF::getString(void)
{
return m_pInputText->c_str();
}
2011-04-19 09:32:33 +08:00
// place holder text property
void CCTextFieldTTF::setPlaceHolder(const char * text)
{
2011-04-29 18:09:54 +08:00
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = (text) ? new std::string(text) : new std::string;
2011-04-19 09:32:33 +08:00
if (! m_pInputText->length())
{
CCLabelTTF::setString(m_pPlaceHolder->c_str());
}
}
const char * CCTextFieldTTF::getPlaceHolder(void)
{
return m_pPlaceHolder->c_str();
}
NS_CC_END;