Skip to content

Commit

Permalink
Moved mfi controller handling to base view controller class to suppor…
Browse files Browse the repository at this point in the history
…t both iPhone and iPad; fixed showing mapped control on the keyboard key; fixed binding controls after resetting mappings; fixed setting key remapping handling delegate when showing keyboard
  • Loading branch information
Yoshi Sugawara committed Apr 23, 2016
1 parent 04873db commit fd9a884
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 111 deletions.
10 changes: 4 additions & 6 deletions dospad.xcodeproj/project.pbxproj
Expand Up @@ -3195,11 +3195,6 @@
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0460;
TargetAttributes = {
E7BE1297128BF31A0046990B = {
DevelopmentTeam = 4C8YZND49H;
};
};
};
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "dospad" */;
compatibilityVersion = "Xcode 3.2";
Expand Down Expand Up @@ -4006,6 +4001,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
Expand Down Expand Up @@ -4041,6 +4037,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
Expand Down Expand Up @@ -4326,6 +4323,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
Expand Down Expand Up @@ -4398,7 +4396,7 @@
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
Expand Down
7 changes: 6 additions & 1 deletion dospad/Shared/DOSPadBaseViewController.h
Expand Up @@ -38,7 +38,7 @@ typedef enum {
} InputSourceType;

@interface DOSPadBaseViewController : UIViewController
<SDL_uikitopenglview_delegate,MouseHoldDelegate>
<SDL_uikitopenglview_delegate,MouseHoldDelegate,KeyDelegate,UIAlertViewDelegate>
{
NSString *configPath;
BOOL autoExit;
Expand All @@ -54,6 +54,11 @@ typedef enum {
UIButton *btnMouseLeft;
UIButton *btnMouseRight;
PianoKeyboard *piano;

BOOL remapControlsModeOn;
UILabel *remappingOnLabel;
UIButton *resetMappingsButton;

}

@property (nonatomic, strong) NSString *configPath;
Expand Down
122 changes: 122 additions & 0 deletions dospad/Shared/DOSPadBaseViewController.m
Expand Up @@ -26,6 +26,16 @@

extern int SDL_SendKeyboardKey(int index, Uint8 state, SDL_scancode scancode);

@interface DOSPadBaseViewController()

@property(nonatomic, strong) KeyMapper *keyMapper;
@property(nonatomic, strong) UIAlertView *keyMapperAlertView;
@property(nonatomic, strong) MfiGameControllerHandler *mfiHandler;
@property(nonatomic, strong) MfiControllerInputHandler *mfiInputHandler;

@end


@implementation DOSPadBaseViewController
@synthesize autoExit;
@synthesize configPath;
Expand Down Expand Up @@ -140,6 +150,44 @@ - (void)viewDidLoad
vk.alpha = 0;
[self.view addSubview:vk];
*/

//---------------------------------------------------
// Remap controls
//---------------------------------------------------

remappingOnLabel = [[UILabel alloc] initWithFrame:CGRectZero];
remappingOnLabel.text = @"Remapping Controls ON";
remappingOnLabel.textColor = [UIColor redColor];
remappingOnLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:remappingOnLabel];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:remappingOnLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:remappingOnLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.75f constant:0.0f]];
remappingOnLabel.hidden = YES;

resetMappingsButton = [[UIButton alloc] initWithFrame:CGRectZero];
[resetMappingsButton setTitle:@"Reset Mappings" forState:UIControlStateNormal];
[resetMappingsButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[resetMappingsButton setTintColor:[UIColor redColor]];
resetMappingsButton.translatesAutoresizingMaskIntoConstraints = NO;
[resetMappingsButton addTarget:self action:@selector(resetMappingsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:resetMappingsButton];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:resetMappingsButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:resetMappingsButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:0.5f constant:0.0f]];
resetMappingsButton.layer.borderWidth = 1.0f;
resetMappingsButton.layer.borderColor = [[UIColor redColor] CGColor];
resetMappingsButton.hidden = YES;

self.keyMapper = [[KeyMapper alloc] init];
[self.keyMapper loadFromDefaults];
self.mfiHandler = [[MfiGameControllerHandler alloc] init];
self.mfiInputHandler = [[MfiControllerInputHandler alloc] init];
self.mfiInputHandler.keyMapper = self.keyMapper;
[self.mfiHandler discoverController:^(GCController *gameController) {
[self.mfiInputHandler setupControllerInputsForController:gameController];
} disconnectedCallback:^{

}];

}


Expand Down Expand Up @@ -504,4 +552,78 @@ - (void)removeInputSource:(InputSourceType)type
}
}

-(void) remapControlsButtonTapped:(id)sender {
remapControlsModeOn = !remapControlsModeOn;
remappingOnLabel.hidden = !remapControlsModeOn;
resetMappingsButton.hidden = !remapControlsModeOn;

if ( remapControlsModeOn ) {
kbd.externKeyDelegate = self;
} else {
kbd.externKeyDelegate = nil;
}
}

-(void) refreshKeyMappingsInViews {
for (KeyView *keyView in kbd.keys) {
NSArray *mappedButtons = [self.keyMapper getControlsForMappedKey:keyView.code];
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]]];
}
keyView.mappedKey = displayText;
} else {
keyView.mappedKey = @"";
}
[keyView setNeedsDisplay];
}
}

-(void) resetMappingsButtonTapped:(id)sender {
[self.keyMapper resetToDefaults];
[self refreshKeyMappingsInViews];
[self.keyMapper saveKeyMapping];
[self.mfiInputHandler setupControllerInputsForController:[[GCController controllers] firstObject]];
}

# pragma - mark KeyDelegate
-(void)onKeyDown:(KeyView*)k {
}

-(void)onKeyUp:(KeyView*)k {
// show alert view
self.keyMapperAlertView = [[UIAlertView alloc] initWithTitle:@"Remap Key" message:[NSString stringWithFormat:@"Press a button to map the [%@] key",k.title] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Unbind",nil];
self.keyMapperAlertView.tag = k.code;
[self.keyMapperAlertView show];
[self.mfiInputHandler startRemappingControlsForMfiControllerForKey:k.code];

__weak __typeof(self) weakSelf = self;

self.mfiInputHandler.dismiss = ^{
[weakSelf.keyMapperAlertView dismissWithClickedButtonIndex:0 animated:YES];

[weakSelf.mfiInputHandler setupControllerInputsForController:[[GCController controllers] firstObject]];
[weakSelf.keyMapper saveKeyMapping];
[weakSelf refreshKeyMappingsInViews];
};

}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if ( buttonIndex == 1 ) {
SDL_scancode mappedKey = alertView.tag;
[self.keyMapper unmapKey:mappedKey];
[self.keyMapper saveKeyMapping];
[self refreshKeyMappingsInViews];
[self.mfiInputHandler setupControllerInputsForController:[[GCController controllers] firstObject]];
}
}


@end
9 changes: 9 additions & 0 deletions dospad/Shared/KeyView.m
Expand Up @@ -211,6 +211,15 @@ - (void)drawRectNew:(CGRect)rect {
offsetY = (self.bounds.size.height/2 - size.height)/2;
[altTitle drawInRect:CGRectMake(offsetX,offsetY,size.width,size.height) withFont:font];
}

if ( self.mappedKey != nil && [self.mappedKey length] > 0 ) {
UIFont *font = [UIFont systemFontOfSize:isIPad?12:10];
CGSize size = [self.mappedKey sizeWithFont:font];
float offsetX = self.bounds.size.width - size.width;
float offsetY = self.bounds.size.height - size.height;
[self.mappedKey drawInRect:CGRectMake(offsetX, offsetY, size.width, size.height) withFont:font];
}

}

- (void)drawRect:(CGRect)rect
Expand Down
5 changes: 5 additions & 0 deletions dospad/Shared/MfiControllerInputHandler.m
Expand Up @@ -171,6 +171,11 @@ -(void) setupControllerInputsForController:(GCController*)controller {
controller.extendedGamepad.leftThumbstick.valueChangedHandler = joystickHandler;
}

// reset all button handlers
for (GCControllerButtonInput *buttonInput in @[buttonX,buttonY,buttonA,buttonB,buttonRS,buttonRT,buttonLS,buttonLT]) {
buttonInput.valueChangedHandler = nil;
}

//
// mapped keys
NSInteger mappedKey = [self.keyMapper getMappedKeyForControl:MFI_BUTTON_X];
Expand Down
39 changes: 39 additions & 0 deletions dospad/iPad/DosPadViewController.m
Expand Up @@ -22,6 +22,9 @@
#include <string.h>
#import "Common.h"
#import "CommandListView.h"
#import "KeyMapper.h"
#import "MfiGameControllerHandler.h"
#import "MfiControllerInputHandler.h"

#include "SDL.h"

Expand All @@ -41,6 +44,23 @@
};
#define NUM_BUTTON_INFO (sizeof(toggleButtonInfo)/sizeof(toggleButtonInfo[0]))

@interface DOSPadBaseViewController()

-(void) remapControlsButtonTapped:(id)sender;
-(void) refreshKeyMappingsInViews;
-(void) resetMappingsButtonTapped:(id)sender;

@end

@interface DosPadViewController()

@property(nonatomic, strong) KeyMapper *keyMapper;
@property(nonatomic, strong) UIAlertView *keyMapperAlertView;
@property(nonatomic, strong) MfiGameControllerHandler *mfiHandler;
@property(nonatomic, strong) MfiControllerInputHandler *mfiInputHandler;

@end

@implementation DosPadViewController

- (BOOL)isFullscreen
Expand Down Expand Up @@ -237,6 +257,12 @@ - (void)refreshFullscreenPanel
[btnOpt setImage:[UIImage imageNamed:@"options.png"] forState:UIControlStateNormal];
[btnOpt addTarget:self action:@selector(showOption) forControlEvents:UIControlEventTouchUpInside];
[items addObject:btnOpt];

// Remap controls button
UIButton *btnRemap = [[UIButton alloc] initWithFrame:CGRectMake(0,0,72,36)];
[btnRemap setTitle:@"R" forState:UIControlStateNormal];
[btnRemap addTarget:self action:@selector(remapControlsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[items addObject:btnRemap];

[fullscreenPanel setItems:items];
}
Expand Down Expand Up @@ -294,6 +320,7 @@ - (void)createPCKeyboard
frame:CGRectMake(0,self.view.bounds.size.height-250,1024,250)];
kbd.alpha = [self floatAlpha];
[self.view addSubview:kbd];
[self refreshKeyMappingsInViews];
CGPoint ptOld = kbd.center;
kbd.center = CGPointMake(ptOld.x, ptOld.y+kbd.frame.size.height);
[UIView beginAnimations:nil context:NULL];
Expand Down Expand Up @@ -594,5 +621,17 @@ - (void)showCommandList
[v show];
}

-(void) remapControlsButtonTapped:(id)sender {
[super remapControlsButtonTapped:sender];
}

-(void) refreshKeyMappingsInViews {
[super refreshKeyMappingsInViews];
}

-(void) resetMappingsButtonTapped:(id)sender {
[super resetMappingsButtonTapped:sender];
}


@end
3 changes: 0 additions & 3 deletions dospad/iPhone/DosPadViewController_iPhone.h
Expand Up @@ -54,9 +54,6 @@
FloatPanel *fullscreenPanel;

BOOL useOriginalScreenSize;
BOOL remapControlsModeOn;
UILabel *remappingOnLabel;
UIButton *resetMappingsButton;
}

- (void)refreshFullscreenPanel;
Expand Down

0 comments on commit fd9a884

Please sign in to comment.