summary refs log tree commit diff stats
path: root/Classes/GameModeSelection.m
diff options
context:
space:
mode:
Diffstat (limited to 'Classes/GameModeSelection.m')
-rw-r--r--Classes/GameModeSelection.m219
1 files changed, 219 insertions, 0 deletions
diff --git a/Classes/GameModeSelection.m b/Classes/GameModeSelection.m new file mode 100644 index 0000000..69f1ddb --- /dev/null +++ b/Classes/GameModeSelection.m
@@ -0,0 +1,219 @@
1//
2// GameModeSelection.m
3// Cartographic
4//
5// Created by Starla Insigna on 8/18/11.
6// Copyright 2011 Four Island. All rights reserved.
7//
8
9#import "GameModeSelection.h"
10#import "UIImage+ColorMasking.h"
11#import "NMPanelMenu.h"
12#import "Highscore.h"
13
14@implementation GameModeSelection
15
16@synthesize name, location, unlocked, delegate;
17
18+ (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked
19{
20 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlocked:unlocked] autorelease];
21}
22
23+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition
24{
25 return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlockCondition:unlockCondition] autorelease];
26}
27
28- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked;
29{
30 self = [super initWithTarget:nil selector:nil];
31
32 if (nil != self)
33 {
34 self.anchorPoint = CGPointMake(0.5f, 0.5f);
35
36 name = m_name;
37 location = m_location;
38 unlocked = m_unlocked;
39
40 contentSize_ = CGSizeMake(128, 320);
41
42 NSString* filenameMod;
43
44 if (unlocked)
45 {
46 filenameMod = [NSString stringWithFormat:@"%@-unlocked", filename];
47 } else {
48 filenameMod = [NSString stringWithFormat:@"%@-locked", filename];
49 name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0];
50 location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0];
51 }
52
53 // First, create the frame that we will put the level picture inside
54 CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage];
55 CGImageRef topLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 0, 8, 8));
56 CGImageRef topRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 0, 8, 8));
57 CGImageRef bottomLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 8, 8, 8));
58 CGImageRef bottomRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 8, 8, 8));
59 CGImageRef topBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 16, 8, 8));
60 CGImageRef leftBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 16, 8, 8));
61 CGImageRef rightBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 24, 8, 8));
62 CGImageRef bottomBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 24, 8, 8));
63 UIImage* topLeft = [UIImage imageWithCGImage:topLeftRef];
64 UIImage* topRight = [UIImage imageWithCGImage:topRightRef];
65 UIImage* bottomLeft = [UIImage imageWithCGImage:bottomLeftRef];
66 UIImage* bottomRight = [UIImage imageWithCGImage:bottomRightRef];
67 UIImage* topBorder = [UIImage imageWithCGImage:topBorderRef];
68 UIImage* leftBorder = [UIImage imageWithCGImage:leftBorderRef];
69 UIImage* rightBorder = [UIImage imageWithCGImage:rightBorderRef];
70 UIImage* bottomBorder = [UIImage imageWithCGImage:bottomBorderRef];
71 CGImageRelease(topLeftRef);
72 CGImageRelease(topRightRef);
73 CGImageRelease(bottomLeftRef);
74 CGImageRelease(bottomRightRef);
75 CGImageRelease(topBorderRef);
76 CGImageRelease(leftBorderRef);
77 CGImageRelease(rightBorderRef);
78 CGImageRelease(bottomBorderRef);
79
80 CGSize boxSize = CGSizeMake(128+12, 128+12);
81 UIGraphicsBeginImageContext(boxSize);
82 CGContextRef context = UIGraphicsGetCurrentContext();
83 [topLeft drawInRect:CGRectMake(0, 0, 8, 8)];
84 [topBorder drawInRect:CGRectMake(8, 0, boxSize.width-16, 8)];
85 [topRight drawInRect:CGRectMake(8+boxSize.width-16, 0, 8, 8)];
86 [rightBorder drawInRect:CGRectMake(8+boxSize.width-16, 8, 8, boxSize.height-16)];
87 [bottomRight drawInRect:CGRectMake(8+boxSize.width-16, 8+boxSize.height-16, 8, 8)];
88 [bottomBorder drawInRect:CGRectMake(8, 8+boxSize.height-16, boxSize.width-16, 8)];
89 [bottomLeft drawInRect:CGRectMake(0, 8+boxSize.height-16, 8, 8)];
90 [leftBorder drawInRect:CGRectMake(0, 8, 8, boxSize.height-16)];
91 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
92 CGContextFillRect(context, CGRectMake(8, 8, boxSize.width-16, boxSize.height-16));
93 UIImage* selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
94
95 // Now we want to put the level image inside the frame without messing up the frame itself
96 UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]];
97 CGContextClipToMask(context, CGRectMake(0, 0, boxSize.width, boxSize.height), [[selectionBackground opaqueMaskFromWhiteImage] CGImage]);
98 [innerPicture drawInRect:CGRectMake(6, 6, 128, 128)];
99 selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
100 UIGraphicsEndImageContext();
101
102 // The frame needs a shadow, so let's redraw it in a new context
103 UIGraphicsBeginImageContext(CGSizeMake(boxSize.width+10, boxSize.height+10));
104 context = UIGraphicsGetCurrentContext();
105 CGContextSaveGState(context);
106
107 if (unlocked)
108 {
109 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
110 [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height)];
111 } else {
112 // Draw the picture in grayscale if the level has not yet been unlocked
113 CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
114 CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
115 CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
116 CGContextRestoreGState(context);
117
118 CGContextSaveGState(context);
119 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
120 [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height) blendMode:kCGBlendModeLuminosity alpha:1.0];
121 }
122
123 CGContextRestoreGState(context);
124 CGImageRef pictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
125
126 CCSprite* picture = [CCSprite spriteWithCGImage:pictureRef key:[NSString stringWithFormat:@"gms-%@", filenameMod]];
127
128 // We're also going to need a "selected" image state for the button
129 CGContextSaveGState(context);
130 CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
131 CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
132 CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
133 CGContextRestoreGState(context);
134 CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
135 UIGraphicsEndImageContext();
136 CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filenameMod]];
137
138 CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
139 NMPanelMenu* theMenu = [NMPanelMenu menuWithItems:pictureMenuItem, nil];
140 theMenu.position = ccp(-5, 0);
141 [self addChild:theMenu];
142
143 // Render the titles
144 UIFont* titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.0f];
145 CGSize titleSize = [location sizeWithFont:titleFont constrainedToSize:CGSizeMake(128, 0)];
146 UIFont* subtitleFont = [UIFont fontWithName:@"AmericanTypewriter" size:18.0f];
147 CGSize subtitleSize = [name sizeWithFont:subtitleFont constrainedToSize:CGSizeMake(128, 0)];
148 CGSize combinedTitleSize = CGSizeMake(128, titleSize.height + 10 + subtitleSize.height + 10);
149
150 UIGraphicsBeginImageContext(combinedTitleSize);
151 context = UIGraphicsGetCurrentContext();
152 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
153 [location drawInRect:CGRectMake(10, 0, 128, titleSize.height) withFont:titleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
154 [name drawInRect:CGRectMake(10, titleSize.height, 128, subtitleSize.height) withFont:subtitleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
155
156 CGImageRef titleImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
157 UIGraphicsEndImageContext();
158 CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]];
159 titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2);
160 [self addChild:titleSprite];
161
162 if (unlocked)
163 {
164 Highscore* localHighscore = [Highscore localHighscoreForGameMode:name];
165
166 if (localHighscore != nil)
167 {
168 // Render the highscore label
169 NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", localHighscore.score];
170 UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f];
171 CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont];
172
173 UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10));
174 CGContextRef context = UIGraphicsGetCurrentContext();
175 CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
176 [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont];
177
178 CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
179 UIGraphicsEndImageContext();
180 CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore", filename]];
181 highscoreSprite.position = ccp(-5, 0-64-(highscoreSize.height)/2-10);
182 [self addChild:highscoreSprite];
183 }
184 }
185 }
186
187 return self;
188}
189
190- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename unlockCondition:(NSString*)m_unlockCondition
191{
192 self = [self initWithName:m_name location:m_location filename:m_filename unlocked:NO];
193
194 if (nil != self)
195 {
196 unlockCondition = m_unlockCondition;
197 }
198
199 return self;
200}
201
202- (void)buttonTapped
203{
204 if (unlocked)
205 {
206 if (delegate != nil)
207 {
208 [delegate didSelectGameMode:self];
209 } else {
210 NSLog(@"I don't have a GameModeSelectionDelegate to call for some reason...");
211 }
212 } else {
213 UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"To unlock this game mode:" message:unlockCondition delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
214 [alertView show];
215 [alertView release];
216 }
217}
218
219@end