Fixed retina display input scaling
authorSam Lantinga <slouken@libsdl.org>
Tue, 18 Sep 2012 01:45:15 -0700
changeset 643629a35c72905a
parent 6435 6172658e3ce9
child 6437 3e52c17e2a07
Fixed retina display input scaling
src/video/uikit/SDL_uikitview.h
src/video/uikit/SDL_uikitview.m
     1.1 --- a/src/video/uikit/SDL_uikitview.h	Tue Sep 18 00:50:41 2012 -0700
     1.2 +++ b/src/video/uikit/SDL_uikitview.h	Tue Sep 18 01:45:15 2012 -0700
     1.3 @@ -50,7 +50,7 @@
     1.4  @public
     1.5      SDL_uikitviewcontroller *viewcontroller;
     1.6  }
     1.7 -- (CGPoint)touchLocation:(UITouch *)touch;
     1.8 +- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize;
     1.9  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    1.10  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
    1.11  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
     2.1 --- a/src/video/uikit/SDL_uikitview.m	Tue Sep 18 00:50:41 2012 -0700
     2.2 +++ b/src/video/uikit/SDL_uikitview.m	Tue Sep 18 01:45:15 2012 -0700
     2.3 @@ -76,14 +76,24 @@
     2.4  
     2.5  }
     2.6  
     2.7 -- (CGPoint)touchLocation:(UITouch *)touch
     2.8 +- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
     2.9  {
    2.10      CGPoint point = [touch locationInView: self];
    2.11      CGRect frame = [self frame];
    2.12  
    2.13      frame = CGRectApplyAffineTransform(frame, [self transform]);
    2.14 -    point.x /= frame.size.width;
    2.15 -    point.y /= frame.size.height;
    2.16 +
    2.17 +    // Get the display scale and apply that to the input coordinates
    2.18 +    SDL_Window *window = self->viewcontroller.window;
    2.19 +    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
    2.20 +    SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
    2.21 +    point.x *= displaymodedata->scale;
    2.22 +    point.y *= displaymodedata->scale;
    2.23 +    
    2.24 +    if (normalize) {
    2.25 +        point.x /= frame.size.width;
    2.26 +        point.y /= frame.size.height;
    2.27 +    }
    2.28      return point;
    2.29  }
    2.30  
    2.31 @@ -93,7 +103,7 @@
    2.32      UITouch *touch = (UITouch*)[enumerator nextObject];
    2.33  
    2.34      if (touch) {
    2.35 -        CGPoint locationInView = [touch locationInView: self];
    2.36 +        CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
    2.37  
    2.38          /* send moved event */
    2.39          SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
    2.40 @@ -104,7 +114,7 @@
    2.41  
    2.42  #ifdef FIXED_MULTITOUCH
    2.43      while(touch) {
    2.44 -        CGPoint locationInView = [self touchLocation:touch];
    2.45 +        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
    2.46  
    2.47  #ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
    2.48          //FIXME: TODO: Using touch as the fingerId is potentially dangerous
    2.49 @@ -143,7 +153,7 @@
    2.50  
    2.51  #ifdef FIXED_MULTITOUCH
    2.52      while(touch) {
    2.53 -        CGPoint locationInView = [self touchLocation:touch];
    2.54 +        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
    2.55  
    2.56  #ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
    2.57          SDL_SendFingerDown(touchId, (long)touch,
    2.58 @@ -183,7 +193,7 @@
    2.59      UITouch *touch = (UITouch*)[enumerator nextObject];
    2.60  
    2.61      if (touch) {
    2.62 -        CGPoint locationInView = [touch locationInView: self];
    2.63 +        CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
    2.64  
    2.65          /* send moved event */
    2.66          SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
    2.67 @@ -191,7 +201,7 @@
    2.68  
    2.69  #ifdef FIXED_MULTITOUCH
    2.70      while(touch) {
    2.71 -        CGPoint locationInView = [self touchLocation:touch];
    2.72 +        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
    2.73  
    2.74  #ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
    2.75          SDL_SendTouchMotion(touchId, (long)touch,