Re: Hit detection on shapes within window
Re: Hit detection on shapes within window
- Subject: Re: Hit detection on shapes within window
- From: John Scalo <email@hidden>
- Date: Wed, 13 Nov 2002 23:36:12 -0800
Since the archives are convoluted on this subject (yes I had searched them
before sending), here is what ended up working for me-
* Create two images representing your "at rest" and "pressed" states
* The "at rest" state must have an alpha mask (use photoshop or graphic
converter, save "maintaining transparency") which is used for hit testing
* Create a subclass of NSImageView overriding these methods
- (void)mouseDown:(NSEvent *)event
{
NSBeep();//do your mouse down work
[self setImage:[[NSImage imageNamed:@"imagePressed"] retain]];
}
- (void)mouseUp:(NSEvent *)theEvent
{
NSBeep();//do your mouse up work
[self setImage:[[NSImage imageNamed:@"imageAtRest"] retain]];
}
- (NSView *) hitTest: (NSPoint)aPoint
{
NSView *superHitTest = [super hitTest: aPoint];
if (superHitTest == self)
{
NSColor *colorAtPoint;
[[self image] lockFocus];
colorAtPoint = NSReadPixel([self convertPoint: aPoint fromView:
nil]);
[[self image] unlockFocus];
return [colorAtPoint alphaComponent] > 0.1 ? self : nil;
}
else
return superHitTest;
}
* Use IB to place the at rest image in an NSImageView and change its custom
class to your shiny new NSImageView subclass
That's a pretty clean solution, at least cleaner than most of the other
ideas pointed to in the archives. If you're not savvy with alpha masks then
you can test against the background color like someone else suggested.
Thanks
John
On 11/12/02 4:33 PM, "John Scalo" <email@hidden> wrote:
>
I have a window with several closely placed images with odd shapes (i.e. not
>
rectangular or oval). I'd like to detect mouse clicks on these shapes and
>
momentarily change their image to a "pressed" state (using another image)
>
until the mouse is released.
>
>
My first inclination is to map the NSImages to NSBezierPaths and use
>
containsPoint: sending it the mouse down coordinates when the mouse is
>
clicked. However for the life of me I can't see how to get an NSBezierPath
>
from an NSImage. Is this possible? Perhaps in AppKit World all NSImages are
>
rectangular regardless of their content and alpha mask.
>
>
Another thought was to just implement these images as buttons with no border
>
and "momentary change" behavior (using the "pressed" image as the alt
>
image), but that really doesn't work because the rectangular representation
>
of the button is still used for hit detection rather than alpha mask part of
>
the image. Can NSButtonCell be subclassed to alter this behavior?
>
>
Any other approaches to consider here?
>
>
Thanks!
>
John
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.