user256411
user256411

Reputation: 51

Translating clicked NSPoint in NSImageView to correct pixel coordinates in the underlying NSBitmapImageRep

Here is what I'm trying to accomplish: I'm working on an open source TI calculator emulator where I'm currently trying to add skin support. The skin image is just an NSImageView with its image set to the skin image. I override the mouseDown: method and get the location of the mouse in the NSImageView coordinates using convertPointFromBase: with locationInWindow return value as the point to convert.

I then grab the underlying NSBitmapImageRep from the NSImageView using bestRepresentationForDevice: with a nil argument. Finally I call getPixel:atX:y on the NSBitmapImageRep with point.x and point.y for the location.

However, the origin of the NSImageView begins in the bottom left (as usual), whereas the coordinates for the NSBitmapImageRep begin in the top left. I can't seem to find an obvious way to compensate for this, so the hit testing for button clicks doesn't function properly. Any help is greatly appreciated.

Upvotes: 4

Views: 1077

Answers (2)

user256411
user256411

Reputation: 51

I never considered overriding isFlipped to return YES in my NSImageView subclass. That takes care of the problem.

Upvotes: 1

Rob Keniger
Rob Keniger

Reputation: 46020

Seems pretty easy to me, just calculate the y coordinate based on the height of the rect:

NSPoint pointInFlippedRect(NSPoint inPoint, NSRect aRect)
{
    return NSMakePoint(inPoint.x , NSHeight(aRect) - inPoint.y);
}

Upvotes: 0

Related Questions