diff options
author | Starla Insigna <starla4444@gmail.com> | 2011-08-07 10:04:54 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2011-08-07 10:04:54 -0400 |
commit | 1d9ed882de4e2e3a53cdd5e90edc25e8ae10af1b (patch) | |
tree | 4a8f7445cf57b4de4295f81ff0a70c6b5f49c978 /Classes/TutorialBubble.m | |
parent | 0a8fed12704bf343ad9604f964b71b3f50495382 (diff) | |
download | cartcollect-1d9ed882de4e2e3a53cdd5e90edc25e8ae10af1b.tar.gz cartcollect-1d9ed882de4e2e3a53cdd5e90edc25e8ae10af1b.tar.bz2 cartcollect-1d9ed882de4e2e3a53cdd5e90edc25e8ae10af1b.zip |
Implemented tutorial bubbles
GameLayer now has support for pausing game flow and displaying a tutorial bubble that the user can tap to dismiss. No code has been written, however, to make use of this, because I think it may be simpler to abstract GameLayer out somewhat and create a separate game mode for the tutorial. Deliberation required. PauseLayer has also been removed and the behavior has been brought into GameLayer. Refs #193
Diffstat (limited to 'Classes/TutorialBubble.m')
-rw-r--r-- | Classes/TutorialBubble.m | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/Classes/TutorialBubble.m b/Classes/TutorialBubble.m new file mode 100644 index 0000000..b85aa31 --- /dev/null +++ b/Classes/TutorialBubble.m | |||
@@ -0,0 +1,177 @@ | |||
1 | // | ||
2 | // TutorialBubble.m | ||
3 | // Cart Collect | ||
4 | // | ||
5 | // Created by Starla Insigna on 8/4/11. | ||
6 | // Copyright 2011 Four Island. All rights reserved. | ||
7 | // | ||
8 | |||
9 | #import "TutorialBubble.h" | ||
10 | |||
11 | @implementation TutorialBubble | ||
12 | |||
13 | @synthesize name; | ||
14 | |||
15 | - (id)initWithText:(NSString*)text name:(NSString*)m_name | ||
16 | { | ||
17 | self = [super init]; | ||
18 | |||
19 | textView = [[UILabel alloc] init]; | ||
20 | textView.text = text; | ||
21 | textView.font = [UIFont systemFontOfSize:14.0f]; | ||
22 | CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 300) lineBreakMode:UILineBreakModeWordWrap]; | ||
23 | textView.lineBreakMode = UILineBreakModeWordWrap; | ||
24 | textView.numberOfLines = 0; | ||
25 | textView.frame = CGRectMake(8, 8, size.width, size.height); | ||
26 | |||
27 | button = [UIButton buttonWithType:UIButtonTypeCustom]; | ||
28 | [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; | ||
29 | |||
30 | CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage]; | ||
31 | CGImageRef topLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 0, 8, 8)); | ||
32 | CGImageRef topRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 0, 8, 8)); | ||
33 | CGImageRef bottomLeftRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 8, 8, 8)); | ||
34 | CGImageRef bottomRightRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 8, 8, 8)); | ||
35 | CGImageRef topBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 16, 8, 8)); | ||
36 | CGImageRef leftBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 16, 8, 8)); | ||
37 | CGImageRef rightBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 24, 8, 8)); | ||
38 | CGImageRef bottomBorderRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(8, 24, 8, 8)); | ||
39 | UIImage* topLeft = [UIImage imageWithCGImage:topLeftRef]; | ||
40 | UIImage* topRight = [UIImage imageWithCGImage:topRightRef]; | ||
41 | UIImage* bottomLeft = [UIImage imageWithCGImage:bottomLeftRef]; | ||
42 | UIImage* bottomRight = [UIImage imageWithCGImage:bottomRightRef]; | ||
43 | UIImage* topBorder = [UIImage imageWithCGImage:topBorderRef]; | ||
44 | UIImage* leftBorder = [UIImage imageWithCGImage:leftBorderRef]; | ||
45 | UIImage* rightBorder = [UIImage imageWithCGImage:rightBorderRef]; | ||
46 | UIImage* bottomBorder = [UIImage imageWithCGImage:bottomBorderRef]; | ||
47 | CGImageRelease(topLeftRef); | ||
48 | CGImageRelease(topRightRef); | ||
49 | CGImageRelease(bottomLeftRef); | ||
50 | CGImageRelease(bottomRightRef); | ||
51 | CGImageRelease(topBorderRef); | ||
52 | CGImageRelease(leftBorderRef); | ||
53 | CGImageRelease(rightBorderRef); | ||
54 | CGImageRelease(bottomBorderRef); | ||
55 | |||
56 | CGSize boxSize = CGSizeMake(size.width, size.height); | ||
57 | |||
58 | UIGraphicsBeginImageContext(CGSizeMake(boxSize.width+16, boxSize.height+16)); | ||
59 | CGContextRef context = UIGraphicsGetCurrentContext(); | ||
60 | UIGraphicsPushContext(context); | ||
61 | [topLeft drawInRect:CGRectMake(0, 0, 8, 8)]; | ||
62 | [topBorder drawInRect:CGRectMake(8, 0, boxSize.width, 8)]; | ||
63 | [topRight drawInRect:CGRectMake(8+boxSize.width, 0, 8, 8)]; | ||
64 | [rightBorder drawInRect:CGRectMake(8+boxSize.width, 8, 8, boxSize.height)]; | ||
65 | [bottomRight drawInRect:CGRectMake(8+boxSize.width, 8+boxSize.height, 8, 8)]; | ||
66 | [bottomBorder drawInRect:CGRectMake(8, 8+boxSize.height, boxSize.width, 8)]; | ||
67 | [bottomLeft drawInRect:CGRectMake(0, 8+boxSize.height, 8, 8)]; | ||
68 | [leftBorder drawInRect:CGRectMake(0, 8, 8, boxSize.height)]; | ||
69 | CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); | ||
70 | CGContextFillRect(context, CGRectMake(8, 8, boxSize.width, boxSize.height)); | ||
71 | UIGraphicsPopContext(); | ||
72 | background = UIGraphicsGetImageFromCurrentImageContext(); | ||
73 | UIGraphicsEndImageContext(); | ||
74 | |||
75 | imageView = [[UIImageView alloc] initWithImage:background]; | ||
76 | [imageView setFrame:CGRectMake(0, 0, boxSize.width+16, boxSize.height+16)]; | ||
77 | [button addSubview:imageView]; | ||
78 | [button addSubview:textView]; | ||
79 | |||
80 | button.frame = CGRectMake(0, 0, boxSize.width+16, boxSize.height+16); | ||
81 | [self addSubview:button]; | ||
82 | self.frame = CGRectMake(240-(boxSize.width+16)/2, 160-(boxSize.height+16)/2, boxSize.width+16, boxSize.height+16); | ||
83 | |||
84 | name = [m_name retain]; | ||
85 | |||
86 | return self; | ||
87 | } | ||
88 | |||
89 | - (id)initWithText:(NSString*)text name:(NSString*)m_name spriteReference:(CCSprite*)spriteReference | ||
90 | { | ||
91 | self = [self initWithText:text name:m_name]; | ||
92 | |||
93 | button.frame = CGRectMake(8, 8, button.frame.size.width, button.frame.size.height); | ||
94 | self.frame = CGRectMake(0, 0, button.frame.size.width+16, button.frame.size.height+16); | ||
95 | |||
96 | 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); | ||
97 | CGPoint boxLoc; | ||
98 | CGPoint arrowLoc; | ||
99 | int arrowRotation; | ||
100 | |||
101 | if (spriteBounds.origin.y > self.frame.size.height) | ||
102 | { | ||
103 | arrowRotation = 0; | ||
104 | |||
105 | if (CGRectGetMidX(spriteBounds) < button.frame.size.width) | ||
106 | { | ||
107 | boxLoc = CGPointMake(0, spriteBounds.origin.y-self.frame.size.height - 8); | ||
108 | arrowLoc = CGPointMake(spriteBounds.origin.x + 4, 8+button.frame.size.height); | ||
109 | } else { | ||
110 | boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds) - self.frame.size.width, spriteBounds.origin.y-self.frame.size.height - 8); | ||
111 | arrowLoc = CGPointMake(button.frame.size.width - spriteBounds.size.width/2 + 4, 8+button.frame.size.height); | ||
112 | } | ||
113 | } else if (spriteBounds.origin.x > self.frame.size.width) | ||
114 | { | ||
115 | arrowRotation = 270; | ||
116 | |||
117 | if (CGRectGetMidY(spriteBounds) < button.frame.size.height) | ||
118 | { | ||
119 | boxLoc = CGPointMake(spriteBounds.origin.x-self.frame.size.width-8, 0); | ||
120 | arrowLoc = CGPointMake(8+button.frame.size.width, spriteBounds.origin.y+4); | ||
121 | } else { | ||
122 | boxLoc = CGPointMake(spriteBounds.origin.y-self.frame.size.width-8, CGRectGetMaxY(spriteBounds) - self.frame.size.height); | ||
123 | arrowLoc = CGPointMake(8+button.frame.size.width, button.frame.size.height - spriteBounds.size.height/2 + 4); | ||
124 | } | ||
125 | } else if ((480 - CGRectGetMaxX(spriteBounds)) > self.frame.size.width) | ||
126 | { | ||
127 | arrowRotation = 90; | ||
128 | |||
129 | if (CGRectGetMidY(spriteBounds) < button.frame.size.height) | ||
130 | { | ||
131 | boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds), 0); | ||
132 | arrowLoc = CGPointMake(0, spriteBounds.origin.y+4); | ||
133 | } else { | ||
134 | boxLoc = CGPointMake(CGRectGetMaxX(spriteBounds), CGRectGetMaxY(spriteBounds) - self.frame.size.height); | ||
135 | arrowLoc = CGPointMake(0, button.frame.size.height - spriteBounds.size.height/2 + 4); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | CGImageRef framestuff = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"framestuff" ofType:@"png"]] CGImage]; | ||
140 | CGImageRef arrowRef = CGImageCreateWithImageInRect(framestuff, CGRectMake(0, 32, 8, 8)); | ||
141 | UIImage* arrow = [UIImage imageWithCGImage:arrowRef]; | ||
142 | CGImageRelease(arrowRef); | ||
143 | |||
144 | arrowView = [[UIImageView alloc] initWithImage:arrow]; | ||
145 | arrowView.transform = CGAffineTransformMakeRotation(arrowRotation * (M_PI / 180)); | ||
146 | arrowView.frame = CGRectMake(arrowLoc.x, arrowLoc.y, 8, 8); | ||
147 | [self addSubview:arrowView]; | ||
148 | |||
149 | self.frame = CGRectMake(boxLoc.x, boxLoc.y, self.frame.size.width, self.frame.size.height); | ||
150 | |||
151 | return self; | ||
152 | } | ||
153 | |||
154 | - (void)buttonPressed:(id)sender | ||
155 | { | ||
156 | [self removeFromSuperview]; | ||
157 | |||
158 | if (target != nil) | ||
159 | { | ||
160 | [target performSelector:action]; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | - (void)setTarget:(id)sender action:(SEL)m_action | ||
165 | { | ||
166 | target = sender; | ||
167 | action = m_action; | ||
168 | } | ||
169 | |||
170 | - (void)dealloc | ||
171 | { | ||
172 | [name release]; | ||
173 | |||
174 | [super dealloc]; | ||
175 | } | ||
176 | |||
177 | @end | ||