X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=TargetController.m;h=1da2c99a6e7157f038ac8d13a5eb6fa8a6cacab3;hp=b5c57b46cf03299929fb56d9a475f7ca72639570;hb=e2a4d830dd9817f6a515a3b1b6aa152d3bb98c2b;hpb=9480992fac2c0b71851db8e4906e77e2128d8919 diff --git a/TargetController.m b/TargetController.m index b5c57b4..1da2c99 100644 --- a/TargetController.m +++ b/TargetController.m @@ -7,11 +7,11 @@ #import "TargetController.h" -#import "ConfigsController.h" -#import "Config.h" -#import "JSAction.h" -#import "JoystickController.h" -#import "KeyInputTextView.h" +#import "NJMappingsController.h" +#import "NJMapping.h" +#import "NJInput.h" +#import "NJInputController.h" +#import "NJKeyInputField.h" #import "TargetConfig.h" #import "TargetController.h" #import "TargetKeyboard.h" @@ -22,67 +22,83 @@ @implementation TargetController -- (IBAction)radioChanged:(id)sender { - NSInteger row, col; - [radioButtons getRow:&row column:&col ofCell:sender]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; +- (void)cleanUpInterface { + NSInteger row = radioButtons.selectedRow; - if (row != 1) - keyInput.vk = -1; + if (row != 1) { + keyInput.keyCode = -1; + [keyInput resignIfFirstResponder]; + } - if (row != 2) - [configPopup selectItemAtIndex:-1]; - else if (!configPopup.selectedItem) - [configPopup selectItemAtIndex:0]; + if (row != 2) { + [mappingPopup selectItemAtIndex:-1]; + [mappingPopup resignIfFirstResponder]; + } else if (!mappingPopup.selectedItem) + [mappingPopup selectItemAtIndex:0]; - if (row != 3) + if (row != 3) { mouseDirSelect.selectedSegment = -1; - else if (mouseDirSelect.selectedSegment == -1) + [mouseDirSelect resignIfFirstResponder]; + } else if (mouseDirSelect.selectedSegment == -1) mouseDirSelect.selectedSegment = 0; - if (row != 4) + if (row != 4) { mouseBtnSelect.selectedSegment = -1; - else if (mouseBtnSelect.selectedSegment == -1) + [mouseBtnSelect resignIfFirstResponder]; + } else if (mouseBtnSelect.selectedSegment == -1) mouseBtnSelect.selectedSegment = 0; - if (row != 5) + if (row != 5) { scrollDirSelect.selectedSegment = -1; - else if (scrollDirSelect.selectedSegment == -1) - scrollDirSelect.selectedSegment = 0; - + [scrollDirSelect resignIfFirstResponder]; + } else if (scrollDirSelect.selectedSegment == -1) + scrollDirSelect.selectedSegment = 0; +} + +- (IBAction)radioChanged:(NSView *)sender { + [sender.window makeFirstResponder:sender]; + if (radioButtons.selectedRow == 1) + [keyInput.window makeFirstResponder:keyInput]; [self commit]; } -- (void)keyChanged { - [radioButtons setState:1 atRow:1 column:0]; +- (void)keyInputField:(NJKeyInputField *)keyInput didChangeKey:(CGKeyCode)keyCode { + [radioButtons selectCellAtRow:1 column:0]; + [radioButtons.window makeFirstResponder:radioButtons]; [self commit]; } -- (void)configChosen:(id)sender { - [radioButtons setState:1 atRow:2 column:0]; +- (void)keyInputFieldDidClear:(NJKeyInputField *)keyInput { + [radioButtons selectCellAtRow:0 column:0]; [self commit]; } -- (void)mdirChanged:(id)sender { - [radioButtons setState:1 atRow:3 column:0]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; +- (void)mappingChosen:(id)sender { + [radioButtons selectCellAtRow:2 column:0]; + [mappingPopup.window makeFirstResponder:mappingPopup]; [self commit]; } -- (void)mbtnChanged:(id)sender { - [radioButtons setState:1 atRow:4 column:0]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; +- (void)mdirChanged:(NSView *)sender { + [radioButtons selectCellAtRow:3 column:0]; + [sender.window makeFirstResponder:sender]; [self commit]; } -- (void)sdirChanged:(id)sender { - [radioButtons setState:1 atRow:5 column:0]; - [[NSApplication sharedApplication].mainWindow makeFirstResponder:sender]; +- (void)mbtnChanged:(NSView *)sender { + [radioButtons selectCellAtRow:4 column:0]; + [sender.window makeFirstResponder:sender]; + [self commit]; +} + +- (void)sdirChanged:(NSView *)sender { + [radioButtons selectCellAtRow:5 column:0]; + [sender.window makeFirstResponder:sender]; [self commit]; } - (Target *)currentTarget { - return configsController.currentConfig[joystickController.selectedAction]; + return mappingsController.currentMapping[joystickController.selectedInput]; } - (Target *)makeTarget { @@ -90,9 +106,9 @@ case 0: return nil; case 1: - if (keyInput.hasKey) { + if (keyInput.hasKeyCode) { TargetKeyboard *k = [[TargetKeyboard alloc] init]; - k.vk = keyInput.vk; + k.vk = keyInput.keyCode; return k; } else { return nil; @@ -100,24 +116,22 @@ break; case 2: { TargetConfig *c = [[TargetConfig alloc] init]; - if (!configPopup.selectedItem) - [configPopup selectItemAtIndex:0]; - c.config = configsController.configs[configPopup.indexOfSelectedItem]; + c.mapping = mappingsController.mappings[mappingPopup.indexOfSelectedItem]; return c; } case 3: { TargetMouseMove *mm = [[TargetMouseMove alloc] init]; - mm.dir = mouseDirSelect.selectedSegment; + mm.axis = mouseDirSelect.selectedSegment; return mm; } case 4: { TargetMouseBtn *mb = [[TargetMouseBtn alloc] init]; - mb.which = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight; + mb.button = mouseBtnSelect.selectedSegment == 0 ? kCGMouseButtonLeft : kCGMouseButtonRight; return mb; } case 5: { TargetMouseScroll *ms = [[TargetMouseScroll alloc] init]; - ms.howMuch = scrollDirSelect.selectedSegment ? 1 : -1; + ms.amount = scrollDirSelect.selectedSegment ? 1 : -1; return ms; } case 6: { @@ -130,13 +144,9 @@ } - (void)commit { - configsController.currentConfig[joystickController.selectedAction] = [self makeTarget]; -} - -- (void)reset { - [keyInput clear]; - [radioButtons setState:1 atRow:0 column:0]; - [self refreshConfigsPreservingSelection:NO]; + [self cleanUpInterface]; + mappingsController.currentMapping[joystickController.selectedInput] = [self makeTarget]; + [mappingsController save]; } - (BOOL)enabled { @@ -146,70 +156,80 @@ - (void)setEnabled:(BOOL)enabled { [radioButtons setEnabled:enabled]; [keyInput setEnabled:enabled]; - [configPopup setEnabled:enabled]; + [mappingPopup setEnabled:enabled]; [mouseDirSelect setEnabled:enabled]; [mouseBtnSelect setEnabled:enabled]; [scrollDirSelect setEnabled:enabled]; } -- (void)load { - JSAction *act = joystickController.selectedAction; - if (!act) { +- (void)loadTarget:(Target *)target forInput:(NJInput *)input { + if (!input) { self.enabled = NO; title.stringValue = @""; - return; } else { self.enabled = YES; + NSString *inpFullName = input.name; + for (id cur = input.base; cur; cur = cur.base) { + inpFullName = [[NSString alloc] initWithFormat:@"%@ > %@", cur.name, inpFullName]; + } + title.stringValue = [[NSString alloc] initWithFormat:@"%@ > %@", mappingsController.currentMapping.name, inpFullName]; } - - 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 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 - indexOfObject:[(TargetConfig *)target config]]]; + + if ([target isKindOfClass:TargetKeyboard.class]) { + [radioButtons selectCellAtRow:1 column:0]; + keyInput.keyCode = [(TargetKeyboard*)target vk]; + } else if ([target isKindOfClass:TargetConfig.class]) { + [radioButtons selectCellAtRow:2 column:0]; + NSUInteger idx = [mappingsController.mappings + indexOfObject:[(TargetConfig *)target mapping]]; + if (idx == NSNotFound) { + [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0]; + [mappingPopup selectItemAtIndex:-1]; + } else + [mappingPopup selectItemAtIndex:idx]; } - else if ([target isKindOfClass:[TargetMouseMove class]]) { - [radioButtons setState:1 atRow:3 column:0]; - [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target dir]]; + else if ([target isKindOfClass:TargetMouseMove.class]) { + [radioButtons selectCellAtRow:3 column:0]; + [mouseDirSelect setSelectedSegment:[(TargetMouseMove *)target axis]]; } - else if ([target isKindOfClass:[TargetMouseBtn class]]) { - [radioButtons setState:1 atRow:4 column:0]; - mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target which] == kCGMouseButtonLeft ? 0 : 1; + else if ([target isKindOfClass:TargetMouseBtn.class]) { + [radioButtons selectCellAtRow:4 column:0]; + mouseBtnSelect.selectedSegment = [(TargetMouseBtn *)target button] == kCGMouseButtonLeft ? 0 : 1; } - else if ([target isKindOfClass:[TargetMouseScroll class]]) { - [radioButtons setState:1 atRow:5 column:0]; - scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target howMuch] > 0; + else if ([target isKindOfClass:TargetMouseScroll.class]) { + [radioButtons selectCellAtRow:5 column:0]; + scrollDirSelect.selectedSegment = [(TargetMouseScroll *)target amount] > 0; } - else if ([target isKindOfClass:[TargetToggleMouseScope class]]) { - [radioButtons setState:1 atRow:6 column:0]; + else if ([target isKindOfClass:TargetToggleMouseScope.class]) { + [radioButtons selectCellAtRow:6 column:0]; } else { - [radioButtons setState:1 atRow:0 column:0]; + [radioButtons selectCellAtRow:self.enabled ? 0 : -1 column:0]; } + [self cleanUpInterface]; +} + +- (void)loadCurrent { + [self loadTarget:self.currentTarget forInput:joystickController.selectedInput]; } - (void)focusKey { - Target *currentTarget = [self currentTarget]; - if (!currentTarget || [currentTarget isKindOfClass:[TargetKeyboard class]]) - [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:keyInput]; + if (radioButtons.selectedRow <= 1) + [keyInput.window makeFirstResponder:keyInput]; else - [keyInput resignFirstResponder]; + [keyInput resignIfFirstResponder]; } -- (void)refreshConfigsPreservingSelection:(BOOL)preserve { - int initialIndex = [configPopup indexOfSelectedItem]; - [configPopup removeAllItems]; - for (Config *config in configsController.configs) - [configPopup addItemWithTitle:config.name]; - [configPopup selectItemAtIndex:preserve ? initialIndex : -1]; +- (void)refreshMappings { + NSInteger initialIndex = mappingPopup.indexOfSelectedItem; + [mappingPopup.menu removeAllItems]; + for (NJMapping *mapping in mappingsController.mappings) { + NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name + action:@selector(mappingChosen:) + keyEquivalent:@""]; + item.target = self; + [mappingPopup.menu addItem:item]; + } + [mappingPopup selectItemAtIndex:initialIndex]; } @end