first cut at support remapped keys for mfi controller

This commit is contained in:
Yoshi Sugawara 2016-04-13 16:03:36 -10:00
parent ac889bb00e
commit c44ca5ec3f
8 changed files with 218 additions and 39 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 215 KiB

View File

@ -1,21 +0,0 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "3800713644_bbfa97eb60_b.jpg",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@ -12,6 +12,10 @@
@interface GameControllerKeyRemapController : UIViewController
@property (nonatomic, strong) IBOutlet UIView *keyboardContainerView;
@property (nonatomic, strong) IBOutlet UIButton *saveButton;
@property (nonatomic, strong) IBOutlet UIButton *cancelButton;
@property (nonatomic, strong) KeyMapper *keyMapper;
@property(nonatomic, copy) void (^onDismissal)();
@end

View File

@ -109,6 +109,10 @@ struct KeyCap keyCapDefinitions[] = {
- (void)viewDidLoad {
[super viewDidLoad];
self.keyCapViews = [NSMutableArray array];
self.saveButton.layer.borderWidth = 1.0f;
self.saveButton.layer.borderColor = [self.view.tintColor CGColor];
self.cancelButton.layer.borderWidth = 1.0f;
self.cancelButton.layer.borderColor = [self.view.tintColor CGColor];
[self constructKeyboard];
}
@ -220,8 +224,9 @@ struct KeyCap keyCapDefinitions[] = {
- (void) onKeyTap:(UITapGestureRecognizer*)sender {
KeyCapView *view = (KeyCapView*) sender.view;
self.alertView = [[UIAlertView alloc] initWithTitle:@"Remap Key" message:[NSString stringWithFormat:@"Press a button to map the [%@] key",[view.keyDef objectAtIndex:KeyCapIndexKey]] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
self.alertView = [[UIAlertView alloc] initWithTitle:@"Remap Key" message:[NSString stringWithFormat:@"Press a button to map the [%@] key",[view.keyDef objectAtIndex:KeyCapIndexKey]] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Unbind",nil];
[self.alertView show];
self.alertView.tag = [[view.keyDef objectAtIndex:KeyCapIndexCode] integerValue];
[self startRemappingControlsForMfiControllerForKey:[view.keyDef objectAtIndex:KeyCapIndexCode]];
}
@ -364,12 +369,29 @@ struct KeyCap keyCapDefinitions[] = {
}
}
-(IBAction)saveButtonTapped:(id)sender {
[self.keyMapper saveKeyMapping];
[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
self.onDismissal();
}];
}
-(IBAction)cancelButtonTapped:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:YES completion:^{
self.onDismissal();
}];
}
#
# pragma mark - UIAlertViewDelegate
#
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self stopRemappingControls];
if ( buttonIndex == 1 ) {
AppleKeyboardKey mappedKey = alertView.tag;
[self.keyMapper unmapKey:mappedKey];
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {

View File

@ -8,7 +8,9 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="GameControllerKeyRemapController">
<connections>
<outlet property="cancelButton" destination="zAx-JO-gDf" id="i1M-NV-0UN"/>
<outlet property="keyboardContainerView" destination="PbL-95-ilz" id="UPI-70-2J7"/>
<outlet property="saveButton" destination="Ihv-aC-sb7" id="Byt-Gy-MVK"/>
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
</connections>
</placeholder>
@ -17,34 +19,56 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="keyboardBackground" translatesAutoresizingMaskIntoConstraints="NO" id="uys-KE-zQa">
<rect key="frame" x="0.0" y="0.0" width="600" height="360"/>
</imageView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PbL-95-ilz" userLabel="Keyboard Container View">
<rect key="frame" x="0.0" y="360" width="600" height="240"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ihv-aC-sb7">
<rect key="frame" x="96" y="203" width="107" height="44"/>
<constraints>
<constraint firstAttribute="width" constant="107" id="82d-Qy-Xfg"/>
<constraint firstAttribute="height" constant="44" id="bOc-8m-OXA"/>
</constraints>
<state key="normal" title="Save"/>
<connections>
<action selector="saveButtonTapped:" destination="-1" eventType="touchUpInside" id="dbc-hJ-1z4"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="zAx-JO-gDf">
<rect key="frame" x="396.5" y="203" width="107" height="44"/>
<state key="normal" title="Cancel"/>
<connections>
<action selector="cancelButtonTapped:" destination="-1" eventType="touchUpInside" id="byq-hr-jDY"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Remap Keys to Gamepad Buttons" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="x2H-qm-EGT">
<rect key="frame" x="172" y="64" width="256" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="x2H-qm-EGT" firstAttribute="centerY" secondItem="i5M-Pr-FkT" secondAttribute="centerY" multiplier="0.25" id="3zs-qM-hjM"/>
<constraint firstItem="zAx-JO-gDf" firstAttribute="top" secondItem="Ihv-aC-sb7" secondAttribute="top" id="6TO-tV-bng"/>
<constraint firstItem="PbL-95-ilz" firstAttribute="height" secondItem="i5M-Pr-FkT" secondAttribute="height" multiplier="0.4" id="7Tq-sc-r5F"/>
<constraint firstItem="PbL-95-ilz" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="ALZ-jN-EW8"/>
<constraint firstItem="PbL-95-ilz" firstAttribute="top" secondItem="uys-KE-zQa" secondAttribute="bottom" id="RE5-pz-I4K"/>
<constraint firstAttribute="bottom" secondItem="uys-KE-zQa" secondAttribute="bottom" id="UX3-iO-Kgu"/>
<constraint firstAttribute="trailing" secondItem="uys-KE-zQa" secondAttribute="trailing" id="ZD9-nb-sMQ"/>
<constraint firstItem="zAx-JO-gDf" firstAttribute="height" secondItem="Ihv-aC-sb7" secondAttribute="height" id="HFd-QQ-Vpx"/>
<constraint firstItem="zAx-JO-gDf" firstAttribute="centerX" secondItem="i5M-Pr-FkT" secondAttribute="centerX" multiplier="1.5" id="Kdj-w0-y90"/>
<constraint firstItem="x2H-qm-EGT" firstAttribute="centerX" secondItem="i5M-Pr-FkT" secondAttribute="centerX" id="ZTO-dh-bwz"/>
<constraint firstItem="zAx-JO-gDf" firstAttribute="centerX" secondItem="Ihv-aC-sb7" secondAttribute="centerX" multiplier="2" id="hJc-6p-crb"/>
<constraint firstAttribute="bottom" secondItem="PbL-95-ilz" secondAttribute="bottom" id="kCc-QA-uYA"/>
<constraint firstItem="uys-KE-zQa" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="qDJ-fw-MAq"/>
<constraint firstItem="uys-KE-zQa" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" id="tmE-f1-uny"/>
<constraint firstItem="zAx-JO-gDf" firstAttribute="width" secondItem="Ihv-aC-sb7" secondAttribute="width" id="mPI-1j-gTO"/>
<constraint firstItem="Ihv-aC-sb7" firstAttribute="centerX" secondItem="i5M-Pr-FkT" secondAttribute="centerX" multiplier="0.5" id="niB-eU-bnx"/>
<constraint firstItem="Ihv-aC-sb7" firstAttribute="centerY" secondItem="i5M-Pr-FkT" secondAttribute="centerY" multiplier="0.75" id="vLe-Gk-22l"/>
<constraint firstAttribute="trailing" secondItem="PbL-95-ilz" secondAttribute="trailing" id="zbm-Dr-7Jd"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="UX3-iO-Kgu"/>
<exclude reference="hJc-6p-crb"/>
</mask>
</variation>
</view>
</objects>
<resources>
<image name="keyboardBackground" width="512" height="165"/>
</resources>
</document>

View File

@ -569,6 +569,8 @@ extern int findCode(const char* _s);
[self setInputMode:INPUTMODE_ACCESS+INPUTMODE_HIDDEN];
[self setMenuBarVisibility:TRUE]; // So First time users are not lost!
self.keyMapper = [[KeyMapper alloc] init];
self.mfiControllerHandler = [[MfiGameControllerHandler alloc] init];
__weak typeof(self) weakSelf = self;
[self.mfiControllerHandler discoverController:^(GCController *gameController) {
@ -578,6 +580,7 @@ extern int findCode(const char* _s);
} disconnectedCallback:^{
[pManager setNotificationText:@"mFi Controller Disconnected"];
}];
}
-(void) setupMfiController:(GCController*)controller {
@ -599,8 +602,15 @@ extern int findCode(const char* _s);
joyX = xvalue;
joyY = yvalue * -1.0;
};
GCControllerButtonInput *buttonX = controller.extendedGamepad ? controller.extendedGamepad.buttonX : controller.gamepad.buttonX;
GCControllerButtonInput *buttonA = controller.extendedGamepad ? controller.extendedGamepad.buttonA : controller.gamepad.buttonA;
GCControllerButtonInput *buttonY = controller.extendedGamepad ? controller.extendedGamepad.buttonY : controller.gamepad.buttonY;
GCControllerButtonInput *buttonB = controller.extendedGamepad ? controller.extendedGamepad.buttonB : controller.gamepad.buttonB;
GCControllerButtonInput *buttonRS = controller.extendedGamepad ? controller.extendedGamepad.rightShoulder : controller.gamepad.rightShoulder;
GCControllerButtonInput *buttonLS = controller.extendedGamepad ? controller.extendedGamepad.leftShoulder : controller.gamepad.leftShoulder;
GCControllerButtonInput *buttonRT = controller.extendedGamepad ? controller.extendedGamepad.rightTrigger : nil;
GCControllerButtonInput *buttonLT = controller.extendedGamepad ? controller.extendedGamepad.leftTrigger : nil;
GCControllerDirectionPad *dpad = controller.extendedGamepad ? controller.extendedGamepad.dpad : controller.gamepad.dpad;
buttonX.valueChangedHandler = appleJoyButton0Handler;
@ -611,7 +621,131 @@ extern int findCode(const char* _s);
controller.extendedGamepad.leftThumbstick.valueChangedHandler = appleJoystickhHandler;
}
self.keyMapper = [[KeyMapper alloc] init];
//
// mapped keys
AppleKeyboardKey mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_X];
if ( mappedKey != NSNotFound ) {
buttonX.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_Y];
if ( mappedKey != NSNotFound ) {
buttonY.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_A];
if ( mappedKey != NSNotFound ) {
buttonA.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_B];
if ( mappedKey != NSNotFound ) {
buttonB.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_LS];
if ( mappedKey != NSNotFound ) {
buttonLS.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
__block AppleKeyboardKey mappedKeyRS = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_RS];
if ( mappedKeyRS != NSNotFound ) {
buttonRS.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKeyRS, 0);
} else {
add_event_key((int)mappedKeyRS, 1);
}
};
}
mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_LT];
if ( mappedKey != NSNotFound && buttonLT != nil ) {
buttonLT.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_RT];
if ( mappedKey != NSNotFound && buttonLT != nil ) {
buttonRT.valueChangedHandler = ^(GCControllerButtonInput *button, float value, BOOL pressed) {
if ( pressed ) {
add_event_key((int)mappedKey, 0);
} else {
add_event_key((int)mappedKey, 1);
}
};
}
AppleKeyboardKey mappedKeyDpadUp = [self.keyMapper getMappedKeyForControl:MFI_DPAD_UP];
AppleKeyboardKey mappedKeyDpadDown = [self.keyMapper getMappedKeyForControl:MFI_DPAD_DOWN];
AppleKeyboardKey mappedKeyDpadLeft = [self.keyMapper getMappedKeyForControl:MFI_DPAD_LEFT];
AppleKeyboardKey mappedKeyDpadRight = [self.keyMapper getMappedKeyForControl:MFI_DPAD_RIGHT];
if ( mappedKeyDpadUp != NSNotFound || mappedKeyDpadDown != NSNotFound || mappedKeyDpadLeft != NSNotFound || mappedKeyDpadRight != NSNotFound ) {
dpad.valueChangedHandler = ^(GCControllerDirectionPad *, float xvalue, float yvalue) {
if ( mappedKeyDpadUp != NSNotFound && yvalue > 0.0 ) {
add_event_key((int)mappedKeyDpadUp, 0);
} else if ( mappedKeyDpadUp != NSNotFound && yvalue <= 0.0 ) {
add_event_key((int)mappedKeyDpadUp, 1);
}
if ( mappedKeyDpadDown != NSNotFound && yvalue < 0.0 ) {
add_event_key((int)mappedKeyDpadDown, 0);
} else if ( mappedKeyDpadDown != NSNotFound && yvalue >= 0.0 ) {
add_event_key((int)mappedKeyDpadDown, 1);
}
if ( mappedKeyDpadRight != NSNotFound && xvalue > 0.0 ) {
add_event_key((int)mappedKeyDpadRight, 0);
} else if ( mappedKeyDpadRight != NSNotFound && xvalue <= 0.0 ) {
add_event_key((int)mappedKeyDpadRight, 1);
}
if ( mappedKeyDpadLeft != NSNotFound && xvalue < 0.0 ) {
add_event_key((int)mappedKeyDpadLeft, 0);
} else if ( mappedKeyDpadLeft != NSNotFound && xvalue >= 0.0 ) {
add_event_key((int)mappedKeyDpadLeft, 1);
}
// pass the joystick input through
joyX = xvalue;
joyY = yvalue * -1.0;
};
}
}
int hardwarekeyboard= 0;
@ -972,6 +1106,10 @@ extern int x_frame_rate ;
r_sim65816.pause();
GameControllerKeyRemapController *remapController = [[GameControllerKeyRemapController alloc] initWithNibName:@"GameControllerKeyRemapController" bundle:nil];
remapController.keyMapper = self.keyMapper;
remapController.onDismissal = ^{
[self setupMfiController:[[GCController controllers] firstObject]];
r_sim65816.resume();
};
[self presentViewController:remapController animated:YES completion:nil];
}

View File

@ -107,7 +107,9 @@ typedef NS_ENUM(NSInteger, KeyCapIndex) {
@interface KeyMapper : NSObject
-(void) saveKeyMapping;
-(void) mapKey:(AppleKeyboardKey)keyboardKey ToControl:(KeyMapMappableButton)button;
-(void) unmapKey:(AppleKeyboardKey)keyboardKey;
-(AppleKeyboardKey) getMappedKeyForControl:(KeyMapMappableButton)button;
-(KeyMapMappableButton) getControlForMappedKey:(AppleKeyboardKey) keyboardKey;
+(NSString*) controlToDisplayName:(KeyMapMappableButton)button;

View File

@ -16,16 +16,19 @@
-(instancetype) init {
if ( self = [super init] ) {
self.keyMapping = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"keyMapping"] mutableCopy];
if ( self.keyMapping == nil ) {
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"keyMapping"];
if ( data == nil || ![data isKindOfClass:[NSData class]] ) {
self.keyMapping = [NSMutableDictionary dictionary];
} else {
NSDictionary *fetchedDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
self.keyMapping = [fetchedDict mutableCopy];
}
}
return self;
}
-(void) saveKeyMapping {
[[NSUserDefaults standardUserDefaults] setObject:self.keyMapping forKey:@"keyMapping"];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:self.keyMapping] forKey:@"keyMapping"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@ -34,11 +37,18 @@
[self.keyMapping setObject:[NSNumber numberWithInteger:keyboardKey] forKey:buttonKey];
}
-(void) unmapKey:(AppleKeyboardKey)keyboardKey {
KeyMapMappableButton button = [self getControlForMappedKey:keyboardKey];
if ( button != NSNotFound ) {
[self.keyMapping removeObjectForKey:[NSNumber numberWithInteger:button]];
}
}
-(AppleKeyboardKey) getMappedKeyForControl:(KeyMapMappableButton)button {
NSNumber *buttonKey = [NSNumber numberWithInteger:button];
NSNumber *mappedKey = [self.keyMapping objectForKey:buttonKey];
if ( mappedKey != nil ) {
return [mappedKey integerValue];
return [mappedKey intValue];
} else {
return NSNotFound;
}