Re: Mouse position from NSEvent starts at (0, 1) ?
Re: Mouse position from NSEvent starts at (0, 1) ?
- Subject: Re: Mouse position from NSEvent starts at (0, 1) ?
- From: Manfred Schwind <email@hidden>
- Date: Sat, 19 May 2007 00:28:56 +0200
Humm...from what I see it seems to still be offset by
one pixel, but the cross is 2 pixels wide, so its harder to
see.
In my opinion you have found a bug in NSView's convertPoint:fromView:.
I changed your code to the following:
@implementation RRView
- (void)resetCursorRects
{
[self addCursorRect:[self bounds] cursor:[NSCursor crosshairCursor]];
}
- (void)drawRect:(NSRect)rect
{
[[NSColor darkGrayColor] set];
NSRectFill(rect);
NSLog(@"x = %f, y = %f", x, y);
[[NSColor yellowColor] set];
[NSBezierPath fillRect:NSMakeRect(x, y-100, 1, 200)];
[NSBezierPath fillRect:NSMakeRect(x-100, y, 200, 1)];
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint p = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
x = roundf(p.x);
y = roundf(p.y);
[self setNeedsDisplay:YES];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint p = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
x = roundf(p.x);
y = roundf(p.y);
[self setNeedsDisplay:YES];
}
@end
A few things that I've changed in your original code:
- With resetCursorRects the cursor changes to the cross hair cursor
only when the mouse is really inside the PRView. When moving the
mouse slowly at the bottom into the view, I can prove that the cross
hairs "hot spot" is correct.
- I draw different colors to better see the results.
- I round the mouse coordinates. Normally you get integers, but I
found out that when using the Zooming option of Mac OS X (and
probably with upcoming resolution independency) this is no longer true.
- I print the coordinates to the console.
So if you move the mouse from the bottom into the view to y
coordinate 0.0, you can see that the cross hair cursors hot spot is
correct. Its at the "most bottom" line of the gray view. Now when you
click, the drawn yellow cross is one pixel too high. And the printed
coordinate in the console is 1.0. So it seems [self convertPoint:
[theEvent locationInWindow] fromView:nil] really returns wrong y
coordinates! The mouse is at the lowest possible coordinate, but you
never can get 0.0 when just clicking (only when dragging) the mouse.
I think you should file a bug report to Apple.
Regards,
Mani
--
http://www.mani.de
iVolume - Loudness adjustment for iTunes.
LittleSecrets - The encrypted notepad.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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