多的不说,我这个学渣,我写的代码比较搓!忍耐下吧!
CursorTextFIEld.h
#ifndef _CursorTextFIEld_H_#define _CursorTextFIEld_H_#include "cocos2d.h"USING_NS_CC;class CursorTextFIEld : public TextFIEldTTF,public TextFIEldDelegate,public IMEDelegate{private: // 点击开始位置 Point m_beginPos; // 光标精灵 Sprite * m_pCursorSprite; // 光标动画 Action *m_pCursorAction; // 光标坐标 Point m_cursorPos; //输入框长度 float inputFrameWIDth; //允许输入的最大字符数Unicode float inputMaxLength; int nLenCount; int codeNumType[50]; //每个字符对应的字节数量 int codeCur; //当前第几个字符 int startCur; //行开头字符下标 int endCur; //行末尾下标 // 输入框总内容 std::string m_pinputText; std::string inputext; //当前输入框内容public: CursorTextFIEld(); ~CursorTextFIEld(); // static static CursorTextFIEld * textFIElDWithPlaceHolder(cocos2d::Node * node,const char *placeholder,const char *Fontname,float FontSize); // Layer voID onEnter(); voID onExit(); bool init(); // 初始化光标精灵 voID initCursorSprite(int nHeight); // TextFIEldDelegate virtual bool onTextFIEldAttachWithIME(TextFIEldTTF *pSender) overrIDe; virtual bool onTextFIEldDetachWithIME(TextFIEldTTF * pSender) overrIDe; virtual bool onTextFIEldInsertText(cocos2d::TextFIEldTTF* sender,const char * text,size_t nLen) overrIDe; virtual bool onTextFIEldDeleteBackward(cocos2d::TextFIEldTTF* sender,const char * delText,size_t nLen) overrIDe; virtual voID setposition(const Point& pos); virtual voID setposition(float &x,float &y); voID setCursorpositionX(float x); // 设置光标x位置 // 把光标添加到和输入框一起的层中 voID AddCursor(Node *node); // Layer touch bool ontouchBegan(touch *ptouch,Event *pEvent); voID ontouchended(touch *ptouch,Event *pEvent); // 判断是否点击在TextFIEld处 bool isInTextFIEld(touch *ptouch); // 得到TextFIEld矩形 Rect getRect(); // 打开输入法 voID openIME(); // 关闭输入法 voID closeIME(); std::string split_text(std::string name,int len,int start); const char* getinputText(); voID setInpuntText(char* text); voID setinputWIDth(float wIDth); voID setinputMaxLength(float length); cocos2d::Node * parentNode;protected: EventListenertouchOneByOne * Listener;};#endifCursorTextFIEld.cpp
#include "CursorTextFIEld.h"#include "MyCharSet.h"const static float DELTA = 0.5f;using namespace cocos2d;using namespace std;CursorTextFIEld::CursorTextFIEld(){ TextFIEldTTF(); m_pCursorSprite = NulL;}CursorTextFIEld::~CursorTextFIEld(){ }voID CursorTextFIEld::onEnter(){ TextFIEldTTF::onEnter(); Listener = EventListenertouchOneByOne::create(); Listener->setSwallowtouches(true); Listener->ontouchBegan = CC_CALLBACK_2(CursorTextFIEld::ontouchBegan,this); Listener->ontouchended = CC_CALLBACK_2(CursorTextFIEld::ontouchended,this); Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,this); this->setDelegate(this);}CursorTextFIEld * CursorTextFIEld::textFIElDWithPlaceHolder(Node * node,float FontSize){ auto pRet = new CursorTextFIEld(); pRet->parentNode = node; if (pRet && ((TextFIEldTTF*)pRet)->initWithPlaceHolder(placeholder,Fontname,FontSize)) { pRet->autorelease(); if (placeholder) { pRet->setPlaceHolder(placeholder); } pRet->init(); pRet->initCursorSprite(FontSize); pRet->setHorizontalAlignment(kCCTextAlignmentleft); return pRet; } CC_SAFE_DELETE(pRet); return NulL;}bool CursorTextFIEld::init(){ this->inputFrameWIDth = 400; this->inputMaxLength = 16; this->nLenCount = 0; memset(codeNumType,sizeof(codeNumType)); this->codeCur = 0; this->startCur = 0; this->endCur = 0; inputext = ""; return true;}voID CursorTextFIEld::initCursorSprite(const int mHeight){ int column = 2; int nHeight = 20; int pixels[50][2]; for (int i = 0; i < nHeight; ++i) { for (int j = 0; j < column; ++j) { pixels[i][j] = 0xffffffff; } } Texture2D* texture = new Texture2D(); texture->initWithData(pixels,20,Texture2D::PixelFormat::RGB888,4,mHeight,CCSizeMake(column,nHeight)); m_pCursorSprite = Sprite::createWithTexture(texture); texture->autorelease(); if (m_pCursorSprite == nullptr) { cclOG("NulL"); } Size winSize = getContentSize(); m_pCursorSprite->setVisible(false); parentNode->addChild(m_pCursorSprite); m_pCursorAction = RepeatForever::create(Sequence::create(FadeOut::create(0.25f),FadeIn::create(0.25f),NulL)); m_pCursorSprite->runAction(m_pCursorAction);}voID CursorTextFIEld::setposition(float &x,float &y){ Point posi(x,y); setposition(posi);}voID CursorTextFIEld::setposition(const Point& pos){ TextFIEldTTF::setposition(pos); // 设置光标位置 if (NulL != m_pCursorSprite) { Size winSize = getContentSize(); m_cursorPos = ccp(0,0/*winSize.height / 2*/); m_cursorPos = m_cursorPos + pos; m_pCursorSprite->setposition(m_cursorPos.x,m_cursorPos.y + m_pCursorSprite->getContentSize().height / 2.0); }}voID CursorTextFIEld::setCursorpositionX(float x) // 设置光标x位置{ Point pt = getposition(); // 获取输入框位置 m_pCursorSprite->setpositionX(pt.x + x);}// 把光标添加到和输入框一起的层中voID CursorTextFIEld::AddCursor(Node *node){ if (NulL != node && NulL != m_pCursorSprite) { node->addChild(m_pCursorSprite); m_pCursorSprite->setpositionY(getContentSize().height / 2.0); m_pCursorSprite->runAction(m_pCursorAction); }}bool CursorTextFIEld::ontouchBegan(cocos2d::touch *ptouch,cocos2d::Event *pEvent){ m_beginPos = ptouch->getLocation(); return true;}Rect CursorTextFIEld::getRect(){ Size size = getContentSize(); return CCRectMake(0,-size.height / 2,inputFrameWIDth,size.height);}//获取输入框内容const char* CursorTextFIEld::getinputText(){ const char* text = m_pinputText.c_str(); return text;}//设置输入框内容voID CursorTextFIEld::setInpuntText(char* text){ m_pinputText = ""; setString(text); m_pCursorSprite->setpositionX(this->getposition().x); memset(codeNumType,sizeof(codeNumType)); codeCur = 0; startCur = 0; endCur = 0; inputext = "";}//设置输入框宽度 一旦字符串宽度超度这个长度 字符串会自动向左缩进voID CursorTextFIEld::setinputWIDth(float wIDth){ this->inputFrameWIDth = wIDth;}//设置输入宽显示的最大字符数量UnicodevoID CursorTextFIEld::setinputMaxLength(float length){ this->inputMaxLength = length;}//判断点击事件,是否响应在输入框范围内bool CursorTextFIEld::isInTextFIEld(cocos2d::touch *ptouch){ return getRect().containsPoint(converttouchToNodeSpaceAR(ptouch));}voID CursorTextFIEld::ontouchended(cocos2d::touch *ptouch,cocos2d::Event *pEvent){ Point endPos = ptouch->getLocation(); // 判断是否为点击事件 if (::abs(endPos.x - m_beginPos.x) > DELTA || ::abs(endPos.y - m_beginPos.y)) { // 不是点击事件 m_beginPos.x = m_beginPos.y = -1; return; } // 判断是打开输入法还是关闭输入法 isInTextFIEld(ptouch) ? openIME() : closeIME();}//d出手机键盘时响应事件bool CursorTextFIEld::onTextFIEldAttachWithIME(cocos2d::TextFIEldTTF *pSender){ if (m_pinputText.empty()) { return false; } m_pCursorSprite->setpositionX(this->getposition().x + getContentSize().wIDth); return false;}//当有输入进来时响应//@param pSender 发送事件对象//@param text 输入内容//@param 内容字节长度bool CursorTextFIEld::onTextFIEldInsertText(cocos2d::TextFIEldTTF* sender,size_t nLen){ int j = 0 ; std::string mytext = text; for (j = 0; j < mytext.size();) { std::string sText = m_pinputText.c_str(); wchar_t* wText = new wchar_t[200]; char t[200]; memset(t,sizeof(t)); strcpy(t,sText.c_str()); int unisize = 0; int cou = MyCharSet::getinstence()->utf8_to_unicode((uint8_t*)t,(uint16_t **)wText,&unisize); std::string ss = split_text(mytext,1,j); j += ss.length(); //当字符数量超过规定值 不做处理 if (cou >= inputMaxLength) { CC_SAFE_DELETE_ARRAY(wText); return true; } //return true; //屏蔽回车输入 if (ss == "\n") { CC_SAFE_DELETE_ARRAY(wText); continue; } //输入框总内容添加 m_pinputText.append(ss); //输入框当前字符串添加 inputext.append(ss); //当前字符的长度 codeNumType[codeCur++] = ss.length(); std::string localText = m_pinputText; setString(m_pinputText); //如果总字符串的长度大于指定宽度 if (getContentSize().wIDth > inputFrameWIDth){ //大于,截取字符串,直到字符串的长度小于指定宽度为止 setString(inputext); while (getContentSize().wIDth > inputFrameWIDth){ int nnLen = ss.length(); if (codeNumType[startCur] == 1){ nnLen = 1; } if (codeNumType[startCur] == 3){ nnLen = 3; } startCur++; nLenCount += nnLen; float gap = localText.size() - nLenCount; inputext = localText.substr(nLenCount,gap); setString(inputext.c_str()); float coWIDth = getContentSize().wIDth; } } else{ //小于,直接设置显示总字符串 nLenCount = 0; startCur = 0; setString(m_pinputText); } //设置光标位置 m_pCursorSprite->setpositionX(this->getposition().x + getContentSize().wIDth); CC_SAFE_DELETE_ARRAY(wText); //AndroIDshowtext(mychar,1); } return true;}//当有输入进来时响应//@param pSender 发送事件对象//@param text 删除内容//@param 内容字节长度bool CursorTextFIEld::onTextFIEldDeleteBackward(cocos2d::TextFIEldTTF* sender,size_t nLen){ // 将总字符串长度减去nLen字节长 m_pinputText.resize(m_pinputText.size() - nLen); //当前字符数减一 codeNumType[codeCur--] = 0; std::string localText = m_pinputText; setString(m_pinputText); if (getContentSize().wIDth > inputFrameWIDth){ //大于指定宽度,截取字符串,直到字符串长度小于指定宽度 while (getContentSize().wIDth > inputFrameWIDth){ int nnLen = nLen; if (codeNumType[startCur - 1] == 1){ nnLen = 1; } if (codeNumType[startCur - 1] == 3){ nnLen = 3; } nLenCount -= nnLen; startCur--; if (startCur <= 0) startCur = 0; if (nLenCount <= 0) nLenCount = 0; float gap = localText.size() - nLenCount; const std::string text = localText.substr(nLenCount,gap); setString(text); inputext = text; } } else{ nLenCount = 0; startCur = 0; setString(m_pinputText.c_str()); } //设置光标位置 m_pCursorSprite->setpositionX(this->getposition().x + getContentSize().wIDth); if (m_pinputText.empty()) { m_pCursorSprite->setpositionX(this->getposition().x); } return true;}bool CursorTextFIEld::onTextFIEldDetachWithIME(cocos2d::TextFIEldTTF *pSender){ return false;}voID CursorTextFIEld::openIME(){ m_pCursorSprite->setVisible(true); setString(m_pinputText); ((TextFIEldTTF *)this)->attachWithIME();}voID CursorTextFIEld::closeIME(){ m_pCursorSprite->setVisible(false); //auto pTextFIEld = (TextFIEldTTF *)pRet; ((TextFIEldTTF *)this)->detachWithIME();}voID CursorTextFIEld::onExit(){ TextFIEldTTF::onExit(); Director::getInstance()->getEventdispatcher()->removeEventListener(Listener);}std::string CursorTextFIEld::split_text(std::string name,int start){ int i = start; std::string str; if (name[i] < 0) { i++; } //while (name[i] < 0) //{ // i++; // if (i - start == 2) // { // break; // } //} if (start == i) { str = name.substr(start,1); } else { str = name.substr(start,3); } //log("mysubstr %s",str.c_str()); return str;}
字节转换的
MyCharSet.h
#pragma once#include "stdint.h"#include "stdio.h"#include <string.h>#include <malloc.h>#include <memory.h>class MyCharSet{public: MyCharSet(); ~MyCharSet(); static MyCharSet * getinstence(); static voID destoryinstence(); int utf8_to_unicode(uint8_t *in,uint16_t **out,int *outsize); int unicode_to_utf8(uint16_t *in,int insize,uint8_t **out);};
MyCharSet.cpp
#include "MyCharSet.h"static MyCharSet *_MyCharSet = NulL;MyCharSet::MyCharSet(){}MyCharSet::~MyCharSet(){}MyCharSet * MyCharSet::getinstence(){ if (_MyCharSet == NulL) { _MyCharSet = new MyCharSet(); } return _MyCharSet;}voID MyCharSet::destoryinstence(){ if (_MyCharSet != NulL) { delete _MyCharSet; }}int MyCharSet::utf8_to_unicode(uint8_t *in,int *outsize){ uint8_t *p = in; uint16_t *result = NulL; int resultsize = 0; uint8_t *tmp = NulL; result = (uint16_t *)malloc(strlen((char *)in) * 2 + 2); /* should be enough */ memset(result,strlen((char*)in) * 2 + 2); tmp = (uint8_t *)result; while (*p) { if (*p >= 0x00 && *p <= 0x7f) { *tmp = *p; tmp++; *tmp = 'CursorTextFIEld *m_pCursorinputLayer = CursorTextFIEld::textFIElDWithPlaceHolder(this,"click and input","Fonts/Marker Felt.ttf",40); m_pCursorinputLayer->setAnchorPoint(ccp(0.0f,0.0f)); m_pCursorinputLayer->setinputWIDth(visibleSize.wIDth); Vec2 posi(0,visibleSize.height / 2); m_pCursorinputLayer->setposition(posi); this->addChild(m_pCursorinputLayer,1);'; resultsize += 1; } else if ((*p & (0xff << 5)) == 0xc0) { uint16_t t = 0; uint8_t t1 = 0; uint8_t t2 = 0; t1 = *p & (0xff >> 3); p++; t2 = *p & (0xff >> 2); *tmp = t2 | ((t1 & (0xff >> 6)) << 6);//t1 >> 2; tmp++; *tmp = t1 >> 2;//t2 | ((t1 & (0xff >> 6)) << 6); tmp++; resultsize += 1; } else if ((*p & (0xff << 4)) == 0xe0) { uint16_t t = 0; uint8_t t1 = 0; uint8_t t2 = 0; uint8_t t3 = 0; t1 = *p & (0xff >> 3); p++; t2 = *p & (0xff >> 2); p++; t3 = *p & (0xff >> 2); //little Endian *tmp = ((t2 & (0xff >> 6)) << 6) | t3;//(t1 << 4) | (t2 >> 2); tmp++; *tmp = (t1 << 4) | (t2 >> 2);//((t2 & (0xff >> 6)) << 6) | t3; tmp++; resultsize += 1; } p++; } //*tmp = ''; //tmp++; //*tmp = ''; //resultsize += 2; //*out = result; //*outsize = resultsize; return resultsize;}int MyCharSet::unicode_to_utf8(uint16_t *in,uint8_t **out){ int i = 0; int outsize = 0; int charscount = 0; uint8_t *result = NulL; uint8_t *tmp = NulL; charscount = insize / sizeof(uint16_t); result = (uint8_t *)malloc(charscount * 3 + 1); memset(result,charscount * 3 + 1); tmp = result; for (i = 0; i < charscount; i++) { uint16_t unicode = in[i]; if (unicode >= 0x0000 && unicode <= 0x007f) { *tmp = (uint8_t)unicode; tmp += 1; outsize += 1; } else if (unicode >= 0x0080 && unicode <= 0x07ff) { *tmp = 0xc0 | (unicode >> 6); tmp += 1; *tmp = 0x80 | (unicode & (0xff >> 2)); tmp += 1; outsize += 2; } else if (unicode >= 0x0800 && unicode <= 0xffff) { *tmp = 0xe0 | (unicode >> 12); tmp += 1; *tmp = 0x80 | (unicode >> 6 & 0x00ff); tmp += 1; *tmp = 0x80 | (unicode & (0xff >> 2)); tmp += 1; outsize += 3; } } *tmp = ''; *out = result; return outsize;}
如果使用直接:
这个输入自行扩展,现在我的需求不用不需要,也就没写,大家可以自行输入。
给个思路大家吧,在切割字符的时候可以自行添加回车,我暂时想到这样来做!
我做的效果!
需求来了,希望可以移动光标进行删除指定的!所以下一篇讲讲光标移动实现!
总结以上是内存溢出为你收集整理的cocos2dx3.6 实现带光标的输入框全部内容,希望文章能够帮你解决cocos2dx3.6 实现带光标的输入框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)