From b137d771836731c63f68a5195f675670b0ae3374 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Tue, 20 Dec 2022 09:30:58 +0100 Subject: [PATCH] add touch events --- docs/api/system.lua | 5 +++++ src/api/system.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/docs/api/system.lua b/docs/api/system.lua index 49687523..17c379fa 100644 --- a/docs/api/system.lua +++ b/docs/api/system.lua @@ -46,6 +46,11 @@ system = {} --- * "mousemoved" -> x, y, relative_x, relative_y --- * "mousewheel" -> y, x --- +---Touch events: +--- * "touchpressed" -> x, y, finger_id +--- * "touchreleased" -> x, y, finger_id +--- * "touchmoved" -> x, y, distance_x, distance_y, finger_id +--- ---@return string type ---@return any? arg1 ---@return any? arg2 diff --git a/src/api/system.c b/src/api/system.c index 59b29b29..362cdf85 100644 --- a/src/api/system.c +++ b/src/api/system.c @@ -172,8 +172,9 @@ static void push_win32_error(lua_State *L, DWORD rc) { static int f_poll_event(lua_State *L) { char buf[16]; - int mx, my; + int mx, my, w, h; SDL_Event e; + SDL_Event event_plus; top: if ( !SDL_PollEvent(&e) ) { @@ -299,7 +300,6 @@ top: case SDL_MOUSEMOTION: SDL_PumpEvents(); - SDL_Event event_plus; while (SDL_PeepEvents(&event_plus, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) { e.motion.x = event_plus.motion.x; e.motion.y = event_plus.motion.y; @@ -325,6 +325,42 @@ top: #endif return 3; + case SDL_FINGERDOWN: + SDL_GetWindowSize(window, &w, &h); + + lua_pushstring(L, "touchpressed"); + lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w)); + lua_pushinteger(L, (lua_Integer)(e.tfinger.y * h)); + lua_pushinteger(L, e.tfinger.fingerId); + return 4; + + case SDL_FINGERUP: + SDL_GetWindowSize(window, &w, &h); + + lua_pushstring(L, "touchreleased"); + lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w)); + lua_pushinteger(L, (lua_Integer)(e.tfinger.y * h)); + lua_pushinteger(L, e.tfinger.fingerId); + return 4; + + case SDL_FINGERMOTION: + SDL_PumpEvents(); + while (SDL_PeepEvents(&event_plus, 1, SDL_GETEVENT, SDL_FINGERMOTION, SDL_FINGERMOTION) > 0) { + e.tfinger.x = event_plus.tfinger.x; + e.tfinger.y = event_plus.tfinger.y; + e.tfinger.dx += event_plus.tfinger.dx; + e.tfinger.dy += event_plus.tfinger.dy; + } + SDL_GetWindowSize(window, &w, &h); + + lua_pushstring(L, "touchmoved"); + lua_pushinteger(L, (lua_Integer)(e.tfinger.x * w)); + lua_pushinteger(L, (lua_Integer)(e.tfinger.y * h)); + lua_pushinteger(L, (lua_Integer)(e.tfinger.dx * w)); + lua_pushinteger(L, (lua_Integer)(e.tfinger.dy * h)); + lua_pushinteger(L, e.tfinger.fingerId); + return 6; + default: goto top; }