-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextureController+Tree.m
312 lines (274 loc) · 10.1 KB
/
TextureController+Tree.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// TextureController#.m
// LevelEditor
//
// Created by Michael Markowski on 29.01.10.
// Copyright 2010 Artifacts. All rights reserved.
//
#import "TextureController+Tree.h"
#import "SpriteType.h"
#import "AFGameEditor.h"
#import "Model.h"
#import "ImageAndTextCell.h"
@implementation TextureController (Tree)
- (void)invalidateTree {
rootTreeNode = nil;
[treeView reloadData];
}
- (NSTreeNode*)rootTreeNode {
if (rootTreeNode==nil) {
rootTreeNode = [[NSTreeNode alloc] initWithRepresentedObject:@"root"];
for (SpriteType *st in [spriteTypeTreeController content]) {
// NSLog(@"adding st %@", [st description]);
NSTreeNode *spriteTypeTreeNode = [NSTreeNode treeNodeWithRepresentedObject:st];
NSMutableArray *keyTreeNodes = [spriteTypeTreeNode mutableChildNodes];
for (NSString *key in st.keys) {
NSTreeNode *treeNodeKey = [NSTreeNode treeNodeWithRepresentedObject:key];
[keyTreeNodes addObject:treeNodeKey];
}
[[rootTreeNode mutableChildNodes] addObject:spriteTypeTreeNode];
}
}
return rootTreeNode;
}
// The NSOutlineView uses 'nil' to indicate the root item. We return our root tree node for that case.
- (NSArray *)childrenForItem:(id)item {
if (item == nil) {
return [self.rootTreeNode childNodes];
} else {
return [item childNodes];
}
}
//- (NSIndexSet *)outlineView:(NSOutlineView *)outlineView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes
- (BOOL)browser:(NSBrowser *)sender selectRow:(NSInteger)row inColumn:(NSInteger)column {
NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:row];
[spriteTypeTreeController setSelectionIndexPath:indexPath];
return YES;
}
- (IBAction)addSpriteTypeAction:(id)sender {
AFGameEditor *doc = (AFGameEditor*)[[NSDocumentController sharedDocumentController] currentDocument];
NSManagedObjectContext *moc = [doc managedObjectContext];
SpriteType *spriteType = [NSEntityDescription insertNewObjectForEntityForName:@"SpriteType" inManagedObjectContext:moc];
[spriteTypeTreeController addObject:spriteType];
[self invalidateTree];
}
- (IBAction)removeSpriteTypeAction:(id)obj {
SpriteType *spriteType = [spriteTypeTreeController selection];
[spriteTypeTreeController remove:spriteType];
NSIndexPath *indexPath = [spriteTypeTreeController selectionIndexPath];
[treeView selectRow:[indexPath indexAtPosition:0] byExtendingSelection:NO];
[self invalidateTree];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldExpandItem:(id)item {
if (treeView.isDragging==NO) {
[outlineView collapseItem:nil];
}
id obj = [item representedObject];
if ([obj isKindOfClass:[SpriteType class]]) {
return [[obj keys] count]>0;
} else {
return NO;
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
id obj = [item representedObject];
if ([obj isKindOfClass:[SpriteType class]]) {
return [[obj keys] count]>0;
} else {
return NO;
}
}
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
// 'item' may potentially be nil for the root item.
NSArray *children = [self childrenForItem:item];
return [children count];
}
/*
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
NSInteger num = 0;
if (item==nil) {
NSArray *array = [spriteTypeTreeController arrangedObjects];
num = [array count];
} else {
NSTreeNode *tn = item;
num = [[tn childNodes] count];
}
NSLog(@"numberOfChildrenOfItem %@: %d", [item description], num);
return num;*/
/*
id obj = [item representedObject];
if ([obj isKindOfClass:[SpriteType class]]) {
return [[obj keys] count];
} else {
return 0;
}
*/
//}
/*
- (NSArray *)childrenForItem:(id)item {
id obj = [item representedObject];
NSMutableArray *nodes = [NSMutableArray array];
if (item == nil) {
} else {
for (NSString *s in [obj keys]) {
NSTreeNode *tn = [NSTreeNode treeNodeWithRepresentedObject:s];
[nodes addObject:tn];
}
}
NSLog(@"childrenForItem %@: %@", [item description], [nodes description]);
return nodes;
}
*/
// Required methods.
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
// 'item' may potentially be nil for the root item.
NSArray *children = [self childrenForItem:item];
// This will return an NSTreeNode with our model object as the representedObject
return [children objectAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
// NSLog(@"objectValueForTableColumn: %@ byItem: %@", [tableColumn description], [item description]);
id objectValue = nil;
// The return value from this method is used to configure the state of the items cell via setObjectValue:
id obj = [item representedObject];
if ((tableColumn == nil) || [[tableColumn identifier] isEqualToString:@"Name"]) {
if ([obj isKindOfClass:[SpriteType class]]) {
objectValue = [obj name];
} else {
objectValue = nil;
}
} else if ([[tableColumn identifier] isEqualToString:@"Key"]) {
if ([obj isKindOfClass:[NSString class]]) {
objectValue = obj;
}
} else if ([[tableColumn identifier] isEqualToString:@"Image"]) {
if ([obj isKindOfClass:[NSString class]]) {
Texture *texture = [self textureForKey:obj];
objectValue = texture.img;
}
}
// NSTreeNode *treeNode = [NSTreeNode treeNodeWithRepresentedObject:objectValue];
return objectValue;
}
// Optional method: needed to allow editing.
- (void)outlineView:(NSOutlineView *)ov setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
SpriteType *s = [item representedObject];
if ((tableColumn == nil) || [[tableColumn identifier] isEqualToString:@"Name"]) {
s.name = object;
}
}
//- (BOOL)browser:(NSBrowser *)browser writeRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column toPasteboard:(NSPasteboard *)pasteboard {
// return YES;
//}
- (NSDragOperation)outlineView:(NSOutlineView *)ov validateDrop:(id <NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)childIndex {
// NSLog(@"outlineView:validateDrop:proposedItem:%@ proposedChildIndex:%ld", item, (long)childIndex);
if (!item) return NSDragOperationNone;
return NSDragOperationLink;
}
- (BOOL)outlineView:(NSOutlineView *)ov acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)childIndex {
// NSLog(@"acceptDrop: %d", childIndex);
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [info draggingSourceOperationMask];
pboard = [info draggingPasteboard];
if ( [[pboard types] containsObject:TextureTypePboardType] ) {
NSData *data = [pboard dataForType:TextureTypePboardType];
NSArray *textureArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (!item) return NO;
SpriteType *spriteType = [item representedObject];
NSArray *keys = spriteType.keys;
NSMutableArray *newKeys = [NSMutableArray arrayWithArray:keys];
for (Texture *texture in textureArray) {
if (![newKeys containsObject:texture.key]) {
[newKeys addObject:texture.key];
}
}
spriteType.keys = newKeys;
// NSLog(@"spriteType %@'s keys: %@", spriteType.name, [spriteType.keys description]);
[self invalidateTree];
}
return YES;
}
- (CGFloat)outlineView:(NSOutlineView *)outlineView heightOfRowByItem:(id)item
{
CGFloat height;
id obj = [item representedObject];
if ([obj isKindOfClass:[NSString class]]) {
height = 64;
} else {
height = 20;
}
return height;
}
/*
- (NSIndexSet*)expandedRowsAwareIndexSetByIndexSet:(NSIndexSet*)aSet {
NSMutableIndexSet *resultSet = [NSMutableIndexSet indexSet];
NSUInteger current_index = [aSet firstIndex];
while (current_index != NSNotFound)
{
[treeView
current_index = [aSet indexGreaterThanIndex: current_index];
}
return resultSet;
}
*/
/*
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(NSCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item {
id obj = [item representedObject];
ImageAndTextCell *imageAndTextCell = (ImageAndTextCell *)cell;
[imageAndTextCell setImage:nil];
if ((tableColumn == nil) || [[tableColumn identifier] isEqualToString:@"Image"]) {
if ([obj isKindOfClass:[NSString class]]) {
Texture *texture = [self textureForKey:obj];
[imageAndTextCell setImage:[self imageThumbnailFromImage:texture.img]];
}
} else if ([[tableColumn identifier] isEqualToString:@"Image"]) {
}
}*/
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
NSTreeNode *treeNode = item;
id obj = [item representedObject];
if ([obj isKindOfClass:[SpriteType class]]) {
NSIndexPath *indexPath = [treeNode indexPath];
[spriteTypeTreeController setSelectionIndexPath:indexPath];
} else {
[spriteTypeTreeController setSelectionIndexPath:nil];
}
return YES;
}
- (void)deleteSelectedTextures:(NSNotification*)notif {
NSIndexSet *indexSet = [treeView selectedRowIndexes];
NSUInteger current_index = [indexSet firstIndex];
SpriteType *parent;
NSTreeNode *item;
NSMutableArray *keys;
while (current_index != NSNotFound)
{
item = [treeView itemAtRow:current_index];
NSString *textureKey = [item representedObject];
parent = [[item parentNode] representedObject];
keys = [NSMutableArray arrayWithArray:parent.keys];
if (!keys) continue;
[keys removeObject:textureKey];
parent.keys = keys;
NSLog(@"deleting: %@", [textureKey description]);
current_index = [indexSet indexGreaterThanIndex: current_index];
}
[self invalidateTree];
}
- (IBAction)outlineViewAction:(id)sender {
}
- (NSImage*)imageThumbnailFromImage:(NSImage*)sourceImage {
if (!sourceImage) return nil;
float resizeHeight = 56.0;
float aspect = sourceImage.size.width / sourceImage.size.height;
float resizeWidth = resizeHeight * aspect;
NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(resizeWidth, resizeHeight)];
NSSize originalSize = [sourceImage size];
[resizedImage lockFocus];
[sourceImage drawInRect: NSMakeRect(0, 0, resizeWidth, resizeHeight) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];
return resizedImage;
}
@end