X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=NJMappingsController.m;h=ff9b42c68bfefa4606a6a1efce34522b054a45e6;hp=a1171a0088ffc0f69c64d26e43f23d449467cf69;hb=b18839d1b922bcf00b5ada21e1748b6d78c6773f;hpb=f0909929a979b62a110e230e8b384cafaed33eba diff --git a/NJMappingsController.m b/NJMappingsController.m index a1171a0..ff9b42c 100644 --- a/NJMappingsController.m +++ b/NJMappingsController.m @@ -13,9 +13,12 @@ #import "NJOutputController.h" #import "NJEvents.h" +#define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow" + @implementation NJMappingsController { NSMutableArray *_mappings; NJMapping *manualMapping; + NSString *draggingName; } - (id)init { @@ -28,6 +31,11 @@ return self; } +- (void)awakeFromNib { + [tableView registerForDraggedTypes:@[PB_ROW]]; + [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO]; +} + - (NJMapping *)objectForKeyedSubscript:(NSString *)name { for (NJMapping *mapping in _mappings) if ([name isEqualToString:mapping.name]) @@ -99,8 +107,8 @@ - (IBAction)addPressed:(id)sender { NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"]; [_mappings addObject:newMapping]; - [self mappingsChanged]; [self activateMapping:newMapping]; + [self mappingsChanged]; [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES]; } @@ -109,8 +117,8 @@ return; [_mappings removeObjectAtIndex:tableView.selectedRow]; - [self mappingsChanged]; [self activateMapping:_mappings[0]]; + [self mappingsChanged]; } -(void)tableViewSelectionDidChange:(NSNotification *)notify { @@ -260,9 +268,8 @@ [_mappings addObject:mapping]; } - [self mappingsChanged]; [self activateMapping:mapping]; - [outputController loadCurrent]; + [self mappingsChanged]; if (conflict && !mergeInto) { [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO]; @@ -292,16 +299,8 @@ if (result != NSFileHandlingPanelOKButton) return; [panel close]; - [NSProcessInfo.processInfo disableSuddenTermination]; NSError *error; - NSDictionary *serialization = [mapping serialize]; - NSData *json = [NSJSONSerialization dataWithJSONObject:serialization - options:NSJSONWritingPrettyPrinted - error:&error]; - if (!error) - [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error]; - - [NSProcessInfo.processInfo enableSuddenTermination]; + [mapping writeToURL:panel.URL error:&error]; if (error) { [window presentError:error modalForWindow:window @@ -313,7 +312,9 @@ } - (IBAction)mappingPressed:(id)sender { - [popover showRelativeToRect:popoverActivate.bounds ofView:popoverActivate preferredEdge:NSMinXEdge]; + [popover showRelativeToRect:popoverActivate.bounds + ofView:popoverActivate + preferredEdge:NSMinXEdge]; } - (void)popoverWillShow:(NSNotification *)notification { @@ -340,5 +341,63 @@ } } +- (BOOL)tableView:(NSTableView *)tableView + acceptDrop:(id )info + row:(NSInteger)row + dropOperation:(NSTableViewDropOperation)dropOperation { + NSPasteboard *pboard = [info draggingPasteboard]; + if ([pboard.types containsObject:PB_ROW]) { + NSString *value = [pboard stringForType:PB_ROW]; + NSUInteger srcRow = [value intValue]; + [_mappings moveObjectAtIndex:srcRow toIndex:row]; + [self mappingsChanged]; + return YES; + } else { + return NO; + } +} + +- (NSDragOperation)tableView:(NSTableView *)tableView_ + validateDrop:(id )info + proposedRow:(NSInteger)row + proposedDropOperation:(NSTableViewDropOperation)dropOperation { + NSPasteboard *pboard = [info draggingPasteboard]; + if ([pboard.types containsObject:PB_ROW]) { + [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove]; + return NSDragOperationGeneric; + } else { + return NSDragOperationNone; + } +} + +- (NSArray *)tableView:(NSTableView *)tableView_ +namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination +forDraggedRowsWithIndexes:(NSIndexSet *)indexSet { + NJMapping *toSave = self[indexSet.firstIndex]; + NSString *filename = [[toSave.name stringByFixingPathComponent] + stringByAppendingPathExtension:@"enjoyable"]; + NSURL *dst = [dropDestination URLByAppendingPathComponent:filename]; + dst = [NSFileManager.defaultManager generateUniqueURLWithBase:dst]; + NSError *error; + if (![toSave writeToURL:dst error:&error]) { + [tableView_ presentError:error]; + return @[]; + } else { + return @[dst.lastPathComponent]; + } +} + +- (BOOL)tableView:(NSTableView *)tableView_ +writeRowsWithIndexes:(NSIndexSet *)rowIndexes + toPasteboard:(NSPasteboard *)pboard { + if (rowIndexes.count == 1 && rowIndexes.firstIndex != 0) { + [pboard declareTypes:@[PB_ROW, NSFilesPromisePboardType] owner:nil]; + [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW]; + [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType]; + return YES; + } else { + return NO; + } +} @end