cocos2d-x中带光标的输入框(CCTextFieldTTF)(这个不完善,后期再继续研究)

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。

http://blog.csdn.net/azhou_hui/article/details/8167881

 

cocos2d-x中输入框实现的效果很不好,完全没法和cocos2d比。必须自己再去封装。下面的这个还不完善,目前先做到这

 

(1)CursorTextField.h

#ifndef __CURSORTEXTFIELD_H__
#define __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(void);
	virtual ~CursorTextField(void);

	// 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);

	// CCLayer Touch
	bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

	// 判断是否点击在TextField处
	bool isInTextField(CCTouch *pTouch);
	// 得到TextField矩形
	CCRect getRect();

	// 打开输入法
	void openIME();
	// 关闭输入法
	void closeIME();

};

#endif  // __CURSORTEXTFIELD_H__


(2)CursorTextField.cpp

#include "CursorTextField.h"

const static float DELTA = 0.5f;

CursorTextField::CursorTextField(void)
{
	CCTextFieldTTF();

	m_pCursorSprite = NULL;
	m_pCursorAction = NULL;

	m_pInputText = NULL;
}

CursorTextField::~CursorTextField(void)
{
	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)
{
	// 初始化光标
	int column = 4;
	//int pixels[nHeight][column];
	int pixels[64][4];
	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::spriteWithTexture(texture);
	CCSize winSize = getContentSize();
	m_cursorPos = ccp(0, winSize.height / 2);
	m_pCursorSprite->setPosition(m_cursorPos);
	this->addChild(m_pCursorSprite);

	m_pCursorAction = CCRepeatForever::actionWithAction((CCActionInterval *) CCSequence::actions(CCFadeOut::actionWithDuration(0.25f), CCFadeIn::actionWithDuration(0.25f), NULL));

	m_pCursorSprite->runAction(m_pCursorAction);

	m_pInputText = new std::string();
}

bool CursorTextField::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{    
	m_beginPos = pTouch->locationInView();
	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 CCRect::CCRectContainsPoint(getRect(), convertTouchToNodeSpaceAR(pTouch));
}

void CursorTextField::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
	CCPoint endPos = pTouch->locationInView();
	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);

	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);
}


(3)调用(HelloWorldLayer.cpp中)含有包含的头文件等

#include "CursorTextField.h"
		
//带光标的输入框
CursorTextField *m_pCursorTextField = CursorTextField::textFieldWithPlaceHolder("Input Text", "Thonburi", 30);
m_pCursorTextField->setPosition(ccp(300,300));
this->addChild(m_pCursorTextField);


 

你可能感兴趣的:(cocos2d-x中带光标的输入框(CCTextFieldTTF)(这个不完善,后期再继续研究))