Skip to content

Commit

Permalink
IPHONE: Move mouse coordinate conversion code to iPhoneView.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Schickel committed Feb 23, 2012
1 parent c5ccb32 commit e83e31c
Showing 1 changed file with 64 additions and 64 deletions.
128 changes: 64 additions & 64 deletions backends/platform/iphone/iphone_video.mm
Expand Up @@ -184,64 +184,6 @@ uint getSizeNextPOT(uint size) {
return [documentsDirectory UTF8String];
}

/**
* Converts portrait mode coordinates into rotated mode coordinates.
*/
static bool convertToRotatedCoords(UIDeviceOrientation orientation, CGPoint point, CGPoint *result) {
switch (orientation) {
case UIDeviceOrientationLandscapeLeft:
result->x = point.y;
result->y = _renderBufferWidth - point.x;
return true;

case UIDeviceOrientationLandscapeRight:
result->x = _renderBufferHeight - point.y;
result->y = point.x;
return true;

case UIDeviceOrientationPortrait:
result->x = point.x;
result->y = point.y;
return true;

default:
return false;
}
}

static bool getMouseCoords(UIDeviceOrientation orientation, CGPoint point, int *x, int *y) {
if (!convertToRotatedCoords(orientation, point, &point))
return false;

CGRect *area;
int width, height, offsetY;
if (_overlayIsEnabled) {
area = &_overlayRect;
width = _videoContext.overlayWidth;
height = _videoContext.overlayHeight;
offsetY = _scaledShakeOffsetY;
} else {
area = &_gameScreenRect;
width = _videoContext.screenWidth;
height = _videoContext.screenHeight;
offsetY = _videoContext.shakeOffsetY;
}

point.x = (point.x - CGRectGetMinX(*area)) / CGRectGetWidth(*area);
point.y = (point.y - CGRectGetMinY(*area)) / CGRectGetHeight(*area);

*x = (int)(point.x * width);
// offsetY describes the translation of the screen in the upward direction,
// thus we need to add it here.
*y = (int)(point.y * height + offsetY);

// Clip coordinates
if (*x < 0 || *x > CGRectGetWidth(*area) || *y < 0 || *y > CGRectGetHeight(*area))
return false;

return true;
}

@implementation iPhoneView

+ (Class)layerClass {
Expand Down Expand Up @@ -722,6 +664,64 @@ - (void)addEvent:(NSDictionary *)event {
[_events addObject: event];
}

/**
* Converts portrait mode coordinates into rotated mode coordinates.
*/
- (bool)convertToRotatedCoords:(CGPoint)point result:(CGPoint *)result {
switch (_orientation) {
case UIDeviceOrientationLandscapeLeft:
result->x = point.y;
result->y = _renderBufferWidth - point.x;
return true;

case UIDeviceOrientationLandscapeRight:
result->x = _renderBufferHeight - point.y;
result->y = point.x;
return true;

case UIDeviceOrientationPortrait:
result->x = point.x;
result->y = point.y;
return true;

default:
return false;
}
}

- (bool)getMouseCoords:(CGPoint)point eventX:(int *)x eventY:(int *)y {
if (![self convertToRotatedCoords:point result:&point])
return false;

CGRect *area;
int width, height, offsetY;
if (_overlayIsEnabled) {
area = &_overlayRect;
width = _videoContext.overlayWidth;
height = _videoContext.overlayHeight;
offsetY = _scaledShakeOffsetY;
} else {
area = &_gameScreenRect;
width = _videoContext.screenWidth;
height = _videoContext.screenHeight;
offsetY = _videoContext.shakeOffsetY;
}

point.x = (point.x - CGRectGetMinX(*area)) / CGRectGetWidth(*area);
point.y = (point.y - CGRectGetMinY(*area)) / CGRectGetHeight(*area);

*x = (int)(point.x * width);
// offsetY describes the translation of the screen in the upward direction,
// thus we need to add it here.
*y = (int)(point.y * height + offsetY);

// Clip coordinates
if (*x < 0 || *x > CGRectGetWidth(*area) || *y < 0 || *y > CGRectGetHeight(*area))
return false;

return true;
}

- (void)deviceOrientationChanged:(UIDeviceOrientation)orientation {
switch (orientation) {
case UIDeviceOrientationLandscapeLeft:
Expand Down Expand Up @@ -752,7 +752,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
case 1: {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (!getMouseCoords(_orientation, point, &x, &y))
if (![self getMouseCoords:point eventX:&x eventY:&y])
return;

_firstTouch = touch;
Expand All @@ -770,7 +770,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
case 2: {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (!getMouseCoords(_orientation, point, &x, &y))
if (![self getMouseCoords:point eventX:&x eventY:&y])
return;

_secondTouch = touch;
Expand All @@ -794,7 +794,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (touch == _firstTouch) {
CGPoint point = [touch locationInView:self];
if (!getMouseCoords(_orientation, point, &x, &y))
if (![self getMouseCoords:point eventX:&x eventY:&y])
return;

[self addEvent:
Expand All @@ -807,7 +807,7 @@ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
];
} else if (touch == _secondTouch) {
CGPoint point = [touch locationInView:self];
if (!getMouseCoords(_orientation, point, &x, &y))
if (![self getMouseCoords:point eventX:&x eventY:&y])
return;

[self addEvent:
Expand All @@ -830,7 +830,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
case 1: {
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint point = [touch locationInView:self];
if (!getMouseCoords(_orientation, point, &x, &y))
if (![self getMouseCoords:point eventX:&x eventY:&y])
return;

[self addEvent:
Expand All @@ -847,7 +847,7 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
case 2: {
UITouch *touch = [[allTouches allObjects] objectAtIndex:1];
CGPoint point = [touch locationInView:self];
if (!getMouseCoords(_orientation, point, &x, &y))
if (![self getMouseCoords:point eventX:&x eventY:&y])
return;

[self addEvent:
Expand Down

0 comments on commit e83e31c

Please sign in to comment.