diff options
| author | SND\MaddTheSane_cp <SND\MaddTheSane_cp@e17a0e51-4ae3-4d35-97c3-1a29b211df97> | 2012-12-15 21:07:55 +0000 |
|---|---|---|
| committer | SND\MaddTheSane_cp <SND\MaddTheSane_cp@e17a0e51-4ae3-4d35-97c3-1a29b211df97> | 2012-12-15 21:07:55 +0000 |
| commit | be3d963074ee8adf5da0371761e090488b3681d3 (patch) | |
| tree | 24ac0f45405136899584fe3ef2c022c148bfb352 /macosx/HotkeyController.m | |
| parent | c0cba1977a8cac317af37c3004c53417fbce31bc (diff) | |
| download | pcsxr-be3d963074ee8adf5da0371761e090488b3681d3.tar.gz | |
Mac Patch 13437.
git-svn-id: https://pcsxr.svn.codeplex.com/svn/pcsxr@81770 e17a0e51-4ae3-4d35-97c3-1a29b211df97
Diffstat (limited to 'macosx/HotkeyController.m')
| -rw-r--r-- | macosx/HotkeyController.m | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/macosx/HotkeyController.m b/macosx/HotkeyController.m new file mode 100644 index 00000000..65ec1055 --- /dev/null +++ b/macosx/HotkeyController.m @@ -0,0 +1,206 @@ +/** + * HotkeyController.m + * Pcsxr + * + * Created by Nicolas Pepin-Perreault on 12-12-10. + * + * Adapted from the Cocoa port of DeSMuMe + */ + +#import "HotkeyController.h" + +#define INPUT_HOLD_TIME 0.1 + +@implementation HotkeyController + +@synthesize configInput; + +- (void)initialize +{ + lastConfigButton = nil; + configInput = 0; + hotkeysList = [[NSMutableDictionary alloc] initWithCapacity:16]; + keyNameTable = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"KeyNames" ofType:@"plist"]]; + hotkeyOutlets = [[NSMutableDictionary alloc] initWithCapacity:8]; + + [self mapOutletToIdentifier:FastForward forIdentifier:@"FastForward"]; + [self mapOutletToIdentifier:SaveState forIdentifier:@"SaveState"]; + [self mapOutletToIdentifier:LoadState forIdentifier:@"LoadState"]; + [self mapOutletToIdentifier:NextState forIdentifier:@"NextState"]; + [self mapOutletToIdentifier:PrevState forIdentifier:@"PrevState"]; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + [hotkeysList release]; + [keyNameTable release]; + [hotkeyOutlets release]; + + [super dealloc]; +} + +- (void)mapOutletToIdentifier:(id)outlet forIdentifier:(NSString*)identifier +{ + [hotkeyOutlets setObject:outlet forKey:identifier]; + [self setHotkeyDisplay:identifier]; +} + +- (void)setHotkeyDisplay:(NSString*)keyIdent +{ + NSString *label = [self parseMappingDisplayString:keyIdent]; + NSTextField *displayField = [hotkeyOutlets objectForKey:keyIdent]; + + if(displayField) { + [[displayField cell] setStringValue:label]; + } +} + +- (void)mouseDown:(NSEvent *)theEvent +{ + BOOL isHandled = [self handleMouseDown:theEvent]; + if (!isHandled) + { + [super mouseDown:theEvent]; + } +} + +- (void)mouseDragged:(NSEvent *)theEvent +{ + [self mouseDown:theEvent]; +} + +- (void)rightMouseDown:(NSEvent *)theEvent +{ + BOOL isHandled = [self handleMouseDown:theEvent]; + if (!isHandled) + { + [super rightMouseDown:theEvent]; + } +} + +- (void)rightMouseDragged:(NSEvent *)theEvent +{ + [self rightMouseDown:theEvent]; +} + +- (void)otherMouseDown:(NSEvent *)theEvent +{ + BOOL isHandled = [self handleMouseDown:theEvent]; + if (!isHandled) + { + [super otherMouseDown:theEvent]; + } +} + +- (void)otherMouseDragged:(NSEvent *)theEvent +{ + [self otherMouseDown:theEvent]; +} + +- (BOOL) handleMouseDown:(NSEvent *)mouseEvent +{ + if (configInput != 0) + { + [self hotkeyCancel]; + } + + return YES; +} + + +- (void)keyDown:(NSEvent *)theEvent +{ + NSString *keyCode = [NSString stringWithFormat:@"%d", [theEvent keyCode]]; + NSString *keyLabel = (NSString *)[keyNameTable valueForKey:keyCode]; + + if (configInput != 0) + { + // Save input + NSString *ident = [lastConfigButton identifier]; + [self saveHotkey:ident device:@"NSEventKeyboard" deviceLabel:@"Keyboard" code:keyCode label:keyLabel]; + [self setHotkeyDisplay:ident]; + [self hotkeyCancel]; + } +} + +- (void)saveHotkey:(NSString*)keyIdent device:(NSString*)device deviceLabel:(NSString*)deviceLabel code:(NSString*)keyCode label:(NSString*)keyLabel +{ + NSMutableDictionary *tempUserMappings = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"HotkeyBindings"]]; + [tempUserMappings setValue:[[NSDictionary alloc] initWithObjectsAndKeys: + device, @"device", + deviceLabel, @"deviceName", + keyCode, @"keyCode", + keyLabel, @"keyLabel", + nil] forKey:keyIdent]; + [[NSUserDefaults standardUserDefaults] setValue:tempUserMappings forKey:@"HotkeyBindings"]; +} + +- (NSString *) parseMappingDisplayString:(NSString *)keyString +{ + NSDictionary *userMappings = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"HotkeyBindings"]; + NSDictionary *binding = (NSDictionary *)[userMappings valueForKey:keyString]; + + NSString *displayString = @""; + if(binding) { + NSString *deviceLabel = (NSString *)[binding valueForKey:@"deviceName"]; + NSString *keyLabel = (NSString *)[binding valueForKey:@"keyLabel"]; + + displayString = [NSString stringWithString:deviceLabel]; + displayString = [displayString stringByAppendingString:@": "]; + displayString = [displayString stringByAppendingString:keyLabel]; + } + + return displayString; +} + +- (IBAction) hotkeySet:(id)sender +{ + NSButton *theButton = (NSButton *)sender; + + if (configInput && lastConfigButton != theButton) + { + [lastConfigButton setState:NSOffState]; + } + + if ([theButton state] == NSOnState) + { + lastConfigButton = theButton; + [hotkeysList removeAllObjects]; + configInput = [theButton tag]; + } + else + { + [self hotkeyCancel]; + } + +} + +- (void) hotkeyCancel +{ + if (lastConfigButton != nil) + { + [lastConfigButton setState:NSOffState]; + lastConfigButton = nil; + } + + configInput = 0; +} + +- (BOOL)acceptsFirstResponder +{ + return YES; +} + +- (BOOL)becomeFirstResponder +{ + return YES; +} + +- (BOOL)resignFirstResponder +{ + return YES; +} + +@end |
