X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;h=531d1f469a54aa57c31869ffc23587a10330610f;hp=2b122b6298755bb11dc87bf90c77ad1452efdbcc;hb=f41f770d67370cbd71002515d81c0842b50cb04b;hpb=4ce3f2876a51400f7e3baa213bd23ecd0101398a diff --git a/ApplicationController.m b/ApplicationController.m index 2b122b6..531d1f4 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -11,15 +11,12 @@ #import "ConfigsController.h" #import "JoystickController.h" #import "TargetController.h" +#import "NJEvents.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]; @@ -28,70 +25,75 @@ - (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]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(mappingDidChange:) + name:NJEventMappingChanged + object:nil]; } - (void)applicationWillTerminate:(NSNotification *)aNotification { - [[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; + [NSUserDefaults.standardUserDefaults synchronize]; } - (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; + + if (sendRealEvents) { + [NSWorkspace.sharedWorkspace.notificationCenter + addObserver:self + selector:@selector(didSwitchApplication:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + NSLog(@"Listening for application changes."); + } else { + [NSWorkspace.sharedWorkspace.notificationCenter + removeObserver:self + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + NSLog(@"Ignoring application changes."); + } + } -- (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:@""]; - [self configChanged]; + 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]; } -- (void)configChanged { - NSUInteger firstConfig = [self firstConfigMenuIndex]; - Config *current = self.configsController.currentConfig; +- (void)mappingDidChange:(NSNotification *)note { + NSInteger firstConfig = self.firstConfigMenuIndex; + Config *current = note.object; NSArray *configs = self.configsController.configs; - for (int i = 0; i < configs.count; ++i) + for (NSUInteger i = 0; i < configs.count; ++i) [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current; } - (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