support remapping keys for icade

This commit is contained in:
Yoshi Sugawara 2016-04-14 10:35:27 -10:00
parent d07151f48a
commit fa0f4b5d2c
5 changed files with 255 additions and 28 deletions

View File

@ -100,11 +100,13 @@ struct KeyCap keyCapDefinitions[] = {
{ 0,0,0,0 }
};
@interface GameControllerKeyRemapController () <UIAlertViewDelegate>
@interface GameControllerKeyRemapController () <UIAlertViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray *keyCapViews;
@property (nonatomic, strong) UIAlertView *alertView;
@property (nonatomic, strong) KeyMapper *keyMapperWorkingCopy;
@property (nonatomic, strong) MfiGameControllerHandler *controllerHandler;
@property (nonatomic, strong) UITextField *textFieldForICadeInput;
@property (nonatomic, strong) NSNumber *currentlyMappingKey;
@end
@implementation GameControllerKeyRemapController
@ -123,6 +125,13 @@ struct KeyCap keyCapDefinitions[] = {
self.cancelButton.layer.borderColor = [[UIColor redColor] CGColor];
self.defaultsButton.layer.borderWidth = 1.0f;
self.defaultsButton.layer.borderColor = [self.view.tintColor CGColor];
// for detecting icade input
self.textFieldForICadeInput = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.textFieldForICadeInput.delegate = self;
self.textFieldForICadeInput.autocapitalizationType= UITextAutocorrectionTypeNo;
self.currentlyMappingKey = nil;
[self constructKeyboard];
}
@ -238,6 +247,11 @@ struct KeyCap keyCapDefinitions[] = {
[self.alertView show];
self.alertView.tag = [[view.keyDef objectAtIndex:KeyCapIndexCode] integerValue];
[self startRemappingControlsForMfiControllerForKey:[view.keyDef objectAtIndex:KeyCapIndexCode]];
self.alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alertView textFieldAtIndex:0].delegate = self;
[self.alertView textFieldAtIndex:0].autocapitalizationType = UITextAutocorrectionTypeNo;
// start icade detection
self.currentlyMappingKey = [view.keyDef objectAtIndex:KeyCapIndexCode];
}
- (void) startRemappingControlsForMfiControllerForKey:(NSNumber*)keyCode {
@ -377,6 +391,7 @@ struct KeyCap keyCapDefinitions[] = {
} else {
controller.gamepad.valueChangedHandler = nil;
}
self.currentlyMappingKey = nil;
}
-(IBAction)saveButtonTapped:(id)sender {
@ -417,4 +432,72 @@ struct KeyCap keyCapDefinitions[] = {
[self refreshAllKeyCapViews];
}
#
# pragma mark - UITextFieldDelegate
#
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ( self.currentlyMappingKey == nil ) {
return NO;
}
const char* s = [string UTF8String];
char c;
int i=0;
while( (c = s[i++]) != 0)
{
switch(c) {
case 'e': // up released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_DPAD_UP];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'z': // down released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_DPAD_DOWN];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'q': // left released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_DPAD_LEFT];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'c': // right released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_DPAD_RIGHT];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 't': // button 1 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_1];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'r': // button 2 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_2];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'f': // button 3 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_3];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'n': // button 4 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_4];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'm': // button 5 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_5];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'p': // button 6 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_6];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'g': // button 7 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_7];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
case 'v': // button 8 released
[self.keyMapperWorkingCopy mapKey:self.currentlyMappingKey.integerValue ToControl:ICADE_BUTTON_8];
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
break;
default:
break;
}
}
return NO;
}
@end

View File

@ -456,7 +456,7 @@ extern int findCode(const char* _s);
self.debugIndicator.hidden = TRUE;
self.debugIndicator.backgroundColor = [UIColor lightGrayColor];
self.debugIndicator.font = [UIFont systemFontOfSize:(CGFloat)12.0];
self.debugIndicator.lineBreakMode=UILineBreakModeClip;
self.debugIndicator.lineBreakMode=NSLineBreakByClipping;
[self.interfaceView addSubview:self.debugIndicator];
[self showDebug:FALSE];
@ -2213,7 +2213,7 @@ int x_adb_get_keypad_y()
else
if (!bForceOnScreenKeyboard)
{
AppleKeyboardKey mappedKey = NSNotFound;
char c;
int i=0;
while( (c = s[i++]) != 0)
@ -2222,44 +2222,165 @@ int x_adb_get_keypad_y()
{
case 'w': // up
keypad_y = -32767;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_UP];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'e': // !verti
case 'z':
case 'e': // !verti : up released
keypad_y = 0;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_UP];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'z': // down released
keypad_y = 0;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_DOWN];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'x': // down
keypad_y = 32767;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_DOWN];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'a': // left
keypad_x = -32767;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_LEFT];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'q': // !hori
case 'c':
case 'q': // left released
keypad_x = 0;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_LEFT];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
case 'c': // right released
keypad_x = 0;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_RIGHT];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'd': // right
keypad_x = 32767;
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_DPAD_RIGHT];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'y': // button 1 pressed
add_event_key(0x37, 0);
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_1];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 't': // button 1 depressed
add_event_key(0x37, 1);
// add_event_key(0x37, 1);
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_1];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'h': // button 2 pressed
add_event_key(0x3a, 0);
// add_event_key(0x3a, 0);
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_2];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'r': // button 2 depressed
add_event_key(0x3a, 1);
// add_event_key(0x3a, 1);
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_2];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'g': //à faire sur un keydepressed
// display keyboard
printf("*** forcing on-screeen keyboard");
[self OnScreenKeyboard:TRUE];
case 'u': // button 3 pressed
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_3];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'f': // button 3 released
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_3];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'j': // button 4 pressed
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_4];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'n': // button 4 released
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_4];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'i': // button 5 pressed
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_5];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'm': // button 5 released
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_5];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'k': // button 6 pressed
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_5];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'p': // button 6 released
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_5];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
}
break;
case 'o':
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_6];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'g': //à faire sur un keydepressed : coin
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_6];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
} else {
// display keyboard
printf("*** forcing on-screeen keyboard");
[self OnScreenKeyboard:TRUE];
}
break;
case 'v': //à faire sur un keydepressed
// toggle la menu bar
[self setMenuBarVisibility:!bMenuBarVisibility];
case 'l':
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_7];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 0);
}
break;
case 'v': //à faire sur un keydepressed : start
mappedKey = [self.keyMapper getMappedKeyForControl:ICADE_BUTTON_7];
if ( mappedKey != NSNotFound ) {
add_event_key((int)mappedKey, 1);
} else {
// toggle la menu bar
[self setMenuBarVisibility:!bMenuBarVisibility];
}
break;
default:
break;

View File

@ -31,9 +31,17 @@
self.keyLabelAlt.text = @"";
}
self.mappedButtonLabel.text = @"";
KeyMapMappableButton mappedButton = [keyMapper getControlForMappedKey:[[self.keyDef objectAtIndex:KeyCapIndexCode] intValue]];
if ( mappedButton != NSNotFound ) {
self.mappedButtonLabel.text = [KeyMapper controlToDisplayName:mappedButton];
NSArray *mappedButtons = [keyMapper getControlsForMappedKey:[[self.keyDef objectAtIndex:KeyCapIndexCode] integerValue]];
if ( mappedButtons.count > 0 ) {
NSMutableString *displayText = [NSMutableString string];
int index = 0;
for (NSNumber *button in mappedButtons) {
if ( index++ > 0 ) {
[displayText appendString:@","];
}
[displayText appendString:[NSString stringWithFormat:@"%@",[KeyMapper controlToDisplayName:button.integerValue]]];
}
self.mappedButtonLabel.text = displayText;
}
}

View File

@ -113,7 +113,7 @@ typedef NS_ENUM(NSInteger, KeyCapIndex) {
-(void) mapKey:(AppleKeyboardKey)keyboardKey ToControl:(KeyMapMappableButton)button;
-(void) unmapKey:(AppleKeyboardKey)keyboardKey;
-(AppleKeyboardKey) getMappedKeyForControl:(KeyMapMappableButton)button;
-(KeyMapMappableButton) getControlForMappedKey:(AppleKeyboardKey) keyboardKey;
+(NSString*) controlToDisplayName:(KeyMapMappableButton)button;
-(NSArray*) getControlsForMappedKey:(AppleKeyboardKey) keyboardKey;
@end

View File

@ -32,7 +32,9 @@
-(NSMutableDictionary*) defaultMapping {
return [@{ [NSNumber numberWithInteger:MFI_BUTTON_X] : [NSNumber numberWithInteger:KEY_OPTION],
[NSNumber numberWithInteger:MFI_BUTTON_A] : [NSNumber numberWithInteger:KEY_APPLE]
[NSNumber numberWithInteger:MFI_BUTTON_A] : [NSNumber numberWithInteger:KEY_APPLE],
[NSNumber numberWithInteger:ICADE_BUTTON_1] : [NSNumber numberWithInteger:KEY_OPTION],
[NSNumber numberWithInteger:ICADE_BUTTON_2] : [NSNumber numberWithInteger:KEY_APPLE]
} mutableCopy];
}
@ -51,9 +53,9 @@
}
-(void) unmapKey:(AppleKeyboardKey)keyboardKey {
KeyMapMappableButton button = [self getControlForMappedKey:keyboardKey];
if ( button != NSNotFound ) {
[self.keyMapping removeObjectForKey:[NSNumber numberWithInteger:button]];
NSArray *mappedButtons = [self getControlsForMappedKey:keyboardKey];
for (NSNumber *button in mappedButtons) {
[self.keyMapping removeObjectForKey:button];
}
}
@ -67,14 +69,15 @@
}
}
-(KeyMapMappableButton) getControlForMappedKey:(AppleKeyboardKey) keyboardKey {
-(NSArray*) getControlsForMappedKey:(AppleKeyboardKey) keyboardKey {
NSMutableArray *foundControls = [NSMutableArray array];
for (NSNumber *buttonKey in self.keyMapping) {
NSNumber *mappedKey = [self.keyMapping objectForKey:buttonKey];
if ( mappedKey != nil && [mappedKey integerValue] == keyboardKey ) {
return [buttonKey integerValue];
[foundControls addObject:buttonKey];
}
}
return NSNotFound;
return foundControls;
}
+(NSString*) controlToDisplayName:(KeyMapMappableButton)button {
@ -139,6 +142,18 @@
case ICADE_BUTTON_8:
return @"i8";
break;
case ICADE_DPAD_UP:
return @"i⬆";
break;
case ICADE_DPAD_DOWN:
return @"i⬇";
break;
case ICADE_DPAD_LEFT:
return @"i⬅";
break;
case ICADE_DPAD_RIGHT:
return @"i➡";
break;
default:
return @"?";
break;