Changes to unify mouse and keyboard events for Lua

This commit is contained in:
Simon Robertshaw 2011-08-20 18:18:09 +01:00
parent 03ee03ed1e
commit 99b67598aa
5 changed files with 45 additions and 19 deletions

View File

@ -132,7 +132,7 @@ typedef struct ui_richtext ui_richtext;
int SLALT;
extern SDLMod sdl_mod;
extern int sdl_key, sdl_wheel, sdl_caps, sdl_ascii, sdl_zoom_trig;
extern int sdl_key, sdl_rkey, sdl_wheel, sdl_caps, sdl_ascii, sdl_zoom_trig;
#if (defined(LIN32) || defined(LIN64)) && defined(SDL_VIDEO_DRIVER_X11)
extern SDL_SysWMinfo sdl_wminfo;
extern Atom XA_CLIPBOARD, XA_TARGETS;

View File

@ -11,10 +11,16 @@
#endif
#include <defines.h>
#define LUACON_MDOWN 1
#define LUACON_MUP 2
#define LUACON_MPRESS 3
#define LUACON_KDOWN 1
#define LUACON_KUP 2
void luacon_open();
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_mouseevent(int mx, int my, int mb, int event);
int luacon_keyevent(char key, int modifier, int event);
int luacon_eval(char *command);
char *luacon_geterror();
void luacon_close();

View File

@ -26,7 +26,7 @@
#endif
SDLMod sdl_mod;
int sdl_key, sdl_wheel, sdl_caps=0, sdl_ascii, sdl_zoom_trig=0;
int sdl_key, sdl_rkey, sdl_wheel, sdl_caps=0, sdl_ascii, sdl_zoom_trig=0;
#if (defined(LIN32) || defined(LIN64)) && defined(SDL_VIDEO_DRIVER_X11)
SDL_SysWMinfo sdl_wminfo;
Atom XA_CLIPBOARD, XA_TARGETS;
@ -2206,7 +2206,7 @@ int color_menu_ui(pixel *vid_buf, int i, int *cr, int *cg, int *cb, int b, int b
int sdl_poll(void)
{
SDL_Event event;
sdl_key=sdl_wheel=sdl_ascii=0;
sdl_key=sdl_rkey=sdl_wheel=sdl_ascii=0;
while (SDL_PollEvent(&event))
{
switch (event.type)
@ -2266,6 +2266,7 @@ int sdl_poll(void)
break;
case SDL_KEYUP:
sdl_rkey=event.key.keysym.sym;
if (event.key.keysym.sym == SDLK_CAPSLOCK)
sdl_caps = 0;
if (event.key.keysym.sym == 'z')

View File

@ -39,6 +39,10 @@ void luacon_open(){
{"unregister_mouseclick", &luatpt_unregister_mouseclick},
{"register_keypress", &luatpt_register_keypress},
{"unregister_keypress", &luatpt_unregister_keypress},
{"register_mouseevent", &luatpt_register_mouseclick},
{"unregister_mouseevent", &luatpt_unregister_mouseclick},
{"register_keyevent", &luatpt_register_keypress},
{"unregister_keyevent", &luatpt_unregister_keypress},
{"input", &luatpt_input},
{"message_box", &luatpt_message_box},
{"get_numOfParts", &luatpt_get_numOfParts},
@ -70,7 +74,7 @@ void luacon_open(){
lua_pushinteger(l, 0);
lua_setfield(l, tptProperties, "mousey");
}
int luacon_keypress(char key, int modifier){
int luacon_keyevent(char key, int modifier, int event){
int i = 0, kpcontinue = 1;
if(keypress_function_count){
for(i = 0; i < keypress_function_count && kpcontinue; i++){
@ -78,7 +82,8 @@ int luacon_keypress(char key, int modifier){
lua_pushstring(l, &key);
lua_pushinteger(l, key);
lua_pushinteger(l, modifier);
lua_pcall(l, 3, 1, 0);
lua_pushinteger(l, event);
lua_pcall(l, 4, 1, 0);
if(lua_isboolean(l, -1)){
kpcontinue = lua_toboolean(l, -1);
}
@ -87,15 +92,15 @@ int luacon_keypress(char key, int modifier){
}
return kpcontinue;
}
int luacon_mouseclick(int mx, int my, int mb, int mbq){
int luacon_mouseevent(int mx, int my, int mb, int event){
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_pushinteger(l, mb);
lua_pushinteger(l, event);
lua_pcall(l, 4, 1, 0);
if(lua_isboolean(l, -1)){
mpcontinue = lua_toboolean(l, -1);
@ -107,12 +112,12 @@ int luacon_mouseclick(int mx, int my, int mb, int mbq){
}
int luacon_step(int mx, int my){
int tempret = 0, tempb, i, callret;
if(step_functions[0]){
//Set mouse globals
lua_pushinteger(l, my);
lua_pushinteger(l, mx);
lua_setfield(l, tptProperties, "mousex");
lua_setfield(l, tptProperties, "mousey");
if(step_functions[0]){
//Set mouse globals
for(i = 0; i<6; i++){
if(step_functions[i]){
lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);

View File

@ -1576,7 +1576,7 @@ int main(int argc, char *argv[])
void *http_ver_check, *http_session_check = NULL;
char *ver_data=NULL, *check_data=NULL, *tmp;
//char console_error[255] = "";
int result, i, j, bq, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len, new_message_len=0;
int result, i, j, bq, bc, fire_fc=0, do_check=0, do_s_check=0, old_version=0, http_ret=0,http_s_ret=0, major, minor, old_ver_len, new_message_len=0;
#ifdef INTERNAL
int vs = 0;
#endif
@ -2003,9 +2003,13 @@ int main(int argc, char *argv[])
}
#ifdef LUACONSOLE
if(sdl_key){
if(!luacon_keypress(sdl_key, sdl_mod))
if(!luacon_keyevent(sdl_key, sdl_mod, LUACON_KDOWN))
sdl_key = 0;
}
if(sdl_rkey){
if(!luacon_keyevent(sdl_rkey, sdl_mod, LUACON_KUP))
sdl_rkey = 0;
}
#endif
#ifdef PYCONSOLE
if(sdl_key){
@ -2515,11 +2519,21 @@ int main(int argc, char *argv[])
}
bq = b; // bq is previous mouse state
b = SDL_GetMouseState(&x, &y); // b is current mouse state
bc = b = SDL_GetMouseState(&x, &y); // b is current mouse state
#ifdef LUACONSOLE
if(b){
if(!luacon_mouseclick(x/sdl_scale, y/sdl_scale, b, bq)){
if(bc && bq){
if(!luacon_mouseevent(x/sdl_scale, y/sdl_scale, bc, LUACON_MPRESS)){
b = 0;
}
}
else if(bc && !bq){
if(!luacon_mouseevent(x/sdl_scale, y/sdl_scale, bc, LUACON_MDOWN)){
b = 0;
}
}
else if(!bc && bq){
if(!luacon_mouseevent(x/sdl_scale, y/sdl_scale, bq, LUACON_MUP)){
b = 0;
}
}