Refactor new macOS code to reduce #ifs, increase shared code, and fix warnings

This commit is contained in:
Cameron Gutman
2018-03-27 00:34:38 -07:00
parent 6cc165b589
commit e8832ed746
22 changed files with 73 additions and 293 deletions

View File

@@ -8,9 +8,8 @@
#import "ControllerSupport.h"
#if TARGET_OS_IPHONE
#import "OnScreenControls.h"
#else
#if !TARGET_OS_IPHONE
#import "Gamepad.h"
#import "Control.h"
#endif
@@ -28,7 +27,6 @@
NSLock *_controllerStreamLock;
NSMutableDictionary *_controllers;
#if TARGET_OS_IPHONE
OnScreenControls *_osc;
// This controller object is shared between on-screen controls
@@ -37,7 +35,6 @@
#define EMULATING_SELECT 0x1
#define EMULATING_SPECIAL 0x2
#endif
bool _oscEnabled;
char _controllerNumbers;
@@ -85,7 +82,6 @@
}
}
#if TARGET_OS_IPHONE
-(void) handleSpecialCombosReleased:(Controller*)controller releasedButtons:(int)releasedButtons
{
if ((controller.emulatingButtonFlags & EMULATING_SELECT) &&
@@ -120,7 +116,6 @@
}
}
#endif
-(void) updateButtonFlags:(Controller*)controller flags:(int)flags
{
@@ -129,14 +124,12 @@
// This must be called before handleSpecialCombosPressed
// because we clear the original button flags there
#if TARGET_OS_IPHONE
int releasedButtons = (controller.lastButtonFlags ^ flags) & ~flags;
int pressedButtons = (controller.lastButtonFlags ^ flags) & flags;
[self handleSpecialCombosReleased:controller releasedButtons:releasedButtons];
[self handleSpecialCombosPressed:controller pressedButtons:pressedButtons];
#endif
}
}
@@ -144,9 +137,7 @@
{
@synchronized(controller) {
controller.lastButtonFlags |= flags;
#if TARGET_OS_IPHONE
[self handleSpecialCombosPressed:controller pressedButtons:flags];
#endif
}
}
@@ -154,9 +145,7 @@
{
@synchronized(controller) {
controller.lastButtonFlags &= ~flags;
#if TARGET_OS_IPHONE
[self handleSpecialCombosReleased:controller releasedButtons:flags];
#endif
}
}
@@ -258,7 +247,6 @@
}
}
#if TARGET_OS_IPHONE
-(void) updateAutoOnScreenControlMode
{
// Auto on-screen control support may not be enabled
@@ -285,7 +273,9 @@
}
}
#if TARGET_OS_IPHONE
[_osc setLevel:level];
#endif
}
-(void) initAutoOnScreenControlMode:(OnScreenControls*)osc
@@ -294,7 +284,6 @@
[self updateAutoOnScreenControlMode];
}
#endif
-(void) assignController:(GCController*)controller {
for (int i = 0; i < 4; i++) {
@@ -303,7 +292,7 @@
controller.playerIndex = i;
Controller* limeController;
#if TARGET_OS_IPHONE
if (i == 0) {
// Player 0 shares a controller object with the on-screen controls
limeController = _player0osc;
@@ -311,10 +300,6 @@
limeController = [[Controller alloc] init];
limeController.playerIndex = i;
}
#else
limeController = [[Controller alloc] init];
limeController.playerIndex = i;
#endif
[_controllers setObject:limeController forKey:[NSNumber numberWithInteger:controller.playerIndex]];
@@ -387,10 +372,10 @@
_controllers = [[NSMutableDictionary alloc] init];
_controllerNumbers = 0;
#if TARGET_OS_IPHONE
_player0osc = [[Controller alloc] init];
_player0osc.playerIndex = 0;
#if TARGET_OS_IPHONE
DataManager* dataMan = [[DataManager alloc] init];
_oscEnabled = (OnScreenControlsLevel)[[dataMan getSettings].onscreenControls integerValue] != OnScreenControlsLevelOff;
#else
@@ -403,10 +388,7 @@
for (GCController* controller in [GCController controllers]) {
[self assignController:controller];
[self registerControllerCallbacks:controller];
#if TARGET_OS_IPHONE
[self updateAutoOnScreenControlMode];
#endif
}
self.connectObserver = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidConnectNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
@@ -418,10 +400,8 @@
// Register callbacks on the new controller
[self registerControllerCallbacks:controller];
#if TARGET_OS_IPHONE
// Re-evaluate the on-screen control mode
[self updateAutoOnScreenControlMode];
#endif
}];
self.disconnectObserver = [[NSNotificationCenter defaultCenter] addObserverForName:GCControllerDidDisconnectNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
Log(LOG_I, @"Controller disconnected!");
@@ -435,10 +415,8 @@
[self updateFinished:[_controllers objectForKey:[NSNumber numberWithInteger:controller.playerIndex]]];
[_controllers removeObjectForKey:[NSNumber numberWithInteger:controller.playerIndex]];
#if TARGET_OS_IPHONE
// Re-evaluate the on-screen control mode
[self updateAutoOnScreenControlMode];
#endif
}];
return self;
}