X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;h=2b122b6298755bb11dc87bf90c77ad1452efdbcc;hp=c87a6ee0ebff6f13e6a09f49ee21a5ef6032e979;hb=4ce3f2876a51400f7e3baa213bd23ecd0101398a;hpb=530009447c5bbd360ac5023979cffc6d32a28df3 diff --git a/ApplicationController.m b/ApplicationController.m index c87a6ee..2b122b6 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -4,82 +4,94 @@ // // Created by Sam McCall on 4/05/09. // -#include -@implementation ApplicationController +#import "ApplicationController.h" -@synthesize jsController, targetController, configsController; +#import "Config.h" +#import "ConfigsController.h" +#import "JoystickController.h" +#import "TargetController.h" -static BOOL active; +@implementation ApplicationController { + BOOL active; +} -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData); +@synthesize jsController; +@synthesize targetController; +@synthesize configsController; --(void) applicationDidFinishLaunching: (NSNotification*) notification { - [jsController setup]; - [drawer open]; - [targetController setEnabled: false]; - [self setActive: NO]; - [configsController load]; - EventTypeSpec et; - et.eventClass = kEventClassApplication; - et.eventKind = kEventAppFrontSwitched; - EventHandlerUPP handler = NewEventHandlerUPP(appSwitch); - InstallApplicationEventHandler(handler, 1, &et, self, NULL); +- (void)didSwitchApplication:(NSNotification *)notification { + NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; + [self.configsController activateConfigForProcess:currentApp.localizedName]; } --(void) applicationWillTerminate: (NSNotification *)aNotification { - [configsController save]; +- (void)applicationDidFinishLaunching:(NSNotification *)notification { + [drawer open]; + self.targetController.enabled = NO; + self.active = NO; + [self.jsController setup]; + [self.configsController load]; + [[NSWorkspace sharedWorkspace].notificationCenter + addObserver:self + selector:@selector(didSwitchApplication:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; } -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication - hasVisibleWindows:(BOOL)flag -{ - [mainWindow makeKeyAndOrderFront:self]; - return YES; +- (void)applicationWillTerminate:(NSNotification *)aNotification { + [[NSUserDefaults standardUserDefaults] synchronize]; + [[NSWorkspace sharedWorkspace].notificationCenter + removeObserver:self + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; } -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) { - ApplicationController* self = (ApplicationController*)userData; - NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication]; - ProcessSerialNumber psn; - psn.lowLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue]; - psn.highLongOfPSN = [[currentApp objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue]; - [self->configsController applicationSwitchedTo: [currentApp objectForKey:@"NSApplicationName"] withPsn: psn]; - return noErr; -} +// TODO: Active state should probably be in the ConfigsController or +// JoystickController, not here. --(BOOL) active { - return active; +- (BOOL)active { + return active; } --(void) setActive: (BOOL) newActive { - [activeButton setLabel: (newActive ? @"Stop" : @"Start")]; - [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]]; - [activeMenuItem setState: (newActive ? 1 : 0)]; - active = newActive; +- (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 setActive: ![self active]]; +- (IBAction)toggleActivity:(id)sender { + self.active = !self.active; } --(void) configsChanged { - while([dockMenuBase numberOfItems] > 2) - [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)]; +- (NSUInteger)firstConfigMenuIndex { + NSUInteger count = dockMenuBase.numberOfItems; + for (int i = 0; i < count; ++i) + if ([dockMenuBase itemAtIndex:i].isSeparatorItem) + return i + 1; + return count; +} - for(Config* config in [configsController configs]) { - [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""]; - } - [self configChanged]; +- (void)configsChanged { + NSUInteger 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:@""]; + [self configChanged]; } --(void) configChanged { - Config* current = [configsController currentConfig]; - NSArray* configs = [configsController configs]; - for(int i=0; i<[configs count]; i++) - [[dockMenuBase itemAtIndex: (2+i)] setState: (([configs objectAtIndex:i] == current) ? YES : NO)]; + +- (void)configChanged { + NSUInteger firstConfig = [self firstConfigMenuIndex]; + Config *current = self.configsController.currentConfig; + NSArray *configs = self.configsController.configs; + for (int i = 0; i < configs.count; ++i) + [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current; } --(void) chooseConfig: (id) sender { - [configsController activateConfig: [[configsController configs] objectAtIndex: ([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL]; +- (void)chooseConfig:(id)sender { + int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex]; + Config *chosen = self.configsController.configs[idx]; + [configsController activateConfig:chosen]; } @end