Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Hit detection using CGContextPathContainsPoint
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Hit detection using CGContextPathContainsPoint



Hi everybody,

On page 94, Apple's latest "Cocoa Drawing Guide" gives an example on how to perform hit a mouse hit detection on a stroked path using CGContextPathContainsPoint.

- (BOOL)pathContainsPoint:(NSPoint)point forMode:(CGPathDrawingMode)mode
{
CGPathRef path = [self quartzPath]; // Custom method to create a CGPath
CGContextRef cgContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGPoint cgPoint;
BOOL containsPoint = NO;


    cgPoint.x = point.x;
    cgPoint.y = point.y;

    // Save the graphics state before doing the hit detection.
    CGContextSaveGState(cgContext);

CGContextAddPath(cgContext, path);
containsPoint = CGContextPathContainsPoint(cgContext, cgPoint, mode);


    CGContextRestoreGState(cgContext);

    return containsPoint;
}

What I currently don't understand is how to provide a valid CGContext during the mouseDown: event phase in which the hit detection has to be performed. The [[NSGraphicsContext currentContext] graphicsPort] which is used in the example is only valid during the drawing phase.

Has anybody an idea how this could work? Is it sensible to create a temporary PDF/Bitmap context just for detecting hits?

Cheers

Frank

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.