Normalize touch events to the render viewport (thanks Sylvain!)
authorSam Lantinga <slouken@libsdl.org>
Thu, 12 Oct 2017 08:37:55 -0700
changeset 11603d94abaa07d8e
parent 11602 8dd72342f1f1
child 11604 634c34c79fbc
Normalize touch events to the render viewport (thanks Sylvain!)
src/render/SDL_render.c
     1.1 --- a/src/render/SDL_render.c	Thu Oct 12 08:27:22 2017 -0700
     1.2 +++ b/src/render/SDL_render.c	Thu Oct 12 08:37:55 2017 -0700
     1.3 @@ -219,7 +219,35 @@
     1.4              event->button.x = (int)(event->button.x / (renderer->scale.x * renderer->dpi_scale.x));
     1.5              event->button.y = (int)(event->button.y / (renderer->scale.y * renderer->dpi_scale.y));
     1.6          }
     1.7 +    } else if (event->type == SDL_FINGERDOWN ||
     1.8 +               event->type == SDL_FINGERUP ||
     1.9 +               event->type == SDL_FINGERMOTION) {
    1.10 +        if (renderer->logical_w) {
    1.11 +            int w = 1;
    1.12 +            int h = 1;
    1.13 +            SDL_GetRendererOutputSize(renderer, &w, &h);
    1.14 +
    1.15 +            event->tfinger.x *= (w - 1);
    1.16 +            event->tfinger.y *= (h - 1);
    1.17 +
    1.18 +            event->tfinger.x -= (renderer->viewport.x * renderer->dpi_scale.x);
    1.19 +            event->tfinger.y -= (renderer->viewport.y * renderer->dpi_scale.y);
    1.20 +            event->tfinger.x = (event->tfinger.x / (renderer->scale.x * renderer->dpi_scale.x));
    1.21 +            event->tfinger.y = (event->tfinger.y / (renderer->scale.y * renderer->dpi_scale.y));
    1.22 +
    1.23 +            if (renderer->logical_w > 1) {
    1.24 +                event->tfinger.x = event->tfinger.x / (renderer->logical_w - 1);
    1.25 +            } else {
    1.26 +                event->tfinger.x = 0.5f;
    1.27 +            }
    1.28 +            if (renderer->logical_h > 1) {
    1.29 +                event->tfinger.y = event->tfinger.y / (renderer->logical_h - 1);
    1.30 +            } else {
    1.31 +                event->tfinger.y = 0.5f;
    1.32 +            }
    1.33 +        }
    1.34      }
    1.35 +
    1.36      return 0;
    1.37  }
    1.38