Adding the 1st TextInputTest test from cpp test to lua test.
Since clang is not working on window you have to do the work by hand.
A number of files will be added or modified.
I will include the new files and some of the modified files.
Two new facilities have to be added:
cc.KeyboardNotificationLayer:create()
a static function
KeyboardNotificationLayer:getString()
a member function
Most of the code is taken form TextInputTest.cpp and TextInputTest.h in TestCpp
new files created:
E:\cocos2d-x\cocos\2d\CCKeyboardNotificationLayer.cpp
E:\cocos2d-x\cocos\2d\CCKeyboardNotificationLayer.h
They are added to the project libcocos2d in the test_input_node folder.
E:\cocos2d-x\samples\Lua\TestLua\Resources\luaScript\TextInputTest\TextInputTest.lua
For testing
Most of the code come from TextInputTest.cpp except for
create
static function and the getString()
member function.
Since I am including all those modified files I will not detail the changes.
change to file E:\cocos2d-x\samples\Lua\TestLua\Resources\luaScript\mainMenu.lua
is not included
add
require "luaScript/TextInputTest/TextInputTest"
to the import list
set isSupported to true for TextInputTest
{ isSupported = true, name = "TextInputTest", create_func=TextInputTestMain},
Bindings:
-
create a new table ccu and add the user functionality in this table
like spine and physics (maybe later) -
extend: lua_cocos2dx_auto.cpp (much easier)
laziness is a programmer virtue
This code is inserted at line 6 of file lua_cocos2dx_auto.cpp
#include "CCKeyboardNotificationLayer.h"
int lua_cocos2dx_KeyboardNotificationLayer_create(lua_State* tolua_S)
{
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertable(tolua_S,1,"KeyboardNotificationLayer",0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(tolua_S) - 1;
if (argc == 0)
{
if(!ok)
return 0;
cocos2d::KeyboardNotificationLayer* ret = cocos2d::KeyboardNotificationLayer::create();
do {
if (NULL != ret){
std::string hashName = typeid(*ret).name();
auto iter = g_luaType.find(hashName);
std::string className = "";
if(iter != g_luaType.end()){
className = iter->second.c_str();
} else {
className = "KeyboardNotificationLayer";
}
cocos2d::Object *dynObject = dynamic_cast<cocos2d::Object *>((cocos2d::KeyboardNotificationLayer*)ret);
if (NULL != dynObject) {
int ID = ret ? (int)(dynObject->_ID) : -1;
int* luaID = ret ? &(dynObject->_luaID) : NULL;
toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret,className.c_str());
} else {
tolua_pushusertype(tolua_S,(void*)ret,className.c_str());
}} else {
lua_pushnil(tolua_S);
}
} while (0);
return 1;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "create",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_KeyboardNotificationLayer_create'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_KeyboardNotificationLayer_getString(lua_State* tolua_S)
{
int argc = 0;
cocos2d::KeyboardNotificationLayer* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(tolua_S, 1, "KeyboardNotificationLayer", 0, &tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::KeyboardNotificationLayer*)tolua_tousertype(tolua_S, 1, 0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S, "invalid 'cobj' in function 'lua_cocos2dx_KeyboardNotificationLayer_getString'", NULL);
return 0;
}
#endif
argc = lua_gettop(tolua_S) - 1;
if (argc == 0)
{
if (!ok)
return 0;
const std::string& ret = cobj->getString();
tolua_pushcppstring(tolua_S, ret);
return 1;
}
CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getString", argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S, "#ferror in function 'lua_cocos2dx_KeyboardNotificationLayer_getString'.", &tolua_err);
#endif
return 0;
}
int lua_register_cocos2dx_KeyboardNotificationLayer(lua_State* tolua_S)
{
tolua_usertype(tolua_S,"KeyboardNotificationLayer");
tolua_cclass(tolua_S,"KeyboardNotificationLayer","KeyboardNotificationLayer","Node",NULL);
tolua_beginmodule(tolua_S,"KeyboardNotificationLayer");
tolua_function(tolua_S,"create", lua_cocos2dx_KeyboardNotificationLayer_create);
tolua_function(tolua_S, "getString", lua_cocos2dx_KeyboardNotificationLayer_getString);
tolua_endmodule(tolua_S);
std::string typeName = typeid(cocos2d::KeyboardNotificationLayer).name();
g_luaType[typeName] = "KeyboardNotificationLayer";
return 1;
}
How to code this monster. It is relatively easy.
1st find a function which has similar (identical is better) arguments.
Copy it and carefully use find and replace to modify the name.
finally add
lua_register_cocos2dx_KeyboardNotificationLayer(tolua_S);
to function
register_all_cocos2dx(lua_State* tolua_S)
in file lua_cocos2dx_auto.cpp so your new binding will be registered.
Good luck.
Andre
NOTE: I have not yet tested this code on android but I would expect it to work.
Have faith
CCKeyboardNotificationLayer.cpp.zip (2.0 KB)
CCKeyboardNotificationLayer.h.zip (0.7 KB)
TextInputTest.cpp.zip (3.1 KB)
TextInputTest.h.zip (0.9 KB)
TextInputTest.lua.zip (0.5 KB)