-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteSheetAnimationLayer.m
More file actions
206 lines (172 loc) · 9.77 KB
/
SpriteSheetAnimationLayer.m
File metadata and controls
206 lines (172 loc) · 9.77 KB
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
//
// SpriteSheetAnimationLayer.m
// RatherOddJourney
//
// Created by Aaron Eisses on 12-01-14.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "SpriteSheetAnimationLayer.h"
#import "StringUtils.h"
@implementation SpriteSheetAnimationLayer
@synthesize sampleIndex;
@synthesize movementIndex;
@synthesize myDelegate;
-(id)initWithFile:(NSString *)_fileName
{
if (self = [super init]) {
fileName = [[NSString alloc] initWithString:_fileName];
NSString *imageString = [[NSString alloc] initWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:fileName ofType:@"png"]];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageString];
self.contents = (id)image.CGImage;
NSDictionary *plistData;
NSString *localizedPath = [[NSString alloc] initWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]];
plistData = [[NSDictionary alloc] initWithContentsOfFile:localizedPath];
myAnimationPlist = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)[plistData objectForKey:@"frames"]];
animationName = [[NSString alloc] initWithString:fileName];
NSString *dictKey = [[NSString alloc] initWithFormat:@"%@%@.png",fileName,[NSString padZerosToInt:1]];
NSDictionary *tempDictionary = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)[myAnimationPlist objectForKey:dictKey]];
CGRect locationFrame = [(NSString *)[tempDictionary objectForKey:@"frame"] stringToRect];
CGRect sourceColoRect = [(NSString *)[tempDictionary objectForKey:@"sourceColorRect"] stringToRect];
CGSize normalizedSize = CGSizeMake( locationFrame.size.width/CGImageGetWidth(image.CGImage), locationFrame.size.height/CGImageGetHeight(image.CGImage) );
startingRect = CGRectMake(sourceColoRect.origin.x,
sourceColoRect.origin.y,
sourceColoRect.size.width,
sourceColoRect.size.height);
self.contentsRect = CGRectMake(locationFrame.origin.x/CGImageGetWidth(image.CGImage),
locationFrame.origin.y/CGImageGetWidth(image.CGImage),
normalizedSize.width, normalizedSize.height);
}
return self;
}
-(id)initWithFile:(NSString *)_fileName atLocation:(CGPoint)_location sizeInHalf:(BOOL)_sizeInHalf
{
if (self = [super init]) {
sizeInHalf = _sizeInHalf;
startingPoint = _location;
fileName = [[NSString alloc] initWithString:_fileName];
NSString *imageString = [[NSString alloc] initWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:fileName ofType:@"png"]];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageString];
self.contents = (id)image.CGImage;
NSDictionary *plistData;
NSString *localizedPath = [[NSString alloc] initWithFormat:@"%@",[[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]];
plistData = [[NSDictionary alloc] initWithContentsOfFile:localizedPath];
myAnimationPlist = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)[plistData objectForKey:@"frames"]];
animationName = [[NSString alloc] initWithString:fileName];
NSString *dictKey = [[NSString alloc] initWithFormat:@"%@%@.png",fileName,[NSString padZerosToInt:1]];
NSDictionary *tempDictionary = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)[myAnimationPlist objectForKey:dictKey]];
CGRect locationFrame = [(NSString *)[tempDictionary objectForKey:@"frame"] stringToRect];
CGRect sourceColoRect = [(NSString *)[tempDictionary objectForKey:@"sourceColorRect"] stringToRect];
if (sizeInHalf) {
startingRect = CGRectMake(_location.x+sourceColoRect.origin.x,
_location.y+sourceColoRect.origin.y,
sourceColoRect.size.width*0.5,
sourceColoRect.size.height*0.5);
} else {
startingRect = CGRectMake(_location.x+sourceColoRect.origin.x,
_location.y+sourceColoRect.origin.y,
sourceColoRect.size.width,
sourceColoRect.size.height);
}
self.frame = startingRect;
CGSize normalizedSize = CGSizeMake( locationFrame.size.width/CGImageGetWidth(image.CGImage), locationFrame.size.height/CGImageGetHeight(image.CGImage) );
self.contentsRect = CGRectMake(locationFrame.origin.x/CGImageGetWidth(image.CGImage),
locationFrame.origin.y/CGImageGetWidth(image.CGImage),
normalizedSize.width, normalizedSize.height);
}
return self;
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[myDelegate animationOver:fileName];
}
-(void)startAnimation:(int)frames repeatCount:(int)_repeatCount atLocation:(CGPoint)_location withKey:(NSString *)key
{
startingPoint = _location;
self.frame = CGRectMake(_location.x+self.frame.origin.x,
_location.y+self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.fromValue = [NSNumber numberWithInt:1]; // initial frame
anim.toValue = [NSNumber numberWithInt:frames + 1]; // last frame + 1
anim.duration = (float)frames/framerate; // from the first frame to the 6th one in 1 second
anim.repeatCount = _repeatCount; // just keep repeating it
anim.delegate = self;
anim.autoreverses = NO;
startingRect = self.frame;
[self addAnimation:anim forKey:key]; // start
}
-(void)startMovementAnimation:(int)frames repeatCount:(int)_repeatCount movement:(int)_movement withReverse:(BOOL)isReverse isNegative:(BOOL)_isNegative withKey:(NSString *)key
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"movementIndex"];
anim.fromValue = [NSNumber numberWithInt:0];
anim.toValue = [NSNumber numberWithInt:_movement];
anim.duration = (float)frames/framerate;
anim.repeatCount = _repeatCount;
isMovementNegative = _isNegative;
// anim.delegate = self;
anim.autoreverses = isReverse;
[self addAnimation:anim forKey:key];
}
-(void)startAnimation:(int)frames repeatCount:(int)_repeatCount withReverse:(BOOL)isReverse withKey:(NSString *)key
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"sampleIndex"];
anim.fromValue = [NSNumber numberWithInt:1]; // initial frame
anim.toValue = [NSNumber numberWithInt:frames + 1]; // last frame + 1
anim.duration = (float)frames/framerate; // from the first frame to the 6th one in 1 second
anim.repeatCount = _repeatCount; // just keep repeating it
anim.delegate = self;
anim.autoreverses = isReverse;
startingRect = self.frame;
[self addAnimation:anim forKey:key]; // start
}
-(void)removeMyAnimations:(NSString *)_key
{
[self removeAnimationForKey:_key];
((CharacterAnimationLayer*)[self presentationLayer]).sampleIndex = 1;
}
+ (BOOL)needsDisplayForKey:(NSString *)key;
{
return [key isEqualToString:@"sampleIndex"] || [key isEqualToString:@"movementIndex"];
}
+ (id < CAAction >)defaultActionForKey:(NSString *)aKey;
{
if ([aKey isEqualToString:@"contentsRect"] || [aKey isEqualToString:@"position"] || [aKey isEqualToString:@"bounds"])
return (id < CAAction >)[NSNull null];
return [super defaultActionForKey:aKey];
}
-(void)display
{
unsigned int currentSampleIndex = ((CharacterAnimationLayer*)[self presentationLayer]).sampleIndex;
unsigned int movementSampleIndex = ((CharacterAnimationLayer*)[self presentationLayer]).movementIndex;
if (currentSampleIndex) {
NSString *dictKey = [[NSString alloc] initWithFormat:@"%@%@.png",animationName,[NSString padZerosToInt:currentSampleIndex]];
NSDictionary *tempDictionary = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)[myAnimationPlist objectForKey:dictKey]];
CGRect myPlistRect = [(NSString *)[tempDictionary objectForKey:@"frame"] stringToRect];
CGRect m_frame = [(NSString *)[tempDictionary objectForKey:@"sourceColorRect"] stringToRect];
CGRect myTempRect = CGRectMake(myPlistRect.origin.x/CGImageGetWidth((CGImageRef)self.contents),
myPlistRect.origin.y/CGImageGetHeight((CGImageRef)self.contents),
myPlistRect.size.width/CGImageGetWidth((CGImageRef)self.contents),
myPlistRect.size.height/CGImageGetHeight((CGImageRef)self.contents));
self.contentsRect = myTempRect;
if (sizeInHalf) {
self.frame = CGRectMake(startingPoint.x+m_frame.origin.x,
startingPoint.y+m_frame.origin.y,
m_frame.size.width*0.5,
m_frame.size.height*0.5);
} else {
self.frame = CGRectMake(startingPoint.x+m_frame.origin.x,
startingPoint.y+m_frame.origin.y,
m_frame.size.width,
m_frame.size.height);
}
}
if (movementSampleIndex) {
if (isMovementNegative) {
self.frame = CGRectMake(self.frame.origin.x, startingPoint.y - movementSampleIndex, self.frame.size.width, self.frame.size.height);
} else {
self.frame = CGRectMake(self.frame.origin.x, startingPoint.y + movementSampleIndex, self.frame.size.width, self.frame.size.height);
}
}
}
@end