Converting mouse coordinates in fullscreen context?

Sage
Posts: 1,232
Joined: 2002.10
Post: #1
(This is a Cocoa/CG question... I posted this to mac-opengl last week, no replies. So cross-posting here now in hopes of more eyes looking at it...)


I need to get the mouse coordinates in a fullscreen game context. Unrelated factors (minification, etc) have steered my event handling approach to:
1) controller is subclass of NSResponder, and implements mouseDown: etc for normal windowed mode.
2) window is created programatically and has the controller as nextResponder during windowed mode (for keystroke handling.)
3) window's first responder is swapped to the controller when in fullscreen mode, so all events (escape key, mouse) are caught. The event handler just passes relevant events to the normal mouseDown: etc handlers.

Here's my problem: in windowed mode I am getting sensible mouse coordinates via:

- (void) mouseDown:(NSEvent*)event{
NSPoint loc = [viewport convertPoint:[event locationInWindow] fromView:nil];
NSLog(@"mouse down (%@): (%f, %f)", event, loc.x, loc.y);
}

e.g. from (0,0) to (639,399) in a 640x400 window (although the resize widget obscures one corner, as expected.)

But, in a fullscreen context, 896x600 for example, the same function reports mouse coordinates from (0, -454) to (895, 145).
The vertical coordinates change depending on how big the (windowed mode) window is, but they always include some negative values.

Looking at the event, winNum is changing each time I enter fullscreen mode; I guess it's a temporary shielding window CG creates? How can I get the fullscreen window's coordinate system to make sense?

My view returns YES for isFlipped, if that makes any difference. The coordinates are totally screwed up when my fullscreen context is on a second display; they look more like global coordinates, as if convertPoint: hasn't done anything.

The convertPoint docs say:
"Converts aPoint from the coordinate system of aView to that of the receiver. If aView is nil, this method instead converts from window base coordinates. Both aView and the receiver must belong to the same NSWindow."

OK, so maybe my view is no longer in the right window after CG has taken over the screen with it's temporary shielding window, and that's my problem. If so, how do I fix it?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Why not use CoreGraphics to either get the mouse position directly, or get the deltas. Won't deltas be more use to you anyway?
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #3
Quote:Originally posted by OneSadCookie
Why not use CoreGraphics to either get the mouse position directly, or get the deltas.


Because I'd like to reuse all my code which is set up for normal window coordinates, and there *ought* to be a way to get the windowing system to treat the fullscreen GL context's dimensions as window coordinates.

Quote:Won't deltas be more use to you anyway?


No. I already have all my code for windowed mode expecting mousedown/up/drag events in window coordinates, not deltas.


So, can you suggest a way to translate the CG shielding window coordinates? I don't see any API to translate the CG window ID back into an NSWindow... or how to reassign the GL context to a different window once it's been set to fullscreen. Sad
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #4
this problem was discussed here:thread 1461

my solution was this:
Code:
NSPoint mouseLoc = [NSEvent mouseLocation];
    CGRect screenBounds = CGDisplayBounds(kCGDirectMainDisplay);
    mouseLoc.y += screenBounds.size.height;
    mouseLoc.y -= ([[NSScreen mainScreen] frame].size.height + 1);

hope that helps

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #5
Quote:Originally posted by kelvin
this problem was discussed here:thread 1461


Ah, that is very good information, thanks.

Not what I wanted, but at least it is some confirmation (from OSC/wally) of a bug between CGDirectDisplay and NSScreen.

I ended up doing the conversion myself, similarly to how you show, except I have a flipped coordinate system and work with multiple displays.

-alex
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 6,900 Jun 29, 2009 01:32 AM
Last Post: RhinosoRoss
  Converting Local to World Coordinates anthony 0 3,175 Jun 25, 2007 03:50 AM
Last Post: anthony
  Mouse coordinates with Cocoa vnvrymdreglage 13 6,601 Apr 8, 2007 07:26 AM
Last Post: vnvrymdreglage
  Translate mouse coordinates to world coordinates nalenb 4 6,397 Mar 21, 2006 11:59 PM
Last Post: glMatt
  Converting (x, y) into NESW direction unknown 19 10,380 Nov 2, 2005 09:52 AM
Last Post: Zekaric