Drag-and-drop mappings.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 6 Mar 2013 23:47:17 +0000 (00:47 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 6 Mar 2013 23:47:17 +0000 (00:47 +0100)
Enjoyable.xcodeproj/project.pbxproj
Enjoyable_Prefix.pch
NJMappingsController.m
NSMutableArray+MoveObject.h [new file with mode: 0644]
NSMutableArray+MoveObject.m [new file with mode: 0644]

index 19eb58ee3d32233983737d7f8984546cf750f433..aef1ed20bb2bffa9aacbaa5b4b8fd82d4fcb1e26 100644 (file)
@@ -34,6 +34,7 @@
                EE1D7C9216E01E7000B000EB /* NSView+FirstResponder.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1D7C9116E01E7000B000EB /* NSView+FirstResponder.m */; };
                EE1D7C9616E0ECCF00B000EB /* NSError+Description.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */; };
                EE96929416E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.m in Sources */ = {isa = PBXBuildFile; fileRef = EE96929316E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.m */; };
+               EEAA9CE116E808E600256B64 /* NSMutableArray+MoveObject.m in Sources */ = {isa = PBXBuildFile; fileRef = EEAA9CE016E808E600256B64 /* NSMutableArray+MoveObject.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXFileReference section */
@@ -89,6 +90,8 @@
                EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Description.m"; sourceTree = "<group>"; };
                EE96929216E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMenu+RepresentedObjectAccessors.h"; sourceTree = "<group>"; };
                EE96929316E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMenu+RepresentedObjectAccessors.m"; sourceTree = "<group>"; };
+               EEAA9CDF16E808E600256B64 /* NSMutableArray+MoveObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSMutableArray+MoveObject.h"; sourceTree = "<group>"; };
+               EEAA9CE016E808E600256B64 /* NSMutableArray+MoveObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSMutableArray+MoveObject.m"; sourceTree = "<group>"; };
                EEF86B7316E2241000674B87 /* NJInputPathElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJInputPathElement.h; sourceTree = "<group>"; };
                EEF86B7416E298CD00674B87 /* NJEvents.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJEvents.h; sourceTree = "<group>"; };
 /* End PBXFileReference section */
                                EE1D7C9116E01E7000B000EB /* NSView+FirstResponder.m */,
                                EE96929216E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.h */,
                                EE96929316E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.m */,
+                               EEAA9CDF16E808E600256B64 /* NSMutableArray+MoveObject.h */,
+                               EEAA9CE016E808E600256B64 /* NSMutableArray+MoveObject.m */,
                        );
                        name = Categories;
                        sourceTree = "<group>";
                                EE1D7C9216E01E7000B000EB /* NSView+FirstResponder.m in Sources */,
                                EE1D7C9616E0ECCF00B000EB /* NSError+Description.m in Sources */,
                                EE96929416E54B480054A3C8 /* NSMenu+RepresentedObjectAccessors.m in Sources */,
+                               EEAA9CE116E808E600256B64 /* NSMutableArray+MoveObject.m in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
index 1057a2ccc0f70b81f23bbd2df86951537edbae0a..4f501866344e7883406f4bc312bd6ea54f821a0d 100644 (file)
@@ -11,3 +11,4 @@
 #import "NSError+Description.h"
 #import "NSMenu+RepresentedObjectAccessors.h"
 #import "NSView+FirstResponder.h"
+#import "NSMutableArray+MoveObject.h"
index a1171a0088ffc0f69c64d26e43f23d449467cf69..cb5baf11a3a6d726d5950183e1ae5de01b8939cf 100644 (file)
@@ -13,6 +13,8 @@
 #import "NJOutputController.h"
 #import "NJEvents.h"
 
+#define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow"
+
 @implementation NJMappingsController {
     NSMutableArray *_mappings;
     NJMapping *manualMapping;
     return self;
 }
 
+- (void)awakeFromNib {
+    [tableView registerForDraggedTypes:@[PB_ROW]];
+}
+
 - (NJMapping *)objectForKeyedSubscript:(NSString *)name {
     for (NJMapping *mapping in _mappings)
         if ([name isEqualToString:mapping.name])
 - (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];
 }
 
         return;
     
     [_mappings removeObjectAtIndex:tableView.selectedRow];
-    [self mappingsChanged];
     [self activateMapping:_mappings[0]];
+    [self mappingsChanged];
 }
 
 -(void)tableViewSelectionDidChange:(NSNotification *)notify {
                               [_mappings addObject:mapping];
                           }
                           
-                          [self mappingsChanged];
                           [self activateMapping:mapping];
-                          [outputController loadCurrent];
+                          [self mappingsChanged];
                           
                           if (conflict && !mergeInto) {
                               [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
     }
 }
 
+- (BOOL)tableView:(NSTableView *)tableView
+       acceptDrop:(id <NSDraggingInfo>)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 <NSDraggingInfo>)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;
+    }
+}
+
+- (BOOL)tableView:(NSTableView *)tableView
+writeRowsWithIndexes:(NSIndexSet *)rowIndexes
+     toPasteboard:(NSPasteboard *)pboard {
+    if (rowIndexes.count == 1 && rowIndexes.firstIndex != 0) {
+        [pboard declareTypes:@[PB_ROW] owner:nil];
+        [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW];
+        return YES;
+    } else {
+        return NO;
+    }
+    
+}
 
 @end
diff --git a/NSMutableArray+MoveObject.h b/NSMutableArray+MoveObject.h
new file mode 100644 (file)
index 0000000..c1c60d2
--- /dev/null
@@ -0,0 +1,15 @@
+//
+//  NSMutableArray+MoveObject.h
+//  Enjoyable
+//
+//  Created by Joe Wreschnig on 3/7/13.
+//
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSMutableArray (MoveObject)
+
+- (void)moveObjectAtIndex:(NSUInteger)src toIndex:(NSUInteger)dst;
+
+@end
diff --git a/NSMutableArray+MoveObject.m b/NSMutableArray+MoveObject.m
new file mode 100644 (file)
index 0000000..8d9a4ee
--- /dev/null
@@ -0,0 +1,19 @@
+//
+//  NSMutableArray+MoveObject.m
+//  Enjoyable
+//
+//  Created by Joe Wreschnig on 3/7/13.
+//
+//
+
+#import "NSMutableArray+MoveObject.h"
+
+@implementation NSMutableArray (MoveObject)
+
+- (void)moveObjectAtIndex:(NSUInteger)src toIndex:(NSUInteger)dst {
+    id obj = self[src];
+    [self removeObjectAtIndex:src];
+    [self insertObject:obj atIndex:dst > src ? dst - 1 : dst];
+}
+
+@end