SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 06ce30d3a89eee22c7add8a8d76d9a007718ed74 parent c6f39bc60bc0a07cb3c0b46d9b8e9f1e45c0f467 Author: Lior Halphon <LIJI32@gmail.com> Date: Fri, 12 Nov 2021 18:10:03 +0200 Map joysticks to motion controls Diffstat:
| M | Cocoa/GBView.m | 14 | ++++++++++++++ |
| M | JoyKit/JOYButton.h | 10 | ++++++++-- |
| M | JoyKit/JOYButton.m | 5 | +++++ |
| M | JoyKit/JOYController.m | 20 | ++++++++++---------- |
| M | JoyKit/JOYEmulatedButton.h | 2 | +- |
| M | JoyKit/JOYEmulatedButton.m | 9 | ++++++++- |
6 files changed, 46 insertions(+), 14 deletions(-)
diff --git 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 @@ -1,7 +1,5 @@ #import <Foundation/Foundation.h> - - 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 @@ -105,4 +105,9 @@ } return false; } + +- (JOYButtonType)type +{ + return JOYButtonTypeNormal; +} @end diff --git 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 <IOKit/hid/IOHIDLib.h> @@ -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 @@ -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 @@ -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
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.