Mouse coordinates with Cocoa
I've been trying to figure this one out for a long time now..
But I'm making a level editor with my own custom UI drawn inside an NSView. I want to be able to get the mouse coordinates when the button is clicked so I can figure out where the click took place.
I use myPoint = [self convertPoint:[event location] fromView:nil]; in the mouseUp: routine, but it still only returns huge garbled numbers like 872394820, not the standard pixel-by-pixel coordinates of my view. I haven't changed the coordinate system of the view either. What am I doing wrong?
But I'm making a level editor with my own custom UI drawn inside an NSView. I want to be able to get the mouse coordinates when the button is clicked so I can figure out where the click took place.
I use myPoint = [self convertPoint:[event location] fromView:nil]; in the mouseUp: routine, but it still only returns huge garbled numbers like 872394820, not the standard pixel-by-pixel coordinates of my view. I haven't changed the coordinate system of the view either. What am I doing wrong?
Code:
- (void)mouseDown:(NSEvent*)bob
{
NSPoint where = [self convertPoint:[bob locationInWindow] fromView:nil];
The [event location] is already the location within your view...
That's exactly what I've been doing...and I've tried both [event location] and the convertPoint: method, but they return massive numers, like 8392480 instead normal coordinates in pixels.
They are floats; are you somehow treating the memory as an integer?
Basically, I was trying to convert them with [mouseCoord.x intValue] and [mouseCoord.y intValue] to find the coordinate of the mouse click in my view, so that I can find which object on the screen was clicked. Would that not convert the float value to an integer?
That is illegal; it's amazing you're not crashing. You should pay attention when the compiler tells you you're doing something silly!
(int)mouseCoord.x is what you want... if you really want integers at all, which seems slightly unlikely.
(int)mouseCoord.x is what you want... if you really want integers at all, which seems slightly unlikely.
Do something like this to get integer values from the floating point coordinates given to you:
[edit] Ackk! OSC beat me to the punchline again, hehe...
Code:
int mouseX, mouseY;
NSPoint mouseLoc = [theEvent locationInWindow];
mouseX = (int)mouseLoc.x;
mouseY = (int)mouseLoc.y;[edit] Ackk! OSC beat me to the punchline again, hehe...
Those casts shoulnt be there Jake.
Sir, e^iπ + 1 = 0, hence God exists; reply!
Man, BSC is picking up stuff fast!
I've always been under the impression that it is good code form. Is there some disadvantage?
I've always been under the impression that it is good code form. Is there some disadvantage?
Thanks, that looks like it'll work. I need to look for some more cocoa books that cover these things.
and @OSC, I don't need floating point values for the coords because I'm handling the mouse location at the pixel level, right? I don't need to worry about the cursor being at 1.5 pixels, just 1 or 2. Though I'm definitely still a beginner, and you know infinitely more than I do
EDIT: Yup! Works perfectly, thanks everyone.
and @OSC, I don't need floating point values for the coords because I'm handling the mouse location at the pixel level, right? I don't need to worry about the cursor being at 1.5 pixels, just 1 or 2. Though I'm definitely still a beginner, and you know infinitely more than I do

EDIT: Yup! Works perfectly, thanks everyone.
The graphics API does its calculations on a floating point basis behind the scenes, so there's usually no point in feeding it anything but floats to begin with. What you see on screen is the final rasterized image, which as you rightly assume, is represented on a per-pixel basis. But that's just the end of the line. All the positional coordinates going through all the stages up to that point behind the scenes are all floating point (except for certain operations in the graphics pipeline). Converting mouse coordinates to integers, only for the API to have to convert them back to floats for its own purposes is just wasteful. Not a big waste, but still...
Alright, I'll try them with floats. It won't take me long at all to rewrite it.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Local (X, Y) Coordinates from 3D plane coordinates | merrill541 | 5 | 5,532 |
Jun 29, 2009 01:32 AM Last Post: RhinosoRoss |
|
| changing mouse icon in cocoa | Leroy | 13 | 5,712 |
Sep 4, 2007 07:10 PM Last Post: SethWillits |
|
| Translate mouse coordinates to world coordinates | nalenb | 4 | 4,783 |
Mar 21, 2006 11:59 PM Last Post: glMatt |
|
| Cocoa: Mouse location while dragging window | Fenris | 4 | 4,484 |
Jul 14, 2005 04:46 PM Last Post: Fenris |
|
| Converting mouse coordinates in fullscreen context? | arekkusu | 4 | 2,977 |
Feb 26, 2003 02:40 AM Last Post: arekkusu |
|

