From 838db7ec03210b01cbb7100fe8bc91b44e3fceda Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 18 Sep 2012 01:45:15 -0700 Subject: [PATCH] Fixed retina display input scaling --- src/video/uikit/SDL_uikitview.h | 2 +- src/video/uikit/SDL_uikitview.m | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/video/uikit/SDL_uikitview.h b/src/video/uikit/SDL_uikitview.h index 3b5e838c4..20e330745 100755 --- a/src/video/uikit/SDL_uikitview.h +++ b/src/video/uikit/SDL_uikitview.h @@ -50,7 +50,7 @@ @public SDL_uikitviewcontroller *viewcontroller; } -- (CGPoint)touchLocation:(UITouch *)touch; +- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m index 9c9362525..3e4afc561 100755 --- a/src/video/uikit/SDL_uikitview.m +++ b/src/video/uikit/SDL_uikitview.m @@ -76,14 +76,24 @@ - (id)initWithFrame:(CGRect)frame } -- (CGPoint)touchLocation:(UITouch *)touch +- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize { CGPoint point = [touch locationInView: self]; CGRect frame = [self frame]; frame = CGRectApplyAffineTransform(frame, [self transform]); - point.x /= frame.size.width; - point.y /= frame.size.height; + + // Get the display scale and apply that to the input coordinates + SDL_Window *window = self->viewcontroller.window; + SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window); + SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; + point.x *= displaymodedata->scale; + point.y *= displaymodedata->scale; + + if (normalize) { + point.x /= frame.size.width; + point.y /= frame.size.height; + } return point; } @@ -93,7 +103,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event UITouch *touch = (UITouch*)[enumerator nextObject]; if (touch) { - CGPoint locationInView = [touch locationInView: self]; + CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; /* send moved event */ SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y); @@ -104,7 +114,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event #ifdef FIXED_MULTITOUCH while(touch) { - CGPoint locationInView = [self touchLocation:touch]; + CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES]; #ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS //FIXME: TODO: Using touch as the fingerId is potentially dangerous @@ -143,7 +153,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event #ifdef FIXED_MULTITOUCH while(touch) { - CGPoint locationInView = [self touchLocation:touch]; + CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES]; #ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS SDL_SendFingerDown(touchId, (long)touch, @@ -183,7 +193,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event UITouch *touch = (UITouch*)[enumerator nextObject]; if (touch) { - CGPoint locationInView = [touch locationInView: self]; + CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO]; /* send moved event */ SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y); @@ -191,7 +201,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event #ifdef FIXED_MULTITOUCH while(touch) { - CGPoint locationInView = [self touchLocation:touch]; + CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES]; #ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS SDL_SendTouchMotion(touchId, (long)touch,