Skip to content

Instantly share code, notes, and snippets.

@joehoyle
Created March 6, 2011 22:26
Show Gist options
  • Save joehoyle/857789 to your computer and use it in GitHub Desktop.
Save joehoyle/857789 to your computer and use it in GitHub Desktop.
@implementation WPRRoundedRectangleDrawer : CPObject
{
CPArray backgroundColors @accessors;
CPArray corners @accessors;
int innerMargin @accessors;
id shadowSize @accessors;
int shadowBlur @accessors;
}
- (id)init
{
[super init];
backgroundColors = [[CPColor colorWithHexString:@"f9f9f9"], [CPColor colorWithHexString:@"e9e9e9"]];
corners = [YES, YES, YES, YES];
innerMargin = 1;
shadowSize = CGSizeMake(0, 1);
shadowBlur = 2;
return self;
}
- (void)initWithCoder:(CPCoder)coder
{
[super init];
backgroundColors = [coder decodeObjectForKey:@"backgroundColors"];
corners = [coder decodeObjectForKey:@"corners"];
innerMargin = [coder decodeObjectForKey:@"innerMargin"];
shadowSize = [coder decodeObjectForKey:@"shadowSize"];
shadowBlur = [coder decodeObjectForKey:@"shadowBlur"];
return self;
}
- (void)encodeWithCoder:(CPCoder)coder
{
[coder encodeObject:backgroundColors forKey:@"backgroundColors"];
[coder encodeObject:corners forKey:@"corners"];
[coder encodeObject:innerMargin forKey:@"innerMargin"];
[coder encodeObject:shadowSize forKey:@"shadowSize"];
[coder encodeObject:shadowBlur forKey:@"shadowBlur"];
}
- (void)drawView:(CPView)aView inRect:(CPRect)aRect withContext:(id)context
{
var innerFrame = aRect;
var shadowColor = [CPColor blackColor];
//use 4 co-ords for margin
if( typeof( innerMargin ) == @"number" ) {
innerMargin = [innerMargin, innerMargin + 2];
}
if( innerMargin.length == 2 )
innerMargin = [innerMargin[0], innerMargin[1], innerMargin[0], innerMargin[1]];
console.log(innerMargin);
shadowColor = [shadowColor colorWithAlphaComponent:.2];
innerFrame.origin.x += innerMargin[0];
innerFrame.origin.y += innerMargin[1];
innerFrame.size.width -= ( innerMargin[2] * 2 );
innerFrame.size.height -= ( innerMargin[1] * 2 );
console.log(innerFrame);
if( [backgroundColors count] <= 1 )
{
CGContextSetFillColor( context, [backgroundColors objectAtIndex:0] );
CGContextSetShadowWithColor( context, CGSizeMake( 0, 2 ), 2, shadowColor );
CGContextFillRoundedRectangleInRect( context, innerFrame, 5, corners[0], corners[1], corners[2], corners[3] );
}
else
{
var backgroundComponents = [];
var backgroundPositions = [];
for( i = 0; i < [backgroundColors count]; i++ ) {
var backgroundColor = [backgroundColors objectAtIndex:i];
[backgroundComponents addObject:[backgroundColor redComponent]];
[backgroundComponents addObject:[backgroundColor greenComponent]];
[backgroundComponents addObject:[backgroundColor blueComponent]];
[backgroundComponents addObject:[backgroundColor alphaComponent]];
[backgroundPositions addObject: i == 0 ? 0 : [backgroundColors count] / i];
}
var myGrad = CGGradientCreateWithColorComponents(CGColorSpaceCreateDeviceRGB(), backgroundComponents, backgroundPositions, [backgroundColors count]);
var startPoint = innerFrame.origin;
var endPoint = CGPointMake(startPoint.x, startPoint.y + innerFrame.size.height);
CGContextSetFillColor( context, [CPColor whiteColor] );
CGContextSetShadowWithColor( context, shadowSize, shadowBlur, shadowColor );
CGContextFillRoundedRectangleInRect( context, innerFrame, 5, corners[0], corners[1], corners[2], corners[3] );
CGContextDrawLinearGradient(context, myGrad, startPoint, endPoint, 0);
CGContextFillRoundedRectangleInRect( context, innerFrame, 5, corners[0], corners[1], corners[2], corners[3] );
}
}
@end
@implementation WPRInsetRectangleDrawer : CPObject
{
int shadowBlur @accessors;
CPColor shadowColor @accessors;
int shadowOffset @accessors;
}
- (id)init
{
self = [super init];
shadowBlur = 5;
shadowColor = [CPColor blackColor];
shadowOffset = 5;
return self;
}
- (void)drawView:(CPView)aView inRect:(CPRect)aRect withContext:(id)context
{
var frame = aRect;
//top inner shadow
CGContextSetFillColor( context, [CPColor redColor] );
CGContextSetShadowWithColor( context, CGSizeMake(0, shadowOffset), shadowBlur, shadowColor );
frame = CGRectMake( 0, -30, aRect.size.width, 30 );
CGContextFillRoundedRectangleInRect( context, frame, shadowOffset, YES, YES, YES, YES );
//bottom inner shadow
CGContextSetFillColor( context, [CPColor redColor] );
CGContextSetShadowWithColor( context, CGSizeMake( 0, 0 - shadowOffset), shadowBlur, shadowColor );
frame = CGRectMake( 0, aRect.size.height + 2, aRect.size.width, 30 );
CGContextFillRoundedRectangleInRect( context, frame, shadowOffset, YES, YES, YES, YES );
//left inner shadow
CGContextSetFillColor( context, [CPColor redColor] );
CGContextSetShadowWithColor( context, CGSizeMake( shadowOffset, 0), shadowBlur, shadowColor );
frame = CGRectMake( -30, 0, 30, aRect.size.height );
CGContextFillRoundedRectangleInRect( context, frame, shadowOffset, YES, YES, YES, YES );
//right inner shadow
CGContextSetFillColor( context, [CPColor redColor] );
CGContextSetShadowWithColor( context, CGSizeMake( 0 - shadowOffset, 0), shadowBlur, shadowColor );
frame = CGRectMake( aRect.size.width, 0, 30, aRect.size.height );
CGContextFillRoundedRectangleInRect( context, frame, shadowOffset, YES, YES, YES, YES );
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment