add touch events

This commit is contained in:
Jan200101 2022-12-20 09:30:58 +01:00
parent 5ab8dc0275
commit b137d77183
No known key found for this signature in database
GPG Key ID: 5B71B1D78B882E05
2 changed files with 43 additions and 2 deletions

View File

@ -46,6 +46,11 @@ system = {}
--- * "mousemoved" -> x, y, relative_x, relative_y --- * "mousemoved" -> x, y, relative_x, relative_y
--- * "mousewheel" -> y, x --- * "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 string type
---@return any? arg1 ---@return any? arg1
---@return any? arg2 ---@return any? arg2

View File

@ -172,8 +172,9 @@ static void push_win32_error(lua_State *L, DWORD rc) {
static int f_poll_event(lua_State *L) { static int f_poll_event(lua_State *L) {
char buf[16]; char buf[16];
int mx, my; int mx, my, w, h;
SDL_Event e; SDL_Event e;
SDL_Event event_plus;
top: top:
if ( !SDL_PollEvent(&e) ) { if ( !SDL_PollEvent(&e) ) {
@ -299,7 +300,6 @@ top:
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
SDL_PumpEvents(); SDL_PumpEvents();
SDL_Event event_plus;
while (SDL_PeepEvents(&event_plus, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) { while (SDL_PeepEvents(&event_plus, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {
e.motion.x = event_plus.motion.x; e.motion.x = event_plus.motion.x;
e.motion.y = event_plus.motion.y; e.motion.y = event_plus.motion.y;
@ -325,6 +325,42 @@ top:
#endif #endif
return 3; 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: default:
goto top; goto top;
} }