Mouse and keyboard events, replace luaL_openlib() with luaL_register() and move mousex and mousey out of the global table and into the 'tpt' table
This commit is contained in:
parent
98b6c4b001
commit
4ebe56f65c
@ -12,8 +12,9 @@
|
||||
#include <defines.h>
|
||||
|
||||
void luacon_open();
|
||||
int luacon_step(int mx, int my, int mb, int mbq, char key);
|
||||
int luacon_keypress(char key);
|
||||
int luacon_step(int mx, int my);
|
||||
int luacon_mouseclick(int mx, int my, int mb, int mbq);
|
||||
int luacon_keypress(char key, int modifier);
|
||||
int luacon_eval(char *command);
|
||||
char *luacon_geterror();
|
||||
void luacon_close();
|
||||
@ -45,6 +46,10 @@ int luatpt_set_shortcuts(lua_State* l);
|
||||
int luatpt_delete(lua_State* l);
|
||||
int luatpt_register_step(lua_State* l);
|
||||
int luatpt_unregister_step(lua_State* l);
|
||||
int luatpt_register_mouseclick(lua_State* l);
|
||||
int luatpt_unregister_mouseclick(lua_State* l);
|
||||
int luatpt_register_keypress(lua_State* l);
|
||||
int luatpt_unregister_keypress(lua_State* l);
|
||||
int luatpt_input(lua_State* l);
|
||||
int luatpt_message_box(lua_State* l);
|
||||
int luatpt_get_numOfParts(lua_State* l);
|
||||
|
184
src/luaconsole.c
184
src/luaconsole.c
@ -5,6 +5,11 @@
|
||||
|
||||
lua_State *l;
|
||||
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
|
||||
void luacon_open(){
|
||||
const static struct luaL_reg tptluaapi [] = {
|
||||
{"test", &luatpt_test},
|
||||
@ -30,6 +35,10 @@ void luacon_open(){
|
||||
{"delete", &luatpt_delete},
|
||||
{"register_step", &luatpt_register_step},
|
||||
{"unregister_step", &luatpt_unregister_step},
|
||||
{"register_mouseclick", &luatpt_register_mouseclick},
|
||||
{"unregister_mouseclick", &luatpt_unregister_mouseclick},
|
||||
{"register_keypress", &luatpt_register_keypress},
|
||||
{"unregister_keypress", &luatpt_unregister_keypress},
|
||||
{"input", &luatpt_input},
|
||||
{"message_box", &luatpt_message_box},
|
||||
{"get_numOfParts", &luatpt_get_numOfParts},
|
||||
@ -49,38 +58,67 @@ void luacon_open(){
|
||||
|
||||
l = lua_open();
|
||||
luaL_openlibs(l);
|
||||
luaL_openlib(l, "tpt", tptluaapi, 0);
|
||||
luaL_register(l, "tpt", tptluaapi);
|
||||
|
||||
tptProperties = lua_gettop(l);
|
||||
|
||||
lua_pushinteger(l, 0);
|
||||
lua_setfield(l, tptProperties, "mousex");
|
||||
lua_pushinteger(l, 0);
|
||||
lua_setfield(l, tptProperties, "mousey");
|
||||
}
|
||||
int luacon_step(int mx, int my, int mb, int mbq, char key){
|
||||
int luacon_keypress(char key, int modifier){
|
||||
int i = 0, kpcontinue = 1;
|
||||
if(keypress_function_count){
|
||||
for(i = 0; i < keypress_function_count && kpcontinue; i++){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, keypress_functions[i]);
|
||||
lua_pushstring(l, &key);
|
||||
lua_pushinteger(l, key);
|
||||
lua_pushinteger(l, modifier);
|
||||
lua_pcall(l, 3, 1, 0);
|
||||
if(lua_isboolean(l, -1)){
|
||||
kpcontinue = lua_toboolean(l, -1);
|
||||
}
|
||||
lua_pop(l, 1);
|
||||
}
|
||||
}
|
||||
return kpcontinue;
|
||||
}
|
||||
int luacon_mouseclick(int mx, int my, int mb, int mbq){
|
||||
int i = 0, mpcontinue = 1;
|
||||
if(mouseclick_function_count){
|
||||
for(i = 0; i < mouseclick_function_count && mpcontinue; i++){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, mouseclick_functions[i]);
|
||||
lua_pushinteger(l, mbq);
|
||||
lua_pushinteger(l, mb);
|
||||
lua_pushinteger(l, mx);
|
||||
lua_pushinteger(l, my);
|
||||
lua_pcall(l, 4, 1, 0);
|
||||
if(lua_isboolean(l, -1)){
|
||||
mpcontinue = lua_toboolean(l, -1);
|
||||
}
|
||||
lua_pop(l, 1);
|
||||
}
|
||||
}
|
||||
return mpcontinue;
|
||||
}
|
||||
int luacon_step(int mx, int my){
|
||||
int tempret = 0, tempb, i, callret;
|
||||
if(step_functions[0]){
|
||||
//Set mouse globals
|
||||
lua_pushinteger(l, mbq);
|
||||
lua_pushinteger(l, mb);
|
||||
lua_pushinteger(l, my);
|
||||
lua_pushinteger(l, mx);
|
||||
lua_setfield(l, LUA_GLOBALSINDEX, "mousex");
|
||||
lua_setfield(l, LUA_GLOBALSINDEX, "mousey");
|
||||
lua_setfield(l, LUA_GLOBALSINDEX, "mouseb");
|
||||
lua_setfield(l, LUA_GLOBALSINDEX, "mousebq");
|
||||
lua_setfield(l, tptProperties, "mousex");
|
||||
lua_setfield(l, tptProperties, "mousey");
|
||||
for(i = 0; i<6; i++){
|
||||
if(step_functions[i]){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);
|
||||
callret = lua_pcall(l, 0, 1, 0);
|
||||
callret = lua_pcall(l, 0, 0, 0);
|
||||
if (callret)
|
||||
{
|
||||
// failed, TODO: better error reporting
|
||||
printf("%s\n",luacon_geterror());
|
||||
}
|
||||
if(lua_isboolean(l, -1)){
|
||||
tempb = lua_toboolean(l, -1);
|
||||
if(tempb){ //Mouse click has been handled, set the global for future calls
|
||||
lua_pushinteger(l, mb);
|
||||
lua_setfield(l, LUA_GLOBALSINDEX, "mouseb");
|
||||
}
|
||||
tempret |= tempb;
|
||||
}
|
||||
lua_pop(l, 1);
|
||||
}
|
||||
}
|
||||
return tempret;
|
||||
@ -748,6 +786,116 @@ int luatpt_unregister_step(lua_State* l)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int luatpt_register_keypress(lua_State* l)
|
||||
{
|
||||
int *newfunctions, i;
|
||||
if(lua_isfunction(l, 1)){
|
||||
for(i = 0; i<keypress_function_count; i++){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, keypress_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);
|
||||
}
|
||||
newfunctions = calloc(keypress_function_count+1, sizeof(int));
|
||||
if(keypress_functions){
|
||||
memcpy(newfunctions, keypress_functions, keypress_function_count*sizeof(int));
|
||||
free(keypress_functions);
|
||||
}
|
||||
newfunctions[keypress_function_count] = luaL_ref(l, LUA_REGISTRYINDEX);
|
||||
keypress_function_count++;
|
||||
keypress_functions = newfunctions;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int luatpt_unregister_keypress(lua_State* l)
|
||||
{
|
||||
int *newfunctions, i, functionindex = -1;
|
||||
if(lua_isfunction(l, 1)){
|
||||
for(i = 0; i<keypress_function_count; i++){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, keypress_functions[i]);
|
||||
if(lua_equal(l, 1, lua_gettop(l))){
|
||||
functionindex = i;
|
||||
}
|
||||
lua_pop(l, 1);
|
||||
}
|
||||
}
|
||||
if(functionindex != -1){
|
||||
luaL_unref(l, LUA_REGISTRYINDEX, keypress_functions[functionindex]);
|
||||
if(functionindex != keypress_function_count-1){
|
||||
memmove(keypress_functions+functionindex+1, keypress_functions+functionindex+1, (keypress_function_count-functionindex-1)*sizeof(int));
|
||||
}
|
||||
if(keypress_function_count-1 > 0){
|
||||
newfunctions = 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;
|
||||
}
|
||||
int luatpt_register_mouseclick(lua_State* l)
|
||||
{
|
||||
int *newfunctions, i;
|
||||
if(lua_isfunction(l, 1)){
|
||||
for(i = 0; i<mouseclick_function_count; i++){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, mouseclick_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);
|
||||
}
|
||||
newfunctions = calloc(mouseclick_function_count+1, sizeof(int));
|
||||
if(mouseclick_functions){
|
||||
memcpy(newfunctions, mouseclick_functions, mouseclick_function_count*sizeof(int));
|
||||
free(mouseclick_functions);
|
||||
}
|
||||
newfunctions[mouseclick_function_count] = luaL_ref(l, LUA_REGISTRYINDEX);
|
||||
mouseclick_function_count++;
|
||||
mouseclick_functions = newfunctions;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int luatpt_unregister_mouseclick(lua_State* l)
|
||||
{
|
||||
int *newfunctions, i, functionindex = -1;
|
||||
if(lua_isfunction(l, 1)){
|
||||
for(i = 0; i<mouseclick_function_count; i++){
|
||||
lua_rawgeti(l, LUA_REGISTRYINDEX, mouseclick_functions[i]);
|
||||
if(lua_equal(l, 1, lua_gettop(l))){
|
||||
functionindex = i;
|
||||
}
|
||||
lua_pop(l, 1);
|
||||
}
|
||||
}
|
||||
if(functionindex != -1){
|
||||
luaL_unref(l, LUA_REGISTRYINDEX, mouseclick_functions[functionindex]);
|
||||
if(functionindex != mouseclick_function_count-1){
|
||||
memmove(mouseclick_functions+functionindex+1, mouseclick_functions+functionindex+1, (mouseclick_function_count-functionindex-1)*sizeof(int));
|
||||
}
|
||||
if(mouseclick_function_count-1 > 0){
|
||||
newfunctions = 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;
|
||||
}
|
||||
int luatpt_input(lua_State* l)
|
||||
{
|
||||
char *prompt, *title, *result, *shadow, *text;
|
||||
|
18
src/main.c
18
src/main.c
@ -2083,7 +2083,12 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
do_s_check = (do_s_check+1) & 15;
|
||||
}
|
||||
|
||||
#ifdef LUACONSOLE
|
||||
if(sdl_key){
|
||||
if(!luacon_keypress(sdl_key, sdl_mod))
|
||||
sdl_key = 0;
|
||||
}
|
||||
#endif
|
||||
if (sys_shortcuts==1)//all shortcuts can be disabled by python scripts
|
||||
{
|
||||
if (sdl_key=='q' || sdl_key==SDLK_ESCAPE)
|
||||
@ -2511,9 +2516,6 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
}
|
||||
//#ifdef LUACONSOLE
|
||||
//luacon_keypress(sdl_key);
|
||||
//#endif
|
||||
#ifdef PYCONSOLE
|
||||
if (pyready==1 && pygood==1)
|
||||
if (pkey!=NULL && sdl_key!=NULL)
|
||||
@ -2606,8 +2608,12 @@ int main(int argc, char *argv[])
|
||||
b = SDL_GetMouseState(&x, &y); // b is current mouse state
|
||||
|
||||
#ifdef LUACONSOLE
|
||||
if(luacon_step(x/sdl_scale, y/sdl_scale, b, bq, sdl_key))
|
||||
b = 0; //Mouse click was handled by Lua step
|
||||
if(b){
|
||||
if(!luacon_mouseclick(x/sdl_scale, y/sdl_scale, b, bq)){
|
||||
b = 0;
|
||||
}
|
||||
}
|
||||
luacon_step(x/sdl_scale, y/sdl_scale);
|
||||
#endif
|
||||
|
||||
for (i=0; i<SC_TOTAL; i++)//draw all the menu sections
|
||||
|
Reference in New Issue
Block a user