-
Notifications
You must be signed in to change notification settings - Fork 1
/
SKNode+CCBReader.m
605 lines (539 loc) · 15.3 KB
/
SKNode+CCBReader.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
* SpriteBuilder: http://www.spritebuilder.org
*
* Copyright (c) 2014 Apportable Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "SKNode+CCBReader.h"
#import "CCBSpriteKitMacros.h"
#import "CCBSpriteKitReader.h"
#import "CCBReaderDelegate.h"
static NSString* CCBReaderNodeUserObjectKey = @"CCBReader:UserObject";
static NSString* CCBReaderUserDataKeyForContentSizeType = @"CCBReader:contentSizeType";
static NSString* CCBReaderUserDataKeyForScaleType = @"CCBReader:scaleType";
static NSString* CCBReaderUserDataKeyForPositionType = @"CCBReader:positionType";
static NSString* CCBReaderUserDataKeyForContentSize = @"CCBReader:contentSize";
static NSString* CCBReaderUserDataKeyForLoadedFromCCB = @"CCBSpriteKitReader:loadedFromCCB";
@interface CCBReaderSizeType : NSObject
@property (nonatomic) CCSizeType sizeType;
@end
@implementation CCBReaderSizeType
-(id) initWithSizeType:(CCSizeType)sizeType
{
self = [super init];
_sizeType = sizeType;
return self;
}
@end
@interface CCBReaderScaleType : NSObject
@property (nonatomic) CCScaleType scaleType;
@end
@implementation CCBReaderScaleType
-(id) initWithScaleType:(CCScaleType)scaleType
{
self = [super init];
_scaleType = scaleType;
return self;
}
@end
@interface CCBReaderPositionType : NSObject
@property (nonatomic) CCPositionType positionType;
@end
@implementation CCBReaderPositionType
-(id) initWithPositionType:(CCPositionType)positionType
{
self = [super init];
_positionType = positionType;
return self;
}
@end
@interface CCBReaderContentSize : NSObject
@property (nonatomic) CGSize contentSize;
@end
@implementation CCBReaderContentSize
-(id) initWithContentSize:(CGSize)contentSize
{
self = [super init];
_contentSize = contentSize;
return self;
}
@end
@implementation SKNode (CCBReader)
#pragma mark Manage User Data/Objects
@dynamic userObject;
-(void) setUserObject:(id)userObject
{
if (self.userData == nil)
{
self.userData = [NSMutableDictionary dictionary];
}
if (userObject)
{
[self.userData setObject:userObject forKey:CCBReaderNodeUserObjectKey];
}
else
{
[self.userData removeObjectForKey:CCBReaderNodeUserObjectKey];
}
}
-(id) userObject
{
return [self.userData objectForKey:CCBReaderNodeUserObjectKey];
}
-(NSMutableDictionary*) getOrCreateUserData
{
NSMutableDictionary* userData = self.userData;
if (userData == nil)
{
userData = [NSMutableDictionary dictionary];
self.userData = userData;
}
return userData;
}
#pragma mark Properties
@dynamic rotation;
-(void) setRotation:(CGFloat)rotation
{
self.zRotation = CC_DEGREES_TO_RADIANS(-rotation);
}
-(CGFloat) rotation
{
return self.zRotation;
}
@dynamic skewX, skewY;
-(void) setSkewX:(CGFloat)skewX
{
}
-(CGFloat) skewX
{
return 0.0;
}
-(void) setSkewY:(CGFloat)skewY
{
}
-(CGFloat) skewY
{
return 0.0;
}
@dynamic visible;
-(void) setVisible:(BOOL)visible
{
self.hidden = !visible;
}
-(BOOL) visible
{
return !self.hidden;
}
@dynamic spriteFrame;
-(void) setSpriteFrame:(SKTexture *)spriteFrame
{
if ([self isKindOfClass:[SKSpriteNode class]])
{
SKSpriteNode* sprite = (SKSpriteNode*)self;
sprite.texture = spriteFrame;
}
}
-(SKTexture*)spriteFrame
{
if ([self isKindOfClass:[SKSpriteNode class]])
{
SKSpriteNode* sprite = (SKSpriteNode*)self;
return sprite.texture;
}
return nil;
}
@dynamic scaleX, scaleY;
-(void) setScaleX:(CGFloat)scaleX
{
self.xScale = scaleX;
}
-(void) setScaleY:(CGFloat)scaleY
{
self.yScale = scaleY;
}
-(CGFloat) scaleX
{
return self.xScale;
}
-(CGFloat) scaleY
{
return self.yScale;
}
-(CGFloat) scale
{
return self.xScale;
}
@dynamic opacity;
-(void) setOpacity:(CGFloat)opacity
{
self.alpha = opacity;
}
-(CGFloat) opacity
{
return self.alpha;
}
@dynamic scaleType;
-(void) setScaleType:(CCScaleType)scaleType
{
CCBReaderScaleType* proxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForScaleType];
if (proxy)
{
proxy.scaleType = scaleType;
}
else
{
proxy = [[CCBReaderScaleType alloc] initWithScaleType:scaleType];
[[self getOrCreateUserData] setObject:proxy forKey:CCBReaderUserDataKeyForScaleType];
}
}
-(CCScaleType) scaleType
{
CCBReaderScaleType* proxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForScaleType];
return proxy.scaleType;
}
@dynamic contentSize;
-(void) setContentSize:(CGSize)contentSize
{
if ([self respondsToSelector:@selector(setSize:)])
{
[(id)self setSize:contentSize];
}
else
{
NSMutableDictionary* userData = [self getOrCreateUserData];
CCBReaderContentSize* contentSizeProxy = [userData objectForKey:CCBReaderUserDataKeyForContentSize];
if (contentSizeProxy == nil)
{
contentSizeProxy = [[CCBReaderContentSize alloc] initWithContentSize:contentSize];
[userData setObject:contentSizeProxy forKey:CCBReaderUserDataKeyForContentSize];
}
else
{
contentSizeProxy.contentSize = contentSize;
}
}
}
-(CGSize) contentSize
{
if ([self respondsToSelector:@selector(setSize:)])
{
return [(id)self size];
}
else
{
CCBReaderContentSize* contentSizeProxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForContentSize];
if (contentSizeProxy)
{
return contentSizeProxy.contentSize;
}
}
return self.frame.size;
}
@dynamic contentSizeType;
-(void) setContentSizeType:(CCSizeType)contentSizeType
{
CCBReaderSizeType* proxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForContentSizeType];
if (proxy)
{
proxy.sizeType = contentSizeType;
}
else
{
proxy = [[CCBReaderSizeType alloc] initWithSizeType:contentSizeType];
[[self getOrCreateUserData] setObject:proxy forKey:CCBReaderUserDataKeyForContentSizeType];
}
}
-(CCSizeType) contentSizeType
{
CCBReaderSizeType* proxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForContentSizeType];
return proxy.sizeType;
}
@dynamic positionType;
-(void) setPositionType:(CCPositionType)positionType
{
CCBReaderPositionType* proxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForPositionType];
if (proxy)
{
proxy.positionType = positionType;
}
else
{
proxy = [[CCBReaderPositionType alloc] initWithPositionType:positionType];
[[self getOrCreateUserData] setObject:proxy forKey:CCBReaderUserDataKeyForPositionType];
}
}
-(CCPositionType) positionType
{
CCBReaderPositionType* proxy = [[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForPositionType];
return proxy.positionType;
}
-(void) setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"IGNORED: %@ undefined key '%@' for value: %@", NSStringFromClass([self class]), key, value);
}
#pragma mark Post Load Processing
-(void) sendDidLoadFromCCBWithRootNode:(SKNode*)rootNode
{
// send readerDidLoadSelf to every node
if ([self respondsToSelector:@selector(readerDidLoadSelf)])
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector:@selector(readerDidLoadSelf)];
#pragma clang diagnostic pop
}
// also send readerDidLoadChildNode: to the CCB's main node (the 'scene' node)
if (self != rootNode)
{
if ([rootNode respondsToSelector:@selector(readerDidLoadChildNode:)])
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[rootNode performSelector:@selector(readerDidLoadChildNode:) withObject:self];
#pragma clang diagnostic pop
}
}
}
-(void) postProcessAfterLoadFromCCBWithRootNode:(SKNode*)rootNode
{
[self sendDidLoadFromCCBWithRootNode:rootNode];
// apply the positionType, sizeType, scaleType properties here and only once
if ([self respondsToSelector:@selector(setSize:)])
{
CGSize size = [self convertSize:[(id)self contentSize] sizeType:self.contentSizeType];
[(id)self setSize:size];
}
self.scaleAsPoint = [self convertScaleX:self.xScale scaleY:self.yScale scaleType:self.scaleType];
self.position = [self convertPosition:self.position positionType:self.positionType];
// convert only once
[self.userData removeObjectForKey:CCBReaderUserDataKeyForContentSizeType];
[self.userData removeObjectForKey:CCBReaderUserDataKeyForScaleType];
[self.userData removeObjectForKey:CCBReaderUserDataKeyForPositionType];
[self.userData removeObjectForKey:CCBReaderUserDataKeyForLoadedFromCCB];
// remove dictionary if empty (user may have already added custom userData items, hence the check)
if (self.userData.count == 0)
{
self.userData = nil;
}
/*
NSLog(@"%@ (%p) size: {%.1f, %.1f} scale: {%.2f, %.2f}", NSStringFromClass([self class]), self,
[self respondsToSelector:@selector(setSize:)] ? [(id)self size].width : self.frame.size.width,
[self respondsToSelector:@selector(setSize:)] ? [(id)self size].height : self.frame.size.height, self.xScale, self.yScale);
*/
}
#pragma mark Adjust scale based on scaleType
@dynamic scaleAsPoint;
-(CGPoint) scaleAsPoint
{
return CGPointMake(self.xScale, self.yScale);
}
-(void) setScaleAsPoint:(CGPoint)scaleAsPoint
{
self.xScale = scaleAsPoint.x;
self.yScale = scaleAsPoint.y;
}
-(CGPoint) convertScaleX:(CGFloat)scaleX scaleY:(CGFloat)scaleY scaleType:(CCScaleType)scaleType
{
CGPoint newScale = CGPointMake(scaleX, scaleY);
if (scaleType == CCScaleTypeScaled)
{
CGFloat scaleFactor = [CCDirector sharedDirector].UIScaleFactor;
newScale.x *= scaleFactor;
newScale.y *= scaleFactor;
}
return newScale;
}
#pragma mark Adjust Size based on sizeType
-(CGSize) contentSizeFromParent
{
CGSize parentSize = CGSizeZero;
SKNode* parent = self.parent;
if (parent)
{
if ([parent respondsToSelector:@selector(setSize:)])
{
parentSize = [(id)parent size];
}
else
{
parentSize = parent.contentSize;
}
}
if (CGSizeEqualToSize(parentSize, CGSizeZero))
{
// if there's no parent (or parent's size is 0,0) assume "parent" to be scene sized
parentSize = [CCBSpriteKitReader internal_getSceneSize];
}
return parentSize;
}
-(CGSize) convertSize:(CGSize)size sizeType:(CCSizeType)sizeType
{
CGSize newSize = size;
CCDirector* director = [CCDirector sharedDirector];
switch (sizeType.widthUnit)
{
case CCSizeUnitPoints:
// nothing to do
break;
case CCSizeUnitUIPoints:
newSize.width = director.UIScaleFactor * size.width;
break;
case CCSizeUnitNormalized:
newSize.width = size.width * [self contentSizeFromParent].width;
break;
case CCSizeUnitInsetPoints:
newSize.width = [self contentSizeFromParent].width - size.width;
break;
case CCSizeUnitInsetUIPoints:
newSize.width = [self contentSizeFromParent].width - size.width * director.UIScaleFactor;
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"unsupported contentSize unit type for width: %d", sizeType.widthUnit];
break;
}
switch (sizeType.heightUnit)
{
case CCSizeUnitPoints:
// nothing to do
break;
case CCSizeUnitUIPoints:
newSize.height = director.UIScaleFactor * size.height;
break;
case CCSizeUnitNormalized:
newSize.height = size.height * [self contentSizeFromParent].height;
break;
case CCSizeUnitInsetPoints:
newSize.height = [self contentSizeFromParent].height - size.height;
break;
case CCSizeUnitInsetUIPoints:
newSize.height = [self contentSizeFromParent].height - size.height * director.UIScaleFactor;
break;
default:
[NSException raise:NSInternalInconsistencyException format:@"unsupported contentSize unit type for height: %d", sizeType.heightUnit];
break;
}
return newSize;
}
#pragma mark Adjust Position with positionType
-(CGPoint) convertPosition:(CGPoint)originalPosition positionType:(CCPositionType)positionType
{
CGPoint newPosition = originalPosition;
CGPoint anchorPoint = CGPointZero;
if ([self.parent respondsToSelector:@selector(anchorPoint)])
{
anchorPoint = [(SKSpriteNode*)self.parent anchorPoint];
}
else if ([self.parent isKindOfClass:[SKLabelNode class]])
{
anchorPoint = CGPointMake(0.5, 0.5);
}
switch (positionType.xUnit)
{
case CCPositionUnitPoints:
// no adjustment
break;
case CCPositionUnitUIPoints:
newPosition.x *= [CCDirector sharedDirector].UIScaleFactor;
break;
case CCPositionUnitNormalized:
{
CGFloat parentWidth = [self contentSizeFromParent].width;
newPosition.x = newPosition.x * parentWidth - (parentWidth * anchorPoint.x);
break;
}
default:
[NSException raise:NSInternalInconsistencyException format:@"unsupported positionType for x: %d", positionType.xUnit];
break;
}
switch (positionType.yUnit)
{
case CCPositionUnitPoints:
// no adjustment
break;
case CCPositionUnitUIPoints:
newPosition.y *= [CCDirector sharedDirector].UIScaleFactor;
break;
case CCPositionUnitNormalized:
{
CGFloat parentHeight = [self contentSizeFromParent].height;
newPosition.y = newPosition.y * parentHeight - (parentHeight * anchorPoint.y);
break;
}
default:
[NSException raise:NSInternalInconsistencyException format:@"unsupported positionType for y: %d", positionType.yUnit];
break;
}
// Account for reference corner
switch (positionType.corner)
{
default:
case CCPositionReferenceCornerBottomLeft:
// do nothing
break;
case CCPositionReferenceCornerTopLeft:
// Reverse y-axis
newPosition.y = [self contentSizeFromParent].height - newPosition.y;
break;
case CCPositionReferenceCornerTopRight:
// Reverse x-axis and y-axis
newPosition.x = [self contentSizeFromParent].width - newPosition.x;
newPosition.y = [self contentSizeFromParent].height - newPosition.y;
break;
case CCPositionReferenceCornerBottomRight:
// Reverse x-axis
newPosition.x = [self contentSizeFromParent].width - newPosition.x;
break;
}
return newPosition;
}
#pragma mark Cocos2D additions
-(CGPoint) convertToWorldSpace:(CGPoint)position
{
return [self.parent convertPoint:position toNode:self.scene];
}
-(CGPoint) convertToNodeSpace:(CGPoint)position
{
return [self.parent convertPoint:position fromNode:self.scene];
}
-(BOOL) hitTestWithWorldPosition:(CGPoint)worldPosition
{
CGPoint localPosition = [self convertToNodeSpace:worldPosition];
CGSize size = self.contentSize;
BOOL inside = (localPosition.x > size.width) || (localPosition.y > size.height);
return inside;
}
#pragma mark Loaded from CCB
@dynamic loadedFromCCB;
-(void) setLoadedFromCCB:(BOOL)loadedFromCCB
{
if (loadedFromCCB)
{
[[self getOrCreateUserData] setObject:@"YES" forKey:CCBReaderUserDataKeyForLoadedFromCCB];
}
else
{
[[self getOrCreateUserData] removeObjectForKey:CCBReaderUserDataKeyForLoadedFromCCB];
}
}
-(BOOL) loadedFromCCB
{
return ([[self getOrCreateUserData] objectForKey:CCBReaderUserDataKeyForLoadedFromCCB] != nil);
}
@end