TileLoader.m
3.8 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
//
// TimeImageSet.m
// Images
//
// Created by Joseph Gentle on 29/08/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TileLoader.h"
#import "TileImage.h"
#import "TileSource.h"
#import "MathUtils.h"
#import "ScreenProjection.h"
#import "FractalTileProjection.h"
#import "TileImageSet.h"
NSString* const MapImageRemovedFromScreenNotification = @"MapImageRemovedFromScreen";
@implementation TileLoader
@synthesize loadedBounds, loadedZoom;
/*
-(id) initFromRect:(TileRect) rect FromImageSource: (id<TileSource>)source ToDisplayIn:(CGRect)bounds WithTileDelegate: (id)delegate
{
if (![self init])
return nil;
[self assembleFromRect:rect FromImageSource: source ToDisplayIn:bounds WithTileDelegate: delegate];
return self;
}*/
-(TileImage*) makeTileImageFor:(Tile) tile
{
return [tileSource tileImage:tile];
}
-(id) init
{
if (![self initForScreen:nil FromImageSource:nil])
return nil;
return self;
}
-(id) initForScreen: (ScreenProjection*)screen FromImageSource: (id<TileSource>)source
{
if (![super init])
return nil;
images = [[TileImageSet alloc] initWithDelegate:self];
loadedBounds = CGRectMake(0, 0, 0, 0);
loadedTiles.origin.tile = TileDummy();
screenProjection = [screen retain];
tileSource = [source retain];
return self;
}
-(void) dealloc
{
NSLog(@"Imageset dealloced");
[images release];
// [buffer release];
[screenProjection release];
[tileSource release];
[super dealloc];
}
/*
-(void) swapBuffers
{
NSMutableSet *temp = images;
images = buffer;
buffer = temp;
}*/
-(BOOL) screenIsLoaded
{
// return CGRectContainsRect(loadedBounds, [screenProjection screenBounds])
// && loadedZoom == [[tileSource tileProjection] calculateNormalisedZoomFromScale:[screenProjection scale]];
BOOL contained = CGRectContainsRect(loadedBounds, [screenProjection screenBounds]);
float targetZoom = [[tileSource tileProjection] calculateNormalisedZoomFromScale:[screenProjection scale]];
// && loadedZoom == ;
if (contained == NO)
{
NSLog(@"reassembling because its not contained");
}
if (targetZoom != loadedZoom)
{
NSLog(@"reassembling because target zoom = %f, loaded zoom = %d", targetZoom, loadedZoom);
}
return contained && targetZoom == loadedZoom;
}
-(void) tileRemoved: (Tile) tile
{
TileImage *image = [images imageWithTile:tile];
[[NSNotificationCenter defaultCenter] postNotificationName:MapImageRemovedFromScreenNotification object:image];
}
-(void) tileAdded: (Tile) tile WithImage: (TileImage*) image
{
// [[NSNotificationCenter defaultCenter] postNotificationName:MapImageRemovedFromScreenNotification object:[NSValue valueWithBytes:&tile objCType:@encode(Tile)]];
}
-(CGRect) currentRendererBounds
{
return [screenProjection screenBounds];
}
-(void) assemble
{
if ([self screenIsLoaded])
return;
if (tileSource == nil || screenProjection == nil)
return;
NSLog(@"assemble count = %d", [images count]);
FractalTileProjection *tileProjection = [tileSource tileProjection];
TileRect newTileRect = [tileProjection project:screenProjection];
CGRect newLoadedBounds = [images addTiles:newTileRect ToDisplayIn:[self currentRendererBounds]];
if (!TileIsDummy(loadedTiles.origin.tile))
[images removeTiles:loadedTiles];
NSLog(@"-> count = %d", [images count]);
loadedBounds = newLoadedBounds;
loadedZoom = newTileRect.origin.tile.zoom;
loadedTiles = newTileRect;
}
- (void)moveBy: (CGSize) delta
{
[images moveBy:delta];
loadedBounds = TranslateCGRectBy(loadedBounds, delta);
// [self assemble];
}
- (void)zoomByFactor: (float) zoomFactor Near:(CGPoint) center
{
[images zoomByFactor:zoomFactor Near:center];
loadedBounds = ScaleCGRectAboutPoint(loadedBounds, zoomFactor, center);
// [self assemble];
}
-(BOOL) containsRect: (CGRect)bounds
{
return CGRectContainsRect(loadedBounds, bounds);
}
-(void) draw
{
// [self assemble];
[images draw];
}
@end