Created
November 19, 2009 06:44
-
-
Save xhan/238596 to your computer and use it in GitHub Desktop.
SlideShow on Cocos2d-iphone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* SlideShowLayer by Will Larson and Luke Hatcher. 10/29/2008. | |
* Released under MIT License. | |
* Please check out from repository for full license detail. | |
* | |
* Nothing too exiting happening here. | |
* Apologies that it is not in Obj2.0 | |
* syntax, but I'm still ambivalent about | |
* the ambiguousness of properties. | |
*/ | |
#import <UIKit/UIKit.h> | |
#import "CocosNode.h" | |
#import "Layer.h" | |
#import "Label.h" | |
#import "Sprite.h" | |
@interface SlideShowLayer : Layer { | |
int slidePosition; | |
int backgroundXPosition; | |
int backgroundYPosition; | |
int descriptionXPosition; | |
int descriptionYPosition; | |
int descriptionHeight; | |
int descriptionWidth; | |
int fontSize; | |
NSString * fontName; | |
NSMutableArray * backgrounds; | |
NSMutableArray * descriptions; | |
Sprite * background; | |
CocosNode * description; | |
} | |
/* | |
* This is the only method for adding slides to the slide show. | |
*/ | |
-(void)addSlideWithBackground: (NSString *)imageString | |
andDescription: (NSString *)descString; | |
/* | |
* Methods for advancing, retreating, and starting the slideshow. | |
* There currently isn't a UI mechanism for retreating to already | |
* seen slides, but you could rig something up if you tried hard | |
* enough. ;) | |
*/ | |
-(void)displayFirstSlide; | |
-(void)displayNextSlide; | |
-(void)displayPreviousSlide; | |
-(void)displaySlide: (int)slideNumber; | |
-(BOOL)hasPreviousSlide; | |
-(BOOL)hasNextSlide; | |
/* | |
* Mutators and Accessors | |
*/ | |
-(NSMutableArray *)backgrounds; | |
-(NSMutableArray *)descriptions; | |
-(Sprite *)background; | |
-(void)setBackground: (Sprite *)aSprite; | |
-(CocosNode *)description; | |
-(void)setDescription: (CocosNode *)aCocosNode; | |
-(NSString *)fontName; | |
-(void)setFontName: (NSString *)aFontName; | |
-(int)fontSize; | |
-(void)setFontSize: (int)anInt; | |
-(int)slidePosition; | |
-(void)setSlidePosition: (int)anInt; | |
-(int)backgroundXPosition; | |
-(void)setBackgroundXPosition: (int)anInt; | |
-(int)backgroundYPosition; | |
-(void)setBackgroundYPosition: (int)anInt; | |
-(int)descriptionXPosition; | |
-(void)setDescriptionXPosition: (int)anInt; | |
-(int)descriptionYPosition; | |
-(void)setDescriptionYPosition: (int)anInt; | |
-(int)descriptionWidth; | |
-(void)setDescriptionWidth: (int)anInt; | |
-(int)descriptionHeight; | |
-(void)setDescriptionHeight: (int)anInt; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* SlideShowLayer by Will Larson and Luke Hatcher. 10/29/2008. | |
* Released under MIT License. | |
* Please check out from repository for full license detail. | |
*/ | |
#import "SlideShowLayer.h" | |
@implementation SlideShowLayer | |
-(id)init { | |
self = [super init]; | |
if (self) { | |
/* | |
* setting isTouchEnabled is the magic step that | |
* allows the layer to be registered for UI events. | |
* forgetting this step will lead to great confusion :-/ | |
*/ | |
isTouchEnabled = YES; | |
slidePosition = -1; | |
backgrounds = [[NSMutableArray alloc] init]; | |
descriptions = [[NSMutableArray alloc] init]; | |
[self setFontName:@"Helvetica"]; | |
[self setFontSize:24]; | |
} | |
return self; | |
} | |
- (void) dealloc { | |
[backgrounds release]; | |
backgrounds = nil; | |
[descriptions release]; | |
descriptions = nil; | |
[background release]; | |
background = nil; | |
[description release]; | |
description = nil; | |
[fontSize release]; | |
fontSize = nil; | |
[super dealloc]; | |
} | |
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | |
/* | |
* We are using pushScene/popScene instead of replaceScene | |
* (which has superior memory usage characteristics), because | |
* it makes integrating SlideShowLayer much simpler, and helps | |
* avoid binding the code to one application without requiring | |
* us to use the delegate pattern. | |
*/ | |
if ([self hasNextSlide]) [self displayNextSlide]; | |
else [[Director sharedDirector] popScene]; | |
} | |
-(void)addSlideWithBackground: (NSString *)imageString | |
andDescription: (NSString *)descString { | |
[[self backgrounds] addObject:imageString]; | |
[[self descriptions] addObject:descString]; | |
} | |
-(void)displayFirstSlide { | |
if ([[self backgrounds] count] > 0) { | |
[self setSlidePosition:0]; | |
[self displaySlide:[self slidePosition]]; | |
} | |
} | |
-(void)displayNextSlide { | |
if ([self hasNextSlide]) { | |
[self setSlidePosition:[self slidePosition]+1]; | |
[self displaySlide:[self slidePosition]]; | |
} | |
} | |
-(void)displayPreviousSlide { | |
if ([self hasPreviousSlide]) { | |
[self setSlidePosition:[self slidePosition]-1]; | |
[self displaySlide:[self slidePosition]]; | |
} | |
} | |
-(void)displaySlide: (int)slideNumber { | |
/* | |
* Remove existing slide and description. | |
*/ | |
if ([self background]) { | |
[self remove:[self background]]; | |
} | |
if ([self description]) { | |
[self remove:[self description]]; | |
} | |
/* | |
* Retrieve and generate necessary details for next slide. | |
*/ | |
NSString * bgString = [[self backgrounds] objectAtIndex:slideNumber]; | |
NSString * descString = [[self descriptions] objectAtIndex:slideNumber]; | |
Sprite * bg = [Sprite spriteWithFile:bgString]; | |
bg.position = cpv([self backgroundXPosition], [self backgroundYPosition]); | |
CocosNode * desc = [Label labelWithString:descString | |
dimensions:CGSizeMake([self descriptionWidth], | |
[self descriptionHeight]) | |
alignment:UITextAlignmentCenter | |
fontName:[self fontName] | |
fontSize:[self fontSize]]; | |
desc.position = cpv([self descriptionXPosition], [self descriptionYPosition]); | |
// Add the background and desc to the layer. | |
[self setBackground:bg]; | |
[self setDescription:desc]; | |
[self add:bg]; | |
[self add:desc]; | |
} | |
-(BOOL)hasNextSlide { | |
return ([self slidePosition]+1 < [[self backgrounds] count]); | |
} | |
-(BOOL)hasPreviousSlide { | |
return ([self slidePosition] > 0); | |
} | |
-(Sprite *)background { | |
return background; | |
} | |
-(void)setBackground: (Sprite *)aSprite { | |
if (background) [background release]; | |
background = [aSprite retain]; | |
} | |
-(CocosNode *)description { | |
return description; | |
} | |
-(void)setDescription: (CocosNode *)aCocosNode { | |
if (description) [description release]; | |
description = [aCocosNode retain]; | |
} | |
-(NSMutableArray *)backgrounds { | |
return backgrounds; | |
} | |
-(NSMutableArray *)descriptions { | |
return descriptions; | |
} | |
-(NSString *)fontName { | |
return fontName | |
} | |
-(int)fontSize { | |
return fontSize; | |
} | |
-(void)setFontSize: (int)anInt { | |
fontSize = anInt; | |
} | |
-(void)setFontName: (NSString *)aFontName { | |
if (fontName) [fontName release]; | |
fontName = [aFontName retain]; | |
} | |
-(int)slidePosition { | |
return slidePosition; | |
} | |
-(void)setSlidePosition: (int)anInt { | |
slidePosition = anInt; | |
} | |
-(int)backgroundXPosition { | |
return backgroundXPosition; | |
} | |
-(void)setBackgroundXPosition: (int)anInt { | |
backgroundXPosition = anInt; | |
} | |
-(int)backgroundYPosition { | |
return backgroundYPosition; | |
} | |
-(void)setBackgroundYPosition: (int)anInt { | |
backgroundYPosition = anInt; | |
} | |
-(int)descriptionXPosition { | |
return descriptionXPosition; | |
} | |
-(void)setDescriptionXPosition: (int)anInt { | |
descriptionXPosition = anInt; | |
} | |
-(int)descriptionYPosition { | |
return descriptionYPosition; | |
} | |
-(void)setDescriptionYPosition: (int)anInt { | |
descriptionYPosition = anInt; | |
} | |
-(int)descriptionWidth { | |
return descriptionWidth; | |
} | |
-(void)setDescriptionWidth: (int)anInt { | |
descriptionWidth = anInt; | |
} | |
-(int)descriptionHeight { | |
return descriptionHeight; | |
} | |
-(void)setDescriptionHeight: (int)anInt { | |
descriptionHeight = anInt; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize scene and slide show layer. | |
Scene * s = [Scene node]; | |
SlideShowLayer * ssl = [SlideShowLayer node]; | |
// Set positions for the background and labels. | |
// Generally you'll be using full screen backgrounds | |
// (screenshots), and positioning them at the center. | |
[ssl setBackgroundXPosition:100]; | |
[ssl setBackgroundYPosition:100]; | |
[ssl setDescriptionXPosition: 200]; | |
[ssl setDescriptionYPosition: 200]; | |
[ssl setDescriptionWidth:100]; | |
[ssl setDescriptionHeight:300]; | |
[ssl setFontName:@"Helvetica"]; // optional | |
[ssl setFontSize:24]; // optional | |
// Add slides in order they appear. | |
[ssl addSlideWithBackground:@"pic1.png" andDescription:@"Yada yada one."]; | |
[ssl addSlideWithBackground:@"pic2.png" andDescription:@"Yada yada two."]; | |
[ssl addSlideWithBackground:@"pic3.png" andDescription:@"Yada yada 3."]; | |
[ssl addSlideWithBackground:@"pic4.png" andDescription:@"Yada yada 4."]; | |
[ssl addSlideWithBackground:@"pic5.png" andDescription:@"Yada yada 5."]; | |
[ssl addSlideWithBackground:@"pic6.png" andDescription:@"Yada yada six."]; | |
// Start and display the slide show. | |
[ssl displayFirstSlide]; | |
[scene add:ssl]; | |
[[Director sharedDirector] pushScene:scene]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment