From 9480992fac2c0b71851db8e4906e77e2128d8919 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 28 Feb 2013 16:16:29 +0100 Subject: [PATCH] Clean up TargetController formatting and public API. --- TargetController.h | 42 ++++++------ TargetController.m | 156 ++++++++++++++++++++++----------------------- 2 files changed, 98 insertions(+), 100 deletions(-) diff --git a/TargetController.h b/TargetController.h index 4606784..7e24cc4 100644 --- a/TargetController.h +++ b/TargetController.h @@ -13,30 +13,28 @@ @class TargetMouseMove; @interface TargetController : NSObject { - IBOutlet KeyInputTextView* keyInput; - IBOutlet NSMatrix* radioButtons; - IBOutlet NSSegmentedControl* mouseDirSelect; - IBOutlet NSSegmentedControl* mouseBtnSelect; - IBOutlet NSSegmentedControl* scrollDirSelect; - IBOutlet NSTextField* title; - IBOutlet NSPopUpButton* configPopup; - IBOutlet ConfigsController* configsController; - IBOutlet JoystickController* joystickController; - id currentJsaction; + IBOutlet KeyInputTextView *keyInput; + IBOutlet NSMatrix *radioButtons; + IBOutlet NSSegmentedControl *mouseDirSelect; + IBOutlet NSSegmentedControl *mouseBtnSelect; + IBOutlet NSSegmentedControl *scrollDirSelect; + IBOutlet NSTextField *title; + IBOutlet NSPopUpButton *configPopup; + IBOutlet ConfigsController *configsController; + IBOutlet JoystickController *joystickController; } --(void) keyChanged; --(void) load; --(void) commit; --(void) reset; --(void) refreshConfigsPreservingSelection: (BOOL) preserve; --(IBAction)configChosen:(id)sender; --(IBAction)radioChanged:(id)sender; --(IBAction)mdirChanged:(id)sender; --(IBAction)mbtnChanged:(id)sender; --(IBAction)sdirChanged:(id)sender; --(void) focusKey; - @property (assign) BOOL enabled; +- (void)keyChanged; +- (void)load; +- (void)reset; +- (void)refreshConfigsPreservingSelection:(BOOL)preserve; +- (IBAction)configChosen:(id)sender; +- (IBAction)radioChanged:(id)sender; +- (IBAction)mdirChanged:(id)sender; +- (IBAction)mbtnChanged:(id)sender; +- (IBAction)sdirChanged:(id)sender; +- (void)focusKey; + @end diff --git a/TargetController.m b/TargetController.m index 0119980..b5c57b4 100644 --- a/TargetController.m +++ b/TargetController.m @@ -22,29 +22,24 @@ @implementation TargetController -- (void)keyChanged { - [radioButtons setState:1 atRow:1 column:0 ]; - [self commit]; -} - - (IBAction)radioChanged:(id)sender { NSInteger row, col; [radioButtons getRow:&row column:&col ofCell:sender]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; - + [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; + if (row != 1) keyInput.vk = -1; - + if (row != 2) [configPopup selectItemAtIndex:-1]; else if (!configPopup.selectedItem) [configPopup selectItemAtIndex:0]; - + if (row != 3) mouseDirSelect.selectedSegment = -1; else if (mouseDirSelect.selectedSegment == -1) mouseDirSelect.selectedSegment = 0; - + if (row != 4) mouseBtnSelect.selectedSegment = -1; else if (mouseBtnSelect.selectedSegment == -1) @@ -55,47 +50,61 @@ else if (scrollDirSelect.selectedSegment == -1) scrollDirSelect.selectedSegment = 0; - [self commit]; + [self commit]; +} + +- (void)keyChanged { + [radioButtons setState:1 atRow:1 column:0]; + [self commit]; +} + +- (void)configChosen:(id)sender { + [radioButtons setState:1 atRow:2 column:0]; + [self commit]; } -- (IBAction)mdirChanged:(id)sender { +- (void)mdirChanged:(id)sender { [radioButtons setState:1 atRow:3 column:0]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; - [self commit]; + [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; + [self commit]; } -- (IBAction)mbtnChanged:(id)sender { +- (void)mbtnChanged:(id)sender { [radioButtons setState:1 atRow:4 column:0]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; - [self commit]; + [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; + [self commit]; } -- (IBAction)sdirChanged:(id)sender { +- (void)sdirChanged:(id)sender { [radioButtons setState:1 atRow:5 column:0]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; - [self commit]; + [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; + [self commit]; +} + +- (Target *)currentTarget { + return configsController.currentConfig[joystickController.selectedAction]; } - (Target *)makeTarget { - switch (radioButtons.selectedRow) { - case 0: - return nil; - case 1: - if (keyInput.hasKey) { - TargetKeyboard* k = [[TargetKeyboard alloc] init]; + switch (radioButtons.selectedRow) { + case 0: + return nil; + case 1: + if (keyInput.hasKey) { + TargetKeyboard *k = [[TargetKeyboard alloc] init]; k.vk = keyInput.vk; - return k; - } else { + return k; + } else { return nil; } - break; - case 2: { - TargetConfig *c = [[TargetConfig alloc] init]; + break; + case 2: { + TargetConfig *c = [[TargetConfig alloc] init]; if (!configPopup.selectedItem) [configPopup selectItemAtIndex:0]; c.config = configsController.configs[configPopup.indexOfSelectedItem]; - return c; - } + return c; + } case 3: { TargetMouseMove *mm = [[TargetMouseMove alloc] init]; mm.dir = mouseDirSelect.selectedSegment; @@ -117,12 +126,7 @@ } default: return nil; - } -} - --(void)configChosen:(id)sender { - [radioButtons setState:1 atRow:2 column:0]; - [self commit]; + } } - (void)commit { @@ -130,57 +134,53 @@ } - (void)reset { - [keyInput clear]; - [radioButtons setState:1 atRow:0 column:0]; - [self refreshConfigsPreservingSelection:NO]; + [keyInput clear]; + [radioButtons setState:1 atRow:0 column:0]; + [self refreshConfigsPreservingSelection:NO]; } - (BOOL)enabled { - return [radioButtons isEnabled]; + return [radioButtons isEnabled]; } - (void)setEnabled:(BOOL)enabled { - [radioButtons setEnabled:enabled]; - [keyInput setEnabled:enabled]; - [configPopup setEnabled:enabled]; + [radioButtons setEnabled:enabled]; + [keyInput setEnabled:enabled]; + [configPopup setEnabled:enabled]; [mouseDirSelect setEnabled:enabled]; [mouseBtnSelect setEnabled:enabled]; [scrollDirSelect setEnabled:enabled]; } --(void) load { - id jsaction = joystickController.selectedAction; - currentJsaction = jsaction; - if(!jsaction) { + +- (void)load { + JSAction *act = joystickController.selectedAction; + if (!act) { self.enabled = NO; title.stringValue = @""; - return; - } else { + return; + } else { self.enabled = YES; - } - Target *target = configsController.currentConfig[jsaction]; - - id act = jsaction; - NSString* actFullName = [act name]; - while([act base]) { - act = [act base]; - actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", [act name], actFullName]; - } + } + + Target *target = [self currentTarget]; + NSString *actFullName = act.name; + for (JSAction *cur = act.base; cur; cur = cur.base) { + actFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, actFullName]; + } title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", configsController.currentConfig.name, actFullName]; - - if(!target) { - [radioButtons setState:1 atRow:0 column:0]; - } else if([target isKindOfClass:[TargetKeyboard class]]) { - [radioButtons setState:1 atRow:1 column:0]; + + if ([target isKindOfClass:[TargetKeyboard class]]) { + [radioButtons setState:1 atRow:1 column:0]; keyInput.vk = [(TargetKeyboard*)target vk]; - } else if([target isKindOfClass:[TargetConfig class]]) { - [radioButtons setState:1 atRow:2 column:0]; - [configPopup selectItemAtIndex:[configsController.configs + } else if ([target isKindOfClass:[TargetConfig class]]) { + [radioButtons setState:1 atRow:2 column:0]; + [configPopup selectItemAtIndex:[configsController.configs indexOfObject:[(TargetConfig *)target config]]]; } else if ([target isKindOfClass:[TargetMouseMove class]]) { [radioButtons setState:1 atRow:3 column:0]; [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target dir]]; - } + } else if ([target isKindOfClass:[TargetMouseBtn class]]) { [radioButtons setState:1 atRow:4 column:0]; mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target which] == kCGMouseButtonLeft ? 0 : 1; @@ -192,12 +192,12 @@ else if ([target isKindOfClass:[TargetToggleMouseScope class]]) { [radioButtons setState:1 atRow:6 column:0]; } else { - NSLog(@"Unknown target type %@.", target.description); - } + [radioButtons setState:1 atRow:0 column:0]; + } } --(void) focusKey { - Target *currentTarget = configsController.currentConfig[currentJsaction]; +- (void)focusKey { + Target *currentTarget = [self currentTarget]; if (!currentTarget || [currentTarget isKindOfClass:[TargetKeyboard class]]) [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:keyInput]; else @@ -205,10 +205,10 @@ } - (void)refreshConfigsPreservingSelection:(BOOL)preserve { - int initialIndex = [configPopup indexOfSelectedItem]; - [configPopup removeAllItems]; - for (Config *config in configsController.configs) - [configPopup addItemWithTitle:config.name]; + int initialIndex = [configPopup indexOfSelectedItem]; + [configPopup removeAllItems]; + for (Config *config in configsController.configs) + [configPopup addItemWithTitle:config.name]; [configPopup selectItemAtIndex:preserve ? initialIndex : -1]; } -- 2.30.2