X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;h=445456526329370a5b8f3f2fc9f6b66e65848003;hp=09be1c04d6811747fc928e7ab18fb42fd2e43ba6;hb=44a44209d4ce26fb30102014d7040975aea51f93;hpb=19eadf9e688d8c087b47f83f8634593fddd641ac diff --git a/ApplicationController.m b/ApplicationController.m index 09be1c0..4454565 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -5,14 +5,17 @@ // Created by Sam McCall on 4/05/09. // +#import "ApplicationController.h" + +#import "Config.h" +#import "ConfigsController.h" +#import "JoystickController.h" +#import "TargetController.h" + @implementation ApplicationController { BOOL active; } -@synthesize jsController; -@synthesize targetController; -@synthesize configsController; - - (void)didSwitchApplication:(NSNotification *)notification { NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; [self.configsController activateConfigForProcess:currentApp.localizedName]; @@ -21,7 +24,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification { [drawer open]; self.targetController.enabled = NO; - self.active = NO; [self.jsController setup]; [self.configsController load]; [[NSWorkspace sharedWorkspace].notificationCenter @@ -32,52 +34,45 @@ } - (void)applicationWillTerminate:(NSNotification *)aNotification { - // TODO: Save immediately / shortly after changing and then enable - // sudden termination support. - [configsController save]; + [[NSUserDefaults standardUserDefaults] synchronize]; [[NSWorkspace sharedWorkspace].notificationCenter removeObserver:self name:NSWorkspaceDidActivateApplicationNotification object:nil]; } -// TODO: Active state should probably be in the ConfigsController or -// JoystickController, not here. - -- (BOOL)active { - return active; -} - -- (void)setActive:(BOOL)newActive { - activeButton.label = newActive ? @"Stop" : @"Start"; - activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; - activeMenuItem.state = newActive; - active = newActive; -} - - (IBAction)toggleActivity:(id)sender { - self.active = !self.active; + BOOL sendRealEvents = !self.jsController.sendingRealEvents; + self.jsController.sendingRealEvents = sendRealEvents; + activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; + activeMenuItem.state = sendRealEvents; } -- (NSUInteger)firstConfigMenuIndex { - NSUInteger count = dockMenuBase.numberOfItems; - for (int i = 0; i < count; ++i) +- (NSInteger)firstConfigMenuIndex { + for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i) if ([dockMenuBase itemAtIndex:i].isSeparatorItem) return i + 1; - return count; + return dockMenuBase.numberOfItems; } - (void)configsChanged { - NSUInteger removeFrom = [self firstConfigMenuIndex]; + NSInteger removeFrom = [self firstConfigMenuIndex]; while (dockMenuBase.numberOfItems > removeFrom) [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1]; - for (Config *config in self.configsController.configs) - [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""]; + int added = 0; + for (Config *config in self.configsController.configs) { + NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @""; + [dockMenuBase addItemWithTitle:config.name + action:@selector(chooseConfig:) + keyEquivalent:keyEquiv]; + + } + [_targetController refreshConfigs]; [self configChanged]; } - (void)configChanged { - NSUInteger firstConfig = [self firstConfigMenuIndex]; + NSInteger firstConfig = [self firstConfigMenuIndex]; Config *current = self.configsController.currentConfig; NSArray *configs = self.configsController.configs; for (int i = 0; i < configs.count; ++i) @@ -85,8 +80,8 @@ } - (void)chooseConfig:(id)sender { - int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex]; + NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex]; Config *chosen = self.configsController.configs[idx]; - [configsController activateConfig:chosen]; + [_configsController activateConfig:chosen]; } @end