cocos2d-x 聊天输入框实现

聊天输入框  (单行输入框 ,多行可自己扩展)

实现功能:

1.普通输入

2.设置输入框显示最大宽度(PT值,cocos2d-x坐标值)

3.设置输入框允许的最大字符数量(字符Unicode)

4.输入框自动缩进(当输入字符串数量超过显示框最大宽度时,会自动向左缩进,显示最新字符串


输入框实现代码

头文件:

#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;
	//输入框长度
	float inputFrameWidth;
	//允许输入的最大字符数Unicode
	float inputMaxLength;
	int nLenCount;
	int* codeNumType;    //每个字符对应的字节数量
	int codeCur;         //当前第几个字符
	int startCur;        //行开头字符下标
	int endCur;          //行末尾下标
	// 输入框总内容
	std::string *m_pInputText;
	std::string inpuText; //当前输入框内容
public:
	CursorTextField();
	~CursorTextField();
	// static
	static CursorTextField* textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize);
	// CCLayer
	void onEnter();
	void onExit();
	bool init();
	// 初始化光标精灵
	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();

	const char* getInputText();
	void setInpuntText(char* text);
	void setInputWidth(float width);
	void setInputMaxLength(float length);

	int Utf82Unicode(LPWSTR out,  int outsize , LPSTR in,int insize);
};

#endif


cpp文件:

#include "CursorTextField.h"

const static float DELTA = 0.5f;

using namespace cocos2d;
using namespace std;

CursorTextField::CursorTextField()
{
	CCTextFieldTTF();

	m_pCursorSprite = NULL;
	m_pCursorAction = NULL;

	m_pInputText = NULL;
	codeNumType = NULL;
}

CursorTextField::~CursorTextField()
{
	CC_SAFE_DELETE(m_pInputText);
	CC_SAFE_DELETE_ARRAY(codeNumType);
}

void CursorTextField::onEnter()
{
	CCTextFieldTTF::onEnter();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
	this->setDeleg
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值