Game Development Community

getCursorPos in Window

by Howard Dortch · in Torque 3D Professional · 03/19/2010 (9:11 am) · 14 replies

When I call getCursorPos() and my game is in Windowed mode it return the position in screen space. So the upper left corner of my window is 0,0 but the position is reported from the upper left of my display. Any way around this?


#1
03/19/2010 (10:27 am)
It worked the way you want back in Beta 4. It was changed after that to make getCursorPos() and setCursorPos() work in a consistent manner. See the following thread for more info...

www.torquepowered.com/community/forums/viewthread/98455

So if you have Beta 4 laying around, you could just swipe the code from there.
#2
03/19/2010 (1:30 pm)

The cursor position being reported in screen-space is by design since this is the only correct way to handle the mouse.

However, looking at this again, I just discovered that apparently we don't have a way to actually retrieve information about the window position on GuiCanvas or a method to perform screen<->client space conversions. That's definitely a hole in the API.

I'll add methods that do these kind of things for b2.
#3
05/06/2010 (11:09 pm)
Anyone have the beta 4 code for this ? As i need the ability to get/set the mouse position in window space...
#4
02/17/2012 (7:52 am)
Was this ever fixed in the 1.01 version and if so, how do I get the mouse pos relative to a window?
#5
02/18/2012 (8:07 pm)
You can try this:
%cursorpos = Canvas.getCursorPos();
   if( !Canvas.isFullscreen() )
   {
      %windowPosition = Canvas.getWindowPosition();
      echo( "[CLIENT]> %windowPosition: " @ %windowPosition );
      %xPos = getWord(%cursorpos, 0) - getWord( %windowPosition, 0 );
      %yPos = getWord(%cursorpos, 1) - getWord( %windowPosition, 1 );
   }
   else
   {
      %xPos = getWord(%cursorpos, 0);
      %yPos = getWord(%cursorpos, 1);
   }
   
   echo( "[CLIENT]> cursor position: " @ %cursorpos @ " : " @ %xPos @ " : " @ %yPos );
#6
02/21/2012 (10:26 am)
Canvas.getWindowPosition() = unknown command

Thanks for the reply, I found a way to cheat it.
#7
02/21/2012 (10:40 am)
Its not a unknown command, its from the script manual.

Point2I GuiCanvas::getWindowPosition ( )

Get the current position of the platform window associated with the canvas.

Returns:
The window position of the canvas in screen-space.
#8
02/22/2012 (6:20 am)
Sorry, my version 1.01 reports unknown, from the log file

echo(canvas.getWindowPosition());
<input> (0): Unknown command getWindowPosition.
Object Canvas(1322) Canvas -> GuiCanvas -> GuiCanvas -> GuiControl -> SimGroup ->

canvas.dump() doesn't show it either.

I will dig into the engine code and have a look, this version may not expose it to script.
Thanks again...
#9
04/22/2012 (6:52 pm)
i tested to Torque3D 1.2.

getCursorPos() function has problem as ever.

so i use this function use with screenToClient function.

like this.
%GetCurPos = Canvas.screenToClient( Canvas.getCursorPos() );
#10
04/27/2012 (9:15 am)
Thanks Mquaker! Was looking for this in my "spare time" and you caught it first.

For those who are interested, it's defined in guiCanvas.cpp around line 2452 in 1.2.

And I was thinking of hacking together a method to grab the HWND handle from the D3D device....
#11
04/27/2012 (12:18 pm)
Indeed, thanks. Now I can remove my hack..
#12
06/30/2012 (4:36 am)
This works with 'lockMouse = false', elsewhere when 'lockMouse = true' (what we all want because this parameter locks cursor in a game window and changes a cursor's image if we prepared) I get the same cursors coordinates every time. Does anybody know why?
#13
06/30/2012 (6:53 am)
This is taking place because when 'lockMouse = true', Torque locks the cursor in the center of the screen
Does anybody know how to fix this?
#14
07/01/2012 (7:25 am)
I ran into this problem while implementing some object selection code last year. Pretty sure I submitted a bug request but perhaps not. At any rate I didn't see it there any longer. Here is the code used to get past that issue. I guess I better check to see how my v1.2 code is behaving now!

// Fixed to show correct position in window relative to top left corner
// Previously was reporting position from top left of display window
//
Point2I GuiCanvas::getCursorPos()
{
   Point2I p( 0, 0 );
   POINT topleft;  
   topleft.x = 0;
   topleft.y = 0;
  
   Win32Window* w = dynamic_cast&lt;Win32Window*&gt;(mPlatformWindow);  
   
   if(w != NULL)  
      ClientToScreen(w-&gt;getHWND(), &amp;topleft); 

  if( mPlatformWindow )
      mPlatformWindow-&gt;getCursorPosition( p );

   return Point2I( p.x - topleft.x, p.y - topleft.y );
}

@genomegames: locking the mouse decouples the mouse from the movemap. There are several good resources discussing how to address this. Here is one by Dave Myers you could look at: [url]www.garagegames.com/community/blogs/view/2173[url]