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 | |
| 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')
| -rwxr-xr-x | Classes/Cart_CollectAppDelegate.m | 4 | ||||
| -rwxr-xr-x | Classes/GameLayer.h | 14 | ||||
| -rwxr-xr-x | Classes/GameLayer.m | 84 | ||||
| -rwxr-xr-x | Classes/PauseLayer.h | 22 | ||||
| -rwxr-xr-x | Classes/PauseLayer.m | 68 | ||||
| -rw-r--r-- | Classes/TutorialBubble.h | 29 | ||||
| -rw-r--r-- | Classes/TutorialBubble.m | 177 |
7 files changed, 300 insertions, 98 deletions
| diff --git a/Classes/Cart_CollectAppDelegate.m b/Classes/Cart_CollectAppDelegate.m index 201422f..7547601 100755 --- a/Classes/Cart_CollectAppDelegate.m +++ b/Classes/Cart_CollectAppDelegate.m | |||
| @@ -139,9 +139,9 @@ | |||
| 139 | -(void) applicationDidEnterBackground:(UIApplication*)application { | 139 | -(void) applicationDidEnterBackground:(UIApplication*)application { |
| 140 | [[CCDirector sharedDirector] stopAnimation]; | 140 | [[CCDirector sharedDirector] stopAnimation]; |
| 141 | 141 | ||
| 142 | if ([[CCDirector sharedDirector] runningScene].tag == 436) | 142 | if ([[CCDirector sharedDirector] runningScene].tag == GAME_SCENE) |
| 143 | { | 143 | { |
| 144 | [[CCDirector sharedDirector] replaceScene:[PauseLayer sceneWithScene:[[CCDirector sharedDirector] runningScene]]]; | 144 | [((GameLayer*)[[[CCDirector sharedDirector] runningScene] getChildByTag:GAME_LAYER]) pause]; |
| 145 | } | 145 | } |
| 146 | } | 146 | } |
| 147 | 147 | ||
| diff --git a/Classes/GameLayer.h b/Classes/GameLayer.h index 88a1406..e72b551 100755 --- a/Classes/GameLayer.h +++ b/Classes/GameLayer.h | |||
| @@ -15,9 +15,13 @@ | |||
| 15 | #import "Rock.h" | 15 | #import "Rock.h" |
| 16 | #import "GameOverLayer.h" | 16 | #import "GameOverLayer.h" |
| 17 | #import "ValuableObject.h" | 17 | #import "ValuableObject.h" |
| 18 | #import "PauseLayer.h" | ||
| 19 | #import "CocosDenshion.h" | 18 | #import "CocosDenshion.h" |
| 20 | #import "SimpleAudioEngine.h" | 19 | #import "SimpleAudioEngine.h" |
| 20 | #import "TutorialBubble.h" | ||
| 21 | #import "MainMenuLayer.h" | ||
| 22 | |||
| 23 | #define GAME_SCENE 436 | ||
| 24 | #define GAME_LAYER 437 | ||
| 21 | 25 | ||
| 22 | @interface GameLayer : CCLayer { | 26 | @interface GameLayer : CCLayer { |
| 23 | NSMutableSet* objects; | 27 | NSMutableSet* objects; |
| @@ -28,11 +32,19 @@ | |||
| 28 | int lives; | 32 | int lives; |
| 29 | float addSpeed; | 33 | float addSpeed; |
| 30 | CCSprite* cartSprite; | 34 | CCSprite* cartSprite; |
| 35 | TutorialBubble* currentTutorial; | ||
| 36 | |||
| 37 | CCLayerColor* shadedLayer; | ||
| 38 | CCLayer* pauseLayer; | ||
| 31 | } | 39 | } |
| 32 | 40 | ||
| 41 | @property (nonatomic,retain) TutorialBubble* currentTutorial; | ||
| 33 | + (CCScene*)scene; | 42 | + (CCScene*)scene; |
| 34 | - (id)init; | 43 | - (id)init; |
| 35 | - (void)updateLabels; | 44 | - (void)updateLabels; |
| 36 | - (void)pause; | 45 | - (void)pause; |
| 46 | - (void)unpause; | ||
| 47 | - (void)mainmenu; | ||
| 48 | - (void)endTutorial; | ||
| 37 | 49 | ||
| 38 | @end | 50 | @end |
| diff --git a/Classes/GameLayer.m b/Classes/GameLayer.m index 32ca483..2ec8ef5 100755 --- a/Classes/GameLayer.m +++ b/Classes/GameLayer.m | |||
| @@ -11,6 +11,8 @@ | |||
| 11 | 11 | ||
| 12 | @implementation GameLayer | 12 | @implementation GameLayer |
| 13 | 13 | ||
| 14 | @synthesize currentTutorial; | ||
| 15 | |||
| 14 | + (CCScene*)scene | 16 | + (CCScene*)scene |
| 15 | { | 17 | { |
| 16 | CCScene* scene = [CCScene node]; | 18 | CCScene* scene = [CCScene node]; |
| @@ -19,9 +21,10 @@ | |||
| 19 | [scene addChild:backgroundLayer]; | 21 | [scene addChild:backgroundLayer]; |
| 20 | 22 | ||
| 21 | GameLayer* layer = [GameLayer node]; | 23 | GameLayer* layer = [GameLayer node]; |
| 24 | layer.tag = GAME_LAYER; | ||
| 22 | [scene addChild:layer]; | 25 | [scene addChild:layer]; |
| 23 | 26 | ||
| 24 | scene.tag = 436; | 27 | scene.tag = GAME_SCENE; |
| 25 | 28 | ||
| 26 | return scene; | 29 | return scene; |
| 27 | } | 30 | } |
| @@ -266,13 +269,13 @@ | |||
| 266 | return self; | 269 | return self; |
| 267 | } | 270 | } |
| 268 | 271 | ||
| 269 | -(void) onEnter | 272 | - (void)onEnter |
| 270 | { | 273 | { |
| 271 | [super onEnter]; | 274 | [super onEnter]; |
| 272 | 275 | ||
| 273 | [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; | 276 | [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 60)]; |
| 274 | [self schedule:@selector(tick:) interval:1.0f/60.0f]; | 277 | [self schedule:@selector(tick:) interval:1.0f/60.0f]; |
| 275 | [self schedule:@selector(randomlyAddObject:) interval:addSpeed]; | 278 | [self schedule:@selector(randomlyAddObject:) interval:addSpeed]; |
| 276 | } | 279 | } |
| 277 | 280 | ||
| 278 | - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration | 281 | - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration |
| @@ -295,7 +298,78 @@ | |||
| 295 | 298 | ||
| 296 | - (void)pause | 299 | - (void)pause |
| 297 | { | 300 | { |
| 298 | [[CCDirector sharedDirector] replaceScene:[PauseLayer sceneWithScene:[[CCDirector sharedDirector] runningScene]]]; | 301 | if (self.currentTutorial != nil) |
| 302 | { | ||
| 303 | [self.currentTutorial removeFromSuperview]; | ||
| 304 | } | ||
| 305 | |||
| 306 | [self pauseSchedulerAndActions]; | ||
| 307 | |||
| 308 | shadedLayer = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)]; | ||
| 309 | [[[CCDirector sharedDirector] runningScene] addChild:shadedLayer]; | ||
| 310 | |||
| 311 | pauseLayer = [CCLayer node]; | ||
| 312 | CCLabelBMFont* scoreLabel2 = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"]; | ||
| 313 | scoreLabel2.position = ccp(240,90); | ||
| 314 | [pauseLayer addChild:scoreLabel2]; | ||
| 315 | |||
| 316 | CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)]; | ||
| 317 | CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; | ||
| 318 | [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; | ||
| 319 | [pauseLayer addChild:pauseMenu]; | ||
| 320 | |||
| 321 | CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)]; | ||
| 322 | CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil]; | ||
| 323 | myMenu.position = ccp(240, 60); | ||
| 324 | [pauseLayer addChild:myMenu]; | ||
| 325 | |||
| 326 | [[[CCDirector sharedDirector] runningScene] addChild:pauseLayer]; | ||
| 327 | } | ||
| 328 | |||
| 329 | - (void)unpause | ||
| 330 | { | ||
| 331 | [[[CCDirector sharedDirector] runningScene] removeChild:shadedLayer cleanup:YES]; | ||
| 332 | [[[CCDirector sharedDirector] runningScene] removeChild:pauseLayer cleanup:YES]; | ||
| 333 | |||
| 334 | shadedLayer = nil; | ||
| 335 | pauseLayer = nil; | ||
| 336 | |||
| 337 | if (self.currentTutorial != nil) | ||
| 338 | { | ||
| 339 | [[[CCDirector sharedDirector] openGLView] addSubview:self.currentTutorial]; | ||
| 340 | } else { | ||
| 341 | [self resumeSchedulerAndActions]; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | - (void)mainmenu | ||
| 346 | { | ||
| 347 | [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; | ||
| 348 | } | ||
| 349 | |||
| 350 | - (void)setCurrentTutorial:(TutorialBubble *)m_currentTutorial | ||
| 351 | { | ||
| 352 | @synchronized(self) | ||
| 353 | { | ||
| 354 | if (currentTutorial != m_currentTutorial) | ||
| 355 | { | ||
| 356 | [currentTutorial release]; | ||
| 357 | currentTutorial = [m_currentTutorial retain]; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | if (currentTutorial != nil) | ||
| 362 | { | ||
| 363 | [currentTutorial setTarget:self action:@selector(endTutorial)]; | ||
| 364 | [[[CCDirector sharedDirector] openGLView] addSubview:currentTutorial]; | ||
| 365 | [self pauseSchedulerAndActions]; | ||
| 366 | } | ||
| 367 | } | ||
| 368 | |||
| 369 | - (void)endTutorial | ||
| 370 | { | ||
| 371 | self.currentTutorial = nil; | ||
| 372 | [self resumeSchedulerAndActions]; | ||
| 299 | } | 373 | } |
| 300 | 374 | ||
| 301 | @end | 375 | @end |
| diff --git a/Classes/PauseLayer.h b/Classes/PauseLayer.h deleted file mode 100755 index aae5d6c..0000000 --- a/Classes/PauseLayer.h +++ /dev/null | |||
| @@ -1,22 +0,0 @@ | |||
| 1 | // | ||
| 2 | // PauseLayer.h | ||
| 3 | // Cart Collect | ||
| 4 | // | ||
| 5 | // Created by iD Student Account on 7/20/11. | ||
| 6 | // Copyright 2011 __MyCompanyName__. All rights reserved. | ||
| 7 | // | ||
| 8 | |||
| 9 | #import <Foundation/Foundation.h> | ||
| 10 | #import "cocos2d.h" | ||
| 11 | #import "GameLayer.h" | ||
| 12 | |||
| 13 | @interface PauseLayer : CCLayer { | ||
| 14 | CCScene* game; | ||
| 15 | } | ||
| 16 | |||
| 17 | + (CCScene*)sceneWithScene:(CCScene*)scene; | ||
| 18 | - (id)initWithScene:(CCScene*)scene; | ||
| 19 | - (void)unpause; | ||
| 20 | - (void)newgame; | ||
| 21 | |||
| 22 | @end | ||
| diff --git a/Classes/PauseLayer.m b/Classes/PauseLayer.m deleted file mode 100755 index 53574b2..0000000 --- a/Classes/PauseLayer.m +++ /dev/null | |||
| @@ -1,68 +0,0 @@ | |||
| 1 | // | ||
| 2 | // PauseLayer.m | ||
| 3 | // Cart Collect | ||
| 4 | // | ||
| 5 | // Created by iD Student Account on 7/20/11. | ||
| 6 | // Copyright 2011 __MyCompanyName__. All rights reserved. | ||
| 7 | // | ||
| 8 | |||
| 9 | #import "PauseLayer.h" | ||
| 10 | |||
| 11 | @implementation PauseLayer | ||
| 12 | |||
| 13 | + (CCScene*)sceneWithScene:(CCScene*)scene2 | ||
| 14 | { | ||
| 15 | CCScene* scene = [CCScene node]; | ||
| 16 | |||
| 17 | CCLayerColor* backgroundLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; | ||
| 18 | CCSprite* backgroundImage = [CCSprite spriteWithFile:@"SeaBeach.png"]; | ||
| 19 | backgroundImage.position = ccp(240,160); | ||
| 20 | [backgroundLayer addChild:backgroundImage]; | ||
| 21 | [scene addChild:backgroundLayer]; | ||
| 22 | |||
| 23 | CCLayerColor* backgroundLayer2 = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)]; | ||
| 24 | [scene addChild:backgroundLayer2]; | ||
| 25 | |||
| 26 | PauseLayer* layer = [[[PauseLayer alloc] initWithScene:scene2] autorelease]; | ||
| 27 | [scene addChild:layer]; | ||
| 28 | |||
| 29 | return scene; | ||
| 30 | } | ||
| 31 | |||
| 32 | - (id)initWithScene:(CCScene*)scene | ||
| 33 | { | ||
| 34 | self = [super init]; | ||
| 35 | |||
| 36 | if (nil != self) | ||
| 37 | { | ||
| 38 | game = [scene retain]; | ||
| 39 | |||
| 40 | CCLabelBMFont* scoreLabel = [CCLabelBMFont labelWithString:@"PAUSE" fntFile:@"helvetica.fnt"]; | ||
| 41 | scoreLabel.position = ccp(240,90); | ||
| 42 | [self addChild:scoreLabel]; | ||
| 43 | |||
| 44 | CCMenuItemImage* pauseButton = [CCMenuItemImage itemFromNormalImage:@"pause2.png" selectedImage:@"pause.png" target:self selector:@selector(unpause)]; | ||
| 45 | CCMenu* pauseMenu = [CCMenu menuWithItems:pauseButton, nil]; | ||
| 46 | [pauseMenu setPosition:ccp(480-8-16, 320-8-16)]; | ||
| 47 | [self addChild:pauseMenu]; | ||
| 48 | |||
| 49 | CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(newgame)]; | ||
| 50 | CCMenu* myMenu = [CCMenu menuWithItems:newgameMenuItem, nil]; | ||
| 51 | myMenu.position = ccp(240, 60); | ||
| 52 | [self addChild:myMenu]; | ||
| 53 | } | ||
| 54 | |||
| 55 | return self; | ||
| 56 | } | ||
| 57 | |||
| 58 | - (void)unpause | ||
| 59 | { | ||
| 60 | [[CCDirector sharedDirector] replaceScene:game]; | ||
| 61 | } | ||
| 62 | |||
| 63 | - (void)newgame | ||
| 64 | { | ||
| 65 | [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]]; | ||
| 66 | } | ||
| 67 | |||
| 68 | @end | ||
| diff --git a/Classes/TutorialBubble.h b/Classes/TutorialBubble.h new file mode 100644 index 0000000..4ce3352 --- /dev/null +++ b/Classes/TutorialBubble.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | // | ||
| 2 | // TutorialBubble.h | ||
| 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 <UIKit/UIKit.h> | ||
| 10 | #import "cocos2d.h" | ||
| 11 | |||
| 12 | @interface TutorialBubble : UIView { | ||
| 13 | UILabel* textView; | ||
| 14 | UIImage* background; | ||
| 15 | UIImageView* imageView; | ||
| 16 | UIImageView* arrowView; | ||
| 17 | UIButton* button; | ||
| 18 | id target; | ||
| 19 | SEL action; | ||
| 20 | NSString* name; | ||
| 21 | } | ||
| 22 | |||
| 23 | @property (readonly) NSString* name; | ||
| 24 | - (id)initWithText:(NSString*)text name:(NSString*)name; | ||
| 25 | - (id)initWithText:(NSString*)text name:(NSString*)name spriteReference:(CCSprite*)spriteReference; | ||
| 26 | - (void)buttonPressed:(id)sender; | ||
| 27 | - (void)setTarget:(id)sender action:(SEL)action; | ||
| 28 | |||
| 29 | @end | ||
| 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 | ||
