Re: Erasing drawn content
Re: Erasing drawn content
- Subject: Re: Erasing drawn content
- From: Quincey Morris <email@hidden>
- Date: Wed, 9 Jun 2010 14:03:46 -0700
On Jun 9, 2010, at 13:00, Matej Bukovinski wrote:
> // Overlay View
>
> - (void)drawRect:(NSRect)drawRect {
> [NSGraphicsContext saveGraphicsState];
> // Draw all anotations, make sure _annotations is sorted front-to-back
> for (Annotation *a in _annotations) {
> [a draw:drawRect];
> }
> [NSGraphicsContext restoreGraphicsState];
> }
>
> // Annotation
>
> - (void)draw:(NSRect)drawRect {
>
> NSRect nodeRect = [_node boundingBox];
>
> if (!NSIntersectsRect(nodeRect, drawRect)) {
> return; // nothing to draw
> }
>
> // Fill the background
> [[self annotationFillColor] set];
> [NSBezierPath fillRect:nodeRect];
>
> // Stroke the border
> [[self annotationStrokeColor] set];
> [NSBezierPath setDefaultLineWidth:2.0];
> [NSBezierPath strokeRect:nodeRect];
>
> // Draw the title
> [[self labelAttributedString] drawAtPoint:[self annotationLabelPositionFor:nodeRect]];
>
> // Clip
> NSBezierPath* clipPath = [NSBezierPath bezierPathWithRect:drawRect];
> [clipPath appendBezierPathWithRect:nodeRect];
> [clipPath setWindingRule:NSEvenOddWindingRule];
> [clipPath addClip];
> }
I see a couple of minor issues with this:
1. I don't think it's necessary or desirable to save/restore the graphics state around the drawRect: behavior, because I believe that's done for you around the drawRect: invocation itself. You'd only need it if you had other non-clipped drawing to do after drawing your annotations.
2. Instead of this:
[clipPath appendBezierPathWithRect:nodeRect];
I actually meant this:
[clipPath appendBezierPathWithRect: NSIntersectionRect (drawRect, nodeRect)];
Otherwise the compound shape isn't what you want it to be. However, the two versions are probably equivalent in this context, because everything's clipped to drawRect anyway.
3. Since your 2.0-point strokes are drawn centered on the nodeRect path, only the interior half of the strokes get protected by the clipping changes. The effect may or may not be what you want, and may or may not be visible, depending on the alpha values of the various colors involved.
One solution is to draw the strokes *inside* the nodeRect (by creating an inset bezier path specifically for stroking, for example). Or, expand your clipping protection to include the stroke:
[clipPath appendBezierPathWithRect: NSIntersectionRect (drawRect, NSInsetRect (nodeRect, -1.0, -1.0))]; // 1.0 == strokeWidth / 2
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden