tags:

views:

186

answers:

1

I'm writing a game for Mac using Cocoa. I'm currently implementing hit testing and have founds that CALayer offers hit testing, but does not seem to implement the alpha properties. As I have at times many CALayers stacked on top of each other, I really need to find a way to determine what the user actually meant to click on.

I'm thinking if I could somehow get an array that contains pointers to all of the CALayers that contain the click point, I could filter through them some how. However the only way I've got so far to create the array is:

NSMutableArray* anArrayOfLayers = [NSMutableArray array];
    for (CALayer* aLayer in mapLayer.sublayers)
    {
        if ([aLayer containsPoint:mouseCoord])
            [anArrayOfLayers addObject:aLayer];
    }

Then sort the array by the CALayer's z-values then go through checking if the pixel at location is alpha or not. However, between the sort and the alpha check this seems to be an incredible performance hog. (How would you even check the alpha?)

Is there any way to do this?

A: 

Something I stumbled across while scratching my head over a similar problem is that CALayer uses containsPoint: when you send it hitTest:

Its default behaviour is to test against bounds, but we can override and get it to check the alpha channel, and just use CALayer's built in hit-testing to handle the rest:

- (BOOL) containsPoint:(CGPoint)p
{
  if (CGRectContainsPoint(self.bounds, p))
  {
 if (!ImagePointIsTransparent(self.contents, p)) return YES;
  }
  return NO;
}

There's a discussion of testing for a single pixel's alpha at http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage

This worked for my purposes:

BOOL ImagePointIsTransparent(CGImageRef image, CGPoint p)
{
 unsigned char pixel[1] = {0};

 CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 1, NULL,
                                                 kCGImageAlphaOnly);
 CGContextDrawImage(context, CGRectMake(-p.x,
                                           -p.y,
                                           CGImageGetWidth(image),
                                           CGImageGetHeight(image)),
                       image);

 CGContextRelease(context);

 CGFloat alpha = pixel[0]/255.0;

 return (alpha < 0.01);
}

(If you're using renderInContext: to draw to the CALayer rather than setting its contents property, then it's going to be more complicated. This might be useful in that case: http://www.cimgf.com/2009/02/03/record-your-core-animation-animation/)

Chris Devereux