Map joysticks to motion controls

This commit is contained in:
Lior Halphon 2021-11-12 18:10:03 +02:00
parent c6f39bc60b
commit 06ce30d3a8
6 changed files with 46 additions and 14 deletions

View File

@ -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) {

View File

@ -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

View File

@ -105,4 +105,9 @@
}
return false;
}
- (JOYButtonType)type
{
return JOYButtonTypeNormal;
}
@end

View File

@ -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;

View File

@ -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;

View File

@ -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