From: Joe Wreschnig Date: Sun, 10 Mar 2013 10:54:36 +0000 (+0100) Subject: Improvements to mappings controller. Move array munging into a separate category... X-Git-Tag: version-1.1~67 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=a7260fe79808efcad6a92cace15a1b3429f14787;p=enjoyable.git Improvements to mappings controller. Move array munging into a separate category. Remove dead code. Push mapping changes to the output controller through a notification rather than directly. --- diff --git a/Categories/NSMutableArray+MoveObject.h b/Categories/NSMutableArray+MoveObject.h index c1c60d2..ce62234 100644 --- a/Categories/NSMutableArray+MoveObject.h +++ b/Categories/NSMutableArray+MoveObject.h @@ -11,5 +11,24 @@ @interface NSMutableArray (MoveObject) - (void)moveObjectAtIndex:(NSUInteger)src toIndex:(NSUInteger)dst; + // Move the object at index src to (pre-move) index dst. Other + // objects shift up or down as necessary to make room, as in + // insertObject:atIndex:. Because the object is also removed from + // the source index, its resulting index may be one less than dst. + +- (BOOL)moveFirstwards:(id)object upTo:(NSUInteger)minIndex; +- (BOOL)moveLastwards:(id)object upTo:(NSUInteger)maxIndex; + // Move an object one step towards the first or last position in + // the array, up to a minimum or maximum index. Returns YES if the + // array changed; NO if the object was not in the array or was + // already at the minimum/maximum index. + +- (BOOL)moveFirstwards:(id)object; +- (BOOL)moveLastwards:(id)object; + // Move an object towards the first or last position in the array. + // Returns YES if the array changed; NO if the object was not in + // the array or if the object was already in the first/last + // position. + @end diff --git a/Categories/NSMutableArray+MoveObject.m b/Categories/NSMutableArray+MoveObject.m index 8d9a4ee..6c7987e 100644 --- a/Categories/NSMutableArray+MoveObject.m +++ b/Categories/NSMutableArray+MoveObject.m @@ -16,4 +16,34 @@ [self insertObject:obj atIndex:dst > src ? dst - 1 : dst]; } +- (BOOL)moveFirstwards:(id)object upTo:(NSUInteger)minIndex { + NSUInteger idx = [self indexOfObject:object]; + if (idx > minIndex && idx != NSNotFound) { + [self exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1]; + return YES; + } else { + return NO; + } +} + +- (BOOL)moveLastwards:(id)object upTo:(NSUInteger)maxIndex { + maxIndex = MIN(self.count - 1, maxIndex); + NSUInteger idx = [self indexOfObject:object]; + if (idx < maxIndex && idx != NSNotFound) { + [self exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1]; + return YES; + } else { + return NO; + } +} + +- (BOOL)moveFirstwards:(id)object { + return [self moveFirstwards:object upTo:0]; +} + +- (BOOL)moveLastwards:(id)object { + return [self moveLastwards:object upTo:NSNotFound]; +} + + @end diff --git a/Classes/NJMappingsController.h b/Classes/NJMappingsController.h index bca6c1f..dcb44b0 100644 --- a/Classes/NJMappingsController.h +++ b/Classes/NJMappingsController.h @@ -17,7 +17,6 @@ { IBOutlet NSButton *removeButton; IBOutlet NSTableView *tableView; - IBOutlet NJOutputController *outputController; IBOutlet NSButton *popoverActivate; IBOutlet NSPopover *popover; IBOutlet NSButton *moveUp; diff --git a/Classes/NJMappingsController.m b/Classes/NJMappingsController.m index 2ee4239..de66fb8 100644 --- a/Classes/NJMappingsController.m +++ b/Classes/NJMappingsController.m @@ -10,22 +10,20 @@ #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; @@ -50,7 +48,6 @@ - (void)mappingsChanged { [self save]; [tableView reloadData]; - popoverActivate.title = _currentMapping.name; [self updateInterfaceForCurrentMapping]; [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingListChanged @@ -66,13 +63,13 @@ } - (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; } @@ -85,14 +82,14 @@ [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"]; @@ -100,14 +97,13 @@ - (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]; } @@ -197,37 +193,6 @@ } } -- (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; @@ -341,19 +306,13 @@ } - (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_ diff --git a/Classes/NJOutputController.m b/Classes/NJOutputController.m index cbb48f7..5d90bee 100644 --- a/Classes/NJOutputController.m +++ b/Classes/NJOutputController.m @@ -29,6 +29,11 @@ selector:@selector(mappingListDidChange:) name:NJEventMappingListChanged object:nil]; + [NSNotificationCenter.defaultCenter + addObserver:self + selector:@selector(mappingDidChange:) + name:NJEventMappingChanged + object:nil]; } return self; } @@ -289,4 +294,8 @@ [mappingPopup selectItemWithRepresentedObject:current]; } +- (void)mappingDidChange:(NSNotification *)note { + [self loadCurrent]; +} + @end diff --git a/Info.plist b/Info.plist index 76b3f48..2b4ff49 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 84 + 90 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright diff --git a/Resources/English.lproj/MainMenu.xib b/Resources/English.lproj/MainMenu.xib index d6f1307..c8a38ed 100644 --- a/Resources/English.lproj/MainMenu.xib +++ b/Resources/English.lproj/MainMenu.xib @@ -472,7 +472,7 @@ {664, 323} - + 256 @@ -484,7 +484,6 @@ 274 {{20, 20}, {194, 283}} - _NS:22 YES @@ -523,8 +522,7 @@ aW5nLg {232, 321} - - + YES NO YES @@ -619,7 +617,6 @@ aW5nLg {{1, 1}, {232, 321}} - @@ -630,7 +627,6 @@ aW5nLg -2147483392 {{1, 1}, {8, 298}} - NO @@ -642,7 +638,6 @@ aW5nLg -2147483392 {{-100, -100}, {473, 15}} - NO 1 @@ -653,8 +648,7 @@ aW5nLg {234, 323} - - + 150034 @@ -667,7 +661,6 @@ aW5nLg {234, 323} - _NS:9 NSView @@ -681,7 +674,6 @@ aW5nLg 265 {{189, 117}, {224, 20}} - _NS:9 YES @@ -723,8 +715,6 @@ aW5nLg 268 {{343, 31}, {70, 18}} - - _NS:9 YES @@ -755,7 +745,6 @@ aW5nLg 265 {{189, 33}, {150, 20}} - _NS:9 YES @@ -800,7 +789,6 @@ aW5nLg 265 {{191, 24}, {146, 16}} - _NS:9 YES @@ -826,7 +814,6 @@ aW5nLg 265 {{189, 70}, {224, 24}} - _NS:9 YES @@ -880,7 +867,6 @@ aW5nLg 265 {{191, 108}, {220, 16}} - _NS:9 YES @@ -906,7 +892,6 @@ aW5nLg 265 {{191, 196}, {220, 23}} - _NS:9 NJKeyInputField @@ -916,7 +901,6 @@ aW5nLg 265 {{188, 153}, {226, 26}} - YES @@ -949,7 +933,6 @@ aW5nLg 268 {{24, 20}, {163, 250}} - NO 6 @@ -1080,7 +1063,6 @@ aW5nLg 266 {{9, 286}, {369, 17}} - YES @@ -1104,7 +1086,6 @@ aW5nLg 10 {{12, 276}, {363, 5}} - {0, 0} @@ -1131,15 +1112,12 @@ aW5nLg {{233, 0}, {431, 323}} - _NS:9 NSView {664, 323} - - {{0, 0}, {1440, 878}} @@ -1743,14 +1721,6 @@ aW5nLg 816 - - - outputController - - - - 827 - mappingPressed: @@ -3193,7 +3163,6 @@ aW5nLg NSButton NSButton - NJOutputController NSPopover NSButton NSButton @@ -3208,10 +3177,6 @@ aW5nLg moveUp NSButton - - outputController - NJOutputController - popover NSPopover