X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FNJMappingsController.m;h=8f0492cc7d88d3ac00c5aac8e7132f1e37e35849;hp=9c7b641e848ab9e7eec9b7d611a4d89670272afe;hb=4a8e67a7294e8e527e2be9d8f5f39aae60691697;hpb=24bdb92798b9abe86c7954042a47523791736b7c diff --git a/Classes/NJMappingsController.m b/Classes/NJMappingsController.m index 9c7b641..8f0492c 100644 --- a/Classes/NJMappingsController.m +++ b/Classes/NJMappingsController.m @@ -8,7 +8,6 @@ #import "NJMappingsController.h" #import "NJMapping.h" -#import "NJMappingsController.h" #import "NJOutput.h" #import "NJEvents.h" @@ -48,7 +47,6 @@ object:self userInfo:@{ NJMappingListKey: _mappings, NJMappingKey: _currentMapping }]; - [self.mvc changedActiveMappingToIndex:[_mappings indexOfObjectIdenticalTo:_currentMapping]]; } - (void)mappingsChanged { @@ -97,11 +95,12 @@ NSLog(@"Switching to mapping %@.", mapping.name); _manualMapping = mapping; _currentMapping = mapping; - [self.mvc changedActiveMappingToIndex:[_mappings indexOfObjectIdenticalTo:_currentMapping]]; + NSUInteger idx = [_mappings indexOfObjectIdenticalTo:_currentMapping]; [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged object:self - userInfo:@{ NJMappingKey : _currentMapping }]; + userInfo:@{ NJMappingKey : _currentMapping, + NJMappingIndexKey: @(idx) }]; } - (void)save { @@ -132,145 +131,62 @@ _mappings = newMappings; if (selected >= newMappings.count) selected = 0; - [self.mvc reloadData]; [self activateMapping:_mappings[selected]]; [self mappingsSet]; } } -- (void)mappingConflictDidResolve:(NSAlert *)alert - returnCode:(NSInteger)returnCode - contextInfo:(void *)contextInfo { - NSDictionary *userInfo = CFBridgingRelease(contextInfo); - NJMapping *oldMapping = userInfo[@"old mapping"]; - NJMapping *newMapping = userInfo[@"new mapping"]; - [alert.window orderOut:nil]; - switch (returnCode) { - case NSAlertFirstButtonReturn: // Merge - [oldMapping mergeEntriesFrom:newMapping]; - _currentMapping = nil; - [self activateMapping:oldMapping]; - [self mappingsChanged]; - break; - case NSAlertThirdButtonReturn: // New Mapping - [self.mvc.mappingList beginUpdates]; - [_mappings addObject:newMapping]; - [self.mvc addedMappingAtIndex:_mappings.count - 1 startEditing:YES]; - [self.mvc.mappingList endUpdates]; - [self activateMapping:newMapping]; - [self mappingsChanged]; - break; - default: // Cancel, other. - break; - } -} - -- (void)addOrMergeMapping:(NJMapping *)mapping { - [self addOrMergeMapping:mapping atIndex:-1]; +- (NSInteger)indexOfMapping:(NJMapping *)mapping { + return [_mappings indexOfObjectIdenticalTo:mapping]; } -- (void)addOrMergeMapping:(NJMapping *)mapping atIndex:(NSInteger)idx { - NSWindow *window = NSApplication.sharedApplication.keyWindow; - if (mapping) { - NJMapping *mergeInto = self[mapping.name]; - if ([mergeInto hasConflictWith:mapping]) { - NSAlert *conflictAlert = [[NSAlert alloc] init]; - conflictAlert.messageText = NSLocalizedString(@"import conflict prompt", @"Title of import conflict alert"); - conflictAlert.informativeText = - [NSString stringWithFormat:NSLocalizedString(@"import conflict in %@", @"Explanation of import conflict"), - mapping.name]; - [conflictAlert addButtonWithTitle:NSLocalizedString(@"import and merge", @"button to merge imported mappings")]; - [conflictAlert addButtonWithTitle:NSLocalizedString(@"cancel import", @"button to cancel import")]; - [conflictAlert addButtonWithTitle:NSLocalizedString(@"import new mapping", @"button to import as new mapping")]; - [conflictAlert beginSheetModalForWindow:window - modalDelegate:self - didEndSelector:@selector(mappingConflictDidResolve:returnCode:contextInfo:) - contextInfo:(void *)CFBridgingRetain(@{ @"old mapping": mergeInto, - @"new mapping": mapping })]; - } else if (mergeInto) { - [mergeInto mergeEntriesFrom:mapping]; - [self activateMapping:mergeInto]; - [self mappingsChanged]; - } else { - if (idx == -1) - idx = _mappings.count; - [self.mvc.mappingList beginUpdates]; - [_mappings insertObject:mapping atIndex:idx]; - [self.mvc addedMappingAtIndex:idx startEditing:NO]; - [self.mvc.mappingList endUpdates]; - [self activateMapping:mapping]; - [self mappingsChanged]; - } +- (void)mergeMapping:(NJMapping *)mapping intoMapping:(NJMapping *)existing { + [existing mergeEntriesFrom:mapping]; + [self mappingsChanged]; + if (existing == _currentMapping) { + // FIXME: Hack to trigger updates in the rest of the UI. + _currentMapping = nil; + NJMapping *manual = _manualMapping; + [self activateMapping:existing]; + _manualMapping = manual; } } -- (NSInteger)numberOfMappings:(NJMappingsViewController *)dvc { - return _mappings.count; -} - -- (NJMapping *)mappingsViewController:(NJMappingsViewController *)dvc - mappingForIndex:(NSUInteger)idx { - return _mappings[idx]; -} - -- (void)mappingsViewController:(NJMappingsViewController *)mvc - editedMappingAtIndex:(NSInteger)index { +- (void)renameMapping:(NJMapping *)mapping to:(NSString *)name { + mapping.name = name; + if (mapping == _currentMapping) { + // FIXME: Hack to trigger updates in the rest of the UI. + _currentMapping = nil; + NJMapping *manual = _manualMapping; + [self activateMapping:mapping]; + _manualMapping = manual; + } [self mappingsChanged]; } -- (BOOL)mappingsViewController:(NJMappingsViewController *)mvc - canMoveMappingFromIndex:(NSInteger)fromIdx - toIndex:(NSInteger)toIdx { - return fromIdx != toIdx && fromIdx != 0 && toIdx != 0 && toIdx < (NSInteger)_mappings.count; -} - -- (void)mappingsViewController:(NJMappingsViewController *)mvc - moveMappingFromIndex:(NSInteger)fromIdx - toIndex:(NSInteger)toIdx { - [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx]; - [self mappingsChanged]; +- (void)addMapping:(NJMapping *)mapping { + [self insertMapping:mapping atIndex:_mappings.count]; } -- (BOOL)mappingsViewController:(NJMappingsViewController *)mvc - canRemoveMappingAtIndex:(NSInteger)idx { - return idx != 0; +- (void)insertMapping:(NJMapping *)mapping atIndex:(NSInteger)idx { + [_mappings insertObject:mapping atIndex:idx]; + [self mappingsChanged]; } -- (void)mappingsViewController:(NJMappingsViewController *)mvc - removeMappingAtIndex:(NSInteger)idx { - NJMapping *old = self[idx]; - [self.mvc.mappingList beginUpdates]; +- (void)removeMappingAtIndex:(NSInteger)idx { + NSInteger currentIdx = [self indexOfMapping:_currentMapping]; [_mappings removeObjectAtIndex:idx]; - [self.mvc removedMappingAtIndex:idx]; - [self.mvc.mappingList endUpdates]; - if (old == _currentMapping) - [self activateMapping:self[MIN(idx, _mappings.count - 1)]]; + [self activateMapping:self[MIN(currentIdx, _mappings.count - 1)]]; [self mappingsChanged]; } -- (BOOL)mappingsViewController:(NJMappingsViewController *)mvc - importMappingFromURL:(NSURL *)url - atIndex:(NSInteger)index - error:(NSError **)error { - NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url - error:error]; - [self addOrMergeMapping:mapping atIndex:index]; - return !!mapping; -} - -- (void)mappingsViewController:(NJMappingsViewController *)mvc - addMapping:(NJMapping *)mapping { - [self.mvc.mappingList beginUpdates]; - [_mappings addObject:mapping]; - [self.mvc addedMappingAtIndex:_mappings.count - 1 startEditing:YES]; - [self.mvc.mappingList endUpdates]; - [self activateMapping:mapping]; +- (void)moveMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx { + [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx]; [self mappingsChanged]; } -- (void)mappingsViewController:(NJMappingsViewController *)mvc - choseMappingAtIndex:(NSInteger)idx { - [self activateMapping:self[idx]]; +- (NSUInteger)count { + return _mappings.count; } @end