Skip to content

Commit

Permalink
Merge pull request #11365 from AreaScout/master
Browse files Browse the repository at this point in the history
Add: Touchscreen event support for SDL2
  • Loading branch information
hrydgard committed Sep 8, 2018
2 parents fd6c314 + 1fb111c commit 959a999
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion SDL/SDLMain.cpp
Expand Up @@ -355,6 +355,7 @@ int main(int argc, char *argv[]) {


int set_xres = -1; int set_xres = -1;
int set_yres = -1; int set_yres = -1;
int w = 0, h = 0;
bool portrait = false; bool portrait = false;
bool set_ipad = false; bool set_ipad = false;
float set_dpi = 1.0f; float set_dpi = 1.0f;
Expand Down Expand Up @@ -601,7 +602,7 @@ int main(int argc, char *argv[]) {


while (true) { while (true) {
double startTime = time_now_d(); double startTime = time_now_d();
SDL_Event event; SDL_Event event, touchEvent;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
float mx = event.motion.x * g_dpi_scale_x; float mx = event.motion.x * g_dpi_scale_x;
float my = event.motion.y * g_dpi_scale_y; float my = event.motion.y * g_dpi_scale_y;
Expand Down Expand Up @@ -681,6 +682,71 @@ int main(int argc, char *argv[]) {
NativeKey(key); NativeKey(key);
break; 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: case SDL_MOUSEBUTTONDOWN:
switch (event.button.button) { switch (event.button.button) {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
Expand Down

0 comments on commit 959a999

Please sign in to comment.