PageRenderTime 48ms CodeModel.GetById 23ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/MapView/Map/RMMarker.m

http://github.com/route-me/route-me
Objective C | 214 lines | 152 code | 32 blank | 30 comment | 16 complexity | 540dd93ef11c1c8b5ec4af112843bfa4 MD5 | raw file
  1//
  2//  RMMarker.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
 28#import "RMMarker.h"
 29
 30#import "RMPixel.h"
 31
 32@implementation RMMarker
 33
 34@synthesize projectedLocation;
 35@synthesize enableDragging;
 36@synthesize enableRotation;
 37@synthesize data;
 38@synthesize label;
 39@synthesize textForegroundColor;
 40@synthesize textBackgroundColor;
 41
 42#define defaultMarkerAnchorPoint CGPointMake(0.5, 0.5)
 43
 44+ (UIFont *)defaultFont
 45{
 46	return [UIFont systemFontOfSize:15];
 47}
 48
 49// init
 50- (id)init
 51{
 52    if ((self = [super init])) {
 53        label = nil;
 54        textForegroundColor = [UIColor blackColor];
 55        textBackgroundColor = [UIColor clearColor];
 56		enableDragging = YES;
 57		enableRotation = YES;
 58    }
 59    return self;
 60}
 61
 62- (id) initWithUIImage: (UIImage*) image
 63{
 64	return [self initWithUIImage:image anchorPoint: defaultMarkerAnchorPoint];
 65}
 66
 67- (id) initWithUIImage: (UIImage*) image anchorPoint: (CGPoint) _anchorPoint
 68{
 69	if (![self init])
 70		return nil;
 71	
 72	self.contents = (id)[image CGImage];
 73	self.bounds = CGRectMake(0,0,image.size.width,image.size.height);
 74	self.anchorPoint = _anchorPoint;
 75	
 76	self.masksToBounds = NO;
 77	self.label = nil;
 78	
 79	return self;
 80}
 81
 82- (void) replaceUIImage: (UIImage*) image
 83{
 84	[self replaceUIImage:image anchorPoint:defaultMarkerAnchorPoint];
 85}
 86
 87- (void) replaceUIImage: (UIImage*) image
 88			anchorPoint: (CGPoint) _anchorPoint
 89{
 90	self.contents = (id)[image CGImage];
 91	self.bounds = CGRectMake(0,0,image.size.width,image.size.height);
 92	self.anchorPoint = _anchorPoint;
 93	
 94	self.masksToBounds = NO;
 95}
 96
 97- (void) setLabel:(UIView*)aView
 98{
 99	if (label == aView) {
100		return;
101	}
102
103	if (label != nil)
104	{
105		[[label layer] removeFromSuperlayer];
106		[label release];
107		label = nil;
108	}
109	
110	if (aView != nil)
111	{
112		label = [aView retain];
113		[self addSublayer:[label layer]];
114	}
115}
116
117- (void) changeLabelUsingText: (NSString*)text
118{
119	CGPoint position = CGPointMake([self bounds].size.width / 2 - [text sizeWithFont:[RMMarker defaultFont]].width / 2, 4);
120/// \bug hardcoded font name
121	[self changeLabelUsingText:text position:position font:[RMMarker defaultFont] foregroundColor:[self textForegroundColor] backgroundColor:[self textBackgroundColor]];
122}
123
124- (void) changeLabelUsingText: (NSString*)text position:(CGPoint)position
125{
126	[self changeLabelUsingText:text position:position font:[RMMarker defaultFont] foregroundColor:[self textForegroundColor] backgroundColor:[self textBackgroundColor]];
127}
128
129- (void) changeLabelUsingText: (NSString*)text font:(UIFont*)font foregroundColor:(UIColor*)textColor backgroundColor:(UIColor*)backgroundColor
130{
131	CGPoint position = CGPointMake([self bounds].size.width / 2 - [text sizeWithFont:font].width / 2, 4);
132	[self setTextForegroundColor:textColor];
133	[self setTextBackgroundColor:backgroundColor];
134	[self changeLabelUsingText:text  position:position font:font foregroundColor:textColor backgroundColor:backgroundColor];
135}
136
137- (void) changeLabelUsingText: (NSString*)text position:(CGPoint)position font:(UIFont*)font foregroundColor:(UIColor*)textColor backgroundColor:(UIColor*)backgroundColor
138{
139	CGSize textSize = [text sizeWithFont:font];
140	CGRect frame = CGRectMake(position.x,
141							  position.y,
142							  textSize.width+4,
143							  textSize.height+4);
144	
145	UILabel *aLabel = [[UILabel alloc] initWithFrame:frame];
146	[self setTextForegroundColor:textColor];
147	[self setTextBackgroundColor:backgroundColor];
148	[aLabel setNumberOfLines:0];
149	[aLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
150	[aLabel setBackgroundColor:backgroundColor];
151	[aLabel setTextColor:textColor];
152	[aLabel setFont:font];
153	[aLabel setTextAlignment:UITextAlignmentCenter];
154	[aLabel setText:text];
155	
156	[self setLabel:aLabel];
157	[aLabel release];
158}
159
160- (void) toggleLabel
161{
162	if (self.label == nil) {
163		return;
164	}
165	
166	if ([self.label isHidden]) {
167		[self showLabel];
168	} else {
169		[self hideLabel];
170	}
171}
172
173- (void) showLabel
174{
175	if ([self.label isHidden]) {
176		// Using addSublayer will animate showing the label, whereas setHidden is not animated
177		[self addSublayer:[self.label layer]];
178		[self.label setHidden:NO];
179	}
180}
181
182- (void) hideLabel
183{
184	if (![self.label isHidden]) {
185		// Using removeFromSuperlayer will animate hiding the label, whereas setHidden is not animated
186		[[self.label layer] removeFromSuperlayer];
187		[self.label setHidden:YES];
188	}
189}
190
191- (void) dealloc 
192{
193    self.data = nil;
194    self.label = nil;
195    self.textForegroundColor = nil;
196    self.textBackgroundColor = nil;
197	[super dealloc];
198}
199
200- (void)zoomByFactor: (float) zoomFactor near:(CGPoint) center
201{
202	if(enableDragging){
203		self.position = RMScaleCGPointAboutPoint(self.position, zoomFactor, center);
204	}
205}
206
207- (void)moveBy: (CGSize) delta
208{
209	if(enableDragging){
210		[super moveBy:delta];
211	}
212}
213
214@end