summary refs log blame commit diff stats
path: root/Classes/TutorialBubble.m
blob: 74f667fe765f0a52a1f6f7696e7fdaca271ddd7f (plain) (tree)






































































































































                                                                                                                                                                                                                                                                                             









                                                                                                                     

















                                                                                                                                            


















                                                 
//
//  TutorialBubble.m
//  Cart Collect
//
//  Created by Starla Insigna on 8/4/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "TutorialBubble.h"

@implementation TutorialBubble

@synthesize name;

- (id)initWithText:(NSString*)text name:(NSString*)m_name
{
    self = [super init];
    
    textView = [[UILabel alloc] init];
    textView.text = text;
    textView.font = [UIFont systemFontOfSize:14.0f];
    CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 300) lineBreakMode:UILineBreakModeWordWrap];
    textView.lineBreakMode = UILineBreakModeWordWrap;
    textView.numberOfLines = 0;
    textView.frame = CGRectMake(8, 8, size.width, size.height);
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    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(size.width, size.height);
    
    UIGraphicsBeginImageContext(CGSizeMake(boxSize.width+16, boxSize.height+16));
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    [topLeft drawInRect:CGRectMake(0, 0, 8, 8)];
    [topBorder drawInRect:CGRectMake(8, 0, boxSize.width, 8)];
    [topRight drawInRect:CGRectMake(8+boxSize.width, 0, 8, 8)];
    [rightBorder drawInRect:CGRectMake(8+boxSize.width, 8, 8, boxSize.height)];
    [bottomRight drawInRect:CGRectMake(8+boxSize.width, 8+boxSize.height, 8, 8)];
    [bottomBorder drawInRect:CGRectMake(8, 8+boxSize.height, boxSize.width, 8)];
    [bottomLeft drawInRect:CGRectMake(0, 8+boxSize.height, 8, 8)];
    [leftBorder drawInRect:CGRectMake(0, 8, 8, boxSize.height)];
    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
    CGContextFillRect(context, CGRectMake(8, 8, boxSize.width, boxSize.height));
    UIGraphicsPopContext();
    background = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    imageView = [[UIImageView alloc] initWithImage:background];
    [imageView setFrame:CGRectMake(0, 0, boxSize.width+16, boxSize.height+16)];
    [button addSubview:imageView];
    [button addSubview:textView];

    button.frame = CGRectMake(0, 0, boxSize.width+16, boxSize.height+16);
    [self addSubview:button];
    self.frame = CGRectMake(240-(boxSize.width+16)/2, 160-(boxSize.height+16)/2, boxSize.width+16, boxSize.height+16);
    
    name = [m_name retain];
    
    return self;
}

- (id)initWithText:(NSString*)text name:(NSString*)m_name spriteReference:(CCSprite*)spriteReference
{
    self = [self initWithText:text name:m_name];
    
    button.frame = CGRectMake(8, 8, button.frame.size.width, button.frame.size.height);
    self.frame = CGRectMake(0, 0, button.frame.size.width+16, button.frame.size.height+16);
    
    CGRect spriteBounds = CGRectMake(spriteReference.position.x-spriteReference.contentSize.width/2, 320-spriteReference.position.y-spriteReference.contentSize.height/2, spriteReference.contentSize.width*spriteReference.scale, spriteReference.contentSize.height*spriteReference.scale);
    CGPoint boxLoc;
    CGPoint arrowLoc;
    int arrowRotation;
    
    if (spriteBounds.origin.y > self.frame.size.height)
    {
        arrowRotation = 0;
        
        if (CGRectGetMidX(spriteBounds) < button.frame.size.width)
        {
            boxLoc = CGPointMake(0, spriteBounds.origin.y-self.frame.size.height - 8);
            arrowLoc = CGPointMake(spriteBounds.origin.x + 4, 8+button.frame.size.height);
        } else {
            boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds) - self.frame.size.width, spriteBounds.origin.y-self.frame.size.height - 8);
            arrowLoc = CGPointMake(button.frame.size.width - spriteBounds.size.width/2 + 4, 8+button.frame.size.height);
        }
    } else if (spriteBounds.origin.x > self.frame.size.width)
    {
        arrowRotation = 270;
        
        if (CGRectGetMidY(spriteBounds) < button.frame.size.height)
        {
            boxLoc = CGPointMake(spriteBounds.origin.x-self.frame.size.width-8, 0);
            arrowLoc = CGPointMake(8+button.frame.size.width, spriteBounds.origin.y+4);
        } else {
            boxLoc = CGPointMake(spriteBounds.origin.y-self.frame.size.width-8, CGRectGetMaxY(spriteBounds) - self.frame.size.height);
            arrowLoc = CGPointMake(8+button.frame.size.width, button.frame.size.height - spriteBounds.size.height/2 + 4);
        }
    } else if ((480 - CGRectGetMaxX(spriteBounds)) > self.frame.size.width)
    {
        arrowRotation = 90;
        
        if (CGRectGetMidY(spriteBounds) < button.frame.size.height)
        {
            boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds), 0);
            arrowLoc = CGPointMake(0, spriteBounds.origin.y+4);
        } else {
            boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds), CGRectGetMaxY(spriteBounds) - self.frame.size.height);
            arrowLoc = CGPointMake(0, button.frame.size.height - spriteBounds.size.height/2 + 4);
        }
    } else {
        arrowRotation = 180;
        
        if (CGRectGetMidX(spriteBounds) < button.frame.size.width)
        {
            boxLoc = CGPointMake(0, CGRectGetMaxY(spriteBounds)-4);
            arrowLoc = CGPointMake(spriteBounds.origin.x+4, 0);
        } else {
            boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds) - self.frame.size.width, CGRectGetMaxY(spriteBounds)-4);
            arrowLoc = CGPointMake(button.frame.size.width - spriteBounds.size.width/2 + 4, 0);
        }
    }
    
    CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage];
    CGImageRef arrowRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 32, 8, 8));
    UIImage* arrow = [UIImage imageWithCGImage:arrowRef];
    CGImageRelease(arrowRef);
    
    arrowView = [[UIImageView alloc] initWithImage:arrow];
    arrowView.transform = CGAffineTransformMakeRotation(arrowRotation * (M_PI / 180));
    arrowView.frame = CGRectMake(arrowLoc.x, arrowLoc.y, 8, 8);
    [self addSubview:arrowView];
    
    self.frame = CGRectMake(boxLoc.x, boxLoc.y, self.frame.size.width, self.frame.size.height);
    
    return self;
}

- (void)buttonPressed:(id)sender
{
    if (target != nil)
    {
        [target performSelector:action];
    }
}

- (void)setTarget:(id)sender action:(SEL)m_action
{
    target = sender;
    action = m_action;
}

- (void)dealloc
{
    [name release];
    
    [super dealloc];
}

@end