Skip to content

Commit

Permalink
Improved dpad controls
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Jun 9, 2024
1 parent 76c9325 commit 2018f0e
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions iOS/GBBackgroundView.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static GB_key_mask_t angleToKeyMask(double angle)
@implementation GBBackgroundView
{
NSMutableSet<UITouch *> *_touches;
UITouch *_swipePadTouch;
UITouch *_padTouch;
CGPoint _padSwipeOrigin;
UITouch *_screenTouch;
UITouch *_logoTouch;
Expand Down Expand Up @@ -219,10 +219,10 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
else if (CGRectContainsPoint(logoRect, point) && !_logoTouch) {
_logoTouch = touch;
}
else if (_usesSwipePad && !_swipePadTouch) {
else if (!_padTouch) {
if (fabs(point.x - dpadLocation.x) <= dpadRadius &&
fabs(point.y - dpadLocation.y) <= dpadRadius) {
_swipePadTouch = touch;
_padTouch = touch;
_padSwipeOrigin = point;
}
}
Expand All @@ -233,8 +233,8 @@ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (_swipePadTouch && [touches containsObject:_swipePadTouch]) {
_swipePadTouch = nil;
if (_padTouch && [touches containsObject:_padTouch]) {
_padTouch = nil;
}

if (_screenTouch && [touches containsObject:_screenTouch]) {
Expand Down Expand Up @@ -298,8 +298,8 @@ - (void)touchesChanged
bool dpadHandled = false;
if (_usesSwipePad) {
dpadHandled = true;
if (_swipePadTouch) {
CGPoint point = [_swipePadTouch locationInView:self];
if (_padTouch) {
CGPoint point = [_padTouch locationInView:self];
double squaredDistance = CGPointSquaredDistance(point, _padSwipeOrigin);
if (squaredDistance > 16 * 16) {
double angle = CGPointAngle(point, _padSwipeOrigin);
Expand All @@ -315,7 +315,7 @@ - (void)touchesChanged
}
}
for (UITouch *touch in _touches) {
if (touch == _swipePadTouch) continue;
if (_usesSwipePad && touch == _padTouch) continue;
CGPoint point = [touch locationInView:self];

if (touch == _screenTouch) {
Expand Down Expand Up @@ -364,7 +364,17 @@ - (void)touchesChanged

point.x *= factor;
point.y *= factor;
if (CGPointSquaredDistance(point, _layout.aLocation) <= buttonRadiusSquared) {
if (!dpadHandled &&
(touch == _padTouch ||
(fabs(point.x - _layout.dpadLocation.x) <= dpadRadius &&
fabs(point.y - _layout.dpadLocation.y) <= dpadRadius)
) && (fabs(point.x - _layout.dpadLocation.x) >= dpadRadius / 5 ||
fabs(point.y - _layout.dpadLocation.y) >= dpadRadius / 5)) {
dpadHandled = true; // Don't handle the dpad twice
double angle = CGPointAngle(point, _layout.dpadLocation);
mask |= angleToKeyMask(angle);
}
else if (CGPointSquaredDistance(point, _layout.aLocation) <= buttonRadiusSquared) {
mask |= GB_KEY_A_MASK;
}
else if (CGPointSquaredDistance(point, _layout.bLocation) <= buttonRadiusSquared) {
Expand All @@ -380,13 +390,6 @@ - (void)touchesChanged
CGPointSquaredDistance(point, _layout.abComboLocation) <= buttonRadiusSquared) {
mask |= GB_KEY_A_MASK | GB_KEY_B_MASK;
}
else if (!dpadHandled &&
fabs(point.x - _layout.dpadLocation.x) <= dpadRadius &&
fabs(point.y - _layout.dpadLocation.y) <= dpadRadius) {
dpadHandled = true; // Don't handle the dpad twice
double angle = CGPointAngle(point, _layout.dpadLocation);
mask |= angleToKeyMask(angle);
}
}
if (mask != _lastMask) {
_aButtonView.image = [_layout.theme imageNamed:(mask & GB_KEY_A_MASK)? @"buttonAPressed" : @"buttonA"];
Expand Down

0 comments on commit 2018f0e

Please sign in to comment.