Beta version adding new bindings on window


#1

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:

  1. create a new table ccu and add the user functionality in this table
    like spine and physics (maybe later)

  2. 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 :slight_smile:


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)


#2

It actually does not work
first: you have to add
CCKeyboardNotificationLayer.cpp to file E:\cocos2d-x\cocos\2d\Android.mk
to compile.

But it still does not run


#3

The problem on the android

  1. if a physical keyboard is available then
    TextFieldTTF::insertText is not invoked by the IME.

  2. if an on screen keyboard is used then
    TextFieldTTF::insertText is invoked by the IME.
    I logged the character in the insert code.

02-03 10:01:12.824: D/cocos2d-x debug info(12480): Char: a

but the following error occurred:

02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.824: D/cocos2d-x debug info(12480): FILE: Thonburi
02-03 10:01:12.824: D/cocos2d-x debug info(12480): cocos2d: fullPathForFilename: No file found at Thonburi. Possible missing file.
02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.824: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.834: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.834: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.834: E/dalvikvm(12480): JNI ERROR: env->self != thread-self (0x5810e9b8 vs. 0x56e43010); auto-correcting
02-03 10:01:12.834: E/libEGL(12480): call to OpenGL ES API with no current context (logged once per thread)

any hint would be appreciated

Andre

PS: Downloading beta2 :;ok


#4

Can you give me your android phone model,and tell me how to use a physical keyboard,thanks.


#5

I use a tablet Asus tf700.
I moved to beta 2.
In the previous versions I have been able to use the keyboard but on Beta and Beta 2 the keyboard software is not working. I have some more work to do before going back to this problem. I am just hoping that beta 3 will fix the problems.

On Beta 2 I got testcpp to work on Android but the TextInputTest does not work on Android.

Good luck
Andre