summary refs log tree commit diff stats
path: root/Classes/TutorialBubble.h
blob: 4ce3352323c187dd7bde72fc756bab61fc7541ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
//  TutorialBubble.h
//  Cart Collect
//
//  Created by Starla Insigna on 8/4/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "cocos2d.h"

@interface TutorialBubble : UIView {
    UILabel* textView;
    UIImage* background;
    UIImageView* imageView;
    UIImageView* arrowView;
    UIButton* button;
    id target;
    SEL action;
    NSString* name;
}

@property (readonly) NSString* name;
- (id)initWithText:(NSString*)text name:(NSString*)name;
- (id)initWithText:(NSString*)text name:(NSString*)name spriteReference:(CCSprite*)spriteReference;
- (void)buttonPressed:(id)sender;
- (void)setTarget:(id)sender action:(SEL)action;

@end
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//
//  GameModeSelection.m
//  Cartographic
//
//  Created by Starla Insigna on 8/18/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "GameModeSelection.h"
#import "UIImage+ColorMasking.h"
#import "NMPanelMenu.h"
#import "Highscore.h"

@implementation GameModeSelection

@synthesize name, location, unlocked, delegate;

+ (id)selectionWithName:(NSString*)name location:(NSString*)location filename:(NSString*)filename unlocked:(BOOL)unlocked
{
    return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlocked:unlocked] autorelease];
}

+ (id)selectionWithName:(NSString *)name location:(NSString *)location filename:(NSString *)filename unlockCondition:(NSString*)unlockCondition
{
    return [[[GameModeSelection alloc] initWithName:name location:location filename:filename unlockCondition:unlockCondition] autorelease];
}

- (id)initWithName:(NSString*)m_name location:(NSString*)m_location filename:(NSString*)filename unlocked:(BOOL)m_unlocked;
{
    self = [super initWithTarget:nil selector:nil];
    
    if (nil != self)
    {
        self.anchorPoint = CGPointMake(0.5f, 0.5f);
        
        name = m_name;
        location = m_location;
        unlocked = m_unlocked;
        
        contentSize_ = CGSizeMake(128, 320);
        
        NSString* filenameMod;
        
        if (unlocked)
        {
            filenameMod = [NSString stringWithFormat:@"%@-unlocked", filename];
        } else {
            filenameMod = [NSString stringWithFormat:@"%@-locked", filename];
            name = [@"" stringByPaddingToLength:name.length withString:@"?" startingAtIndex:0];
            location = [@"" stringByPaddingToLength:location.length withString:@"?" startingAtIndex:0];
        }
        
        // First, create the frame that we will put the level picture inside
        CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage];
        CGImageRef topLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 0, 8, 8));
        CGImageRef topRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 0, 8, 8));
        CGImageRef bottomLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 8, 8, 8));
        CGImageRef bottomRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 8, 8, 8));
        CGImageRef topBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 16, 8, 8));
        CGImageRef leftBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 16, 8, 8));
        CGImageRef rightBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 24, 8, 8));
        CGImageRef bottomBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 24, 8, 8));
        UIImage* topLeft = [UIImage imageWithCGImage:topLeftRef];
        UIImage* topRight = [UIImage imageWithCGImage:topRightRef];
        UIImage* bottomLeft = [UIImage imageWithCGImage:bottomLeftRef];
        UIImage* bottomRight = [UIImage imageWithCGImage:bottomRightRef];
        UIImage* topBorder = [UIImage imageWithCGImage:topBorderRef];
        UIImage* leftBorder = [UIImage imageWithCGImage:leftBorderRef];
        UIImage* rightBorder = [UIImage imageWithCGImage:rightBorderRef];
        UIImage* bottomBorder = [UIImage imageWithCGImage:bottomBorderRef];
        CGImageRelease(topLeftRef);
        CGImageRelease(topRightRef);
        CGImageRelease(bottomLeftRef);
        CGImageRelease(bottomRightRef);
        CGImageRelease(topBorderRef);
        CGImageRelease(leftBorderRef);
        CGImageRelease(rightBorderRef);
        CGImageRelease(bottomBorderRef);
        
        CGSize boxSize = CGSizeMake(128+12, 128+12);
        UIGraphicsBeginImageContext(boxSize);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [topLeft drawInRect:CGRectMake(0, 0, 8, 8)];
        [topBorder drawInRect:CGRectMake(8, 0, boxSize.width-16, 8)];
        [topRight drawInRect:CGRectMake(8+boxSize.width-16, 0, 8, 8)];
        [rightBorder drawInRect:CGRectMake(8+boxSize.width-16, 8, 8, boxSize.height-16)];
        [bottomRight drawInRect:CGRectMake(8+boxSize.width-16, 8+boxSize.height-16, 8, 8)];
        [bottomBorder drawInRect:CGRectMake(8, 8+boxSize.height-16, boxSize.width-16, 8)];
        [bottomLeft drawInRect:CGRectMake(0, 8+boxSize.height-16, 8, 8)];
        [leftBorder drawInRect:CGRectMake(0, 8, 8, boxSize.height-16)];
        CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
        CGContextFillRect(context, CGRectMake(8, 8, boxSize.width-16, boxSize.height-16));
        UIImage* selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
        
        // Now we want to put the level image inside the frame without messing up the frame itself
        UIImage* innerPicture = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:@"png"]];
        CGContextClipToMask(context, CGRectMake(0, 0, boxSize.width, boxSize.height), [[selectionBackground opaqueMaskFromWhiteImage] CGImage]);
        [innerPicture drawInRect:CGRectMake(6, 6, 128, 128)];
        selectionBackground = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        // The frame needs a shadow, so let's redraw it in a new context
        UIGraphicsBeginImageContext(CGSizeMake(boxSize.width+10, boxSize.height+10));
        context = UIGraphicsGetCurrentContext();
        CGContextSaveGState(context);
        
        if (unlocked)
        {
            CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
            [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height)];
        } else {
            // Draw the picture in grayscale if the level has not yet been unlocked
            CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
            CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
            CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
            CGContextRestoreGState(context);
            
            CGContextSaveGState(context);
            CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
            [selectionBackground drawInRect:CGRectMake(10, 0, boxSize.width, boxSize.height) blendMode:kCGBlendModeLuminosity alpha:1.0];
        }
        
        CGContextRestoreGState(context);
        CGImageRef pictureRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
        
        CCSprite* picture = [CCSprite spriteWithCGImage:pictureRef key:[NSString stringWithFormat:@"gms-%@", filenameMod]];
        
        // We're also going to need a "selected" image state for the button
        CGContextSaveGState(context);
        CGContextClipToMask(context, CGRectMake(10, 0, boxSize.width, boxSize.height), [selectionBackground CGImage]);
        CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]);
        CGContextFillRect(context, CGRectMake(10, 0, boxSize.width, boxSize.height));
        CGContextRestoreGState(context);
        CGImageRef selectedButtonRef = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
        UIGraphicsEndImageContext();
        CCSprite* selectedButton = [CCSprite spriteWithCGImage:selectedButtonRef key:[NSString stringWithFormat:@"gms-%@-selected", filenameMod]];
        
        CCMenuItemSprite* pictureMenuItem = [CCMenuItemSprite itemFromNormalSprite:picture selectedSprite:selectedButton target:self selector:@selector(buttonTapped)];
        NMPanelMenu* theMenu = [NMPanelMenu menuWithItems:pictureMenuItem, nil];
        theMenu.position = ccp(-5, 0);
        [self addChild:theMenu];
        
        // Render the titles
        UIFont* titleFont = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:18.0f];
        CGSize titleSize = [location sizeWithFont:titleFont constrainedToSize:CGSizeMake(128, 0)];
        UIFont* subtitleFont = [UIFont fontWithName:@"AmericanTypewriter" size:18.0f];
        CGSize subtitleSize = [name sizeWithFont:subtitleFont constrainedToSize:CGSizeMake(128, 0)];
        CGSize combinedTitleSize = CGSizeMake(128, titleSize.height + 10 + subtitleSize.height + 10);
        
        UIGraphicsBeginImageContext(combinedTitleSize);
        context = UIGraphicsGetCurrentContext();
        CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
        [location drawInRect:CGRectMake(10, 0, 128, titleSize.height) withFont:titleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
        [name drawInRect:CGRectMake(10, titleSize.height, 128, subtitleSize.height) withFont:subtitleFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
        
        CGImageRef titleImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
        UIGraphicsEndImageContext();
        CCSprite* titleSprite = [CCSprite spriteWithCGImage:titleImage key:[NSString stringWithFormat:@"gms-%@-title", filenameMod]];
        titleSprite.position = ccp(-10, (boxSize.height)/2+(combinedTitleSize.height)/2);
        [self addChild:titleSprite];
        
        if (unlocked)
        {
            Highscore* localHighscore = [Highscore localHighscoreForGameMode:name];
            
            if (localHighscore != nil)
            {
                // Render the highscore label
                NSString* highscoreString = [NSString stringWithFormat:@"Highscore: %d", localHighscore.score];
                UIFont* highscoreFont = [UIFont fontWithName:@"AmericanTypewriter" size:16.0f];
                CGSize highscoreSize = [highscoreString sizeWithFont:highscoreFont];
                
                UIGraphicsBeginImageContext(CGSizeMake(highscoreSize.width+10, highscoreSize.height+10));
                CGContextRef context = UIGraphicsGetCurrentContext();
                CGContextSetShadow(context, CGSizeMake(-6, 6), 4.0f);
                [highscoreString drawInRect:CGRectMake(10, 0, highscoreSize.width, highscoreSize.height) withFont:highscoreFont];
                
                CGImageRef highscoreImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
                UIGraphicsEndImageContext();
                CCSprite* highscoreSprite = [CCSprite spriteWithCGImage:highscoreImage key:[NSString stringWithFormat:@"gms-%@-highscore-%d", filename, localHighscore.score]];
                highscoreSprite.position = ccp(-5, 0-64-(highscoreSize.height)/2-10);
                [self addChild:highscoreSprite];
            }
        }
    }
    
    return self;
}

- (id)initWithName:(NSString *)m_name location:(NSString *)m_location filename:(NSString *)m_filename unlockCondition:(NSString*)m_unlockCondition
{
    self = [self initWithName:m_name location:m_location filename:m_filename unlocked:NO];
    
    if (nil != self)
    {
        unlockCondition = m_unlockCondition;
    }
    
    return self;
}

- (void)buttonTapped
{
    if (unlocked)
    {
        if (delegate != nil)
        {
            [delegate didSelectGameMode:self];
        } else {
            NSLog(@"I don't have a GameModeSelectionDelegate to call for some reason...");
        }
    } else {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"To unlock this game mode:" message:unlockCondition delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }
}

@end