直接贴代码
#ifndef CursorInputDemo_CursorTextField_h
#define CursorInputDemo_CursorTextField_h
#include "cocos2d.h"
USING_NS_CC;
class CursorTextField: public CCTextFieldTTF, public CCTextFieldDelegate, public CCTouchDelegate
{
private:
// 点击开始位置
CCPoint m_beginPos;
// 光标精灵
CCSprite *m_pCursorSprite;
// 光标动画
CCAction *m_pCursorAction;
// 光标坐标
CCPoint m_cursorPos;
// 输入框内容
std::string *m_pInputText;
public:
CursorTextField();
~CursorTextField();
// static
static CursorTextField* textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize);
// CCLayer
void onEnter();
void onExit();
// 初始化光标精灵
void initCursorSprite(int nHeight);
// CCTextFieldDelegate
virtual bool onTextFieldAttachWithIME(CCTextFieldTTF *pSender);
virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * pSender);
virtual bool onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen);
virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen);
int m_limitNum;
//设置字符长度限制,一个汉字三个字符
void setLimitNum(int limitNum);
int getLimitNum();
// CCLayer Touch
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
// 判断是否点击在TextField处
bool isInTextField(CCTouch *pTouch);
// 得到TextField矩形
CCRect getRect();
const char* GetText();
// 打开输入法
void openIME();
// 关闭输入法
void closeIME();
};
#endif
#include "CursorTextField.h"
const static float DELTA = 0.5f;
CursorTextField::CursorTextField()
{
CCTextFieldTTF();
m_pCursorSprite = NULL;
m_pCursorAction = NULL;
m_pInputText = NULL;
m_limitNum = 20;
}
CursorTextField::~CursorTextField()
{
delete m_pInputText;
}
void CursorTextField::onEnter()
{
CCTextFieldTTF::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
this->setDelegate(this);
}
CursorTextField * CursorTextField::textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
{
CursorTextField *pRet = new CursorTextField();
if(pRet && pRet->initWithString("", fontName, fontSize))
{
pRet->autorelease();
if (placeholder)
{
pRet->setPlaceHolder(placeholder);
}
pRet->initCursorSprite(fontSize);
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
void CursorTextField::initCursorSprite(int nHeight)
{
// 初始化光标
const int column = 4;/*
int pixels = new int[nHeight][column];*/
int * pixels = new int[nHeight*column];
for (int i=0; i<nHeight; ++i) {
for (int j=0; j<column; ++j) {
pixels[i*j] = 0xffffffff;
}
}
CCTexture2D *texture = new CCTexture2D();
texture->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(column, nHeight));
m_pCursorSprite = CCSprite::createWithTexture(texture);
CCSize winSize = getContentSize();
m_cursorPos = ccp(0, winSize.height / 2);
m_pCursorSprite->setPosition(m_cursorPos);
this->addChild(m_pCursorSprite);
m_pCursorAction = CCRepeatForever::create((CCActionInterval *) CCSequence::create(CCFadeOut::create(0.25f), CCFadeIn::create(0.25f), NULL));
m_pCursorSprite->runAction(m_pCursorAction);
m_pInputText = new std::string();
setColor(ccc3(0,0,0));
}
bool CursorTextField::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
m_beginPos = pTouch->getLocationInView();
m_beginPos = CCDirector::sharedDirector()->convertToGL(m_beginPos);
return true;
}
CCRect CursorTextField::getRect()
{
CCSize size = getContentSize();
return CCRectMake(-size.width / 2, -size.height / 2, size.width, size.height);
}
bool CursorTextField::isInTextField(cocos2d::CCTouch *pTouch)
{
return getRect().containsPoint(convertTouchToNodeSpaceAR(pTouch));
}
void CursorTextField::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCPoint endPos = pTouch->getLocationInView();
endPos = CCDirector::sharedDirector()->convertToGL(endPos);
// 判断是否为点击事件
if (::abs(endPos.x - m_beginPos.x) > DELTA ||
::abs(endPos.y - m_beginPos.y))
{
// 不是点击事件
m_beginPos.x = m_beginPos.y = -1;
return;
}
CCLOG("width: %f, height: %f.", getContentSize().width, getContentSize().height);
// 判断是打开输入法还是关闭输入法
isInTextField(pTouch) ? openIME() : closeIME();
}
bool CursorTextField::onTextFieldAttachWithIME(cocos2d::CCTextFieldTTF *pSender)
{
if (m_pInputText->empty()) {
return false;
}
m_pCursorSprite->setPositionX(getContentSize().width);
return false;
}
bool CursorTextField::onTextFieldInsertText(cocos2d::CCTextFieldTTF *pSender, const char *text, int nLen)
{
CCLOG("Width: %f", pSender->getContentSize().width);
CCLOG("Text: %s", text);
CCLOG("Length: %d", nLen);
if (strcmp(text,"\r") == 0 || strcmp(text,"\n") == 0)
{
closeIME();
return false;
}
std::string tempStr = m_pInputText->substr();
tempStr.append(text);
if (tempStr.length() > (unsigned int)m_limitNum) {
return true;
}
m_pInputText->append(text);
setString(m_pInputText->c_str());
m_pCursorSprite->setPositionX(getContentSize().width);
return true;
}
bool CursorTextField::onTextFieldDeleteBackward(cocos2d::CCTextFieldTTF *pSender, const char *delText, int nLen)
{
m_pInputText->resize(m_pInputText->size() - nLen);
setString(m_pInputText->c_str());
m_pCursorSprite->setPositionX(getContentSize().width);
if (m_pInputText->empty()) {
m_pCursorSprite->setPositionX(0);
}
return false;
}
bool CursorTextField::onTextFieldDetachWithIME(cocos2d::CCTextFieldTTF *pSender)
{
return false;
}
void CursorTextField::openIME()
{
m_pCursorSprite->setVisible(true);
this->attachWithIME();
}
void CursorTextField::closeIME()
{
m_pCursorSprite->setVisible(false);
this->detachWithIME();
}
void CursorTextField::onExit()
{
CCTextFieldTTF::onExit();
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
void CursorTextField::setLimitNum( int limitNum )
{
m_limitNum = limitNum;
}
int CursorTextField::getLimitNum()
{
return m_limitNum;
}
const char* CursorTextField::GetText()
{
return m_pInputText->c_str();
}