Implemented love.mouse.getEvents() and mouse callbacks

This commit is contained in:
rxi
2016-09-24 10:58:02 +01:00
parent dd44e8d1a0
commit c8453c6f60
2 changed files with 47 additions and 2 deletions

View File

@@ -66,7 +66,16 @@ int main(void) {
"while true do\n" "while true do\n"
/* Update mouse and handle mouse events */ /* Update mouse and handle mouse events */
"love.mouse.update()\n" "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" "for _, e in ipairs(love.keyboard.getEvents()) do\n"
"if e.type == 'down' then\n" "if e.type == 'down' then\n"
"if love.keypressed then love.keypressed(e.code) end\n" "if love.keypressed then love.keypressed(e.code) end\n"

View File

@@ -117,7 +117,43 @@ int l_mouse_update(lua_State *L) {
int l_mouse_getEvents(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;
} }