Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Rodella committed Jul 8, 2011
0 parents commit 964e191
Show file tree
Hide file tree
Showing 239 changed files with 54,660 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions Classes/GameConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// GameConfig.h
// TextBoxLayerSample
//
// Created by Fabio Rodella on 1/19/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//

#ifndef __GAME_CONFIG_H
#define __GAME_CONFIG_H

//
// Supported Autorotations:
// None,
// UIViewController,
// CCDirector
//
#define kGameAutorotationNone 0
#define kGameAutorotationCCDirector 1
#define kGameAutorotationUIViewController 2

//
// Define here the type of autorotation that you want for your game
//
#define GAME_AUTOROTATION kGameAutorotationUIViewController


#endif // __GAME_CONFIG_H
25 changes: 25 additions & 0 deletions Classes/HelloWorldScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// HelloWorldLayer.h
// TextBoxLayerSample
//
// Created by Fabio Rodella on 1/19/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//


// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "TextBoxLayer.h"

// HelloWorld Layer
@interface HelloWorld : CCLayer <TextBoxDelegate>
{
TextBoxLayer *textBox;
}

// returns a Scene that contains the HelloWorld as the only child
+(id) scene;

- (void)gameLoop:(ccTime) dT;

@end
74 changes: 74 additions & 0 deletions Classes/HelloWorldScene.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// HelloWorldLayer.m
// TextBoxLayerSample
//
// Created by Fabio Rodella on 1/19/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//

// Import the interfaces
#import "HelloWorldScene.h"

// HelloWorld implementation
@implementation HelloWorld

+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];

// add layer as a child to scene
[scene addChild: layer];

[layer schedule:@selector(gameLoop:) interval:1/60.0f];

// return the scene
return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {

textBox = [[TextBoxLayer alloc] initWithColor:ccc4(0, 0, 255, 255) width:200 height:80 padding:10 text:@"This is a purposefully long text so as to test the wrapping functionality of the TextBoxLayer, as well as the multiple pages. That's it! This is the end, my only friend, the end."];

// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];

// position the label on the center of the screen
textBox.position = ccp( size.width /2 , size.height/2 );

textBox.delegate = self;

// add the label as a child to this Layer
[self addChild: textBox];
}
return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
[textBox release];
// don't forget to call "super dealloc"
[super dealloc];
}

- (void)gameLoop: (ccTime) dT {
[textBox update:dT];
}

-(void) textBox:(TextBoxLayer *)tbox didFinishAllTextWithPageCount:(int)pc {
[self removeChild:textBox cleanup:YES];
}

@end
16 changes: 16 additions & 0 deletions Classes/RootViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// RootViewController.h
// TextBoxLayerSample
//
// Created by Fabio Rodella on 1/19/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface RootViewController : UIViewController {

}

@end
152 changes: 152 additions & 0 deletions Classes/RootViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//
// RootViewController.m
// TextBoxLayerSample
//
// Created by Fabio Rodella on 1/19/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//

//
// RootViewController + iAd
// If you want to support iAd, use this class as the controller of your iAd
//

#import "cocos2d.h"

#import "RootViewController.h"
#import "GameConfig.h"

@implementation RootViewController

/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

//
// There are 2 ways to support auto-rotation:
// - The OpenGL / cocos2d way
// - Faster, but doesn't rotate the UIKit objects
// - The ViewController way
// - A bit slower, but the UiKit objects are placed in the right place
//

#if GAME_AUTOROTATION==kGameAutorotationNone
//
// EAGLView won't be autorotated.
// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
//
return ( interfaceOrientation == UIInterfaceOrientationPortrait );

#elif GAME_AUTOROTATION==kGameAutorotationCCDirector
//
// EAGLView will be rotated by cocos2d
//
// Sample: Autorotate only in landscape mode
//
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeRight];
} else if( interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[[CCDirector sharedDirector] setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
}

// Since this method should return YES in at least 1 orientation,
// we return YES only in the Portrait orientation
return ( interfaceOrientation == UIInterfaceOrientationPortrait );

#elif GAME_AUTOROTATION == kGameAutorotationUIViewController
//
// EAGLView will be rotated by the UIViewController
//
// Sample: Autorotate only in landscpe mode
//
// return YES for the supported orientations

return ( UIInterfaceOrientationIsLandscape( interfaceOrientation ) );

#else
#error Unknown value in GAME_AUTOROTATION

#endif // GAME_AUTOROTATION


// Shold not happen
return NO;
}

//
// This callback only will be called when GAME_AUTOROTATION == kGameAutorotationUIViewController
//
#if GAME_AUTOROTATION == kGameAutorotationUIViewController
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//
// Assuming that the main window has the size of the screen
// BUG: This won't work if the EAGLView is not fullscreen
///
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect rect;

if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
rect = screenRect;

else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );

CCDirector *director = [CCDirector sharedDirector];
EAGLView *glView = [director openGLView];
float contentScaleFactor = [director contentScaleFactor];

if( contentScaleFactor != 1 ) {
rect.size.width *= contentScaleFactor;
rect.size.height *= contentScaleFactor;
}
glView.frame = rect;
}
#endif // GAME_AUTOROTATION == kGameAutorotationUIViewController


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


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


@end

62 changes: 62 additions & 0 deletions Classes/TextBoxLayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// TextBoxLayer.h
// TextBoxLayerSample
//
// Created by Fabio Rodella on 1/19/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"

#define TEXT_SPEED 60
#define TEXT_FONT_FILE @"arial16.fnt"

@class TextBoxLayer;

@protocol TextBoxDelegate <NSObject>

- (void)textBox:(TextBoxLayer *)tbox
didFinishAllTextWithPageCount:(int)pc;

@optional
- (void)textBox:(TextBoxLayer *)tbox didMoveToPage:(int)p;

@end


@interface TextBoxLayer : CCLayerColor {

CCLabelBMFont *textLabel;

NSString *text;
NSMutableArray *lines;

float progress;
int linesPerPage;
int currentPageIndex;
NSMutableString *currentPage;
int currentPageCharCount;

int totalPages;

id<TextBoxDelegate> delegate;

BOOL ended;
}

@property (readwrite,retain) id<TextBoxDelegate> delegate;

- (id) initWithColor:(ccColor4B)color
width:(GLfloat)w
height:(GLfloat)h
padding:(GLfloat)padding
text:(NSString *)txt;

- (void)update:(float)dt;

- (NSString *)nextPage;

- (int)calculateStringSize:(NSString *)txt;

@end
Loading

0 comments on commit 964e191

Please sign in to comment.