This repository has been archived by the owner on Jan 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
SSKStretchableNode.m
317 lines (249 loc) · 9.35 KB
/
SSKStretchableNode.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
#import "SSKStretchableNode.h"
#pragma mark - C Utilities
typedef enum : NSUInteger {
JSStretchableNodePartTopLeft,
JSStretchableNodePartTop,
JSStretchableNodePartTopRight,
JSStretchableNodePartRight,
JSStretchableNodePartBottomRight,
JSStretchableNodePartBottom,
JSStretchableNodePartBottomLeft,
JSStretchableNodePartLeft,
JSStretchableNodePartCenter
} JSStretchableNodePart;
static CGFloat JSStretchableNodeNoResizing = -9999;
static CGRect JSStretchableNodeGetRectForPart(CGSize totalSize, SSKEdgeInsetsType capInsets, JSStretchableNodePart part)
{
CGRect rect = CGRectZero;
switch (part) {
case JSStretchableNodePartTopLeft: {
rect.origin.y = totalSize.height - capInsets.top;
rect.size = CGSizeMake(capInsets.left, capInsets.top);
}
break;
case JSStretchableNodePartTop: {
rect.origin.x = capInsets.left;
rect.origin.y = totalSize.height - capInsets.top;
rect.size.width = totalSize.width - capInsets.left - capInsets.right;
rect.size.height = capInsets.top;
}
break;
case JSStretchableNodePartTopRight: {
rect.origin.x = totalSize.width - capInsets.right;
rect.origin.y = totalSize.height - capInsets.top;
rect.size = CGSizeMake(capInsets.right, capInsets.top);
}
break;
case JSStretchableNodePartRight: {
rect.origin.x = totalSize.width - capInsets.right;
rect.origin.y = capInsets.bottom;
rect.size.width = capInsets.right;
rect.size.height = totalSize.height - capInsets.top - capInsets.bottom;
}
break;
case JSStretchableNodePartBottomRight: {
rect.origin.x = totalSize.width - capInsets.right;
rect.size = CGSizeMake(capInsets.right, capInsets.bottom);
}
break;
case JSStretchableNodePartBottom: {
rect.origin.x = capInsets.left;
rect.size.width = totalSize.width - capInsets.left - capInsets.right;
rect.size.height = capInsets.bottom;
}
break;
case JSStretchableNodePartBottomLeft:
rect.size = CGSizeMake(capInsets.left, capInsets.bottom);
break;
case JSStretchableNodePartLeft: {
rect.origin.y = capInsets.bottom;
rect.size.width = capInsets.left;
rect.size.height = totalSize.height - capInsets.top - capInsets.bottom;
}
break;
case JSStretchableNodePartCenter: {
rect.origin = CGPointMake(capInsets.left, capInsets.bottom);
rect.size.width = totalSize.width - capInsets.left - capInsets.right;
rect.size.height = totalSize.height - capInsets.top - capInsets.bottom;
}
break;
}
return rect;
}
static CGRect JSStretchableNodeTextureRectFromPartRect(SKTexture *texture, CGRect partRect)
{
const CGSize textureSize = texture.size;
CGRect textureRect;
textureRect.origin.x = partRect.origin.x / textureSize.width;
textureRect.origin.y = partRect.origin.y / textureSize.height;
textureRect.size.width = partRect.size.width / textureSize.width;
textureRect.size.height = partRect.size.height / textureSize.height;
return textureRect;
}
#pragma mark - JSStretchableNode
@interface SSKStretchableNode()
@property (nonatomic, strong) NSArray *partNodes;
@property (nonatomic) SSKEdgeInsetsType textureCapInsets;
@end
@implementation SSKStretchableNode
+ (instancetype)stretchableNodeWithSize:(CGSize)size imageNamed:(NSString *)imageName capInsets:(SSKEdgeInsetsType)capInsets
{
if (!imageName) {
return nil;
}
return [self stretchableNodeWithSize:size
texture:[SKTexture textureWithImageNamed:imageName]
capInsets:capInsets];
}
+ (instancetype)stretchableNodeWithSize:(CGSize)size texture:(SKTexture *)texture capInsets:(SSKEdgeInsetsType)capInsets
{
SSKStretchableNode *node = [self node];
[node setSize:size drawPartNodes:NO];
[node setTexture:texture capInsets:capInsets];
return node;
}
- (void)drawPartNodes
{
[self.partNodes makeObjectsPerformSelector:@selector(removeFromParent)];
if (self.size.width == 0 || self.size.height == 0) {
self.partNodes = nil;
return;
}
if (!self.texture) {
if (self.color) {
SKSpriteNode *colorNode = [SKSpriteNode spriteNodeWithColor:self.color size:self.size];
colorNode.anchorPoint = CGPointZero;
colorNode.colorBlendFactor = self.colorBlendFactor;
[self addChild:colorNode];
self.partNodes = @[colorNode];
}
return;
}
NSMutableArray *partNodes = [NSMutableArray new];
const CGSize textureSize = self.texture.size;
for (JSStretchableNodePart part = 0; part <= JSStretchableNodePartCenter; part++) {
CGRect partRect = JSStretchableNodeGetRectForPart(textureSize, self.textureCapInsets, part);
CGRect partTextureRect = JSStretchableNodeTextureRectFromPartRect(self.texture, partRect);
CGRect partNodeRect = JSStretchableNodeGetRectForPart(self.size, self.textureCapInsets, part);
SKTexture *partTexture = [SKTexture textureWithRect:partTextureRect inTexture:self.texture];
SSKTileableNode *partNode = [SSKTileableNode tileableNodeWithSize:partNodeRect.size texture:partTexture];
partNode.position = partNodeRect.origin;
partNode.color = self.color;
partNode.colorBlendFactor = self.colorBlendFactor;
[self addChild:partNode];
[partNodes addObject:partNode];
}
self.partNodes = partNodes;
}
#pragma mark - Accessor overrides
- (void)setTexture:(SKTexture *)texture capInsets:(SSKEdgeInsetsType)capInsets
{
_texture = texture;
_textureCapInsets = capInsets;
[self drawPartNodes];
}
- (void)setTexture:(SKTexture *)texture
{
if (_texture == texture) {
return;
}
_texture = texture;
[self drawPartNodes];
}
- (void)setZPosition:(CGFloat)zPosition
{
BOOL changed = (self.zPosition != zPosition);
[super setZPosition:zPosition];
if (!changed) {
return;
}
for (SKSpriteNode *partNode in self.partNodes) {
partNode.zPosition = zPosition;
}
}
- (void)setSize:(CGSize)size
{
[self setSize:size drawPartNodes:YES];
}
- (void)setSize:(CGSize)size drawPartNodes:(BOOL)drawPartNodes
{
if (CGSizeEqualToSize(_size, size)) {
return;
}
_size = size;
if (!drawPartNodes) {
return;
}
if ([self.partNodes count] < 2) {
[self drawPartNodes];
return;
}
for (JSStretchableNodePart part = 0; part <= JSStretchableNodePartCenter; part++) {
SSKTileableNode *partNode = [self.partNodes objectAtIndex:part];
CGRect partNodeRect = JSStretchableNodeGetRectForPart(size, self.textureCapInsets, part);
partNode.position = partNodeRect.origin;
partNode.size = partNodeRect.size;
}
}
- (void)setColor:(SKColor *)color
{
if ([_color isEqual:color]) {
return;
}
_color = color;
if ([self.partNodes count] == 0) {
[self drawPartNodes];
return;
}
for (SSKTileableNode *partNode in self.partNodes) {
partNode.color = color;
}
}
- (void)setColorBlendFactor:(CGFloat)colorBlendFactor
{
if (_colorBlendFactor == colorBlendFactor) {
return;
}
_colorBlendFactor = colorBlendFactor;
if ([self.partNodes count] == 0) {
[self drawPartNodes];
return;
}
for (SSKTileableNode *partNode in self.partNodes) {
partNode.colorBlendFactor = colorBlendFactor;
}
}
@end
#pragma mark - SKActions
@implementation SKAction (SSKStretchableNodeActions)
+ (SKAction *)resizeStretchableNodeToWidth:(CGFloat)width duration:(NSTimeInterval)duration
{
return [SKAction resizeStretchableNodeToWidth:width
height:JSStretchableNodeNoResizing
duration:duration];
}
+ (SKAction *)resizeStretchableNodeToHeight:(CGFloat)height duration:(NSTimeInterval)duration
{
return [SKAction resizeStretchableNodeToWidth:JSStretchableNodeNoResizing
height:height
duration:duration];
}
+ (SKAction *)resizeStretchableNodeToWidth:(CGFloat)width height:(CGFloat)height duration:(NSTimeInterval)duration
{
return [SKAction customActionWithDuration:duration actionBlock:^(SKNode *node, CGFloat elapsedTime) {
if (![node isKindOfClass:[SSKStretchableNode class]]) {
return;
}
CGFloat percentageComplete = elapsedTime / duration;
SSKStretchableNode *stretchableNode = (SSKStretchableNode *)node;
CGSize nodeSize = stretchableNode.size;
if (width != JSStretchableNodeNoResizing) {
nodeSize.width += (width - nodeSize.width) * percentageComplete;
}
if (height != JSStretchableNodeNoResizing) {
nodeSize.height += (height - nodeSize.height) * percentageComplete;
}
stretchableNode.size = nodeSize;
}];
}
@end