Mouse coordinates with Cocoa

Member
Posts: 29
Joined: 2006.07
Post: #1
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?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #2
Code:
- (void)mouseDown:(NSEvent*)bob
{
    NSPoint where = [self convertPoint:[bob locationInWindow] fromView:nil];
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
The [event location] is already the location within your view...
Quote this message in a reply
Member
Posts: 29
Joined: 2006.07
Post: #4
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
They are floats; are you somehow treating the memory as an integer?
Quote this message in a reply
Member
Posts: 29
Joined: 2006.07
Post: #6
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?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
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.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #8
Do something like this to get integer values from the floating point coordinates given to you:
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...
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #9
Those casts shoulnt be there Jake.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
ByteSizeCookie
Unregistered
 
Post: #10
"shouldn't" is a bit strong... "needn't" is certainly true, though.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #11
Man, BSC is picking up stuff fast!LOL

I've always been under the impression that it is good code form. Is there some disadvantage?
Quote this message in a reply
Member
Posts: 29
Joined: 2006.07
Post: #12
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 Smile

EDIT: Yup! Works perfectly, thanks everyone.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #13
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...
Quote this message in a reply
Member
Posts: 29
Joined: 2006.07
Post: #14
Alright, I'll try them with floats. It won't take me long at all to rewrite it.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Local (X, Y) Coordinates from 3D plane coordinates merrill541 5 7,172 Jun 29, 2009 01:32 AM
Last Post: RhinosoRoss
  changing mouse icon in cocoa Leroy 13 6,902 Sep 4, 2007 07:10 PM
Last Post: SethWillits
  Translate mouse coordinates to world coordinates nalenb 4 6,753 Mar 21, 2006 11:59 PM
Last Post: glMatt
  Cocoa: Mouse location while dragging window Fenris 4 5,125 Jul 14, 2005 04:46 PM
Last Post: Fenris
  Converting mouse coordinates in fullscreen context? arekkusu 4 3,344 Feb 26, 2003 02:40 AM
Last Post: arekkusu