RMLayerSet.m
2.62 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
//
// RMLayerSet.m
// MapView
//
// Created by Joseph Gentle on 1/10/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "RMLayerSet.h"
#import "RMMapContents.h"
#import "RMMercatorToScreenProjection.h"
@implementation RMLayerSet
- (id)initForContents: (RMMapContents *)_contents
{
if (![super init])
return nil;
set = [[NSMutableArray alloc] init];
mapContents = _contents;
self.masksToBounds = YES;
return self;
}
- (void)correctScreenPosition: (CALayer *)layer
{
if ([layer conformsToProtocol:@protocol(RMMovingMapLayer)])
{
// Kinda ugly.
CALayer<RMMovingMapLayer>* layer_with_proto = (CALayer<RMMovingMapLayer>*)layer;
RMXYPoint location = [layer_with_proto location];
layer_with_proto.position = [[mapContents mercatorToScreenProjection] projectXYPoint:location];
}
}
- (void)setSublayers: (NSArray*)array
{
for (CALayer *layer in array)
{
[self correctScreenPosition:layer];
}
[set removeAllObjects];
[set addObjectsFromArray:array];
[super setSublayers:array];
}
- (void)addSublayer:(CALayer *)layer
{
[self correctScreenPosition:layer];
[set addObject:layer];
[super addSublayer:layer];
}
- (void)insertSublayer:(CALayer *)layer above:(CALayer *)siblingLayer
{
[self correctScreenPosition:layer];
int index = [set indexOfObject:siblingLayer];
[set insertObject:layer atIndex:index + 1];
[super insertSublayer:layer above:siblingLayer];
}
- (void)insertSublayer:(CALayer *)layer below:(CALayer *)siblingLayer
{
[self correctScreenPosition:layer];
int index = [set indexOfObject:siblingLayer];
[set insertObject:layer atIndex:index];
[super insertSublayer:layer below:siblingLayer];
}
- (void)insertSublayer:(CALayer *)layer atIndex:(unsigned)index
{
[self correctScreenPosition:layer];
[set insertObject:layer atIndex:index];
// TODO: Fix this.
[super addSublayer:layer];
}
/*
- (void)insertSublayer:(RMMapLayer*) layer below:(RMMapLayer*)sibling;
- (void)insertSublayer:(RMMapLayer*) layer above:(RMMapLayer*)sibling;
- (void)removeSublayer:(RMMapLayer*) layer;
*/
- (void)moveToXYPoint: (RMXYPoint)aPoint
{
// TODO: Test this. Does it work?
[self correctPositionOfAllSublayers];
}
- (void)moveBy: (CGSize) delta
{
for (id layer in set)
{
if ([layer respondsToSelector:@selector(moveBy:)])
[layer moveBy:delta];
// if layer moves on and offscreen...
}
}
- (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
{
for (id layer in set)
{
if ([layer respondsToSelector:@selector(zoomByFactor:near:)])
[layer zoomByFactor:zoomFactor near:center];
}
}
- (void) correctPositionOfAllSublayers
{
for (id layer in set)
{
[self correctScreenPosition:layer];
}
}
@end