|
| 1 | +// |
| 2 | +// AFGameEditor+LayerManagement.m |
| 3 | +// Leveleditor |
| 4 | +// |
| 5 | +// Created by Michael Markowski on 30.11.09. |
| 6 | +// Copyright 2009 Artifacts. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "AFGameEditor+LayerManagement.h" |
| 10 | +#import "CoreDataHelper.h" |
| 11 | +#import "AFGameEditor.h" |
| 12 | + |
| 13 | +@implementation AFGameEditor (LayerManagement) |
| 14 | + |
| 15 | +- (void)tableViewSelectionDidChange:(NSNotification *)aNotification |
| 16 | +{ |
| 17 | + [levelView setNeedsDisplay:YES]; |
| 18 | +} |
| 19 | + |
| 20 | +- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pasteboard |
| 21 | +{ |
| 22 | + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes]; |
| 23 | + [pasteboard declareTypes:[NSArray arrayWithObject:LayerItemsDropType] owner:self]; |
| 24 | + [pasteboard setData:data forType:LayerItemsDropType]; |
| 25 | + return YES; |
| 26 | +} |
| 27 | + |
| 28 | +- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)op |
| 29 | +{ |
| 30 | + if( [info draggingSource] == layerTableView ) |
| 31 | + { |
| 32 | + if( op == NSTableViewDropOn ) |
| 33 | + [tv setDropRow:row dropOperation:NSTableViewDropAbove]; |
| 34 | + |
| 35 | + if(( [[[NSApplication sharedApplication] currentEvent] modifierFlags] & NSAlternateKeyMask ) ) |
| 36 | + return NSDragOperationCopy; |
| 37 | + else |
| 38 | + return NSDragOperationMove; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + return NSDragOperationNone; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id <NSDraggingInfo>)info |
| 47 | + row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation |
| 48 | +{ |
| 49 | + CoreDataHelper *helper = [[[CoreDataHelper alloc] |
| 50 | + initWithManagedObjectContext:[self managedObjectContext] |
| 51 | + sortDescriptors:[self sortDescriptors] |
| 52 | + entityName:@"Layer"] autorelease]; |
| 53 | + |
| 54 | + NSPasteboard *pasteboard = [info draggingPasteboard]; |
| 55 | + NSData *rowData = [pasteboard dataForType:LayerItemsDropType]; |
| 56 | + NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData]; |
| 57 | + |
| 58 | + NSArray *allItemsArray = [layerArrayController arrangedObjects]; |
| 59 | + NSMutableArray *draggedItemsArray = [NSMutableArray arrayWithCapacity:[rowIndexes count]]; |
| 60 | + |
| 61 | + NSUInteger currentItemIndex; |
| 62 | + NSRange range = NSMakeRange( 0, [rowIndexes lastIndex] + 1 ); |
| 63 | + while([rowIndexes getIndexes:¤tItemIndex maxCount:1 inIndexRange:&range] > 0) |
| 64 | + { |
| 65 | + NSManagedObject *thisItem = [allItemsArray objectAtIndex:currentItemIndex]; |
| 66 | + |
| 67 | + [draggedItemsArray addObject:thisItem]; |
| 68 | + } |
| 69 | + |
| 70 | + if( [info draggingSourceOperationMask] & NSDragOperationMove ) |
| 71 | + { |
| 72 | + int count; |
| 73 | + for( count = 0; count < [draggedItemsArray count]; count++ ) |
| 74 | + { |
| 75 | + NSManagedObject *currentItemToMove = [draggedItemsArray objectAtIndex:count]; |
| 76 | + [currentItemToMove setValue:[NSNumber numberWithInt:kTemporaryViewPosition] forKey:@"viewPosition"]; |
| 77 | + } |
| 78 | + |
| 79 | + int tempRow; |
| 80 | + if( row == 0 ) |
| 81 | + tempRow = -1; |
| 82 | + else |
| 83 | + tempRow = row; |
| 84 | + |
| 85 | + NSArray *startItemsArray = [helper entitiesWithViewPositionBetween:0 and:tempRow]; |
| 86 | + NSArray *endItemsArray = [helper entitiesWithViewPositionGreaterThanOrEqualTo:row]; |
| 87 | + |
| 88 | + int currentViewPosition; |
| 89 | + |
| 90 | + currentViewPosition = [helper renumberViewPositionsOfEntities:startItemsArray startingAt:0]; |
| 91 | + |
| 92 | + currentViewPosition = [helper renumberViewPositionsOfEntities:draggedItemsArray startingAt:currentViewPosition]; |
| 93 | + |
| 94 | + currentViewPosition = [helper renumberViewPositionsOfEntities:endItemsArray startingAt:currentViewPosition]; |
| 95 | + |
| 96 | + return YES; |
| 97 | + } |
| 98 | + else if( [info draggingSourceOperationMask] & NSDragOperationCopy ) |
| 99 | + { |
| 100 | + NSArray *copiedItemsArray = [self copyItems:draggedItemsArray]; |
| 101 | + |
| 102 | + int tempRow; |
| 103 | + if( row == 0 ) |
| 104 | + tempRow = -1; |
| 105 | + else |
| 106 | + tempRow = row; |
| 107 | + |
| 108 | + NSArray *startItemsArray = [helper entitiesWithViewPositionBetween:0 and:tempRow]; |
| 109 | + NSArray *endItemsArray = [helper entitiesWithViewPositionGreaterThanOrEqualTo:row]; |
| 110 | + |
| 111 | + int currentViewPosition; |
| 112 | + |
| 113 | + currentViewPosition = [helper renumberViewPositionsOfEntities:startItemsArray startingAt:0]; |
| 114 | + |
| 115 | + currentViewPosition = [helper renumberViewPositionsOfEntities:copiedItemsArray startingAt:currentViewPosition]; |
| 116 | + |
| 117 | + currentViewPosition = [helper renumberViewPositionsOfEntities:endItemsArray startingAt:currentViewPosition]; |
| 118 | + |
| 119 | + return YES; |
| 120 | + } |
| 121 | + |
| 122 | + return NO; |
| 123 | +} |
| 124 | + |
| 125 | +- (NSArray *)copyItems:(NSArray *)itemsToCopyArray |
| 126 | +{ |
| 127 | + NSMutableArray *arrayOfCopiedItems = [NSMutableArray arrayWithCapacity:[itemsToCopyArray count]]; |
| 128 | + |
| 129 | + int count; |
| 130 | + for( count = 0; count < [itemsToCopyArray count]; count++ ) |
| 131 | + { |
| 132 | + NSManagedObject *itemToCopy = [itemsToCopyArray objectAtIndex:count]; |
| 133 | + NSManagedObject *copiedItem = [NSEntityDescription insertNewObjectForEntityForName:@"Layer" inManagedObjectContext:[self managedObjectContext]]; |
| 134 | + |
| 135 | + [copiedItem setValue:[itemToCopy valueForKey:@"name"] forKey:@"name"]; |
| 136 | + [copiedItem setValue:[itemToCopy valueForKey:@"visibleInEditor"] forKey:@"visibleInEditor"]; |
| 137 | + [copiedItem setValue:[itemToCopy valueForKey:@"visibleInScenery"] forKey:@"visibleInScenery"]; |
| 138 | + [copiedItem setValue:[itemToCopy valueForKey:@"level"] forKey:@"level"]; |
| 139 | + [copiedItem setValue:[itemToCopy valueForKey:@"sprites"] forKey:@"sprites"]; |
| 140 | + [copiedItem setValue:[itemToCopy valueForKey:@"tintColor"] forKey:@"tintColor"]; |
| 141 | + [copiedItem setValue:[itemToCopy valueForKey:@"tintEnabled"] forKey:@"tintEnabled"]; |
| 142 | + [copiedItem setValue:[NSNumber numberWithInt:kTemporaryViewPosition] forKey:@"viewPosition"]; |
| 143 | + |
| 144 | + [arrayOfCopiedItems addObject:copiedItem]; |
| 145 | + } |
| 146 | + |
| 147 | + return arrayOfCopiedItems; |
| 148 | +} |
| 149 | + |
| 150 | +@end |
0 commit comments