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