Skip to content

Commit

Permalink
block-based shadow drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
mpw committed Jan 20, 2013
1 parent 6f0446e commit bdd2ad1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
27 changes: 26 additions & 1 deletion DrawingContext/MPWAbstractCGContext.m
Expand Up @@ -267,6 +267,30 @@ -(void)drawTextLine:(CTLineRef)l {}

#if NS_BLOCKS_AVAILABLE

-(id)setShadowOffset:(NSSize)offset blur:(float)blur color:aColor
{
return self;
}

-(id)clearShadow
{
return self;
}

-withShadowOffset:(NSSize)offset blur:(float)blur color:aColor draw:(DrawingBlock)commands
{
@try {
[self setShadowOffset:offset blur:blur color:aColor];
[commands drawOnContext:self];
}
@finally {
[self clearShadow];
}
return self;
}



-(id)drawLater:(DrawingBlock)commands
{
return [[[MPWDrawingCommands alloc] initWithBlock:commands] autorelease];
Expand Down Expand Up @@ -324,8 +348,8 @@ -(void)setAlpha:(float)newAlpha {}
#endif

@end

#if 0

#import <MPWFoundation/MPWFoundation.h>

@implementation MPWAbstractCGContext(testing)
Expand Down Expand Up @@ -358,3 +382,4 @@ +(void)testGetFloats

@end
#endif

1 change: 1 addition & 0 deletions DrawingContext/MPWDrawingContext.h
Expand Up @@ -94,6 +94,7 @@
#if NS_BLOCKS_AVAILABLE
typedef void (^DrawingBlock)(id <MPWDrawingContext>);

-withShadowOffset:(NSSize)offset blur:(float)blur color:aColor draw:(DrawingBlock)commands;
-ingsave:(DrawingBlock)drawingCommands;
-(id)drawLater:(DrawingBlock)drawingCommands;
-layerWithSize:(NSSize)size content:(DrawingBlock)drawingCommands;
Expand Down
2 changes: 1 addition & 1 deletion DrawingContext/MPWView.m
Expand Up @@ -13,7 +13,7 @@
#else
#import "MPWView.h"
#endif
#import <EGOS_Cocoa/MPWCGDrawingContext.h>
#import "MPWCGDrawingContext.h"

@implementation MPWView

Expand Down
Binary file not shown.
23 changes: 11 additions & 12 deletions Shared/IconView.m
Expand Up @@ -249,10 +249,9 @@ -(void)drawRect:(NSRect)rect onContext:(id <MPWDrawingContext>)context
[[context translate:0.5 * (boundsSize.width - scale * nativeSize.width) :0.5 * (boundsSize.height - scale * nativeSize.height)] scale:@(scale)];

NSRect ellipseRect = NSMakeRect(32, 38, 448, 448);
[context setShadowOffset:NSMakeSize(0, -8 * scale) blur:12 * scale color:[context colorGray:0 alpha: 0.75]];
[context setFillColorGray:0.9 alpha:1.0];
[[context ellipseInRect:ellipseRect] fill];
[context clearShadow];
[context withShadowOffset:NSMakeSize(0, -8 * scale) blur:12 * scale color:[context colorGray:0 alpha: 0.75] draw:^(id <MPWDrawingContext> c ){
[[[c setFillColorGray:0.9 alpha:1.0] ellipseInRect:ellipseRect] fill];
}];
[[context ellipseInRect:ellipseRect] clip];

[context drawLinearGradientFrom:ellipseRect.origin
Expand Down Expand Up @@ -292,15 +291,15 @@ -(void)drawRect:(NSRect)rect onContext:(id <MPWDrawingContext>)context
colors:colors
offsets:@[ @0.0, @0.85 ]];

[context setShadowOffset:NSMakeSize(0, 0) blur:12 * scale color:[context colorGray:0 alpha: 1.0]];
[context ingsave:^(id <MPWDrawingContext> c ){
[c translate:@[ @130 ,@140]];
[c setFont:[context fontWithName:@"ArialMT" size:345]];
[c setFillColorGray:0.9 alpha:1.0];
[c setTextPosition:NSMakePoint(0, 0)];
[c show:@"\u2766"];
[context withShadowOffset:NSMakeSize(0, 0) blur:12 * scale color:[context colorGray:0 alpha: 1.0] draw:^(id <MPWDrawingContext> c ){
[context ingsave:^(id <MPWDrawingContext> c ){
[c translate:@[ @130 ,@140]];
[c setFont:[context fontWithName:@"ArialMT" size:345]];
[c setFillColorGray:0.9 alpha:1.0];
[c setTextPosition:NSMakePoint(0, 0)];
[c show:@"\u2766"];
}];
}];
[context clearShadow];

const CGFloat glossInset = 8;
CGFloat glossRadius = (ellipseCenterRect.size.width * 0.5) - glossInset;
Expand Down

0 comments on commit bdd2ad1

Please sign in to comment.