From 1fb111c2bbb8f81ee5cfd250508ff13adeaa534b Mon Sep 17 00:00:00 2001 From: AreaScout Date: Fri, 7 Sep 2018 10:44:41 +0000 Subject: [PATCH] Add: Touchscreen event support for SDL2 --- SDL/SDLMain.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index d28f21936e77..14afdd040cbd 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -355,6 +355,7 @@ int main(int argc, char *argv[]) { int set_xres = -1; int set_yres = -1; + int w = 0, h = 0; bool portrait = false; bool set_ipad = false; float set_dpi = 1.0f; @@ -601,7 +602,7 @@ int main(int argc, char *argv[]) { while (true) { double startTime = time_now_d(); - SDL_Event event; + SDL_Event event, touchEvent; while (SDL_PollEvent(&event)) { float mx = event.motion.x * g_dpi_scale_x; float my = event.motion.y * g_dpi_scale_y; @@ -681,6 +682,71 @@ int main(int argc, char *argv[]) { NativeKey(key); break; } + case SDL_FINGERMOTION: + { + if (!g_Config.bFullScreen) { break; } + SDL_GetWindowSize(window, &w, &h); + touchEvent.type = SDL_MOUSEMOTION; + touchEvent.motion.type = SDL_MOUSEMOTION; + touchEvent.motion.timestamp = event.tfinger.timestamp; + touchEvent.motion.windowID = SDL_GetWindowID(window); + touchEvent.motion.which = SDL_TOUCH_MOUSEID; + touchEvent.motion.state = SDL_GetMouseState(NULL, NULL); + touchEvent.motion.x = event.tfinger.x * w; + touchEvent.motion.y = event.tfinger.y * h; + + SDL_WarpMouseInWindow(window, event.tfinger.x * w, event.tfinger.y * h); + + SDL_PushEvent(&touchEvent); + break; + } + case SDL_FINGERDOWN: + { + if (!g_Config.bFullScreen) { break; } + SDL_GetWindowSize(window, &w, &h); + touchEvent.type = SDL_MOUSEBUTTONDOWN; + touchEvent.button.type = SDL_MOUSEBUTTONDOWN; + touchEvent.button.timestamp = SDL_GetTicks(); + touchEvent.button.windowID = SDL_GetWindowID(window); + touchEvent.button.which = SDL_TOUCH_MOUSEID; + touchEvent.button.button = SDL_BUTTON_LEFT; + touchEvent.button.state = SDL_PRESSED; + touchEvent.button.clicks = 1; + touchEvent.button.x = event.tfinger.x * w; + touchEvent.button.y = event.tfinger.y * h; + + touchEvent.motion.type = SDL_MOUSEMOTION; + touchEvent.motion.timestamp = SDL_GetTicks(); + touchEvent.motion.windowID = SDL_GetWindowID(window); + touchEvent.motion.which = SDL_TOUCH_MOUSEID; + touchEvent.motion.x = event.tfinger.x * w; + touchEvent.motion.y = event.tfinger.y * h; + // Any real mouse cursor should also move + SDL_WarpMouseInWindow(window, event.tfinger.x * w, event.tfinger.y * h); + // First finger down event also has to be a motion to that position + SDL_PushEvent(&touchEvent); + touchEvent.motion.type = SDL_MOUSEBUTTONDOWN; + // Now we push the mouse button event + SDL_PushEvent(&touchEvent); + break; + } + case SDL_FINGERUP: + { + if(!g_Config.bFullScreen) { break; } + SDL_GetWindowSize(window, &w, &h); + touchEvent.type = SDL_MOUSEBUTTONUP; + touchEvent.button.type = SDL_MOUSEBUTTONUP; + touchEvent.button.timestamp = SDL_GetTicks(); + touchEvent.button.windowID = SDL_GetWindowID(window); + touchEvent.button.which = SDL_TOUCH_MOUSEID; + touchEvent.button.button = SDL_BUTTON_LEFT; + touchEvent.button.state = SDL_RELEASED; + touchEvent.button.clicks = 1; + touchEvent.button.x = event.tfinger.x * w; + touchEvent.button.y = event.tfinger.y * h; + SDL_PushEvent(&touchEvent); + break; + } case SDL_MOUSEBUTTONDOWN: switch (event.button.button) { case SDL_BUTTON_LEFT: