PageRenderTime 54ms CodeModel.GetById 29ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/MapView/Map/RMTiledLayerController.m

http://github.com/route-me/route-me
Objective C | 159 lines | 91 code | 32 blank | 36 comment | 5 complexity | e0624cfc54438073e0a6ac63207fd50d MD5 | raw file
  1//
  2//  RMTiledLayerController.m
  3//
  4// Copyright (c) 2008-2009, Route-Me Contributors
  5// All rights reserved.
  6//
  7// Redistribution and use in source and binary forms, with or without
  8// modification, are permitted provided that the following conditions are met:
  9//
 10// * Redistributions of source code must retain the above copyright notice, this
 11//   list of conditions and the following disclaimer.
 12// * Redistributions in binary form must reproduce the above copyright notice,
 13//   this list of conditions and the following disclaimer in the documentation
 14//   and/or other materials provided with the distribution.
 15//
 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26// POSSIBILITY OF SUCH DAMAGE.
 27#import "RMGlobalConstants.h"
 28#import "RMTiledLayerController.h"
 29#import "RMFractalTileProjection.h"
 30#import "RMTileSource.h"
 31
 32@implementation RMTiledLayerController
 33
 34@synthesize layer;
 35
 36-(id) initWithTileSource: (id <RMTileSource>) _tileSource
 37{
 38	if (![super init])
 39		return nil;
 40	
 41	@throw [NSException exceptionWithName:@"NotImplementedExcption" reason:@"TiledLayerController is not complete. Use CoreAnimationRenderer instead." userInfo:nil];
 42
 43	tileSource = _tileSource;
 44	[tileSource retain];
 45	RMFractalTileProjection *tileProjection = [tileSource tileProjection];
 46	
 47	layer = [CATiledLayer layer];
 48	layer.delegate = self;
 49
 50	layer.levelsOfDetail = tileProjection.maxZoom + 1; // check this.
 51	layer.levelsOfDetailBias = 1; // Allows zoom level 0.
 52	
 53	layer.tileSize = CGSizeMake(tileProjection.tileSideLength,
 54								tileProjection.tileSideLength);
 55	
 56	RMXYRect boundsRect = tileProjection.bounds;
 57	layer.bounds = CGRectMake(boundsRect.origin.x, boundsRect.origin.y, boundsRect.size.width, boundsRect.size.height) ;
 58	layer.position = CGPointZero;
 59
 60	[self setScale:1];
 61	[layer setNeedsDisplay];
 62	
 63	return self;
 64}
 65
 66-(void) dealloc
 67{
 68	layer.delegate = nil;
 69	[layer release];
 70	
 71	[super dealloc];
 72}
 73
 74-(void) setScale: (float) _scale
 75{
 76	scale = _scale;
 77	layer.transform = CATransform3DMakeScale(1/scale,1/scale, 1.0f);
 78}
 79
 80- (float) scale
 81{
 82	return scale;
 83}
 84
 85-(void) centerProjectedPoint: (RMProjectedPoint) aPoint animate: (BOOL) animate
 86{
 87	if (animate == NO)
 88	{
 89		[CATransaction begin];
 90		[CATransaction setValue:[NSNumber numberWithFloat:0.0f]
 91						 forKey:kCATransactionAnimationDuration];
 92	}
 93	
 94//	layer.position = CGPointMake(point.x, point.y);
 95
 96	if (animate == NO)
 97	{
 98		[CATransaction commit];
 99	}
100	
101//	topLeft = point;
102//	topLeft.x -= viewSize.width * scale / 2;
103//	topLeft.y -= viewSize.height * scale / 2;
104}
105
106-(void) centerLatLong: (CLLocationCoordinate2D) aPoint animate: (BOOL) animate
107{
108	[self centerMercator:[RMMercator toMercator:aPoint] animate: animate];
109}
110
111-(void) dragBy: (CGSize) delta
112{	
113	[CATransaction begin];
114	[CATransaction setValue:[NSNumber numberWithFloat:0.0f]
115					 forKey:kCATransactionAnimationDuration];
116	
117	layer.position = CGPointMake(layer.position.x + delta.width,
118									  layer.position.y + delta.height);
119	[CATransaction commit];
120}
121
122-(void) zoomByFactor: (float) zoomFactor near:(CGPoint) center
123{
124	[self setScale: scale * zoomFactor];
125}
126
127- (void)drawLayer:(CALayer *)theLayer
128        inContext:(CGContextRef)theContext
129{
130	RMLog(@"drawLayer:inContext:");
131	
132	//	CGRect visibleRect = [self visibleRect];
133	//	RMLog(@"visibleRect: %d %d %d %d", visibleRect.origin.x, visibleRect.origin.y, visibleRect.size.width, visibleRect.size.height);
134	
135	CGRect rect = CGContextGetClipBoundingBox(theContext);
136	//	RMLog(@"rect: %d %d %d %d", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
137	
138	//CGAffineTransform transform = CGContextGetCTM(theContext);
139	//	RMLog(@"transform scale: a:%f b:%f c:%f d:%f tx:%f ty:%f", transform.a, transform.b, transform.c, transform.d, transform.tx, transform.ty);
140
141	/// \bug magic string literals
142	NSString *path = [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"png"];
143	CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([path UTF8String]);
144	CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, FALSE, kCGRenderingIntentDefault);
145	CGDataProviderRelease(dataProvider);
146	
147	CGContextDrawImage(theContext, rect, image);
148}
149
150- (void)setFrame:(CGRect)frame
151{
152}
153
154- (CALayer*) layer
155{
156	return layer;
157}
158
159@end