Restrict mouse pointer to area of GLCanvas

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
mikegl
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Jul 12, 2019 12:31 am

Restrict mouse pointer to area of GLCanvas

Post by mikegl » Mon Aug 05, 2019 1:11 pm

Hello,

I want to do the following:
1. A user presses CTRL+F10 and the mouse cursor moved to the center of GLCanvas
2. The cursor gets transparent while it gets moved inside GLCanvas
3. When the user moves the cursor outside of GLCanvas (e.g. outside the right border of GLCanvas),
I want to restrict it to the border, so the user is not able to go outside until he does step 4
4. If the user presses CTRL+F10 again the mouse cursor gets displayed again and the area
of movement is no more restricted

I have managed to do step 1,2 and 4, but I have a problem of doing step 3. This is because when
the cursor gets moved right outside of GLCanvas it doesn't get locked to the border. To determine
if the cursor is inside GLCanvas I have handled the events as follows:

Code: Select all

Bind(wxEVT_ENTER_WINDOW, &GLCanvas::OnEnterWnd, this);
Bind(wxEVT_LEAVE_WINDOW, &GLCanvas::OnLeaveWnd, this);
I have tried several things like the following, but it doesn't work.

Code: Select all

void GLCanvas::OnEnterWnd(wxMouseEvent & event)
{
	if (mbMousePointerHidded)
	{
		wxPoint mouseScreenPos = wxGetMousePosition();	// Gets the mouse in screen coordinates
		int mouseX = mouseScreenPos.x - GetScreenPosition().x;	// Gets the x-mouse position relative to widget
		int mouseY = mouseScreenPos.y - GetScreenPosition().y;  // Gets the y-mouse position relative to widget
		wxPoint sceneArea = GetScreenPosition();
		int sceneAreaScreenCoordX = sceneArea.x + GetRect().width;
		int sceneAreaScreenCoordY = sceneArea.y + GetRect().height;
		
		// Locks the mouse to the GLCanvas scene
		if (mouseScreenPos.x > sceneAreaScreenCoordX) {
			WarpPointer(sceneAreaScreenCoordX, mouseY);
		}
	}
}
The calculation and check get done in screen coordinates. In general, I would like to omit WarpPointer because
I have read that it doesn't work on MacOS and I want to keep platform-independent.

Can someone help, please?

User avatar
T-Rex
Moderator
Moderator
Posts: 1182
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: Restrict mouse pointer to area of GLCanvas

Post by T-Rex » Mon Aug 05, 2019 2:51 pm

There are dedicated platform-specific functions for this:
https://docs.microsoft.com/en-us/window ... clipcursor
https://www.x.org/archive/X11R6.8.2/doc ... ter.3.html (+confine_in)

You could create a wrapper class with #ifdef'ed logic for this.

mikegl
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Jul 12, 2019 12:31 am

Re: Restrict mouse pointer to area of GLCanvas

Post by mikegl » Mon Aug 05, 2019 3:43 pm

Thank you for the links and help I will take a look for this solution you provided. Maybe I will change the logic I had at first considered.

EDIT:
Thank you again T-Rex, the solution wrapping the win ops in a separate class was awesome this solved my problem.
I created a Windows class and in future, all the additional win ops will be implemented inside. I will do later the same
for MacOS, Linux etc. operations.

Post Reply