——tolua++使用(将2d_x自定义类_CursorTextField_导入lua)

来源:互联网 发布:mysql 解除表锁定 编辑:程序博客网 时间:2024/04/29 11:30

步骤1. 准备自定义文件,UIEventDispatcher.h, UIEventDispatcher.cpp, CursorTextField.h,CursorTextField.cpp

#ifndef __UIEVENTDISPATCHER_H__#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#define __UIEVENTDISPATCHER_H__#include "cocos2d.h"USING_NS_CC;using namespace std;class UIEventDelegate{public:virtual void keyEvent(UINT m, WPARAM w, LPARAM l) = 0;};class UIEventDispatcher : public CCObject{protected:vector<UIEventDelegate *> _listener;static UIEventDispatcher *_instance;public:static UIEventDispatcher * sharedDispatcher();UIEventDispatcher();~UIEventDispatcher(){}void win32Key(UINT m, WPARAM w, LPARAM l);void addListener(UIEventDelegate *l);void removeListener(UIEventDelegate *l);};void UIEventDispatcherHook(UINT m, WPARAM w, LPARAM l);#endif#endif //__UIEVENTDISPATCHER_H__
#ifndef __UIEVENTDISPATCHER_H__#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#define __UIEVENTDISPATCHER_H__#include "cocos2d.h"USING_NS_CC;using namespace std;class UIEventDelegate{public:virtual void keyEvent(UINT m, WPARAM w, LPARAM l) = 0;};class UIEventDispatcher : public CCObject{protected:vector<UIEventDelegate *> _listener;static UIEventDispatcher *_instance;public:static UIEventDispatcher * sharedDispatcher();UIEventDispatcher();~UIEventDispatcher(){}void win32Key(UINT m, WPARAM w, LPARAM l);void addListener(UIEventDelegate *l);void removeListener(UIEventDelegate *l);};void UIEventDispatcherHook(UINT m, WPARAM w, LPARAM l);#endif#endif //__UIEVENTDISPATCHER_H__

#pragma once#ifndef __CURSORTEXTFIELD_H__#define __CURSORTEXTFIELD_H__#include "cocos2d.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)#include "UIEventDispatcher.h"#endifUSING_NS_CC;class CursorTextField : public CCTextFieldTTF, public CCTextFieldDelegate, public CCTouchDelegate#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32), public UIEventDelegate#endif{private:CCPoint _touchBeginPos;CCSprite *_cursor;CCAction *_cursorAction;CCPoint _cursorPos;bool _password;unsigned int _maxLength;CCSize _designedSize;public:CursorTextField();~CursorTextField();static CursorTextField * create(const char *fontName, float fontSize);static CursorTextField * createWithPlaceHolder(const char *ph, const char *fontName, float fontSize);//CCLayervoid onEnter();void onExit();void initCursorSprite(int height);//CCTextFieldDelegatevirtual bool onTextFieldAttachWithIME(CCTextFieldTTF *s);virtual bool onTextFieldDetachWithIME(CCTextFieldTTF *s);virtual bool onTextFieldInsertText(CCTextFieldTTF *s, const char *t, int len);virtual bool onTextFieldDeleteBackward(CCTextFieldTTF *s, const char *delText, int len);//CCLayerbool ccTouchBegan(CCTouch *t, CCEvent *e);void ccTouchEnded(CCTouch *t, CCEvent *e);bool isInTextField(CCTouch *t);CCRect getRect();void openIME();void closeIME();bool isPassword();void setPassword(bool b);unsigned int getMaxLength();void setMaxLength(unsigned int n);//overload for display "*"void setString(const char *t);void updateDisplay();void setColor(const ccColor3B& c);//area for touch to open IMECCSize getDesignedSize();void setDesignedSize(CCSize s);//UIEventDelegate#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)void keyEvent(UINT m, WPARAM w, LPARAM l);#endif};#endif //__CURSORTEXTFIELD_H__

#include "CursorTextField.h"static int _calcCharCount(const char *t){int n = 0;char ch = 0;while(ch == *t){CC_BREAK_IF(!ch);if(0x80 != (0xC0 & ch))++ n;++ t;}return n;}const static float DELTA = 20.0f;CursorTextField::CursorTextField(): _cursor(NULL), _cursorAction(NULL), _password(false), _maxLength(30){CCTextFieldTTF();}CursorTextField::~CursorTextField(){//CC_SAFE_RELEASE(this->_cursor);CC_SAFE_RELEASE(this->_cursorAction);}CursorTextField * CursorTextField::create(const char *fontName, float fontSize){return createWithPlaceHolder("", fontName, fontSize);}CursorTextField * CursorTextField::createWithPlaceHolder(const char *ph, const char *fontName, float fontSize){CursorTextField *o = new CursorTextField();if(o && o->initWithString("", fontName, fontSize)){o->autorelease();if(ph)o->setPlaceHolder(ph);o->initCursorSprite(fontSize);return o;}CC_SAFE_DELETE(o);return NULL;}void CursorTextField::initCursorSprite(int height){int column = 4;int *pixels = new int[height * column];for(int i = 0; i < height * column; i ++)pixels[i] = 0xffffffff;CCTexture2D *ttr = new CCTexture2D();ttr->initWithData(pixels, kCCTexture2DPixelFormat_RGB888, 1, 1, CCSizeMake(column, height));delete [] pixels;CCSize s = getContentSize();_cursorPos = ccp(0, s.height / 2);if(_cursor != NULL){removeChild(_cursor, true);}_cursor = CCSprite::createWithTexture(ttr);_cursor->setPosition(_cursorPos);_cursor->setVisible(false);addChild(_cursor);_cursorAction = CCRepeatForever::create((CCActionInterval *)CCSequence::create(CCFadeOut::create(0.25f), CCFadeIn::create(0.25f), NULL));_cursorAction->retain();}void CursorTextField::onEnter(){#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)//UIEventDispatcher::sharedDispatcher()->addListener(this);#endifCCTextFieldTTF::onEnter();CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -128, false);this->setDelegate(this);_cursor->runAction(_cursorAction);}void CursorTextField::onExit(){#if(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)//UIEventDispatcher::sharedDispatcher()->removeListener(this);#endif_cursor->stopAction(_cursorAction);this->detachWithIME();CCTextFieldTTF::onExit();CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);}bool CursorTextField::ccTouchBegan(CCTouch *t, CCEvent *e){this->_touchBeginPos = CCDirector::sharedDirector()->convertToGL(t->getLocationInView());return true;}void CursorTextField::ccTouchEnded(CCTouch *t, CCEvent *e){CCPoint ep = CCDirector::sharedDirector()->convertToGL(t->getLocationInView());if(abs(ep.x - this->_touchBeginPos.x) > DELTA || abs(ep.y - this->_touchBeginPos.y) > DELTA){this->_touchBeginPos.x = this->_touchBeginPos.y = -1;}elseisInTextField(t)? openIME() : closeIME();}CCRect CursorTextField::getRect(){CCSize s = &this->_designedSize != NULL? this->_designedSize : getContentSize();return CCRectMake(0 - s.width * getAnchorPoint().x, 0 - s.height * getAnchorPoint().y, s.width, s.height);}CCSize CursorTextField::getDesignedSize(){return this->_designedSize;}void CursorTextField::setDesignedSize(CCSize s){this->_designedSize = s;}bool CursorTextField::isInTextField(CCTouch *t){return getRect().containsPoint(convertTouchToNodeSpaceAR(t));}// ############################# Delegate functions ###############################bool CursorTextField::onTextFieldAttachWithIME(CCTextFieldTTF *s){if(!this->m_pInputText->empty())this->_cursor->setPositionX(getContentSize().width);return false;}bool CursorTextField::onTextFieldInsertText(CCTextFieldTTF *s, const char *t, int len){if(strcmp(t, "\n") == 0){this->closeIME();return false;}if(m_pInputText->length() + len > _maxLength)return true;//std::cout<<"CTF.onInsTxt()"<<t<<"\n";m_pInputText->append(t);this->updateDisplay();_cursor->setPositionX(getTextureRect().size.width);//this->_cursor->setPositionX(getContentSize().width);return true;}bool CursorTextField::onTextFieldDeleteBackward(CCTextFieldTTF *s, const char *delText, int len){if((int)m_pInputText->length() < len)return false;std::string::iterator it = m_pInputText->end();for(int i = 0; i < len; i ++)it --;m_pInputText->erase(it, m_pInputText->end());this->updateDisplay();this->_cursor->setPositionX(m_pInputText->empty()? 0 : getContentSize().width);return true;}bool CursorTextField::onTextFieldDetachWithIME(CCTextFieldTTF *s){return false;}void CursorTextField::openIME(){_cursor->setVisible(true);this->attachWithIME();}void CursorTextField::closeIME(){_cursor->setVisible(false);this->detachWithIME();}bool CursorTextField::isPassword(){return _password;}void CursorTextField::setPassword(bool b){_password = b;}unsigned int CursorTextField::getMaxLength(){return _maxLength;}void CursorTextField::setMaxLength(unsigned int n){_maxLength = n;}void CursorTextField::setString(const char *t){m_pInputText->replace(0, m_pInputText->length(), t? t : "");this->updateDisplay();}void CursorTextField::updateDisplay(){std::string s;if(m_pInputText->length() == 0){s = *m_pPlaceHolder;}else if(_password){for(unsigned int i = 0; i < m_pInputText->size(); i++)s.append("*");s.c_str();}else{ s = *m_pInputText;}CCLabelTTF::setString(s.c_str());}void CursorTextField::setColor(const ccColor3B& c){#if COCOS2D_VERSION < 0x00020100m_sColor = m_sColorUnmodified = c;if(m_bOpacityModifyRGB){m_sColor.r = c.r * m_nOpacity/255.0f;m_sColor.g = c.g * m_nOpacity/255.0f;m_sColor.b = c.b * m_nOpacity/255.0f;}#endifupdateColor();_cursor->setColor(c);}#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)void CursorTextField::keyEvent(UINT m, WPARAM w, LPARAM l){if(!_cursor->isVisible())return;if(m == WM_CHAR){//CCLOG("CursorTextField.keyEvent %d %d %c", m, w, w);if(VK_BACK == w){this->onTextFieldDeleteBackward(this, NULL, 1);}else if(VK_RETURN == w){}else{char s[2];sprintf(s, "%c", w);this->onTextFieldInsertText(this, s, 1);}}}#endif

步骤2. 进入引擎目录


      

  • 注意 编写pkg 文件 要遵循以下的规则


    2.Writing .pkg files 编写pkg文件 1)enum keeps the same 保持枚举类型不变 

    2)remove CC_DLL for the class defines, pay attention to multi inherites 删除CC_DLL的类定义、改为多继承 

    3)remove inline keyword for declaration and implementation 删掉声明的inline关键词 

    4)remove public protect and private 删除访问限定

    5)remove the decalration of class member variable 删除类的成员变量 

    6)keep static keyword 保留statiic关键词

7)remove memeber functions that declared as private or protected 成员函数声明为私人或受保护的都删掉

      这里是编写好的 CursorTextField.pgk

     

class CursorTextField : public CCTextFieldTTF, public CCTextFieldDelegate, public CCTouchDelegate{CursorTextField();~CursorTextField();static CursorTextField * create(const char *fontName, float fontSize);static CursorTextField * createWithPlaceHolder(const char *ph, const char *fontName, float fontSize);//CCLayervoid onEnter();void onExit();void initCursorSprite(int height);//CCTextFieldDelegatevirtual bool onTextFieldAttachWithIME(CCTextFieldTTF *s);virtual bool onTextFieldDetachWithIME(CCTextFieldTTF *s);virtual bool onTextFieldInsertText(CCTextFieldTTF *s, const char *t, int len);virtual bool onTextFieldDeleteBackward(CCTextFieldTTF *s, const char *delText, int len);//CCLayerbool ccTouchBegan(CCTouch *t, CCEvent *e);void ccTouchEnded(CCTouch *t, CCEvent *e);bool isInTextField(CCTouch *t);CCRect getRect();void openIME();void closeIME();bool isPassword();void setPassword(bool b);unsigned int getMaxLength();void setMaxLength(unsigned int n);//overload for display "*"void setString(const char *t);void updateDisplay();void setColor(const ccColor3B& c);//area for touch to open IMECCSize getDesignedSize();void setDesignedSize(CCSize s);//UIEventDelegate};

      编写批处理文件 buildtext.bat 生成 CursorTextFieldTolua.h, CursorTextFieldTolua.cpp

@echo on tolua++.exe -H CursorTextFieldTolua.h -o CursorTextFieldTolua.cpp CursorTextField.pkg @pause

步骤3.将这些文件导入你的lua项目修改 解决他们的报错






步骤4. 在你的程序调用lua之前 执行

  

tolua_CursorTextField_open(pEngine->getLuaStack()->getLuaState());
最后, 在你的lua 文件中使用你的 自定义类

edit = CursorTextField:createWithPlaceHolder("在你的lua文件中使用你的自定义类", "liukai.ttf", 25)edit:setDesignedSize(CCSize(200, 50))edit:setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height*2/5))sceneGame:addChild(edit)

大功告成。
















        

0 0
原创粉丝点击