VS搭建lua开发环境和LuaBridge注册c++类(cocos2dx项目)(三) 给editbox添加事件
直接贴出实现代码
1 void ScriptManager::addEditBoxEvent(LuaEditBox* ui, std::string funName) 2 { 3 std::string editBoxEditingDidBegin = funName + "_editBoxEditingDidBegin"; 4 std::string editBoxEditingDidEnd = funName + "_editBoxEditingDidEnd"; 5 std::string editBoxTextChanged = funName + "_editBoxTextChanged"; 6 std::string editBoxReturn = funName + "_editBoxReturn"; 7 8 auto dbg = new LuaRef(getGlobal(luaState, editBoxEditingDidBegin.c_str())); 9 auto ded = new LuaRef(getGlobal(luaState, editBoxEditingDidEnd.c_str())); 10 auto tc = new LuaRef(getGlobal(luaState, editBoxTextChanged.c_str())); 11 auto br = new LuaRef(getGlobal(luaState, editBoxReturn.c_str())); 12 13 try 14 { 15 if (dbg->isFunction()) 16 { 17 ui->dbg = [dbg](){(*dbg)(); }; 18 } 19 if (ded->isFunction()) 20 { 21 ui->ded = [ded](){(*ded)(); }; 22 } 23 if (tc->isFunction()) 24 { 25 ui->tc = [tc](){(*tc)(); }; 26 } 27 if (br->isFunction()) 28 { 29 ui->br = [br](){(*br)(); }; 30 } 31 } 32 catch (LuaException& e) 33 { 34 CCLog("lua error: %s", e.what()); 35 } 36 37 }
下面是editbox封装类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class LuaEditBox :public Layer, public EditBoxDelegate, public IMEDelegate 2 { 3 public: 4 LuaEditBox(); 5 ~LuaEditBox(); 6 7 static LuaEditBox* create(Node* node, int flag, Vec2 pos); 8 9 std::string getName(); 10 11 std::string getText(); 12 13 static int boxNumber; 14 // LuaEditBox* createEditBox(Node* node,int flag, Vec2 pos = Vec2(0,0) ); 15 16 void editBoxEditingDidBegin(EditBox* editBox) 17 { 18 if (dbg){ dbg(); }; 19 }; 20 void editBoxEditingDidEnd(EditBox* editBox) 21 { 22 if (ded){ ded(); }; 23 }; 24 void editBoxTextChanged(EditBox* editBox, const std::string& text) 25 { 26 if (tc) 27 { 28 tc(); 29 }; 30 }; 31 void editBoxReturn(EditBox* editBox) 32 { 33 if (br){ br(); }; 34 }; 35 virtual void keyboardWillShow(IMEKeyboardNotificationInfo& info){}; 36 37 /*void (*dbg)(); 38 void (*ded)(); 39 void (*tc)(); 40 void (*br)();*/ 41 42 std::function<void()> dbg; 43 std::function<void()> ded; 44 std::function<void()> tc; 45 std::function<void()> br; 46 47 protected: 48 EditBox* m_editBox; 49 private: 50 51 std::string _name; 52 53 };
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "LuaEditBox.h" 2 #include "UIManager.h" 3 4 int LuaEditBox::boxNumber = 0; 5 LuaEditBox::LuaEditBox() :m_editBox(nullptr) 6 { 7 _name = "LuaEditBox_" + boxNumber; 8 } 9 10 11 LuaEditBox::~LuaEditBox() 12 { 13 } 14 15 LuaEditBox* LuaEditBox::create(Node* node, int flag, Vec2 pos) 16 { 17 LuaEditBox* pLuaEditBox = new LuaEditBox(); 18 19 auto editBoxSize = Size(228, 40); 20 std::string pNormalSprite = FileUtils::getInstance()->fullPathForFilename("InputBg.png"); 21 pLuaEditBox->m_editBox = EditBox::create(editBoxSize, Scale9Sprite::create(pNormalSprite)); 22 // m_editBox->setPosition(pos); 23 pLuaEditBox->m_editBox->setFontName("微软雅黑"); 24 pLuaEditBox->m_editBox->setFontSize(20); 25 pLuaEditBox->m_editBox->setFontColor(Color3B::WHITE); 26 // m_editBox->setPlaceHolder("Name:"); 27 pLuaEditBox->m_editBox->setPlaceholderFontColor(Color3B::WHITE); 28 pLuaEditBox->m_editBox->setMaxLength(8); 29 pLuaEditBox->m_editBox->setReturnType(EditBox::KeyboardReturnType::DONE); 30 pLuaEditBox->m_editBox->setInputFlag(EditBox::InputFlag(flag)); 31 pLuaEditBox->m_editBox->setDelegate(pLuaEditBox); 32 33 pLuaEditBox->addChild(pLuaEditBox->m_editBox); 34 pLuaEditBox->setPosition(pos); 35 36 node->addChild(pLuaEditBox); 37 if (pLuaEditBox) 38 { 39 pLuaEditBox->autorelease(); 40 return pLuaEditBox; 41 } 42 else 43 { 44 delete pLuaEditBox; 45 pLuaEditBox = NULL; 46 return NULL; 47 } 48 } 49 50 std::string LuaEditBox::getName() 51 { 52 return _name; 53 } 54 55 std::string LuaEditBox::getText() 56 { 57 std::string str = m_editBox->getText(); 58 return str; 59 }