From 06ce30d3a89eee22c7add8a8d76d9a007718ed74 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 12 Nov 2021 18:10:03 +0200 Subject: [PATCH] Map joysticks to motion controls --- Cocoa/GBView.m | 14 ++++++++++++++ JoyKit/JOYButton.h | 10 ++++++++-- JoyKit/JOYButton.m | 5 +++++ JoyKit/JOYController.m | 20 ++++++++++---------- JoyKit/JOYEmulatedButton.h | 2 +- JoyKit/JOYEmulatedButton.m | 9 ++++++++- 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 6c92c3f..5ff3f3c 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -459,6 +459,12 @@ static const uint8_t workboy_vk_to_key[] = { [lastController setRumbleAmplitude:amp]; } +- (bool)shouldControllerUseJoystickForMotion:(JOYController *)controller +{ + if (!GB_has_accelerometer(_gb)) return false; + return true; +} + - (void)controller:(JOYController *)controller movedAxis:(JOYAxis *)axis { if (![self.window isMainWindow]) return; @@ -481,9 +487,17 @@ static const uint8_t workboy_vk_to_key[] = { } } +- (void)controller:(JOYController *)controller movedAxes2D:(JOYAxes2D *)axes +{ + if ([self shouldControllerUseJoystickForMotion:controller]) { + GB_set_accelerometer_values(_gb, -axes.value.x, -axes.value.y); + } +} + - (void)controller:(JOYController *)controller buttonChangedState:(JOYButton *)button { if (![self.window isMainWindow]) return; + if (button.type == JOYButtonTypeAxes2DEmulated && [self shouldControllerUseJoystickForMotion:controller]) return; unsigned player_count = GB_get_player_count(_gb); if (self.document.partner) { diff --git a/JoyKit/JOYButton.h b/JoyKit/JOYButton.h index 6a67c6c..08c3ace 100644 --- a/JoyKit/JOYButton.h +++ b/JoyKit/JOYButton.h @@ -1,7 +1,5 @@ #import - - typedef enum { JOYButtonUsageNone, JOYButtonUsageA, @@ -41,12 +39,20 @@ typedef enum { JOYButtonUsageGeneric0 = 0x10000, } JOYButtonUsage; +typedef enum { + JOYButtonTypeNormal, + JOYButtonTypeAxisEmulated, + JOYButtonTypeAxes2DEmulated, + JOYButtonTypeHatEmulated, +} JOYButtonType; + @interface JOYButton : NSObject - (NSString *)usageString; + (NSString *)usageToString: (JOYButtonUsage) usage; - (uint64_t)uniqueID; - (bool) isPressed; @property JOYButtonUsage usage; +@property (readonly) JOYButtonType type; @end diff --git a/JoyKit/JOYButton.m b/JoyKit/JOYButton.m index 18970cd..568d383 100644 --- a/JoyKit/JOYButton.m +++ b/JoyKit/JOYButton.m @@ -105,4 +105,9 @@ } return false; } + +- (JOYButtonType)type +{ + return JOYButtonTypeNormal; +} @end diff --git a/JoyKit/JOYController.m b/JoyKit/JOYController.m index 1097ef6..c9a49ac 100644 --- a/JoyKit/JOYController.m +++ b/JoyKit/JOYController.m @@ -3,7 +3,7 @@ #import "JOYElement.h" #import "JOYSubElement.h" #import "JOYFullReportElement.h" - +#import "JOYButton.h" #import "JOYEmulatedButton.h" #include @@ -307,10 +307,10 @@ typedef union { if (axes2DEmulateButtons) { _axes2DEmulatedButtons[@(axes.uniqueID)] = @[ - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:axes.uniqueID | 0x100000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:axes.uniqueID | 0x200000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:axes.uniqueID | 0x300000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:axes.uniqueID | 0x400000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft type:JOYButtonTypeAxes2DEmulated uniqueID:axes.uniqueID | 0x100000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight type:JOYButtonTypeAxes2DEmulated uniqueID:axes.uniqueID | 0x200000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp type:JOYButtonTypeAxes2DEmulated uniqueID:axes.uniqueID | 0x300000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown type:JOYButtonTypeAxes2DEmulated uniqueID:axes.uniqueID | 0x400000000L], ]; } @@ -346,7 +346,7 @@ typedef union { if ([_hacks[JOYEmulateAxisButtons] boolValue]) { _axisEmulatedButtons[@(axis.uniqueID)] = - [[JOYEmulatedButton alloc] initWithUsage:axis.equivalentButtonUsage uniqueID:axis.uniqueID]; + [[JOYEmulatedButton alloc] initWithUsage:axis.equivalentButtonUsage type:JOYButtonTypeAxisEmulated uniqueID:axis.uniqueID]; } @@ -366,10 +366,10 @@ typedef union { [_hats setObject:hat forKey:element]; if (hatsEmulateButtons) { _hatEmulatedButtons[@(hat.uniqueID)] = @[ - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft uniqueID:hat.uniqueID | 0x100000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight uniqueID:hat.uniqueID | 0x200000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp uniqueID:hat.uniqueID | 0x300000000L], - [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown uniqueID:hat.uniqueID | 0x400000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadLeft type:JOYButtonTypeHatEmulated uniqueID:hat.uniqueID | 0x100000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadRight type:JOYButtonTypeHatEmulated uniqueID:hat.uniqueID | 0x200000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadUp type:JOYButtonTypeHatEmulated uniqueID:hat.uniqueID | 0x300000000L], + [[JOYEmulatedButton alloc] initWithUsage:JOYButtonUsageDPadDown type:JOYButtonTypeHatEmulated uniqueID:hat.uniqueID | 0x400000000L], ]; } break; diff --git a/JoyKit/JOYEmulatedButton.h b/JoyKit/JOYEmulatedButton.h index 491e0c7..05ccde8 100644 --- a/JoyKit/JOYEmulatedButton.h +++ b/JoyKit/JOYEmulatedButton.h @@ -4,7 +4,7 @@ #import "JOYHat.h" @interface JOYEmulatedButton : JOYButton -- (instancetype)initWithUsage:(JOYButtonUsage)usage uniqueID:(uint64_t)uniqueID; +- (instancetype)initWithUsage:(JOYButtonUsage)usage type:(JOYButtonType)type uniqueID:(uint64_t)uniqueID; - (bool)updateStateFromAxis:(JOYAxis *)axis; - (bool)updateStateFromAxes2D:(JOYAxes2D *)axes; - (bool)updateStateFromHat:(JOYHat *)hat; diff --git a/JoyKit/JOYEmulatedButton.m b/JoyKit/JOYEmulatedButton.m index b62670a..5e6d1b3 100644 --- a/JoyKit/JOYEmulatedButton.m +++ b/JoyKit/JOYEmulatedButton.m @@ -10,13 +10,15 @@ @implementation JOYEmulatedButton { uint64_t _uniqueID; + JOYButtonType _type; } -- (instancetype)initWithUsage:(JOYButtonUsage)usage uniqueID:(uint64_t)uniqueID; +- (instancetype)initWithUsage:(JOYButtonUsage)usage type:(JOYButtonType)type uniqueID:(uint64_t)uniqueID; { self = [super init]; self.usage = usage; _uniqueID = uniqueID; + _type = type; return self; } @@ -89,4 +91,9 @@ return _state != old; } +- (JOYButtonType)type +{ + return _type; +} + @end