/
CALayer.m
344 lines (259 loc) · 6.9 KB
/
CALayer.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
#import <QuartzCore/CALayer.h>
#import <QuartzCore/CAAnimation.h>
#import <QuartzCore/CALayerContext.h>
#import <QuartzCore/CATransaction.h>
#import <Foundation/NSDictionary.h>
NSString * const kCAFilterLinear=@"linear";
NSString * const kCAFilterNearest=@"nearest";
NSString * const kCAFilterTrilinear=@"trilinear";
@implementation CALayer
+layer {
return [[[self alloc] init] autorelease];
}
-(CALayerContext *)_context {
return _context;
}
-(void)_setContext:(CALayerContext *)context {
if(_context!=context){
[_context deleteTextureId:_textureId];
[_textureId release];
_textureId=nil;
}
_context=context;
[_sublayers makeObjectsPerformSelector:@selector(_setContext:) withObject:context];
}
-(CALayer *)superlayer {
return _superlayer;
}
-(NSArray *)sublayers {
return _sublayers;
}
-(void)setSublayers:(NSArray *)sublayers {
sublayers=[sublayers copy];
[_sublayers release];
_sublayers=sublayers;
[_sublayers makeObjectsPerformSelector:@selector(_setSuperLayer:) withObject:self];
[_sublayers makeObjectsPerformSelector:@selector(_setContext:) withObject:_context];
}
-(id)delegate {
return _delegate;
}
-(void)setDelegate:value {
_delegate=value;
}
-(CGPoint)anchorPoint {
return _anchorPoint;
}
-(void)setAnchorPoint:(CGPoint)value {
_anchorPoint=value;
}
-(CGPoint)position {
return _position;
}
-(void)setPosition:(CGPoint)value {
CAAnimation *animation=[self animationForKey:@"position"];
if(animation==nil && ![CATransaction disableActions]){
id action=[self actionForKey:@"position"];
if(action!=nil)
[self addAnimation:action forKey:@"position"];
}
_position=value;
}
-(CGRect)bounds {
return _bounds;
}
-(void)setBounds:(CGRect)value {
CAAnimation *animation=[self animationForKey:@"bounds"];
if(animation==nil && ![CATransaction disableActions]){
id action=[self actionForKey:@"bounds"];
if(action!=nil)
[self addAnimation:action forKey:@"bounds"];
}
_bounds=value;
}
-(CGRect)frame {
CGRect result;
result.size=_bounds.size;
result.origin.x=_position.x-result.size.width*_anchorPoint.x;
result.origin.y=_position.y-result.size.height*_anchorPoint.y;
return result;
}
-(void)setFrame:(CGRect)value {
CGPoint position;
position.x=value.origin.x+value.size.width*_anchorPoint.x;
position.y=value.origin.y+value.size.height*_anchorPoint.y;
[self setPosition:position];
CGRect bounds=_bounds;
bounds.size=value.size;
[self setBounds:bounds];
}
-(float)opacity {
return _opacity;
}
-(void)setOpacity:(float)value {
CAAnimation *animation=[self animationForKey:@"opacity"];
if(animation==nil && ![CATransaction disableActions]){
id action=[self actionForKey:@"opacity"];
if(action!=nil)
[self addAnimation:action forKey:@"opacity"];
}
_opacity=value;
}
-(BOOL)opaque {
return _opaque;
}
-(void)setOpaque:(BOOL)value {
_opaque=value;
}
-(id)contents {
return _contents;
}
-(void)setContents:(id)value {
value=[value retain];
[_contents release];
_contents=value;
}
-(CATransform3D)transform {
return _transform;
}
-(void)setTransform:(CATransform3D)value {
_transform=value;
}
-(CATransform3D)sublayerTransform {
return _sublayerTransform;
}
-(void)setSublayerTransform:(CATransform3D)value {
_sublayerTransform=value;
}
-(NSString *)minificationFilter {
return _minificationFilter;
}
-(void)setMinificationFilter:(NSString *)value {
value=[value copy];
[_minificationFilter release];
_minificationFilter=value;
}
-(NSString *)magnificationFilter {
return _magnificationFilter;
}
-(void)setMagnificationFilter:(NSString *)value {
value=[value copy];
[_magnificationFilter release];
_magnificationFilter=value;
}
-init {
_superlayer=nil;
_sublayers=[NSArray new];
_delegate=nil;
_anchorPoint=CGPointMake(0.5,0.5);
_position=CGPointZero;
_bounds=CGRectZero;
_opacity=1.0;
_opaque=YES;
_contents=nil;
_transform=CATransform3DIdentity;
_sublayerTransform=CATransform3DIdentity;
_minificationFilter=kCAFilterLinear;
_magnificationFilter=kCAFilterLinear;
_animations=[[NSMutableDictionary alloc] init];
return self;
}
-(void)dealloc {
[_sublayers release];
[_animations release];
[_minificationFilter release];
[_magnificationFilter release];
[super dealloc];
}
-(void)_setSuperLayer:(CALayer *)parent {
_superlayer=parent;
}
-(void)_removeSublayer:(CALayer *)child {
NSMutableArray *layers=[_sublayers mutableCopy];
[layers removeObjectIdenticalTo:child];
[self setSublayers:layers];
[layers release];
}
-(void)addSublayer:(CALayer *)layer {
[self setSublayers:[_sublayers arrayByAddingObject:layer]];
}
-(void)replaceSublayer:(CALayer *)layer with:(CALayer *)other {
NSMutableArray *layers=[_sublayers mutableCopy];
NSUInteger index=[_sublayers indexOfObjectIdenticalTo:layer];
[layers replaceObjectAtIndex:index withObject:other];
[self setSublayers:layers];
[layers release];
layer->_superlayer=nil;
}
-(void)display {
if([_delegate respondsToSelector:@selector(displayLayer:)])
[_delegate displayLayer:self];
else {
#if 0
#warning create bitmap context
[self drawInContext:context];
_contents=image;
[self setContents:image];
#endif
}
}
-(void)displayIfNeeded {
}
-(void)drawInContext:(CGContextRef)context {
if([_delegate respondsToSelector:@selector(drawLayer:inContext:)])
[_delegate drawLayer:self inContext:context];
}
-(BOOL)needsDisplay {
return _needsDisplay;
}
-(void)removeFromSuperlayer {
[_superlayer _removeSublayer:self];
_superlayer=nil;
[self _setContext:nil];
}
-(void)setNeedsDisplay {
_needsDisplay=YES;
}
-(void)setNeedsDisplayInRect:(CGRect)rect {
_needsDisplay=YES;
}
-(void)addAnimation:(CAAnimation *)animation forKey:(NSString *)key {
if(_context==nil)
return;
[_animations setObject:animation forKey:key];
[_context startTimerIfNeeded];
}
-(CAAnimation *)animationForKey:(NSString *)key {
return [_animations objectForKey:key];
}
-(void)removeAllAnimations {
[_animations removeAllObjects];
}
-(void)removeAnimationForKey:(NSString *)key {
[_animations removeObjectForKey:key];
}
-(NSArray *)animationKeys {
return [_animations allKeys];
}
-valueForKey:(NSString *)key {
// FIXME: KVC appears broken for structs
if([key isEqualToString:@"bounds"])
return [NSValue valueWithRect:_bounds];
if([key isEqualToString:@"frame"])
return [NSValue valueWithRect:[self frame]];
return [super valueForKey:key];
}
-(id <CAAction>)actionForKey:(NSString *)key {
CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:key];
[basic setFromValue:[self valueForKey:key]];
return basic;
}
-(NSNumber *)_textureId {
return _textureId;
}
-(void)_setTextureId:(NSNumber *)value {
value=[value copy];
[_textureId release];
_textureId=value;
}
@end