summary refs log tree commit diff stats
path: root/Classes/TutorialBubble.m
blob: b85aa31b42f276cc7988fcb8d7f70a6735d0d704 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
//  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);
        }
    }
    
    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
{
    [self removeFromSuperview];
    
    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