a major rewrite of event registration, it's fully compatible, yet has new advantages

This commit is contained in:
mniip
2013-04-27 01:25:18 +04:00
parent 9f7d45f8fd
commit a1cdef4c90
3 changed files with 254 additions and 186 deletions

View File

@@ -489,86 +489,162 @@ int luacon_elementwrite(lua_State* l){
return 0; return 0;
} }
bool shortcuts = true; bool shortcuts = true;
int luacon_keyevent(int key, int modifier, int event){ int luacon_keyevent(int key, int modifier, int event)
int i = 0, kpcontinue = 1, callret; {
char tempkey[] = {key, 0}; int kycontinue = 1, i, j, callret;
if(keypress_function_count){ lua_State* l=luacon_ci->l;
for(i = 0; i < keypress_function_count && kpcontinue; i++){ lua_pushstring(l, "keyfunctions");
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, keypress_functions[i]); lua_rawget(l, LUA_REGISTRYINDEX);
lua_pushstring(luacon_ci->l, tempkey); if(!lua_istable(l, -1))
lua_pushinteger(luacon_ci->l, key); {
lua_pushinteger(luacon_ci->l, modifier); lua_pop(l, 1);
lua_pushinteger(luacon_ci->l, event); lua_newtable(l);
callret = lua_pcall(luacon_ci->l, 4, 1, 0); lua_pushstring(l, "keyfunctions");
if (callret) lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c=lua_objlen(l, -1);
for(i=1;i<=c && kycontinue;i++)
{
lua_rawgeti(l, -1, i);
lua_pushlstring(l, (const char*)&key, 1);
lua_pushinteger(l, key);
lua_pushinteger(l, modifier);
lua_pushinteger(l, event);
callret = lua_pcall(l, 4, 1, 0);
if (callret)
{
if (!strcmp(luaL_optstring(l, -1, ""), "Error: Script not responding"))
{ {
luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); ui::Engine::Ref().LastTick(clock());
for(j=i;j<=c-1;j++)
{
lua_rawgeti(l, -2, j+1);
lua_rawseti(l, -3, j);
}
lua_pushnil(l);
lua_rawseti(l, -3, c);
c--;
i--;
} }
if(lua_isboolean(luacon_ci->l, -1)){ lua_pop(l, 1);
kpcontinue = lua_toboolean(luacon_ci->l, -1); luacon_ci->Log(CommandInterface::LogError, luacon_geterror());
} }
lua_pop(luacon_ci->l, 1); else
{
if(!lua_isnoneornil(l, -1))
kycontinue = lua_toboolean(l, -1);
lua_pop(l, 1);
} }
} }
return kpcontinue && shortcuts; lua_pop(l, 1);
return kycontinue && shortcuts;
} }
int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel){ int luacon_mouseevent(int mx, int my, int mb, int event, int mouse_wheel)
int i = 0, mpcontinue = 1, callret; {
if(mouseclick_function_count){ int mpcontinue = 1, i, j, callret;
for(i = 0; i < mouseclick_function_count && mpcontinue; i++){ lua_State* l=luacon_ci->l;
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, mouseclick_functions[i]); lua_pushstring(l, "mousefunctions");
lua_pushinteger(luacon_ci->l, mx); lua_rawget(l, LUA_REGISTRYINDEX);
lua_pushinteger(luacon_ci->l, my); if(!lua_istable(l, -1))
lua_pushinteger(luacon_ci->l, mb); {
lua_pushinteger(luacon_ci->l, event); lua_pop(l, 1);
lua_pushinteger(luacon_ci->l, mouse_wheel); lua_newtable(l);
callret = lua_pcall(luacon_ci->l, 5, 1, 0); lua_pushstring(l, "mousefunctions");
if (callret) lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c=lua_objlen(l, -1);
for(i=1;i<=c && mpcontinue;i++)
{
lua_rawgeti(l, -1, i);
lua_pushinteger(l, mx);
lua_pushinteger(l, my);
lua_pushinteger(l, mb);
lua_pushinteger(l, event);
lua_pushinteger(l, mouse_wheel);
callret = lua_pcall(l, 5, 1, 0);
if (callret)
{
if (!strcmp(luaL_optstring(l, -1, ""), "Error: Script not responding"))
{ {
luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); ui::Engine::Ref().LastTick(clock());
for(j=i;j<=c-1;j++)
{
lua_rawgeti(l, -2, j+1);
lua_rawseti(l, -3, j);
}
lua_pushnil(l);
lua_rawseti(l, -3, c);
c--;
i--;
} }
if(lua_isboolean(luacon_ci->l, -1)){ luacon_ci->Log(CommandInterface::LogError, luaL_optstring(l, -1, ""));
mpcontinue = lua_toboolean(luacon_ci->l, -1); lua_pop(l, 1);
} }
lua_pop(luacon_ci->l, 1); else
{
if(!lua_isnoneornil(l, -1))
mpcontinue = lua_toboolean(l, -1);
lua_pop(l, 1);
} }
} }
lua_pop(l, 1);
return mpcontinue; return mpcontinue;
} }
int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::string selectalt, int bsx, int bsy)
int luacon_step(int mx, int my, std::string selectl, std::string selectr, std::string selectalt, int bsx, int bsy){ {
int tempret = 0, tempb, i, callret; int i, j, callret;
lua_pushinteger(luacon_ci->l, bsy); lua_State* l=luacon_ci->l;
lua_pushinteger(luacon_ci->l, bsx); lua_pushinteger(l, bsy);
lua_pushstring(luacon_ci->l, selectalt.c_str()); lua_pushinteger(l, bsx);
lua_pushstring(luacon_ci->l, selectr.c_str()); lua_pushstring(l, selectalt.c_str());
lua_pushstring(luacon_ci->l, selectl.c_str()); lua_pushstring(l, selectr.c_str());
lua_pushinteger(luacon_ci->l, my); lua_pushstring(l, selectl.c_str());
lua_pushinteger(luacon_ci->l, mx); lua_pushinteger(l, my);
lua_setfield(luacon_ci->l, tptProperties, "mousex"); lua_pushinteger(l, mx);
lua_setfield(luacon_ci->l, tptProperties, "mousey"); lua_setfield(l, tptProperties, "mousex");
lua_setfield(luacon_ci->l, tptProperties, "selectedl"); lua_setfield(l, tptProperties, "mousey");
lua_setfield(luacon_ci->l, tptProperties, "selectedr"); lua_setfield(l, tptProperties, "selectedl");
lua_setfield(luacon_ci->l, tptProperties, "selecteda"); lua_setfield(l, tptProperties, "selectedr");
lua_setfield(luacon_ci->l, tptProperties, "brushx"); lua_setfield(l, tptProperties, "selecteda");
lua_setfield(luacon_ci->l, tptProperties, "brushy"); lua_setfield(l, tptProperties, "brushx");
for(i = 0; i<6; i++){ lua_setfield(l, tptProperties, "brushy");
if(step_functions[i]){ lua_pushstring(l, "stepfunctions");
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, step_functions[i]); lua_rawget(l, LUA_REGISTRYINDEX);
callret = lua_pcall(luacon_ci->l, 0, 0, 0); if(!lua_istable(l, -1))
if (callret) {
lua_pop(l, 1);
lua_newtable(l);
lua_pushstring(l, "stepfunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
}
int c=lua_objlen(l, -1);
for(i=1;i<=c;i++)
{
lua_rawgeti(l, -1, i);
callret = lua_pcall(l, 0, 0, 0);
if (callret)
{
if (!strcmp(luaL_optstring(l, -1, ""), "Error: Script not responding"))
{ {
if (!strcmp(luacon_geterror(),"Error: Script not responding")) ui::Engine::Ref().LastTick(clock());
for(j=i;j<=c-1;j++)
{ {
ui::Engine::Ref().LastTick(clock()); lua_rawgeti(l, -2, j+1);
lua_pushcfunction(luacon_ci->l, &luatpt_unregister_step); lua_rawseti(l, -3, j);
lua_rawgeti(luacon_ci->l, LUA_REGISTRYINDEX, step_functions[i]);
lua_pcall(luacon_ci->l, 1, 0, 0);
} }
luacon_ci->Log(CommandInterface::LogError, luacon_geterror()); lua_pushnil(l);
lua_rawseti(l, -3, c);
c--;
i--;
} }
luacon_ci->Log(CommandInterface::LogError, luaL_optstring(l, -1, ""));
lua_pop(l, 1);
} }
} }
lua_pop(l, 1);
return 0; return 0;
} }
@@ -870,11 +946,11 @@ int luatpt_log(lua_State* l)
std::string text = ""; std::string text = "";
for(int i = 1; i <= args; i++) for(int i = 1; i <= args; i++)
{ {
luaL_tostring(l, lua_gettop(l)); luaL_tostring(l, -1);
if(text.length()) if(text.length())
text=std::string(luaL_optstring(l, lua_gettop(l), "")) + ", " + text; text=std::string(luaL_optstring(l, -1, "")) + ", " + text;
else else
text=std::string(luaL_optstring(l, lua_gettop(l), "")); text=std::string(luaL_optstring(l, -1, ""));
lua_pop(l, 2); lua_pop(l, 2);
} }
if((*luacon_currentCommand)) if((*luacon_currentCommand))
@@ -1485,155 +1561,157 @@ int luatpt_delete(lua_State* l)
int luatpt_register_step(lua_State* l) int luatpt_register_step(lua_State* l)
{ {
int ref, i, ifree = -1; if(lua_isfunction(l, 1))
if(lua_isfunction(l, 1)){ {
for(i = 0; i<6; i++){ lua_pushstring(l, "stepfunctions");
if(!step_functions[i]){ lua_rawget(l, LUA_REGISTRYINDEX);
if (ifree<0) ifree = i; if(!lua_istable(l, -1))
} else {
lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);
if(lua_equal(l, 1, lua_gettop(l))){
lua_pop(l, 1);
return luaL_error(l, "Function already registered");
}
lua_pop(l, 1);
}
}
if (ifree>=0)
{ {
ref = luaL_ref(l, LUA_REGISTRYINDEX); lua_pop(l, -1);
step_functions[ifree] = ref; lua_newtable(l);
return 0; lua_pushstring(l, "stepfunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
} }
else return luaL_error(l, "Step function limit reached"); int c = lua_objlen(l, -1);
lua_pushvalue(l, 1);
lua_rawseti(l, -2, c+1);
} }
return 0; return 0;
} }
int luatpt_unregister_step(lua_State* l) int luatpt_unregister_step(lua_State* l)
{ {
int i; if(lua_isfunction(l, 1))
if(lua_isfunction(l, 1)){ {
for(i = 0; i<6; i++){ lua_pushstring(l, "stepfunctions");
if (step_functions[i]){ lua_rawget(l, LUA_REGISTRYINDEX);
lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]); if(!lua_istable(l, -1))
if(lua_equal(l, 1, lua_gettop(l))){ {
lua_pop(l, 1); lua_pop(l, -1);
luaL_unref(l, LUA_REGISTRYINDEX, step_functions[i]); lua_newtable(l);
step_functions[i] = 0; lua_pushstring(l, "stepfunctions");
} lua_pushvalue(l, -2);
else lua_pop(l, 1); lua_rawset(l, LUA_REGISTRYINDEX);
}
int c = lua_objlen(l, -1);
int d = 0;
int i = 0;
for(i=1;i<=c;i++)
{
lua_rawgeti(l, -1, i+d);
if(lua_equal(l, 1, -1))
{
lua_pop(l, 1);
d++;
i--;
} }
else
lua_rawseti(l, -2, i);
} }
} }
return 0; return 0;
} }
int luatpt_register_keypress(lua_State* l) int luatpt_register_keypress(lua_State* l)
{ {
int *newfunctions, i; if(lua_isfunction(l, 1))
if(lua_isfunction(l, 1)){ {
for(i = 0; i<keypress_function_count; i++){ lua_pushstring(l, "keyfunctions");
lua_rawgeti(l, LUA_REGISTRYINDEX, keypress_functions[i]); lua_rawget(l, LUA_REGISTRYINDEX);
if(lua_equal(l, 1, lua_gettop(l))){ if(!lua_istable(l, -1))
lua_pop(l, 1); {
return luaL_error(l, "Function already registered");
}
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l);
lua_pushstring(l, "keyfunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
} }
newfunctions = (int*)calloc(keypress_function_count+1, sizeof(int)); int c = lua_objlen(l, -1);
if(keypress_functions){ lua_pushvalue(l, 1);
memcpy(newfunctions, keypress_functions, keypress_function_count*sizeof(int)); lua_rawseti(l, -2, c+1);
free(keypress_functions);
}
newfunctions[keypress_function_count] = luaL_ref(l, LUA_REGISTRYINDEX);
keypress_function_count++;
keypress_functions = newfunctions;
} }
return 0; return 0;
} }
int luatpt_unregister_keypress(lua_State* l) int luatpt_unregister_keypress(lua_State* l)
{ {
int *newfunctions, i, functionindex = -1; if(lua_isfunction(l, 1))
if(lua_isfunction(l, 1)){ {
for(i = 0; i<keypress_function_count; i++){ lua_pushstring(l, "keyfunctions");
lua_rawgeti(l, LUA_REGISTRYINDEX, keypress_functions[i]); lua_rawget(l, LUA_REGISTRYINDEX);
if(lua_equal(l, 1, lua_gettop(l))){ if(!lua_istable(l, -1))
functionindex = i; {
}
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l);
lua_pushstring(l, "keyfunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
} }
} int c = lua_objlen(l, -1);
if(functionindex != -1){ int d = 0;
luaL_unref(l, LUA_REGISTRYINDEX, keypress_functions[functionindex]); int i = 0;
if(functionindex != keypress_function_count-1){ for(i=1;i<=c;i++)
memmove(keypress_functions+functionindex+1, keypress_functions+functionindex+1, (keypress_function_count-functionindex-1)*sizeof(int)); {
lua_rawgeti(l, -1, i+d);
if(lua_equal(l, 1, -1))
{
lua_pop(l, 1);
d++;
i--;
}
else
lua_rawseti(l, -2, i);
} }
if(keypress_function_count-1 > 0){
newfunctions = (int*)calloc(keypress_function_count-1, sizeof(int));
memcpy(newfunctions, keypress_functions, (keypress_function_count-1)*sizeof(int));
free(keypress_functions);
keypress_functions = newfunctions;
} else {
free(keypress_functions);
keypress_functions = NULL;
}
keypress_function_count--;
} else {
return luaL_error(l, "Function not registered");
} }
return 0; return 0;
} }
int luatpt_register_mouseclick(lua_State* l) int luatpt_register_mouseclick(lua_State* l)
{ {
int *newfunctions, i; if(lua_isfunction(l, 1))
if(lua_isfunction(l, 1)){ {
for(i = 0; i<mouseclick_function_count; i++){ lua_pushstring(l, "mousefunctions");
lua_rawgeti(l, LUA_REGISTRYINDEX, mouseclick_functions[i]); lua_rawget(l, LUA_REGISTRYINDEX);
if(lua_equal(l, 1, lua_gettop(l))){ if(!lua_istable(l, -1))
lua_pop(l, 1); {
return luaL_error(l, "Function already registered");
}
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l);
lua_pushstring(l, "mousefunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
} }
newfunctions = (int*)calloc(mouseclick_function_count+1, sizeof(int)); int c = lua_objlen(l, -1);
if(mouseclick_functions){ lua_pushvalue(l, 1);
memcpy(newfunctions, mouseclick_functions, mouseclick_function_count*sizeof(int)); lua_rawseti(l, -2, c+1);
free(mouseclick_functions);
}
newfunctions[mouseclick_function_count] = luaL_ref(l, LUA_REGISTRYINDEX);
mouseclick_function_count++;
mouseclick_functions = newfunctions;
} }
return 0; return 0;
} }
int luatpt_unregister_mouseclick(lua_State* l) int luatpt_unregister_mouseclick(lua_State* l)
{ {
int *newfunctions, i, functionindex = -1; if(lua_isfunction(l, 1))
if(lua_isfunction(l, 1)){ {
for(i = 0; i<mouseclick_function_count; i++){ lua_pushstring(l, "mousefunctions");
lua_rawgeti(l, LUA_REGISTRYINDEX, mouseclick_functions[i]); lua_rawget(l, LUA_REGISTRYINDEX);
if(lua_equal(l, 1, lua_gettop(l))){ if(!lua_istable(l, -1))
functionindex = i; {
}
lua_pop(l, 1); lua_pop(l, 1);
lua_newtable(l);
lua_pushstring(l, "mousefunctions");
lua_pushvalue(l, -2);
lua_rawset(l, LUA_REGISTRYINDEX);
} }
} int c = lua_objlen(l, -1);
if(functionindex != -1){ int d = 0;
luaL_unref(l, LUA_REGISTRYINDEX, mouseclick_functions[functionindex]); int i = 0;
if(functionindex != mouseclick_function_count-1){ for(i=1;i<=c;i++)
memmove(mouseclick_functions+functionindex+1, mouseclick_functions+functionindex+1, (mouseclick_function_count-functionindex-1)*sizeof(int)); {
lua_rawgeti(l, -1, i+d);
if(lua_equal(l, 1, -1))
{
lua_pop(l, 1);
d++;
i--;
}
else
lua_rawseti(l, -2, i);
} }
if(mouseclick_function_count-1 > 0){
newfunctions = (int*)calloc(mouseclick_function_count-1, sizeof(int));
memcpy(newfunctions, mouseclick_functions, (mouseclick_function_count-1)*sizeof(int));
free(mouseclick_functions);
mouseclick_functions = newfunctions;
} else {
free(mouseclick_functions);
mouseclick_functions = NULL;
}
mouseclick_function_count--;
} else {
return luaL_error(l, "Function not registered");
} }
return 0; return 0;
} }

View File

@@ -14,11 +14,6 @@ extern std::string *luacon_lastError;
extern int *lua_el_func, *lua_el_mode, *lua_gr_func; extern int *lua_el_func, *lua_el_mode, *lua_gr_func;
extern int getPartIndex_curIdx; extern int getPartIndex_curIdx;
extern int step_functions[6];//[6] = {0, 0, 0, 0, 0, 0};
extern int keypress_function_count;// = 0;
extern int *keypress_functions;// = NULL;
extern int mouseclick_function_count;// = 0;
extern int *mouseclick_functions;// = NULL;
extern int tptProperties; //Table for some TPT properties extern int tptProperties; //Table for some TPT properties
extern int tptPropertiesVersion; extern int tptPropertiesVersion;
extern int tptElements; //Table for TPT element names extern int tptElements; //Table for TPT element names

View File

@@ -61,11 +61,6 @@ std::string *luacon_lastError;
int *lua_el_func, *lua_el_mode, *lua_gr_func; int *lua_el_func, *lua_el_mode, *lua_gr_func;
int getPartIndex_curIdx; int getPartIndex_curIdx;
int step_functions[6] = {0, 0, 0, 0, 0, 0};
int keypress_function_count = 0;
int *keypress_functions = NULL;
int mouseclick_function_count = 0;
int *mouseclick_functions = NULL;
int tptProperties; //Table for some TPT properties int tptProperties; //Table for some TPT properties
int tptPropertiesVersion; int tptPropertiesVersion;
int tptElements; //Table for TPT element names int tptElements; //Table for TPT element names