Mouse position not looping

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
m0rr0w
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 16, 2017 4:25 pm

Mouse position not looping

Post by m0rr0w »

Hey I'm trying to mimic the behavior i see in game engines where the mouse loops back around when it hits the edge of the screen. For example, once it hits the right of the screen, it resets the position and comes back around from the left. I have this code:

Code: Select all

void MyGLCanvas::resetMouse()
{
	int screenMaxX = wxSystemSettings::GetMetric(wxSYS_SCREEN_X);
	int screenMaxY = wxSystemSettings::GetMetric(wxSYS_SCREEN_Y);

	wxPoint pt = wxGetMousePosition();
	int x = pt.x;
	int y = pt.y;

	if (x >= screenMaxX)
		parentFrame->WarpPointer(0 - x, y);

	if(x <= 0)									 //doesn't work, left of screen
		parentFrame->WarpPointer(screenMaxX, y);
	

	if (y >= screenMaxY)						//doesn't work, bottom of screen
		parentFrame->WarpPointer(x, 0 - y);  

	if (y <= 0)
		parentFrame->WarpPointer(x, screenMaxY);
}
it works for the right and top of the screen but not for the bottom and the left. Am I doing something wrong or is there some info im missing about the screen layout or something? HELP! [-o<


-edit-

Ok i got it to work for all sides except the left. I changed the bottom of the screen to

Code: Select all

	if (y >= (screenMaxY - 1))						
		parentFrame->WarpPointer(x, 0 - y); 
and now it works. How come the left isn't working?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Mouse position not looping

Post by doublemax »

Code: Select all

if(x <= 0)                            //doesn't work, left of screen
      parentFrame->WarpPointer(screenMaxX, y);
Try "screenMaxX-1" here. I'm surprised this works at all, as you're setting negative coordinates in some cases:

Code: Select all

if (x >= screenMaxX)
      parentFrame->WarpPointer(0 - x, y);
Use the source, Luke!
Post Reply