diff options
Diffstat (limited to 'src/script/event/luaopen_event.cpp')
-rw-r--r-- | src/script/event/luaopen_event.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/script/event/luaopen_event.cpp b/src/script/event/luaopen_event.cpp new file mode 100644 index 0000000..2935761 --- /dev/null +++ b/src/script/event/luaopen_event.cpp @@ -0,0 +1,110 @@ +/** +* Event module +*/ +#include <SDl2/SDL.h> + +#include "3rdparty/luax/luax.h" +#include "libjin/input/event.h" +#include "libjin/input/event.h" +#include "libjin/input/mouse.h" +#include "libjin/input/keyboard.h" + +using namespace jin::input; + +namespace jin +{ +namespace lua +{ + /** + * Load event poll, return a iterator(a table). + */ + static int l_event_poll(lua_State *L) + { + // table to store events + luax_newtable(L); + SDL_Event e; + int i = 1; + poll: + while (SDL_PollEvent(&e)) + { + // each event is a table + luax_newtable(L); + switch (e.type) + { + case SDL_QUIT: + luax_setfield_string(L, "type", "quit"); + break; + + case SDL_KEYDOWN: + luax_setfield_string(L, "type", "keydown"); + luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); + break; + + case SDL_KEYUP: + luax_setfield_string(L, "type", "keyup"); + luax_setfield_string(L, "key", SDL_GetKeyName(e.key.keysym.sym)); + break; + + case SDL_MOUSEMOTION: + luax_setfield_string(L, "type", "mousemotion"); + luax_setfield_number(L, "x", e.motion.x); + luax_setfield_number(L, "y", e.motion.y); + break; + + case SDL_MOUSEBUTTONDOWN: + luax_setfield_string(L, "type", "mousebuttondown"); + luax_setfield_string(L, "button", buttonStr(e.button.button)); + luax_setfield_number(L, "x", e.button.x); + luax_setfield_number(L, "y", e.button.y); + break; + + case SDL_MOUSEBUTTONUP: + luax_setfield_string(L, "type", "mousebuttonup"); + luax_setfield_string(L, "button", buttonStr(e.button.button)); + luax_setfield_number(L, "x", e.button.x); + luax_setfield_number(L, "y", e.button.y); + break; + + case SDL_MOUSEWHEEL: + luax_setfield_string(L, "type", "wheel"); + if(e.wheel.x == -1) + luax_setfield_string(L, "x", "left"); + else if(e.wheel.x == 1) + luax_setfield_string(L, "x", "right"); + else + luax_setfield_string(L, "x", "none"); + + if (e.wheel.y == -1) + luax_setfield_string(L, "y", "near"); + else if (e.wheel.y == 1) + luax_setfield_string(L, "y", "far"); + else + luax_setfield_string(L, "y", "none"); + break; + + default: + /* ignore other events */ + luax_pop(L, 1); // pop table out + goto poll; + break; + } + luax_rawseti(L, -2, i++); + } + return 1; + } + + static const luaL_Reg f[] = { + {"poll", l_event_poll}, + {0 ,0 } + }; + + /** + * load event module + */ + int luaopen_event(lua_State* L) + { + luax_newlib(L, f); + return 1; + } +} +}
\ No newline at end of file |