From c8453c6f60450ce33ab933cf9bfe37ea36d48d64 Mon Sep 17 00:00:00 2001 From: rxi Date: Sat, 24 Sep 2016 10:58:02 +0100 Subject: [PATCH] Implemented love.mouse.getEvents() and mouse callbacks --- src/main.c | 11 ++++++++++- src/mouse.c | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index d3fc2b2..bcf1c49 100644 --- a/src/main.c +++ b/src/main.c @@ -66,7 +66,16 @@ int main(void) { "while true do\n" /* Update mouse and handle mouse events */ "love.mouse.update()\n" - /* Keyboard Events*/ + "for _, e in ipairs(love.mouse.getEvents()) do\n" + "if e.type == 'motion' then\n" + "if love.mousemoved then love.mousemoved(e.x, e.y, e.dx, e.dy) end\n" + "elseif e.type == 'pressed' then\n" + "if love.mousepressed then love.mousepressed(e.x, e.y, e.button) end\n" + "elseif e.type == 'released' then\n" + "if love.mousereleased then love.mousereleased(e.x, e.y, e.button) end\n" + "end\n" + "end\n" + /* Keyboard Events */ "for _, e in ipairs(love.keyboard.getEvents()) do\n" "if e.type == 'down' then\n" "if love.keypressed then love.keypressed(e.code) end\n" diff --git a/src/mouse.c b/src/mouse.c index c95fdde..77e904b 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -117,7 +117,43 @@ int l_mouse_update(lua_State *L) { int l_mouse_getEvents(lua_State *L) { - return 0; + int i; + int n = 1; + lua_newtable(L); + + /* Add motion event if mouse moved */ + if ( mouse_x != mouse_lastX || mouse_y != mouse_lastY ) { + lua_newtable(L); + lua_pushstring(L, "motion"); + lua_setfield(L, -2, "type"); + lua_pushinteger(L, mouse_x); + lua_setfield(L, -2, "x"); + lua_pushinteger(L, mouse_y); + lua_setfield(L, -2, "y"); + lua_pushinteger(L, mouse_x - mouse_lastX); + lua_setfield(L, -2, "dx"); + lua_pushinteger(L, mouse_y - mouse_lastY); + lua_setfield(L, -2, "dy"); + lua_rawseti(L, -2, n++); + } + + /* Add `pressed` and `released` events */ + for (i = 0; i < MOUSE_BUTTON_MAX; i++) { + if ( mouse_buttonsPressed[i] || mouse_buttonsReleased[i] ) { + lua_newtable(L); + lua_pushstring(L, mouse_buttonsPressed[i] ? "pressed" : "released"); + lua_setfield(L, -2, "type"); + lua_pushinteger(L, i + 1); + lua_setfield(L, -2, "button"); + lua_pushinteger(L, mouse_x); + lua_setfield(L, -2, "x"); + lua_pushinteger(L, mouse_y); + lua_setfield(L, -2, "y"); + lua_rawseti(L, -2, n++); + } + } + + return 1; }