Game Development Community

GuiCanvas::getCursorPos() broken (Fix Inside!)

by Stefan Lundmark · in Torque 3D Professional · 11/20/2012 (10:00 am) · 5 replies

Hi,

getCursorPos() in GuiCanvas is totally useless given that it doesn't return the position for the cursor on the canvas, but the global position of the cursor on the screen. This position can even be outside the T3D window!

This also makes GuiCanvas::cursorInControl() useless too, as it relies on the above-mentioned method.

This isn't very shocking as these methods aren't used everywhere, but I'm a bit surprised it has been around for so long. I don't have a fix yet but will return soon with one.

#1
11/20/2012 (10:05 am)
Alright..

GuiCanvas::getCursorPos() usually looks like this:
Point2I GuiCanvas::getCursorPos()
{
   Point2I p( 0, 0 );
   
   if( mPlatformWindow )
      mPlatformWindow->getCursorPosition( p );
      
   return p;
}

The fixed version looks like this:
Point2I GuiCanvas::getCursorPos()
{
   Point2I p( 0, 0 );
   
   if( mPlatformWindow )
   {
      mPlatformWindow->getCursorPosition( p );
      p = mPlatformWindow->screenToClient( p );
   }
      
   return p;
}
#2
11/20/2012 (11:32 am)
Quote:
This position can even be outside the T3D window!

o_O
Yeah, that sounds about right! ;)

Nice fix. I suppose what would be really useful would be the getting the cursor position relative the window which is active or the cursor is actually over.
#3
11/20/2012 (12:18 pm)
That's easy, just use the globalToLocal() / localToGlobal() helper functions. :)
#4
01/03/2013 (10:41 am)
It can also be done with a script like this :
function getCursorPos()
{
    return canvas.screenToClient(canvas.getCursorPos());
}
#5
11/02/2014 (1:58 pm)
As discussed in #180, I decided not to 'fix' getCursorPos, but to update its documentation. I did, however, fix cursorInControl, which used this method incorrectly.