This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
SDReader.m
264 lines (215 loc) · 8.98 KB
/
SDReader.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
/*
SDReader.m
Copyright (c) 2012 andrew0
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#import "SDReader.h"
#ifdef __CC_PLATFORM_MAC
#define CGSizeFromString(__s__) NSSizeToCGSize(NSSizeFromString(__s__))
#endif
static inline ccColor3B ColorFromNSString(NSString *string)
{
ccColor3B color;
sscanf([string cStringUsingEncoding:NSUTF8StringEncoding], "{%u, %u, %u}", &color.r, &color.g, &color.b);
return color;
}
@interface CCNode (Private)
- (void)_setZOrder:(NSInteger)z;
@end
@interface SDReader ()
- (void)validateArray:(NSArray *)array;
- (CCNode *)nodeForDictionary:(NSDictionary *)dict;
- (CCNode *)nodeNamed:(NSString *)name inArray:(NSArray *)array;
- (NSString *)nameForTag:(NSInteger)tag inArray:(NSArray *)array;
@end
@implementation SDReader
@synthesize sceneSize = _sceneSize;
+ (id)readerWithFile:(NSString *)file
{
return [[[self alloc] initWithFile:file] autorelease];
}
+ (id)readerWithURL:(NSURL *)url
{
return [self readerWithFile:[url path]];
}
- (id)init
{
NSAssert(NO, @"SDReader: use designated initializer initWithFile:");
return nil;
}
- (id)initWithFile:(NSString *)file
{
self = [super init];
if (self)
{
// try to get full path of file
NSString *path = [[NSBundle mainBundle] pathForResource:[file stringByDeletingPathExtension] ofType:[file pathExtension]];
// try to get dictionary
_dictionary = [[NSDictionary dictionaryWithContentsOfFile:path] retain];
NSAssert(_dictionary != nil, @"SDReader: file %@ non-existant or not a property list", path);
_array = [[_dictionary valueForKey:@"children"] retain];
NSAssert(_array != nil, @"SDReader: file %@ does not contain children array", path);
_sceneSize = CGSizeFromString([_dictionary valueForKey:@"sceneSize"]);
if (_sceneSize.width <= 0)
_sceneSize.width = 480;
if (_sceneSize.height <= 0)
_sceneSize.height = 320;
// search for errors in array
[self validateArray:_array];
}
return self;
}
- (void)dealloc
{
[_dictionary release];
[_array release];
[super dealloc];
}
- (void)validateArray:(NSArray *)array
{
NSMutableArray *names = [NSMutableArray array];
for (NSDictionary *dict in array)
{
NSAssert([dict isKindOfClass:[NSDictionary class]], @"SDReader: children array contains non-dictionary child");
NSString *className = [dict valueForKey:@"className"];
NSAssert(className != nil, @"SDReader: dictionary does not contain className key");
NSAssert([className isKindOfClass:[NSString class]], @"SDReader: className value is not a string");
NSAssert(NSClassFromString(className) != nil, @"SDReader: class named \"%@\" not found", className);
NSAssert([NSClassFromString(className) isSubclassOfClass:[CCNode class]], @"SDReader: class named \"%@\" not a subclass of CCNode", className);
NSAssert([dict valueForKey:@"children"] != nil, @"SDReader: dictionary does not contain children key");
NSAssert([[dict valueForKey:@"children"] isKindOfClass:[NSArray class]], @"SDReader: children value is not an array");
NSString *name = [dict valueForKey:@"name"];
NSAssert(name != nil, @"SDReader: dictionary does not contain name key");
NSAssert([name isKindOfClass:[NSString class]], @"SDReader: name value is not a string");
NSAssert(![names containsObject:name], @"SDReader: duplicate name \"%@\"", name);
[self validateArray:[dict valueForKey:@"children"]];
}
}
- (CCNode *)nodeForDictionary:(NSDictionary *)dict
{
CCNode *retVal = nil;
NSString *className = [dict valueForKey:@"className"];
if ([className isEqualToString:@"CCNode"])
retVal = [CCNode node];
else if ([className isEqualToString:@"CCSprite"])
retVal = [CCSprite node];
else if ([className isEqualToString:@"CCLayer"])
retVal = [CCLayer node];
else if ([className isEqualToString:@"CCLayerColor"])
retVal = [CCLayerColor node];
Class nodeClass = NSClassFromString(className);
if ([nodeClass isSubclassOfClass:[CCNode class]])
{
retVal.position = CGPointFromString([dict valueForKey:@"position"]);
retVal.anchorPoint = CGPointFromString([dict valueForKey:@"anchorPoint"]);
retVal.scaleX = [[dict valueForKey:@"scaleX"] floatValue];
retVal.scaleY = [[dict valueForKey:@"scaleY"] floatValue];
retVal.contentSize = CGSizeFromString([dict valueForKey:@"contentSize"]);
[retVal _setZOrder:[[dict valueForKey:@"zOrder"] integerValue]];
retVal.rotation = [[dict valueForKey:@"rotation"] floatValue];
retVal.tag = [[dict valueForKey:@"tag"] integerValue];
retVal.visible = [[dict valueForKey:@"visible"] boolValue];
retVal.isRelativeAnchorPoint = [[dict valueForKey:@"isRelativeAnchorPoint"] boolValue];
}
if ([nodeClass isSubclassOfClass:[CCSprite class]])
{
CCSprite *sprite = (CCSprite *)retVal;
NSString *path = [dict valueForKey:@"path"];
NSURL *url = [NSURL URLWithString:path];
NSAssert([[NSFileManager defaultManager] fileExistsAtPath:path], @"SDReader: could not find sprite \"%@\"", [url relativeString]);
sprite.texture = [[CCTextureCache sharedTextureCache] addImage:[url relativeString]];
sprite.textureRect = CGRectFromString([dict valueForKey:@"textureRect"]);
sprite.opacity = [[dict valueForKey:@"opacity"] unsignedCharValue];
sprite.color = ColorFromNSString([dict valueForKey:@"color"]);
sprite.flipX = [[dict valueForKey:@"flipX"] boolValue];
sprite.flipY = [[dict valueForKey:@"flipY"] boolValue];
}
if ([nodeClass isSubclassOfClass:[CCLayer class]])
{
CCLayer *layer = (CCLayer *)retVal;
#ifdef __CC_PLATFORM_IOS
layer.isAccelerometerEnabled = [[dict valueForKey:@"isAccelerometerEnabled"] boolValue];
#elif defined(__CC_PLATFORM_MAC)
layer.isMouseEnabled = [[dict valueForKey:@"isMouseEnabled"] boolValue];
layer.isKeyboardEnabled = [[dict valueForKey:@"isKeyboardEnabled"] boolValue];
#endif
layer.isTouchEnabled = [[dict valueForKey:@"isTouchEnabled"] boolValue];
layer.contentSize = CGSizeFromString([dict valueForKey:@"contentSize"]);
}
if ([nodeClass isSubclassOfClass:[CCLayerColor class]])
{
CCLayerColor *layer = (CCLayerColor *)retVal;
[layer setColor:ColorFromNSString([dict valueForKey:@"color"])];
[layer setOpacity:[[dict valueForKey:@"opacity"] unsignedCharValue]];
}
NSArray *children = [dict valueForKey:@"children"];
if ([children count] > 0)
for (NSDictionary *child in children)
[retVal addChild:[self nodeForDictionary:child]];
return retVal;
}
- (CCScene *)scene
{
CCScene *retVal = [CCScene node];
retVal.contentSize = _sceneSize;
for (NSDictionary *child in _array)
{
CCNode *node = [self nodeForDictionary:child];
if (node)
[retVal addChild:node];
}
return retVal;
}
- (CCNode *)nodeNamed:(NSString *)name inArray:(NSArray *)array
{
CCNode *retVal = nil;
for (NSDictionary *child in array)
{
NSString *dictName = [child valueForKey:@"name"];
if ([dictName isEqualToString:name])
retVal = [self nodeForDictionary:child];
else
retVal = [self nodeNamed:name inArray:[child valueForKey:@"children"]];
if (retVal != nil)
break;
}
return retVal;
}
- (CCNode *)nodeNamed:(NSString *)name
{
return [self nodeNamed:name inArray:_array];
}
- (NSString *)nameForTag:(NSInteger)tag inArray:(NSArray *)array
{
NSString *retVal = @"";
for (NSDictionary *child in array)
{
if ([[child valueForKey:@"tag"] integerValue] == tag)
{
retVal = [child valueForKey:@"name"];
break;
}
NSArray *children = [child valueForKey:@"children"];
if ([children count] > 0)
retVal = [self nameForTag:tag inArray:children];
}
return retVal;
}
- (CCNode *)nodeForTag:(NSInteger)tag
{
return [self nodeNamed:[self nameForTag:tag inArray:_array]];
}
@end