Re: Getting the mouse location in a CAlayer's coordinates
Re: Getting the mouse location in a CAlayer's coordinates
- Subject: Re: Getting the mouse location in a CAlayer's coordinates
- From: Nathan Vander Wilt <email@hidden>
- Date: Tue, 8 Apr 2008 10:20:55 -0700
On Apr 8, 2008, at 9:15 AM, douglas a. welton wrote:
Hi All,
Would someone provide me a pointer for how to get the mouse location
in the coordinate system of a transformed layer that is underneath
the mouse. I thought I could do something like this:
Mouse_Location = [Target_Layer convertPoint:
NSPointToCGPoint( [NSEvent mouseLocation] ) fromLayer: nil];
using "nil" for the fromLayer because Target_Layer is the view's
"topmost" layer and contains all other sublayers. Unfortunately, the
results I get back are incorrect (no conversion appears to be done)
and when the Target_Layer has a transform (scaling) applied, the
results are very wrong.
I still don't fully understand how all the coordinate systems interact
(see thread:http://www.cocoabuilder.com/archive/message/cocoa/2008/3/12/201189)
but I think the following steps will get you what you need.
1. Convert the NSEvent's -locationInWindow to a a view coordinate, as
describe in the documentation for that accessor:
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location fromView:nil];
2. Then convert the view coordinate to the root CALayer coordinate
(since this may not be the same with resolution independence) using -
[NSView convertPointToBase:] as follows:
NSPoint root_point = [self convertPointToBase:local_point];
3. Now you can convert it to the layer you want:
CGPoint layer_point = [rootLayer convertPoint:
NSPointToCGPoint(root_point) toLayer:desiredLayer];
Note that none of the CALayer point conversions say they accept nil
(as -[NSView convertPoint:fromView:] allows). In fact they are
explicit in saying that "The receiver and layer and must share a
common parent layer.", though I'm pretty sure this still allows for
the root layer to convert into its children's systems.
hope this helps,
-natevw
_______________________________________________
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