Skip to content

Commit

Permalink
* Don't require style draw methods to return a BOOL (keeps it simpler)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehewitt committed Apr 15, 2009
1 parent b8a6e0f commit fe8a0d6
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 99 deletions.
2 changes: 1 addition & 1 deletion samples/TTCatalog/Classes/StyledTextTestController.m
Expand Up @@ -12,7 +12,7 @@ - (TTStyle*)blueText {
- (TTStyle*)blueBox {
return
[TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:TT_ROUNDED] next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(2, -5, 0, -5) next:
[TTInsetStyle styleWithInset:UIEdgeInsetsMake(2, -5, -2, -5) next:
[TTShadowStyle styleWithColor:[UIColor grayColor] blur:2 offset:CGSizeMake(1,1) next:
[TTSolidFillStyle styleWithColor:[UIColor cyanColor] next:
[TTSolidBorderStyle styleWithColor:[UIColor grayColor] width:1 next:nil]]]]];
Expand Down
2 changes: 1 addition & 1 deletion src/TTDefaultStyleSheet.m
Expand Up @@ -26,7 +26,7 @@ - (TTStyle*)linkTextHighlighted {
- (TTStyle*)linkHighlighted {
return
[TTShapeStyle styleWithShape:[TTRoundedRectangleShape shapeWithRadius:4.5] next:
[TTSolidFillStyle styleWithColor:[UIColor colorWithWhite:0 alpha:0.4] next:nil]];
[TTSolidFillStyle styleWithColor:[UIColor colorWithWhite:0 alpha:0.25] next:nil]];
}

- (TTStyle*)thumbView:(UIControlState)state {
Expand Down
3 changes: 2 additions & 1 deletion src/TTLabel.m
Expand Up @@ -41,7 +41,8 @@ - (void)drawRect:(CGRect)rect {
context.contentFrame = context.frame;
context.font = _font;

if (![self.style draw:context]) {
[self.style draw:context];
if (!context.didDrawContent) {
[self drawContent:self.bounds];
}
}
Expand Down
146 changes: 71 additions & 75 deletions src/TTStyle.m
Expand Up @@ -10,6 +10,46 @@

///////////////////////////////////////////////////////////////////////////////////////////////////

@implementation TTStyleContext

@synthesize delegate = _delegate, frame = _frame, contentFrame = _contentFrame, shape = _shape,
font = _font, didDrawContent = _didDrawContent;

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (id)init {
if (self = [super init]) {
_delegate = nil;
_frame = CGRectZero;
_contentFrame = CGRectZero;
_shape = nil;
_font = nil;
_didDrawContent = NO;
}
return self;
}

- (void)dealloc {
[_shape release];
[_font release];
[super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (TTShape*)shape {
if (!_shape) {
_shape = [[TTRectangleShape shape] retain];
}
return _shape;
}

@end

///////////////////////////////////////////////////////////////////////////////////////////////////

@implementation TTStyle

@synthesize next = _next;
Expand Down Expand Up @@ -65,12 +105,8 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (BOOL)draw:(TTStyleContext*)context {
if (_next) {
return [self.next draw:context];
} else {
return NO;
}
- (void)draw:(TTStyleContext*)context {
[self.next draw:context];
}

- (UIEdgeInsets)addToInsets:(UIEdgeInsets)insets forSize:(CGSize)size {
Expand Down Expand Up @@ -121,10 +157,13 @@ + (TTContentStyle*)styleWithNext:(TTStyle*)next {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
[context.delegate drawLayer:context withStyle:self];
- (void)draw:(TTStyleContext*)context {
if ([context.delegate respondsToSelector:@selector(drawLayer:withStyle:)]) {
[context.delegate drawLayer:context withStyle:self];
context.didDrawContent = YES;
}

[self.next draw:context];
return YES;
}

@end
Expand Down Expand Up @@ -162,11 +201,11 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
UIEdgeInsets shapeInsets = [_shape insetsForSize:context.frame.size];
context.contentFrame = TTRectInset(context.contentFrame, shapeInsets);
context.shape = _shape;
return [self.next draw:context];
[self.next draw:context];
}

- (UIEdgeInsets)addToInsets:(UIEdgeInsets)insets forSize:(CGSize)size {
Expand Down Expand Up @@ -222,12 +261,12 @@ - (id)initWithNext:(TTStyle*)next {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGRect rect = context.frame;
context.frame = CGRectMake(rect.origin.x+_inset.left, rect.origin.y+_inset.top,
rect.size.width - (_inset.left + _inset.right),
rect.size.height - (_inset.top + _inset.bottom));
return [self.next draw:context];
[self.next draw:context];
}

- (UIEdgeInsets)addToInsets:(UIEdgeInsets)insets forSize:(CGSize)size {
Expand Down Expand Up @@ -272,9 +311,9 @@ - (id)initWithNext:(TTStyle*)next {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
context.contentFrame = TTRectInset(context.contentFrame, _padding);
return [self.next draw:context];
[self.next draw:context];
}

- (CGSize)addToSize:(CGSize)size context:(TTStyleContext*)context {
Expand Down Expand Up @@ -438,24 +477,22 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
BOOL handled = NO;

- (void)draw:(TTStyleContext*)context {
if ([context.delegate respondsToSelector:@selector(textForLayerWithStyle:)]) {
NSString* text = [context.delegate textForLayerWithStyle:self];
if (text) {
handled = YES;
context.didDrawContent = YES;
[self drawText:text context:context];
}
}

if (!handled && [context.delegate respondsToSelector:@selector(drawLayer:withStyle:)]) {
if (!context.didDrawContent
&& [context.delegate respondsToSelector:@selector(drawLayer:withStyle:)]) {
[context.delegate drawLayer:context withStyle:self];
handled = YES;
context.didDrawContent = YES;
}

[self.next draw:context];
return handled;
}

- (CGSize)addToSize:(CGSize)size context:(TTStyleContext*)context {
Expand Down Expand Up @@ -558,7 +595,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
if (_image) {
[_image drawInRect:context.frame contentMode:_contentMode];
} else if (_defaultImage) {
Expand Down Expand Up @@ -618,7 +655,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
if (_mask) {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
Expand All @@ -630,9 +667,8 @@ - (BOOL)draw:(TTStyleContext*)context {
CGRect maskRect = CGRectMake(0, 0, _mask.size.width, _mask.size.height);
CGContextClipToMask(ctx, maskRect, _mask.CGImage);

BOOL ok = [self.next draw:context];
[self.next draw:context];
CGContextRestoreGState(ctx);
return ok;
} else {
return [self.next draw:context];
}
Expand Down Expand Up @@ -673,7 +709,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGContextRef ctx = UIGraphicsGetCurrentContext();

CGContextSaveGState(ctx);
Expand Down Expand Up @@ -725,7 +761,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = context.frame;

Expand Down Expand Up @@ -779,7 +815,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = context.frame;

Expand Down Expand Up @@ -858,7 +894,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGFloat blurSize = round(_blur / 2);
UIEdgeInsets inset = UIEdgeInsetsMake(blurSize, blurSize, blurSize, blurSize);
if (_offset.width < 0) {
Expand Down Expand Up @@ -886,11 +922,10 @@ - (BOOL)draw:(TTStyleContext*)context {
CGContextSetShadowWithColor(ctx, CGSizeMake(_offset.width, -_offset.height), _blur,
_color.CGColor);
CGContextBeginTransparencyLayer(ctx, nil);
BOOL ok = [self.next draw:context];
[self.next draw:context];
CGContextEndTransparencyLayer(ctx);

CGContextRestoreGState(ctx);
return ok;
}

- (CGSize)addToSize:(CGSize)size context:(TTStyleContext*)context {
Expand All @@ -914,7 +949,7 @@ @implementation TTInnerShadowStyle
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);

Expand Down Expand Up @@ -976,7 +1011,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);

Expand Down Expand Up @@ -1040,7 +1075,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGRect rect = context.frame;
CGRect strokeRect = CGRectInset(rect, _width/2, _width/2);
[context.shape openPath:strokeRect];
Expand Down Expand Up @@ -1137,7 +1172,7 @@ - (void)dealloc {
///////////////////////////////////////////////////////////////////////////////////////////////////
// TTStyle

- (BOOL)draw:(TTStyleContext*)context {
- (void)draw:(TTStyleContext*)context {
CGRect strokeRect = CGRectInset(context.frame, _width/2, _width/2);
[context.shape openPath:strokeRect];

Expand Down Expand Up @@ -1204,42 +1239,3 @@ - (BOOL)draw:(TTStyleContext*)context {
}

@end

///////////////////////////////////////////////////////////////////////////////////////////////////

@implementation TTStyleContext

@synthesize delegate = _delegate, frame = _frame, contentFrame = _contentFrame, shape = _shape,
font = _font;

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (id)init {
if (self = [super init]) {
_delegate = nil;
_frame = CGRectZero;
_contentFrame = CGRectZero;
_shape = nil;
_font = nil;
}
return self;
}

- (void)dealloc {
[_shape release];
[_font release];
[super dealloc];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (TTShape*)shape {
if (!_shape) {
_shape = [[TTRectangleShape shape] retain];
}
return _shape;
}

@end
3 changes: 2 additions & 1 deletion src/TTStyledText.m
Expand Up @@ -424,7 +424,8 @@ - (void)drawInRect:(CGRect)rect {
[style draw:context];
} else {
if (_style) {
if ([_style draw:context]) {
[_style draw:context];
if (context.didDrawContent) {
return;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/TTView.m
Expand Up @@ -37,7 +37,8 @@ - (void)drawRect:(CGRect)rect {
context.frame = self.bounds;
context.contentFrame = context.frame;

if (![style draw:context]) {
[style draw:context];
if (!context.didDrawContent) {
[self drawContent:self.bounds];
}
} else {
Expand Down

0 comments on commit fe8a0d6

Please sign in to comment.