Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Changes:

June 18, 2012: Send current page is done typing message to the delegate after a touch completes the page - Tebruno99

June 16, 2012: Add new notification that the current page is done typing. - Tebruno99

June 16, 2012: Refactor the typing so that text speed is settable and it uses characters per second. -Tebruno99

June 16, 2012: Fix performance issue with main for loop in TextBoxLayer by elminating the for loop and not iterating over the entire array of characters. - Tebruno99

June 16, 2012: Updated to 1.1beta2 cocos2d - Tebruno99
4 changes: 2 additions & 2 deletions Classes/HelloWorldScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ -(id) init

NSString *txt = @"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.";

textBox = [[TextBoxLayer alloc] initWithColor:[UIColor blueColor] width:200 height:80 padding:10 text:txt];
textBox = [[TextBoxLayer alloc] initWithColor:[UIColor blueColor] width:200 height:80 padding:10 speed:0.01f text:txt];
textBox.position = ccp( size.width /2 , size.height/2 );
textBox.delegate = self;

textBoxView = [[TextBoxView alloc] initWithColor:[UIColor blueColor] width:200 height:80 padding:10 text:txt];
textBoxView = [[TextBoxView alloc] initWithColor:[UIColor blueColor] width:200 height:80 padding:10 speed:80 text:txt];
textBoxView.center = ccp( size.width /2 , size.height/2 );
textBoxView.delegate = self;

Expand Down
5 changes: 3 additions & 2 deletions Classes/TextBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
- (id) initWithColor:(UIColor *)color
width:(CGFloat)w
height:(CGFloat)h
padding:(CGFloat)padding
padding:(CGFloat)padding
speed:(CGFloat)ts
text:(NSString *)txt;

- (void)update:(float)dt;
Expand All @@ -29,5 +30,5 @@

@optional
- (void)textBox:(id<TextBox>)tbox didMoveToPage:(int)p;

- (void)textBox:(id<TextBox>)tbox didFinishAllTextOnPage:(int)p;
@end
4 changes: 3 additions & 1 deletion Classes/TextBoxLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
int currentPageIndex;
NSMutableString *currentPage;
int currentPageCharCount;

CGFloat textSpeed;
ccTime countDownTimer;

int totalPages;

id<TextBoxDelegate> delegate;
Expand Down
70 changes: 41 additions & 29 deletions Classes/TextBoxLayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
//

#import "TextBoxLayer.h"
#import "uthash.h"

@implementation TextBoxLayer

@synthesize delegate;

- (id) initWithColor:(UIColor *)color width:(CGFloat)w height:(CGFloat)h padding:(CGFloat)padding text:(NSString *)txt {
- (id) initWithColor:(UIColor *)color width:(CGFloat)w height:(CGFloat)h padding:(CGFloat)padding speed:(CGFloat)ts text:(NSString *)txt {

int numComponents = CGColorGetNumberOfComponents(color.CGColor);

Expand All @@ -30,9 +31,11 @@ - (id) initWithColor:(UIColor *)color width:(CGFloat)w height:(CGFloat)h padding
ccColor4B col = ccc4((GLubyte) r * 255 , (GLubyte) g * 255, (GLubyte) b * 255, (GLubyte) a * 255);

if ((self = [super initWithColor:col width:w + (padding * 2) height:h + (padding * 2)])) {

self.isTouchEnabled = YES;

self.isTouchEnabled = YES;
textSpeed = ts;
countDownTimer = textSpeed; // Arm the countdown timer.
ended = NO;
currentPageIndex = 0;

Expand Down Expand Up @@ -103,30 +106,33 @@ - (void)dealloc {
[super dealloc];
}

- (void)update:(float)dt {

progress += (dt * TEXT_SPEED);

int visible = progress;

if (visible > currentPageCharCount) {
progress = visible = currentPageCharCount;
}

// Each character sprite is assigned a tag corresponding to its index in the string,
// and even though line-breaks are skipped, they are still counted for tag purposes.
// Therefore, we use an offset so that the tag is correct.
int offset = 0;

for (int i = 0; i < visible; i++) {

if ([currentPage characterAtIndex:i + offset] == '\n') {
offset++;
}

CCSprite *charSpr = (CCSprite *) [textLabel getChildByTag:i + offset];
charSpr.opacity = 255;
}
- (void)update:(ccTime)dt {
static int newLineOffset = 0;
if(progress > currentPageCharCount - 1) {
newLineOffset = 0;
return;
}

countDownTimer = countDownTimer - dt;

if(countDownTimer < 0) {
if ([currentPage characterAtIndex:progress + newLineOffset] == '\n') {
newLineOffset++;
}

CCSprite *charSpr = (CCSprite *) [textLabel getChildByTag:progress + newLineOffset];
charSpr.opacity = 255;

progress++;

countDownTimer = textSpeed;
}

if(progress > currentPageCharCount - 1) {
if ([delegate respondsToSelector:@selector(textBox:didFinishAllTextOnPage:)]) {
[delegate textBox:(id<TextBox>) self didFinishAllTextOnPage:currentPageIndex];
}
}
}

- (NSString *)nextPage {
Expand Down Expand Up @@ -160,8 +166,9 @@ - (int)calculateStringSize:(NSString *)txt {
for (int i = 0; i < [txt length] / [CCDirector sharedDirector].contentScaleFactor; i++) {

int c = [txt characterAtIndex:i];
ccBMFontDef def = conf->BMFontArray_[c];
totalSize += def.xAdvance;
ccBMFontDef *def = NULL;
HASH_FIND_INT(conf->BMFontHash_,&c,def);
totalSize += def->xAdvance;
}

return totalSize;
Expand All @@ -176,6 +183,11 @@ -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
charSpr.opacity = 255;
}
progress = currentPageCharCount;
if(progress > currentPageCharCount - 1) {
if ([delegate respondsToSelector:@selector(textBox:didFinishAllTextOnPage:)]) {
[delegate textBox:(id<TextBox>) self didFinishAllTextOnPage:currentPageIndex];
}
}

} else {

Expand Down
1 change: 1 addition & 0 deletions Classes/TextBoxView.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
float progress;

int currentPage;
CGFloat textSpeed;

NSMutableArray *pages;

Expand Down
5 changes: 3 additions & 2 deletions Classes/TextBoxView.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ @implementation TextBoxView

@synthesize delegate;

- (id) initWithColor:(UIColor *)color width:(CGFloat)w height:(CGFloat)h padding:(CGFloat)padding text:(NSString *)txt {
- (id) initWithColor:(UIColor *)color width:(CGFloat)w height:(CGFloat)h padding:(CGFloat)padding speed:(CGFloat)ts text:(NSString *)txt {

if ((self = [super initWithFrame:CGRectMake(0, 0, w + (padding * 2), h + (padding * 2))])) {
self.backgroundColor = color;

width = w;
height = h;

textSpeed = ts;
currentPage = 0;
text = [txt retain];

Expand Down Expand Up @@ -68,7 +69,7 @@ - (void)update:(float)dt {

NSString *page = [pages objectAtIndex:currentPage];

progress += (dt * TEXT_SPEED);
progress += (dt * textSpeed);

int visible = progress;
if (visible > [page length]) {
Expand Down
Loading