forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTLayer.m
More file actions
1108 lines (897 loc) · 33.1 KB
/
CPTLayer.m
File metadata and controls
1108 lines (897 loc) · 33.1 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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "CPTLayer.h"
#import "CPTGraph.h"
#import "CPTPathExtensions.h"
#import "CPTPlatformSpecificCategories.h"
#import "CPTPlatformSpecificFunctions.h"
#import "CPTShadow.h"
#import "CPTUtilities.h"
#import "CorePlotProbes.h"
#import "NSCoderExtensions.h"
#import <objc/runtime.h>
#import <tgmath.h>
NSString *const CPTLayerBoundsDidChangeNotification = @"CPTLayerBoundsDidChangeNotification";
/** @defgroup animation Animatable Properties
* @brief Custom layer properties that can be animated using Core Animation.
* @if MacOnly
* @since Custom layer property animation is supported on MacOS 10.6 and later.
* @endif
**/
/** @defgroup notification Notifications
* @brief Notifications used by Core Plot.
**/
/// @cond
@interface CPTLayer()
@property (nonatomic, readwrite, getter = isRenderingRecursively) BOOL renderingRecursively;
@property (nonatomic, readwrite, assign) BOOL useFastRendering;
-(void)applyTransform:(CATransform3D)transform toContext:(nonnull CGContextRef)context;
-(nonnull NSString *)subLayersAtIndex:(NSUInteger)idx;
@end
/// @endcond
#pragma mark -
/** @brief Base class for all Core Animation layers in Core Plot.
*
* Unless @ref useFastRendering is @YES,
* all drawing is done in a way that preserves the
* drawing vectors. Sublayers are arranged automatically to fill the layer’s
* bounds, minus any padding. Default animations for changes in position, bounds,
* and sublayers are turned off. The default layer is not opaque and does not mask
* to bounds.
**/
@implementation CPTLayer
/** @property nullable CPTGraph *graph
* @brief The graph for the layer.
**/
@synthesize graph;
/** @property CGFloat paddingLeft
* @brief Amount to inset the left side of each sublayer.
**/
@synthesize paddingLeft;
/** @property CGFloat paddingTop
* @brief Amount to inset the top of each sublayer.
**/
@synthesize paddingTop;
/** @property CGFloat paddingRight
* @brief Amount to inset the right side of each sublayer.
**/
@synthesize paddingRight;
/** @property CGFloat paddingBottom
* @brief Amount to inset the bottom of each sublayer.
**/
@synthesize paddingBottom;
/** @property BOOL masksToBorder
* @brief If @YES, a sublayer mask is applied to clip sublayer content to the inside of the border.
**/
@synthesize masksToBorder;
/** @property CGFloat contentsScale
* @brief The scale factor applied to the layer.
**/
@dynamic contentsScale;
/** @property CPTShadow *shadow
* @brief The shadow drawn under the layer content. If @nil (the default), no shadow is drawn.
**/
@synthesize shadow;
/** @property CGSize shadowMargin
* @brief The maximum margin size needed to fully enclose the layer @ref shadow.
**/
@dynamic shadowMargin;
/** @property nullable CGPathRef outerBorderPath
* @brief A drawing path that encompasses the outer boundary of the layer border.
**/
@synthesize outerBorderPath;
/** @property nullable CGPathRef innerBorderPath
* @brief A drawing path that encompasses the inner boundary of the layer border.
**/
@synthesize innerBorderPath;
/** @property nullable CGPathRef maskingPath
* @brief A drawing path that encompasses the layer content including any borders. Set to @NULL when no masking is desired.
*
* This path defines the outline of the layer and is used to mask all drawing. Set to @NULL when no masking is desired.
* The caller must @emph{not} release the path returned by this property.
**/
@dynamic maskingPath;
/** @property nullable CGPathRef sublayerMaskingPath
* @brief A drawing path that encompasses the layer content excluding any borders. Set to @NULL when no masking is desired.
*
* This path defines the outline of the part of the layer where sublayers should draw and is used to mask all sublayer drawing.
* Set to @NULL when no masking is desired.
* The caller must @emph{not} release the path returned by this property.
**/
@dynamic sublayerMaskingPath;
/** @property nullable CPTSublayerSet *sublayersExcludedFromAutomaticLayout
* @brief A set of sublayers that should be excluded from the automatic sublayer layout.
**/
@dynamic sublayersExcludedFromAutomaticLayout;
/** @property BOOL useFastRendering
* @brief If @YES, subclasses should optimize their drawing for speed over precision.
**/
@synthesize useFastRendering;
/** @property nullable id<NSCopying, NSCoding, NSObject> identifier
* @brief An object used to identify the layer in collections.
**/
@synthesize identifier;
// Private properties
@synthesize renderingRecursively;
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Initializes a newly allocated CPTLayer object with the provided frame rectangle.
*
* This is the designated initializer. The initialized layer will have the following properties:
* - @ref paddingLeft = @num{0.0}
* - @ref paddingTop = @num{0.0}
* - @ref paddingRight = @num{0.0}
* - @ref paddingBottom = @num{0.0}
* - @ref masksToBorder = @NO
* - @ref shadow = @nil
* - @ref useFastRendering = @NO
* - @ref graph = @nil
* - @ref outerBorderPath = @NULL
* - @ref innerBorderPath = @NULL
* - @ref identifier = @nil
* - @ref needsDisplayOnBoundsChange = @NO
* - @ref opaque = @NO
* - @ref masksToBounds = @NO
*
* @param newFrame The frame rectangle.
* @return The initialized object.
**/
-(nonnull instancetype)initWithFrame:(CGRect)newFrame
{
if ( (self = [super init]) ) {
paddingLeft = CPTFloat(0.0);
paddingTop = CPTFloat(0.0);
paddingRight = CPTFloat(0.0);
paddingBottom = CPTFloat(0.0);
masksToBorder = NO;
shadow = nil;
renderingRecursively = NO;
useFastRendering = NO;
graph = nil;
outerBorderPath = NULL;
innerBorderPath = NULL;
identifier = nil;
self.frame = newFrame;
self.needsDisplayOnBoundsChange = NO;
self.opaque = NO;
self.masksToBounds = NO;
}
return self;
}
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTLayer object with an empty frame rectangle.
* @return The initialized object.
**/
-(nonnull instancetype)init
{
return [self initWithFrame:CGRectZero];
}
/// @}
/** @brief Override to copy or initialize custom fields of the specified layer.
* @param layer The layer from which custom fields should be copied.
* @return A layer instance with any custom instance variables copied from @par{layer}.
*/
-(nonnull instancetype)initWithLayer:(nonnull id)layer
{
if ( (self = [super initWithLayer:layer]) ) {
CPTLayer *theLayer = (CPTLayer *)layer;
paddingLeft = theLayer->paddingLeft;
paddingTop = theLayer->paddingTop;
paddingRight = theLayer->paddingRight;
paddingBottom = theLayer->paddingBottom;
masksToBorder = theLayer->masksToBorder;
shadow = theLayer->shadow;
renderingRecursively = theLayer->renderingRecursively;
graph = theLayer->graph;
outerBorderPath = CGPathRetain(theLayer->outerBorderPath);
innerBorderPath = CGPathRetain(theLayer->innerBorderPath);
identifier = theLayer->identifier;
}
return self;
}
/// @cond
-(void)dealloc
{
graph = nil;
CGPathRelease(outerBorderPath);
CGPathRelease(innerBorderPath);
}
/// @endcond
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(nonnull NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeCGFloat:self.paddingLeft forKey:@"CPTLayer.paddingLeft"];
[coder encodeCGFloat:self.paddingTop forKey:@"CPTLayer.paddingTop"];
[coder encodeCGFloat:self.paddingRight forKey:@"CPTLayer.paddingRight"];
[coder encodeCGFloat:self.paddingBottom forKey:@"CPTLayer.paddingBottom"];
[coder encodeBool:self.masksToBorder forKey:@"CPTLayer.masksToBorder"];
[coder encodeObject:self.shadow forKey:@"CPTLayer.shadow"];
[coder encodeConditionalObject:self.graph forKey:@"CPTLayer.graph"];
[coder encodeObject:self.identifier forKey:@"CPTLayer.identifier"];
// No need to archive these properties:
// renderingRecursively
// outerBorderPath
// innerBorderPath
}
/// @endcond
/** @brief Returns an object initialized from data in a given unarchiver.
* @param coder An unarchiver object.
* @return An object initialized from data in a given unarchiver.
*/
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)coder
{
if ( (self = [super initWithCoder:coder]) ) {
paddingLeft = [coder decodeCGFloatForKey:@"CPTLayer.paddingLeft"];
paddingTop = [coder decodeCGFloatForKey:@"CPTLayer.paddingTop"];
paddingRight = [coder decodeCGFloatForKey:@"CPTLayer.paddingRight"];
paddingBottom = [coder decodeCGFloatForKey:@"CPTLayer.paddingBottom"];
masksToBorder = [coder decodeBoolForKey:@"CPTLayer.masksToBorder"];
shadow = [[coder decodeObjectOfClass:[CPTShadow class]
forKey:@"CPTLayer.shadow"] copy];
graph = [coder decodeObjectOfClass:[CPTGraph class]
forKey:@"CPTLayer.graph"];
identifier = [[coder decodeObjectOfClass:[NSObject class]
forKey:@"CPTLayer.identifier"] copy];
renderingRecursively = NO;
outerBorderPath = NULL;
innerBorderPath = NULL;
}
return self;
}
#pragma mark -
#pragma mark NSSecureCoding Methods
/// @cond
+(BOOL)supportsSecureCoding
{
return YES;
}
/// @endcond
#pragma mark -
#pragma mark Animation
/// @cond
-(id<CAAction>)actionForKey:(nonnull NSString *)aKey
{
return nil;
}
/// @endcond
#pragma mark -
#pragma mark Drawing
/// @cond
-(void)display
{
if ( self.hidden ) {
return;
}
else {
[super display];
}
}
-(void)drawInContext:(nonnull CGContextRef)context
{
if ( context ) {
self.useFastRendering = YES;
[self renderAsVectorInContext:context];
self.useFastRendering = NO;
}
else {
NSLog(@"%@: Tried to draw into a NULL context", self);
}
}
/// @endcond
/** @brief Draws layer content into the provided graphics context.
*
* This method replaces the CALayer @link CALayer::drawInContext: -drawInContext: @endlink method
* to ensure that layer content is always drawn as vectors
* and objects rather than as a cached bit-mapped image representation.
* Subclasses should do all drawing here and must call @super to set up the clipping path.
*
* @param context The graphics context to draw into.
**/
-(void)renderAsVectorInContext:(nonnull CGContextRef)context
{
// This is where subclasses do their drawing
if ( self.renderingRecursively ) {
[self applyMaskToContext:context];
}
[self.shadow setShadowInContext:context];
}
/** @brief Draws layer content and the content of all sublayers into the provided graphics context.
* @param context The graphics context to draw into.
**/
-(void)recursivelyRenderInContext:(nonnull CGContextRef)context
{
if ( !self.hidden ) {
// render self
CGContextSaveGState(context);
[self applyTransform:self.transform toContext:context];
self.renderingRecursively = YES;
if ( !self.masksToBounds ) {
CGContextSaveGState(context);
}
[self renderAsVectorInContext:context];
if ( !self.masksToBounds ) {
CGContextRestoreGState(context);
}
self.renderingRecursively = NO;
// render sublayers
CPTSublayerArray *sublayersCopy = [self.sublayers copy];
for ( CALayer *currentSublayer in sublayersCopy ) {
CGContextSaveGState(context);
// Shift origin of context to match starting coordinate of sublayer
CGPoint currentSublayerFrameOrigin = currentSublayer.frame.origin;
CGRect currentSublayerBounds = currentSublayer.bounds;
CGContextTranslateCTM(context,
currentSublayerFrameOrigin.x - currentSublayerBounds.origin.x,
currentSublayerFrameOrigin.y - currentSublayerBounds.origin.y);
[self applyTransform:self.sublayerTransform toContext:context];
if ( [currentSublayer isKindOfClass:[CPTLayer class]] ) {
[(CPTLayer *) currentSublayer recursivelyRenderInContext:context];
}
else {
if ( self.masksToBounds ) {
CGContextClipToRect(context, currentSublayer.bounds);
}
[currentSublayer drawInContext:context];
}
CGContextRestoreGState(context);
}
CGContextRestoreGState(context);
}
}
/// @cond
-(void)applyTransform:(CATransform3D)transform3D toContext:(nonnull CGContextRef)context
{
if ( !CATransform3DIsIdentity(transform3D) ) {
if ( CATransform3DIsAffine(transform3D) ) {
CGRect selfBounds = self.bounds;
CGPoint anchorPoint = self.anchorPoint;
CGPoint anchorOffset = CPTPointMake(anchorOffset.x = selfBounds.origin.x + anchorPoint.x * selfBounds.size.width,
anchorOffset.y = selfBounds.origin.y + anchorPoint.y * selfBounds.size.height);
CGAffineTransform affineTransform = CGAffineTransformMakeTranslation(-anchorOffset.x, -anchorOffset.y);
affineTransform = CGAffineTransformConcat( affineTransform, CATransform3DGetAffineTransform(transform3D) );
affineTransform = CGAffineTransformTranslate(affineTransform, anchorOffset.x, anchorOffset.y);
CGRect transformedBounds = CGRectApplyAffineTransform(selfBounds, affineTransform);
CGContextTranslateCTM(context, -transformedBounds.origin.x, -transformedBounds.origin.y);
CGContextConcatCTM(context, affineTransform);
}
}
}
/// @endcond
/** @brief Updates the layer layout if needed and then draws layer content and the content of all sublayers into the provided graphics context.
* @param context The graphics context to draw into.
*/
-(void)layoutAndRenderInContext:(nonnull CGContextRef)context
{
[self layoutIfNeeded];
[self recursivelyRenderInContext:context];
}
/** @brief Draws layer content and the content of all sublayers into a PDF document.
* @return PDF representation of the layer content.
**/
-(nonnull NSData *)dataForPDFRepresentationOfLayer
{
NSMutableData *pdfData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData( (__bridge CFMutableDataRef)pdfData );
const CGRect mediaBox = CPTRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &mediaBox, NULL);
CPTPushCGContext(pdfContext);
CGContextBeginPage(pdfContext, &mediaBox);
[self layoutAndRenderInContext:pdfContext];
CGContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CPTPopCGContext();
CGContextRelease(pdfContext);
CGDataConsumerRelease(dataConsumer);
return pdfData;
}
#pragma mark -
#pragma mark Responder Chain and User interaction
/// @name User Interaction
/// @{
-(BOOL)pointingDeviceDownEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
return NO;
}
-(BOOL)pointingDeviceUpEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
return NO;
}
-(BOOL)pointingDeviceDraggedEvent:(nonnull CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
return NO;
}
-(BOOL)pointingDeviceCancelledEvent:(nonnull CPTNativeEvent *)event
{
return NO;
}
#if TARGET_OS_SIMULATOR || TARGET_OS_IPHONE
#else
-(BOOL)scrollWheelEvent:(nonnull CPTNativeEvent *)event fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
return NO;
}
#endif
/// @}
#pragma mark -
#pragma mark Layout
/**
* @brief Align the receiver’s position with pixel boundaries.
**/
-(void)pixelAlign
{
CGFloat scale = self.contentsScale;
CGPoint currentPosition = self.position;
CGSize boundsSize = self.bounds.size;
CGSize frameSize = self.frame.size;
CGPoint newPosition;
if ( CGSizeEqualToSize(boundsSize, frameSize) ) { // rotated 0° or 180°
CGPoint anchor = self.anchorPoint;
CGPoint newAnchor = CGPointMake(boundsSize.width * anchor.x,
boundsSize.height * anchor.y);
if ( scale == CPTFloat(1.0) ) {
newPosition.x = ceil( currentPosition.x - newAnchor.x - CPTFloat(0.5) ) + newAnchor.x;
newPosition.y = ceil( currentPosition.y - newAnchor.y - CPTFloat(0.5) ) + newAnchor.y;
}
else {
newPosition.x = ceil( (currentPosition.x - newAnchor.x) * scale - CPTFloat(0.5) ) / scale + newAnchor.x;
newPosition.y = ceil( (currentPosition.y - newAnchor.y) * scale - CPTFloat(0.5) ) / scale + newAnchor.y;
}
}
else if ( (boundsSize.width == frameSize.height) && (boundsSize.height == frameSize.width) ) { // rotated 90° or 270°
CGPoint anchor = self.anchorPoint;
CGPoint newAnchor = CGPointMake(boundsSize.height * anchor.y,
boundsSize.width * anchor.x);
if ( scale == CPTFloat(1.0) ) {
newPosition.x = ceil( currentPosition.x - newAnchor.x - CPTFloat(0.5) ) + newAnchor.x;
newPosition.y = ceil( currentPosition.y - newAnchor.y - CPTFloat(0.5) ) + newAnchor.y;
}
else {
newPosition.x = ceil( (currentPosition.x - newAnchor.x) * scale - CPTFloat(0.5) ) / scale + newAnchor.x;
newPosition.y = ceil( (currentPosition.y - newAnchor.y) * scale - CPTFloat(0.5) ) / scale + newAnchor.y;
}
}
else {
if ( scale == CPTFloat(1.0) ) {
newPosition.x = round(currentPosition.x);
newPosition.y = round(currentPosition.y);
}
else {
newPosition.x = round(currentPosition.x * scale) / scale;
newPosition.y = round(currentPosition.y * scale) / scale;
}
}
self.position = newPosition;
}
/// @cond
-(void)setPaddingLeft:(CGFloat)newPadding
{
if ( newPadding != paddingLeft ) {
paddingLeft = newPadding;
[self setNeedsLayout];
}
}
-(void)setPaddingRight:(CGFloat)newPadding
{
if ( newPadding != paddingRight ) {
paddingRight = newPadding;
[self setNeedsLayout];
}
}
-(void)setPaddingTop:(CGFloat)newPadding
{
if ( newPadding != paddingTop ) {
paddingTop = newPadding;
[self setNeedsLayout];
}
}
-(void)setPaddingBottom:(CGFloat)newPadding
{
if ( newPadding != paddingBottom ) {
paddingBottom = newPadding;
[self setNeedsLayout];
}
}
-(CGSize)shadowMargin
{
CGSize margin = CGSizeZero;
CPTShadow *myShadow = self.shadow;
if ( myShadow ) {
CGSize shadowOffset = myShadow.shadowOffset;
CGFloat shadowRadius = myShadow.shadowBlurRadius;
margin = CGSizeMake( ceil( ABS(shadowOffset.width) + ABS(shadowRadius) ), ceil( ABS(shadowOffset.height) + ABS(shadowRadius) ) );
}
return margin;
}
/// @endcond
/// @name Layout
/// @{
/**
* @brief Updates the layout of all sublayers. Sublayers fill the super layer’s bounds minus any padding.
*
* This is where we do our custom replacement for the Mac-only layout manager and autoresizing mask.
* Subclasses should override this method to provide a different layout of their own sublayers.
**/
-(void)layoutSublayers
{
CGRect selfBounds = self.bounds;
CPTSublayerArray *mySublayers = self.sublayers;
if ( mySublayers.count > 0 ) {
CGFloat leftPadding, topPadding, rightPadding, bottomPadding;
[self sublayerMarginLeft:&leftPadding top:&topPadding right:&rightPadding bottom:&bottomPadding];
CGSize subLayerSize = selfBounds.size;
subLayerSize.width -= leftPadding + rightPadding;
subLayerSize.width = MAX( subLayerSize.width, CPTFloat(0.0) );
subLayerSize.width = round(subLayerSize.width);
subLayerSize.height -= topPadding + bottomPadding;
subLayerSize.height = MAX( subLayerSize.height, CPTFloat(0.0) );
subLayerSize.height = round(subLayerSize.height);
CGRect subLayerFrame;
subLayerFrame.origin = CGPointMake( round(leftPadding), round(bottomPadding) );
subLayerFrame.size = subLayerSize;
CPTSublayerSet *excludedSublayers = self.sublayersExcludedFromAutomaticLayout;
Class layerClass = [CPTLayer class];
for ( CALayer *subLayer in mySublayers ) {
if ( [subLayer isKindOfClass:layerClass] && ![excludedSublayers containsObject:subLayer] ) {
subLayer.frame = subLayerFrame;
}
}
}
}
/// @}
/// @cond
-(nullable CPTSublayerSet *)sublayersExcludedFromAutomaticLayout
{
return nil;
}
/// @endcond
/** @brief Returns the margins that should be left between the bounds of the receiver and all sublayers.
* @param left The left margin.
* @param top The top margin.
* @param right The right margin.
* @param bottom The bottom margin.
**/
-(void)sublayerMarginLeft:(nonnull CGFloat *)left top:(nonnull CGFloat *)top right:(nonnull CGFloat *)right bottom:(nonnull CGFloat *)bottom
{
*left = self.paddingLeft;
*top = self.paddingTop;
*right = self.paddingRight;
*bottom = self.paddingBottom;
}
#pragma mark -
#pragma mark Sublayers
/// @cond
-(void)setSublayers:(nullable CPTSublayerArray *)sublayers
{
super.sublayers = sublayers;
Class layerClass = [CPTLayer class];
CGFloat scale = self.contentsScale;
for ( CALayer *layer in sublayers ) {
if ( [layer isKindOfClass:layerClass] ) {
( (CPTLayer *)layer ).contentsScale = scale;
}
}
}
-(void)addSublayer:(nonnull CALayer *)layer
{
[super addSublayer:layer];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)insertSublayer:(nonnull CALayer *)layer atIndex:(unsigned)idx
{
[super insertSublayer:layer atIndex:idx];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)insertSublayer:(nonnull CALayer *)layer below:(nullable CALayer *)sibling
{
[super insertSublayer:layer below:sibling];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)insertSublayer:(nonnull CALayer *)layer above:(nullable CALayer *)sibling
{
[super insertSublayer:layer above:sibling];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)replaceSublayer:(nonnull CALayer *)layer with:(nonnull CALayer *)layer2
{
[super replaceSublayer:layer with:layer2];
if ( [layer2 isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer2 ).contentsScale = self.contentsScale;
}
}
/// @endcond
#pragma mark -
#pragma mark Masking
/// @cond
// default path is the rounded rect layer bounds
-(nullable CGPathRef)maskingPath
{
if ( self.masksToBounds ) {
CGPathRef path = self.outerBorderPath;
if ( path ) {
return path;
}
path = CPTCreateRoundedRectPath(self.bounds, self.cornerRadius);
self.outerBorderPath = path;
CGPathRelease(path);
return self.outerBorderPath;
}
else {
return NULL;
}
}
-(nullable CGPathRef)sublayerMaskingPath
{
return self.innerBorderPath;
}
/// @endcond
/** @brief Recursively sets the clipping path of the given graphics context to the sublayer masking paths of its superlayers.
*
* The clipping path is built by recursively climbing the layer tree and combining the sublayer masks from
* each super layer. The tree traversal stops when a layer is encountered that is not a CPTLayer.
*
* @param context The graphics context to clip.
* @param sublayer The sublayer that called this method.
* @param offset The cumulative position offset between the receiver and the first layer in the recursive calling chain.
**/
-(void)applySublayerMaskToContext:(nonnull CGContextRef)context forSublayer:(nonnull CPTLayer *)sublayer withOffset:(CGPoint)offset
{
CGPoint sublayerBoundsOrigin = sublayer.bounds.origin;
CGPoint layerOffset = offset;
if ( !self.renderingRecursively ) {
CGPoint convertedOffset = [self convertPoint:sublayerBoundsOrigin fromLayer:sublayer];
layerOffset.x += convertedOffset.x;
layerOffset.y += convertedOffset.y;
}
CGAffineTransform sublayerTransform = CATransform3DGetAffineTransform(sublayer.transform);
CGContextConcatCTM( context, CGAffineTransformInvert(sublayerTransform) );
CALayer *superlayer = self.superlayer;
if ( [superlayer isKindOfClass:[CPTLayer class]] ) {
[(CPTLayer *) superlayer applySublayerMaskToContext:context forSublayer:self withOffset:layerOffset];
}
CGPathRef maskPath = self.sublayerMaskingPath;
if ( maskPath ) {
CGContextTranslateCTM(context, -layerOffset.x, -layerOffset.y);
CGContextAddPath(context, maskPath);
CGContextClip(context);
CGContextTranslateCTM(context, layerOffset.x, layerOffset.y);
}
CGContextConcatCTM(context, sublayerTransform);
}
/** @brief Sets the clipping path of the given graphics context to mask the content.
*
* The clipping path is built by recursively climbing the layer tree and combining the sublayer masks from
* each super layer. The tree traversal stops when a layer is encountered that is not a CPTLayer.
*
* @param context The graphics context to clip.
**/
-(void)applyMaskToContext:(nonnull CGContextRef)context
{
CPTLayer *mySuperlayer = (CPTLayer *)self.superlayer;
if ( [mySuperlayer isKindOfClass:[CPTLayer class]] ) {
[mySuperlayer applySublayerMaskToContext:context forSublayer:self withOffset:CGPointZero];
}
CGPathRef maskPath = self.maskingPath;
if ( maskPath ) {
CGContextAddPath(context, maskPath);
CGContextClip(context);
}
}
/// @cond
-(void)setNeedsLayout
{
[super setNeedsLayout];
CPTGraph *theGraph = self.graph;
if ( theGraph ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTGraphNeedsRedrawNotification
object:theGraph];
}
}
-(void)setNeedsDisplay
{
[super setNeedsDisplay];
CPTGraph *theGraph = self.graph;
if ( theGraph ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTGraphNeedsRedrawNotification
object:theGraph];
}
}
/// @endcond
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setPosition:(CGPoint)newPosition
{
super.position = newPosition;
if ( COREPLOT_LAYER_POSITION_CHANGE_ENABLED() ) {
CGRect currentFrame = self.frame;
if ( !CGRectEqualToRect( currentFrame, CGRectIntegral(self.frame) ) ) {
COREPLOT_LAYER_POSITION_CHANGE( (const char *)class_getName([self class]),
(int)lrint( ceil( currentFrame.origin.x * CPTFloat(1000.0) ) ),
(int)lrint( ceil( currentFrame.origin.y * CPTFloat(1000.0) ) ),
(int)lrint( ceil( currentFrame.size.width * CPTFloat(1000.0) ) ),
(int)lrint( ceil( currentFrame.size.height * CPTFloat(1000.0) ) ) );
}
}
}
-(void)setHidden:(BOOL)newHidden
{
if ( newHidden != self.hidden ) {
super.hidden = newHidden;
if ( !newHidden ) {
[self setNeedsDisplay];
}
}
}
-(void)setContentsScale:(CGFloat)newContentsScale
{
NSParameterAssert( newContentsScale > CPTFloat(0.0) );
if ( self.contentsScale != newContentsScale ) {
if ( [CALayer instancesRespondToSelector:@selector(setContentsScale:)] ) {
super.contentsScale = newContentsScale;
[self setNeedsDisplay];
Class layerClass = [CPTLayer class];
for ( CALayer *subLayer in self.sublayers ) {
if ( [subLayer isKindOfClass:layerClass] ) {
subLayer.contentsScale = newContentsScale;
}
}
}
}
}
-(CGFloat)contentsScale
{
CGFloat scale = CPTFloat(1.0);
if ( [CALayer instancesRespondToSelector:@selector(contentsScale)] ) {
scale = super.contentsScale;
}
return scale;
}
-(void)setShadow:(nullable CPTShadow *)newShadow
{
if ( newShadow != shadow ) {
shadow = [newShadow copy];
[self setNeedsLayout];
[self setNeedsDisplay];
}
}
-(void)setOuterBorderPath:(nullable CGPathRef)newPath
{
if ( newPath != outerBorderPath ) {
CGPathRelease(outerBorderPath);
outerBorderPath = CGPathRetain(newPath);
}
}
-(void)setInnerBorderPath:(nullable CGPathRef)newPath
{
if ( newPath != innerBorderPath ) {
CGPathRelease(innerBorderPath);
innerBorderPath = CGPathRetain(newPath);
[self.mask setNeedsDisplay];
}
}
-(CGRect)bounds
{
CGRect actualBounds = super.bounds;
if ( self.shadow ) {
CGSize sizeOffset = self.shadowMargin;
actualBounds.origin.x += sizeOffset.width;
actualBounds.origin.y += sizeOffset.height;
actualBounds.size.width -= sizeOffset.width * CPTFloat(2.0);
actualBounds.size.height -= sizeOffset.height * CPTFloat(2.0);
}
return actualBounds;
}
-(void)setBounds:(CGRect)newBounds
{
if ( !CGRectEqualToRect(self.bounds, newBounds) ) {
if ( self.shadow ) {
CGSize sizeOffset = self.shadowMargin;
newBounds.origin.x -= sizeOffset.width;
newBounds.origin.y -= sizeOffset.height;
newBounds.size.width += sizeOffset.width * CPTFloat(2.0);
newBounds.size.height += sizeOffset.height * CPTFloat(2.0);
}
super.bounds = newBounds;
self.outerBorderPath = NULL;
self.innerBorderPath = NULL;
[[NSNotificationCenter defaultCenter] postNotificationName:CPTLayerBoundsDidChangeNotification
object:self];
}
}
-(CGPoint)anchorPoint
{
CGPoint adjustedAnchor = super.anchorPoint;
if ( self.shadow ) {
CGSize sizeOffset = self.shadowMargin;