#import "NJMapping.h"
#import "NJMappingsController.h"
#import "NJOutput.h"
-#import "NJOutputController.h"
#import "NJEvents.h"
#define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow"
@implementation NJMappingsController {
NSMutableArray *_mappings;
- NJMapping *manualMapping;
- NSString *draggingName;
+ NJMapping *_manualMapping;
}
- (id)init {
if ((self = [super init])) {
_mappings = [[NSMutableArray alloc] init];
_currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
- manualMapping = _currentMapping;
+ _manualMapping = _currentMapping;
[_mappings addObject:_currentMapping];
}
return self;
- (void)mappingsChanged {
[self save];
[tableView reloadData];
- popoverActivate.title = _currentMapping.name;
[self updateInterfaceForCurrentMapping];
[NSNotificationCenter.defaultCenter
postNotificationName:NJEventMappingListChanged
}
- (void)activateMappingForProcess:(NSRunningApplication *)app {
- NJMapping *oldMapping = manualMapping;
+ NJMapping *oldMapping = _manualMapping;
NSArray *names = app.possibleMappingNames;
BOOL found = NO;
for (NSString *name in names) {
NJMapping *mapping = self[name];
if (mapping) {
- [self activateMapping:self[name]];
+ [self activateMapping:mapping];
found = YES;
break;
}
[self mappingsChanged];
}
}
- manualMapping = oldMapping;
+ _manualMapping = oldMapping;
}
- (void)updateInterfaceForCurrentMapping {
NSUInteger selected = [_mappings indexOfObject:_currentMapping];
- [removeButton setEnabled:selected != 0];
- [moveDown setEnabled:selected && selected != _mappings.count - 1];
- [moveUp setEnabled:selected > 1];
+ removeButton.enabled = selected != 0;
+ moveUp.enabled = selected > 1;
+ moveDown.enabled = selected && selected != _mappings.count - 1;
popoverActivate.title = _currentMapping.name;
[tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO];
[NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"];
- (void)activateMapping:(NJMapping *)mapping {
if (!mapping)
- mapping = manualMapping;
+ mapping = _manualMapping;
if (mapping == _currentMapping)
return;
NSLog(@"Switching to mapping %@.", mapping.name);
- manualMapping = mapping;
+ _manualMapping = mapping;
_currentMapping = mapping;
[self updateInterfaceForCurrentMapping];
- [outputController loadCurrent];
[NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
object:_currentMapping];
}
}
}
-- (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
- NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
- [stream open];
- NSDictionary *serialization = !*error
- ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
- : nil;
- [stream close];
-
- if (!([serialization isKindOfClass:NSDictionary.class]
- && [serialization[@"name"] isKindOfClass:NSString.class]
- && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
- *error = [NSError errorWithDomain:@"Enjoyable"
- code:0
- description:@"This isn't a valid mapping file."];
- return nil;
- }
-
- NSDictionary *entries = serialization[@"entries"];
- NJMapping *mapping = [[NJMapping alloc] initWithName:serialization[@"name"]];
- for (id key in entries) {
- NSDictionary *value = entries[key];
- if ([key isKindOfClass:NSString.class]) {
- NJOutput *output = [NJOutput outputDeserialize:value
- withMappings:_mappings];
- if (output)
- mapping.entries[key] = output;
- }
- }
- return mapping;
-}
-
- (void)addMappingWithContentsOfURL:(NSURL *)url {
NSWindow *window = popoverActivate.window;
NSError *error;
}
- (IBAction)moveUpPressed:(id)sender {
- NSUInteger idx = [_mappings indexOfObject:_currentMapping];
- if (idx > 1 && idx != NSNotFound) {
- [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1];
+ if ([_mappings moveFirstwards:_currentMapping upTo:1])
[self mappingsChanged];
- }
}
- (IBAction)moveDownPressed:(id)sender {
- NSUInteger idx = [_mappings indexOfObject:_currentMapping];
- if (idx < _mappings.count - 1) {
- [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1];
+ if ([_mappings moveLastwards:_currentMapping])
[self mappingsChanged];
- }
}
- (BOOL)tableView:(NSTableView *)tableView_
<nil key="NSUserInterfaceItemIdentifier"/>
<string key="NSWindowContentMinSize">{664, 323}</string>
<object class="NSView" key="NSWindowView" id="177223957">
- <reference key="NSNextResponder"/>
+ <nil key="NSNextResponder"/>
<int key="NSvFlags">256</int>
<array class="NSMutableArray" key="NSSubviews">
<object class="NSCustomView" id="734312853">
<int key="NSvFlags">274</int>
<string key="NSFrame">{{20, 20}, {194, 283}}</string>
<reference key="NSSuperview" ref="734312853"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="471332453"/>
<string key="NSReuseIdentifierKey">_NS:22</string>
<bool key="NSEnabled">YES</bool>
<array class="NSMutableArray" key="NSSubviews"/>
<string key="NSFrameSize">{232, 321}</string>
<reference key="NSSuperview" ref="698362889"/>
- <reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="1036252745"/>
+ <reference key="NSNextKeyView" ref="892486973"/>
<bool key="NSEnabled">YES</bool>
<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
<bool key="NSControlAllowsExpansionToolTips">YES</bool>
</array>
<string key="NSFrame">{{1, 1}, {232, 321}}</string>
<reference key="NSSuperview" ref="364857164"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="365506042"/>
<reference key="NSDocView" ref="365506042"/>
<reference key="NSBGColor" ref="834857663"/>
<int key="NSvFlags">-2147483392</int>
<string key="NSFrame">{{1, 1}, {8, 298}}</string>
<reference key="NSSuperview" ref="364857164"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="12898323"/>
<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
<reference key="NSTarget" ref="364857164"/>
<int key="NSvFlags">-2147483392</int>
<string key="NSFrame">{{-100, -100}, {473, 15}}</string>
<reference key="NSSuperview" ref="364857164"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="698362889"/>
<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
<int key="NSsFlags">1</int>
</array>
<string key="NSFrameSize">{234, 323}</string>
<reference key="NSSuperview" ref="734312853"/>
- <reference key="NSWindow"/>
- <reference key="NSNextKeyView" ref="892486973"/>
+ <reference key="NSNextKeyView" ref="698362889"/>
<int key="NSsFlags">150034</int>
<reference key="NSVScroller" ref="1036252745"/>
<reference key="NSHScroller" ref="892486973"/>
</array>
<string key="NSFrameSize">{234, 323}</string>
<reference key="NSSuperview" ref="177223957"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="364857164"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<string key="NSClassName">NSView</string>
<int key="NSvFlags">265</int>
<string key="NSFrame">{{189, 117}, {224, 20}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="385416822"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<int key="NSvFlags">268</int>
<string key="NSFrame">{{343, 31}, {70, 18}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
- <reference key="NSNextKeyView"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<object class="NSButtonCell" key="NSCell" id="868379451">
<int key="NSvFlags">265</int>
<string key="NSFrame">{{189, 33}, {150, 20}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="792189805"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<int key="NSvFlags">265</int>
<string key="NSFrame">{{191, 24}, {146, 16}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="20704797"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<int key="NSvFlags">265</int>
<string key="NSFrame">{{189, 70}, {224, 24}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="921829691"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<int key="NSvFlags">265</int>
<string key="NSFrame">{{191, 108}, {220, 16}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="125828224"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSEnabled">YES</bool>
<int key="NSvFlags">265</int>
<string key="NSFrame">{{191, 196}, {220, 23}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="194275224"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<string key="NSClassName">NJKeyInputField</string>
<int key="NSvFlags">265</int>
<string key="NSFrame">{{188, 153}, {226, 26}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="875916470"/>
<bool key="NSEnabled">YES</bool>
<object class="NSPopUpButtonCell" key="NSCell" id="74311158">
<int key="NSvFlags">268</int>
<string key="NSFrame">{{24, 20}, {163, 250}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="57697638"/>
<bool key="NSAllowsLogicalLayoutDirection">NO</bool>
<int key="NSNumRows">6</int>
<int key="NSvFlags">266</int>
<string key="NSFrame">{{9, 286}, {369, 17}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="497528019"/>
<bool key="NSEnabled">YES</bool>
<object class="NSTextFieldCell" key="NSCell" id="853503577">
<int key="NSvFlags">10</int>
<string key="NSFrame">{{12, 276}, {363, 5}}</string>
<reference key="NSSuperview" ref="471332453"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="120408205"/>
<string key="NSOffsets">{0, 0}</string>
<object class="NSTextFieldCell" key="NSTitleCell">
</array>
<string key="NSFrame">{{233, 0}, {431, 323}}</string>
<reference key="NSSuperview" ref="177223957"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="1016088174"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<string key="NSClassName">NSView</string>
</object>
</array>
<string key="NSFrameSize">{664, 323}</string>
- <reference key="NSSuperview"/>
- <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="734312853"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1440, 878}}</string>
</object>
<int key="connectionID">816</int>
</object>
- <object class="IBConnectionRecord">
- <object class="IBOutletConnection" key="connection">
- <string key="label">outputController</string>
- <reference key="source" ref="468285243"/>
- <reference key="destination" ref="801536542"/>
- </object>
- <int key="connectionID">827</int>
- </object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">mappingPressed:</string>
<dictionary class="NSMutableDictionary" key="outlets">
<string key="moveDown">NSButton</string>
<string key="moveUp">NSButton</string>
- <string key="outputController">NJOutputController</string>
<string key="popover">NSPopover</string>
<string key="popoverActivate">NSButton</string>
<string key="removeButton">NSButton</string>
<string key="name">moveUp</string>
<string key="candidateClassName">NSButton</string>
</object>
- <object class="IBToOneOutletInfo" key="outputController">
- <string key="name">outputController</string>
- <string key="candidateClassName">NJOutputController</string>
- </object>
<object class="IBToOneOutletInfo" key="popover">
<string key="name">popover</string>
<string key="candidateClassName">NSPopover</string>