/MapView/Map/RMCoreAnimationRenderer.m

http://github.com/route-me/route-me · Objective C · 197 lines · 96 code · 29 blank · 72 comment · 11 complexity · fb5b072269ad636665b65bb7858fe51e MD5 · raw file

  1. //
  2. // RMCoreAnimationRenderer.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 "RMCoreAnimationRenderer.h"
  29. #import <QuartzCore/QuartzCore.h>
  30. #import "RMTile.h"
  31. #import "RMTileLoader.h"
  32. #import "RMPixel.h"
  33. #import "RMTileImage.h"
  34. #import "RMTileImageSet.h"
  35. @implementation RMCoreAnimationRenderer
  36. - (id) initWithContent: (RMMapContents *)_contents
  37. {
  38. if (![super initWithContent:_contents])
  39. return nil;
  40. // NOTE: RMMapContents may still be initialising when this function
  41. // is called. Be careful using any of methods - they might return
  42. // strange data.
  43. layer = [[CAScrollLayer layer] retain];
  44. layer.anchorPoint = CGPointZero;
  45. layer.masksToBounds = YES;
  46. // If the frame is set incorrectly here, it will be fixed when setRenderer is called in RMMapContents
  47. layer.frame = [content screenBounds];
  48. NSMutableDictionary *customActions = [NSMutableDictionary dictionaryWithDictionary:[layer actions]];
  49. [customActions setObject:[NSNull null] forKey:@"sublayers"];
  50. layer.actions = customActions;
  51. layer.delegate = self;
  52. tiles = [[NSMutableArray alloc] init];
  53. return self;
  54. }
  55. -(void) dealloc
  56. {
  57. for ( RMTileImage *image in tiles ) {
  58. [image layer].delegate = nil;
  59. }
  60. layer.delegate = nil;
  61. [tiles release];
  62. [layer release];
  63. [super dealloc];
  64. }
  65. /// \bug this is a no-op
  66. -(void)mapImageLoaded: (NSNotification*)notification
  67. {
  68. }
  69. - (id<CAAction>)actionForLayer:(CALayer *)theLayer
  70. forKey:(NSString *)key
  71. {
  72. if (theLayer == layer)
  73. {
  74. // RMLog(@"base layer key: %@", key);
  75. return nil;
  76. }
  77. // || [key isEqualToString:@"onLayout"]
  78. if ([key isEqualToString:@"position"]
  79. || [key isEqualToString:@"bounds"])
  80. return nil;
  81. // return (id<CAAction>)[NSNull null];
  82. else
  83. {
  84. // RMLog(@"key: %@", key);
  85. return nil;
  86. }
  87. }
  88. - (void)tileAdded: (RMTile) tile WithImage: (RMTileImage*) image
  89. {
  90. // RMLog(@"tileAdded: %d %d %d at %f %f %f %f", tile.x, tile.y, tile.zoom, image.screenLocation.origin.x, image.screenLocation.origin.y,
  91. // image.screenLocation.size.width, image.screenLocation.size.height);
  92. // RMLog(@"tileAdded");
  93. NSUInteger min = 0, max = [tiles count];
  94. CALayer *sublayer = [image layer];
  95. sublayer.delegate = self;
  96. while (min < max) {
  97. // Binary search for insertion point
  98. NSUInteger pivot = (min + max) / 2;
  99. RMTileImage *other = [tiles objectAtIndex:pivot];
  100. RMTile otherTile = other.tile;
  101. if (otherTile.zoom <= tile.zoom) {
  102. min = pivot + 1;
  103. }
  104. if (otherTile.zoom > tile.zoom) {
  105. max = pivot;
  106. }
  107. }
  108. [tiles insertObject:image atIndex:min];
  109. [layer insertSublayer:sublayer atIndex:min];
  110. }
  111. -(void) tileRemoved: (RMTile) tile
  112. {
  113. RMTileImage *image = nil;
  114. NSUInteger i = [tiles count];
  115. while (i--)
  116. {
  117. RMTileImage *potential = [tiles objectAtIndex:i];
  118. if (RMTilesEqual(tile, potential.tile))
  119. {
  120. [tiles removeObjectAtIndex:i];
  121. image = potential;
  122. break;
  123. }
  124. }
  125. // RMLog(@"tileRemoved: %d %d %d at %f %f %f %f", tile.x, tile.y, tile.zoom, image.screenLocation.origin.x, image.screenLocation.origin.y,
  126. // image.screenLocation.size.width, image.screenLocation.size.height);
  127. [[image layer] removeFromSuperlayer];
  128. }
  129. - (void)setFrame:(CGRect)frame
  130. {
  131. layer.frame = [content screenBounds];
  132. }
  133. - (CALayer*) layer
  134. {
  135. return layer;
  136. }
  137. /*
  138. - (void)moveBy: (CGSize) delta
  139. {
  140. [CATransaction begin];
  141. [CATransaction setValue:[NSNumber numberWithFloat:0.0f]
  142. forKey:kCATransactionAnimationDuration];
  143. [CATransaction setValue:(id)kCFBooleanTrue
  144. forKey:kCATransactionDisableActions];
  145. [super moveBy:delta];
  146. [tileLoader moveBy:delta];
  147. [CATransaction commit];
  148. }
  149. - (void)zoomByFactor: (float) zoomFactor Near:(CGPoint) center
  150. {
  151. [CATransaction begin];
  152. [CATransaction setValue:[NSNumber numberWithFloat:0.0f]
  153. forKey:kCATransactionAnimationDuration];
  154. [CATransaction setValue:(id)kCFBooleanTrue
  155. forKey:kCATransactionDisableActions];
  156. [super zoomByFactor:zoomFactor Near:center];
  157. [tileLoader zoomByFactor:zoomFactor Near:center];
  158. [CATransaction commit];
  159. }
  160. */
  161. @end