CCTextFieldTTF的使用和再封装

     CCTextFieldTTF是cocos2d-x封装的输入框控件,主要配合CCTextFieldDelegate和CCTouchDelegate两个代理接口使用,前者是用来处理文字的输入删除操作的代理类,后者是用来触发打开和关闭输入法键盘的。

      借鉴网友oneRain88的“带光标的输入框”文章,在其基础上加入了密码框星号显示设置,字符长度设置,触摸弹出输入法区域设置。

     文章链接:http://blog.csdn.net/onerain88/article/details/7572315,这篇文章对CCTextFieldTTF的使用原理讲的很好,封装的思路也很好,想了解原理可以去读读,本人只是在他的基础上加了一些小功能。在此也谢谢oneRain88的分享。

Cpp代码   收藏代码
  1. #ifndef DTCursorTextField_h  
  2. #define DTCursorTextField_h  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. USING_NS_CC;  
  7.   
  8. class DTCursorTextField: public CCTextFieldTTF, public CCTouchDelegate  
  9.     ,public CCTextFieldDelegate  
  10. {  
  11. private:  
  12.     // 点击开始位置  
  13.     CCPoint m_beginPos;  
  14.       
  15.     // 光标精灵  
  16.     CCSprite *m_pCursorSprite;  
  17.       
  18.     // 光标动画  
  19.     CCAction *m_pCursorAction;  
  20.                    
  21.     // 光标坐标  
  22.     CCPoint m_cursorPos;  
  23.       
  24.     // 是否加密显示  
  25.     bool isPsw;  
  26.     int m_limitNum;  
  27.     // 输入框内容  
  28.     //std::string *m_pInputText;  
  29.     CCSize m_designedSize;  
  30.       
  31. public:  
  32.     DTCursorTextField();  
  33.     ~DTCursorTextField();  
  34.       
  35.     // static,暂时不能使用  
  36. //    static DTCursorTextField * textFieldWithPlaceHolder(const char *placeholder, const CCSize& dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);  
  37.       
  38.     /** creates a CCLabelTTF from a fontname and font size */  
  39.     static DTCursorTextField * textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize);  
  40.       
  41.     // CCLayer  
  42.     void onEnter();  
  43.     void onExit();  
  44.       
  45.     // 初始化光标精灵  
  46.     void initCursorSprite(int nHeight);  
  47.     void setColor(const ccColor3B& color3);  
  48.     // CCTextFieldDelegate  
  49.     virtual bool onTextFieldAttachWithIME(CCTextFieldTTF *pSender);  
  50.     virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * pSender);  
  51.     virtual bool onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen);  
  52.     virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen);  
  53.       
  54.     // CCLayer Touch  
  55.     bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  56.     void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  
  57.       
  58.     // 判断是否点击在TextField处  
  59.     bool isInTextField(CCTouch *pTouch);  
  60.     // 得到TextField矩形  
  61.     CCRect getRect();  
  62.       
  63.     // 打开输入法  
  64.     void openIME();  
  65.     // 关闭输入法  
  66.     void closeIME();  
  67.       
  68.     //设置是否星号显示  
  69.     bool getIsPsw();  
  70.     void setIsPsw(bool bFlag);  
  71.     //设置字符长度限制,一个汉字三个字符  
  72.     void setLimitNum(int limitNum);  
  73.     int getLimitNum();  
  74.     //重载原函数,用来显示星号  
  75.     void setString(const char* displayTx, const char* inputTx);  
  76.     //点击弹出输入法的尺寸范围  
  77.     void setDesignedSize(CCSize size);  
  78.     CCSize getDesignedSize();  
  79. };  
  80.   
  81. #endif  

 

Cpp代码   收藏代码
  1. #include "DTCursorTextField.h"  
  2. #include "CCNotificationCenter.h"  
  3.   
  4. static int _calcCharCount(const char * pszText)  
  5. {  
  6.     int n = 0;  
  7.     char ch = 0;  
  8.     while ((ch = *pszText))  
  9.     {  
  10.         CC_BREAK_IF(! ch);  
  11.           
  12.         if (0x80 != (0xC0 & ch))  
  13.         {  
  14.             ++n;  
  15.         }  
  16.         ++pszText;  
  17.     }  
  18.     return n;  
  19. }  
  20.   
  21. const static float DELTA = 20.0f;  
  22.   
  23. DTCursorTextField::DTCursorTextField()  
  24. {  
  25.     CCTextFieldTTF();  
  26.       
  27.     m_pCursorSprite = NULL;  
  28.     m_pCursorAction = NULL;  
  29.       
  30.     m_pInputText = NULL;  
  31.     isPsw = false;  
  32.     m_limitNum = 30;  
  33. }  
  34.   
  35. DTCursorTextField::~DTCursorTextField()  
  36. {  
  37.     //delete m_pInputText;  
  38. }  
  39.   
  40. void DTCursorTextField::onEnter()  
  41. {  
  42.     CCTextFieldTTF::onEnter();  
  43.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, false);  
  44.     this->setDelegate(this);  
  45. }  
  46. //静态生成函数  
  47. DTCursorTextField * DTCursorTextField::textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)  
  48. {  
  49.     DTCursorTextField *pRet = new DTCursorTextField();  
  50.       
  51.     if(pRet && pRet->initWithString("", fontName, fontSize))  
  52.     {  
  53.         pRet->autorelease();  
  54.         if (placeholder)  
  55.         {  
  56.             pRet->setPlaceHolder(placeholder);  
  57.         }  
  58.         pRet->initCursorSprite(fontSize);  
  59.           
  60.         return pRet;  
  61.     }  
  62.       
  63.     CC_SAFE_DELETE(pRet);  
  64.       
  65.     return NULL;  
  66. }  
  67.   
  68. void DTCursorTextField::initCursorSprite(int nHeight)  
  69. {  
  70.     // 初始化光标  
  71.     int column = 4;  
  72.     int pixels[nHeight][column];  
  73.     for (int i=0; i<nHeight; ++i) {  
  74.         for (int j=0; j<column; ++j) {  
  75.              pixels[i][j] = 0xffffffff;  
  76.         }  
  77.     }  
  78.   
  79.     CCTexture2D *texture = new CCTexture2D();  
  80.     texture->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(column, nHeight));  
  81.       
  82.     m_pCursorSprite = CCSprite::create(texture);  
  83.     CCSize winSize = getContentSize();  
  84.     m_cursorPos = ccp(0, winSize.height / 2);  
  85.     m_pCursorSprite->setPosition(m_cursorPos);  
  86.     this->addChild(m_pCursorSprite);  
  87.       
  88.     m_pCursorAction = CCRepeatForever::create((CCActionInterval *) CCSequence::create(CCFadeOut::create(0.25f), CCFadeIn::create(0.25f), NULL));  
  89.       
  90.     m_pCursorSprite->runAction(m_pCursorAction);  
  91.       
  92.     m_pInputText = new std::string();  
  93. }  
  94.   
  95. bool DTCursorTextField::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  96. {      
  97.     m_beginPos = pTouch->locationInView();  
  98.     m_beginPos = CCDirector::sharedDirector()->convertToGL(m_beginPos);  
  99.       
  100.     return true;  
  101. }  
  102.   
  103. CCRect DTCursorTextField::getRect()  
  104. {  
  105.     CCSize size;  
  106.     if (&m_designedSize != NULL) {  
  107.          size = m_designedSize;  
  108.     }else {  
  109.         size = getContentSize();  
  110.     }  
  111.      
  112.     CCRect rect = CCRectMake(0 - size.width * getAnchorPoint().x, 0 - size.height * getAnchorPoint().y, size.width, size.height);  
  113.     return  rect;  
  114. }  
  115. //设置触摸弹出输入法的区域大小  
  116. void DTCursorTextField::setDesignedSize(cocos2d::CCSize size)  
  117. {  
  118.     m_designedSize = size;  
  119. }  
  120.   
  121. CCSize DTCursorTextField::getDesignedSize()  
  122. {  
  123.     return m_designedSize;  
  124. }  
  125.   
  126. bool DTCursorTextField::isInTextField(cocos2d::CCTouch *pTouch)  
  127. {     
  128.     CCPoint pToushPos = convertTouchToNodeSpaceAR(pTouch);  
  129.     return CCRect::CCRectContainsPoint(getRect(), pToushPos);  
  130. }  
  131.   
  132. void DTCursorTextField::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  133. {  
  134.     CCPoint endPos = pTouch->locationInView();  
  135.     endPos = CCDirector::sharedDirector()->convertToGL(endPos);  
  136.       
  137.     // 判断是否为点击事件  
  138.     if (::abs(endPos.x - m_beginPos.x) > DELTA ||   
  139.         ::abs(endPos.y - m_beginPos.y) > DELTA)   
  140.     {  
  141.         // 不是点击事件  
  142.         m_beginPos.x = m_beginPos.y = -1;  
  143.           
  144.         return;  
  145.     }  
  146.       
  147.     CCLOG("width: %f, height: %f.", getContentSize().width, getContentSize().height);  
  148.       
  149.     // 判断是打开输入法还是关闭输入法  
  150.     isInTextField(pTouch) ? openIME() : closeIME();  
  151. }  
  152.   
  153. bool DTCursorTextField::onTextFieldAttachWithIME(cocos2d::CCTextFieldTTF *pSender)  
  154. {  
  155.     if (m_pInputText->empty()) {  
  156.         return false;  
  157.     }  
  158.       
  159.     m_pCursorSprite->setPositionX(getContentSize().width);  
  160.       
  161.     return false;  
  162. }  
  163.   
  164. bool DTCursorTextField::onTextFieldInsertText(cocos2d::CCTextFieldTTF *pSender, const char *text, int nLen)  
  165. {  
  166.     CCLOG("Width: %f", pSender->getContentSize().width);  
  167.     CCLOG("Text: %s", text);  
  168.     CCLOG("Length: %d", nLen);  
  169.       
  170.     std::string tempStr = m_pInputText->substr();  
  171.     tempStr.append(text);  
  172.     if (tempStr.length() > m_limitNum) {  
  173.         return true;  
  174.     }  
  175.       
  176.     m_pInputText->append(text);  
  177.       
  178.       
  179.     if (isPsw) {  
  180.         std::string tempStr;  
  181.         for (int i = 0; i < m_pInputText->size(); i++) {  
  182.             tempStr.append("*");  
  183.         }  
  184.         setString(tempStr.c_str(), m_pInputText->c_str());  
  185.     }else {  
  186.         setString(m_pInputText->c_str(), m_pInputText->c_str());  
  187.     }      
  188.       
  189.     m_pCursorSprite->setPositionX(getContentSize().width);  
  190.       
  191.     return true;  
  192. }  
  193.   
  194. bool DTCursorTextField::onTextFieldDeleteBackward(cocos2d::CCTextFieldTTF *pSender, const char *delText, int nLen)  
  195. {  
  196.     m_pInputText->resize(m_pInputText->size() - nLen);  
  197.     CCLog(m_pInputText->c_str());  
  198.       
  199.     if (isPsw) {  
  200.         std::string tempStr;  
  201.         for (int i = 0; i < m_pInputText->size(); i++) {  
  202.             tempStr.append("*");  
  203.         }  
  204.         setString(tempStr.c_str(), m_pInputText->c_str());  
  205.     }else {  
  206.         setString(m_pInputText->c_str(), m_pInputText->c_str());  
  207.     }  
  208.       
  209.     m_pCursorSprite->setPositionX(getContentSize().width);  
  210.   
  211.     if (m_pInputText->empty()) {  
  212.         m_pCursorSprite->setPositionX(0);  
  213.     }  
  214.       
  215.     return true;  
  216. }  
  217.   
  218. bool DTCursorTextField::onTextFieldDetachWithIME(cocos2d::CCTextFieldTTF *pSender)  
  219. {  
  220.     return false;  
  221. }  
  222.   
  223. void DTCursorTextField::openIME()  
  224. {  
  225.     m_pCursorSprite->setVisible(true);  
  226.     this->attachWithIME();  
  227. }  
  228.   
  229. void DTCursorTextField::closeIME()  
  230. {  
  231.     m_pCursorSprite->setVisible(false);  
  232.     this->detachWithIME();  
  233. }  
  234.   
  235. void DTCursorTextField::onExit()  
  236. {  
  237.     this->detachWithIME();  
  238.     CCTextFieldTTF::onExit();  
  239.     CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  
  240. }  
  241.   
  242. bool DTCursorTextField::getIsPsw()  
  243. {  
  244.     return isPsw;  
  245. }  
  246. //设置星号显示否  
  247. void DTCursorTextField::setIsPsw( bool bFlag)  
  248. {  
  249.     isPsw = bFlag;  
  250. }  
  251.   
  252. int DTCursorTextField::getLimitNum()  
  253. {  
  254.     return m_limitNum;  
  255. }  
  256. //设置字符长度  
  257. void DTCursorTextField::setLimitNum(int limitNum)  
  258. {  
  259.     m_limitNum = limitNum;  
  260. }  
  261.   
  262. void DTCursorTextField::setString(const char *displayTx, const char* inputTx)  
  263. {  
  264.     CC_SAFE_DELETE(m_pInputText);  
  265.       
  266.     if (inputTx)  
  267.     {  
  268.         m_pInputText = new std::string(inputTx);  
  269.     }  
  270.     else  
  271.     {  
  272.         m_pInputText = new std::string;  
  273.     }  
  274.       
  275.     // if there is no input text, display placeholder instead  
  276.     if (! m_pInputText->length())  
  277.     {  
  278.         CCLabelTTF::setString(m_pPlaceHolder->c_str());  
  279.     }  
  280.     else  
  281.     {  
  282.         CCLabelTTF::setString(displayTx);  
  283.     }  
  284.     m_nCharCount = _calcCharCount(m_pInputText->c_str());  
  285. }  
  286.   
  287. void DTCursorTextField::setColor(const ccColor3B& color3)  
  288. {  
  289.     m_sColor = m_sColorUnmodified = color3;  
  290.       
  291.     if (m_bOpacityModifyRGB)  
  292.     {  
  293.         m_sColor.r = color3.r * m_nOpacity/255.0f;  
  294.         m_sColor.g = color3.g * m_nOpacity/255.0f;  
  295.         m_sColor.b = color3.b * m_nOpacity/255.0f;  
  296.     }  
  297.       
  298.     updateColor();  
  299.     m_pCursorSprite->setColor(color3);  
  300. }  

 

    使用的时候,直接使用textFieldWithPlaceHolder函数生成一个对象指针,setIsPsw,setLimitNum,setDesignedSize,setColor分别实现星号显示,字符长度,触摸弹出输入法区域大小,字符颜色。

注释比较简单,欢迎指正讨论。      

本文来自:http://www.cnblogs.com/Rooty 转载请注明出处,谢谢合作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值