From 0d6e2d1090c09071d8807450e5c374f4d459c1e6 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Fri, 29 Jul 2022 12:41:29 -0500 Subject: [PATCH] Add MOUSE_MODE_CONFINED_HIDDEN to MouseMode enum --- core/os/input.cpp | 3 ++- core/os/input.h | 3 ++- core/os/os.h | 3 ++- doc/classes/Input.xml | 5 ++++- platform/javascript/os_javascript.cpp | 5 +++-- platform/osx/os_osx.mm | 11 ++++++++--- platform/uwp/app.cpp | 6 ++++-- platform/uwp/os_uwp.cpp | 4 +--- platform/windows/os_windows.cpp | 21 +++++++++++++-------- platform/x11/os_x11.cpp | 8 ++++---- 10 files changed, 43 insertions(+), 26 deletions(-) diff --git a/core/os/input.cpp b/core/os/input.cpp index ccc576f8797..4fdbd034b77 100644 --- a/core/os/input.cpp +++ b/core/os/input.cpp @@ -45,7 +45,7 @@ Input *Input::get_singleton() { } void Input::set_mouse_mode(MouseMode p_mode) { - ERR_FAIL_INDEX((int)p_mode, 4); + ERR_FAIL_INDEX((int)p_mode, 5); OS::get_singleton()->set_mouse_mode((OS::MouseMode)p_mode); } @@ -113,6 +113,7 @@ void Input::_bind_methods() { BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN); BIND_ENUM_CONSTANT(MOUSE_MODE_CAPTURED); BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED); + BIND_ENUM_CONSTANT(MOUSE_MODE_CONFINED_HIDDEN); BIND_ENUM_CONSTANT(CURSOR_ARROW); BIND_ENUM_CONSTANT(CURSOR_IBEAM); diff --git a/core/os/input.h b/core/os/input.h index deeb6475568..fb21716e211 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -48,7 +48,8 @@ public: MOUSE_MODE_VISIBLE, MOUSE_MODE_HIDDEN, MOUSE_MODE_CAPTURED, - MOUSE_MODE_CONFINED + MOUSE_MODE_CONFINED, + MOUSE_MODE_CONFINED_HIDDEN, }; #undef CursorShape diff --git a/core/os/os.h b/core/os/os.h index 1f03c0d27ce..55d3028cb1c 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -166,7 +166,8 @@ public: MOUSE_MODE_VISIBLE, MOUSE_MODE_HIDDEN, MOUSE_MODE_CAPTURED, - MOUSE_MODE_CONFINED + MOUSE_MODE_CONFINED, + MOUSE_MODE_CONFINED_HIDDEN, }; virtual void set_mouse_mode(MouseMode p_mode); diff --git a/doc/classes/Input.xml b/doc/classes/Input.xml index 359b91b2608..fa5828f24e8 100644 --- a/doc/classes/Input.xml +++ b/doc/classes/Input.xml @@ -426,7 +426,10 @@ [b]Note:[/b] If you want to process the mouse's movement in this mode, you need to use [member InputEventMouseMotion.relative]. - Makes the mouse cursor visible but confines it to the game window. + Confines the mouse cursor to the game window, and make it visible. + + + Confines the mouse cursor to the game window, and make it hidden. Arrow cursor. Standard, default pointing cursor. diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 43d3dfd77f1..08b1647377f 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -449,9 +449,10 @@ void OS_JavaScript::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_s } void OS_JavaScript::set_mouse_mode(OS::MouseMode p_mode) { - ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED, "MOUSE_MODE_CONFINED is not supported for the HTML5 platform."); - if (p_mode == get_mouse_mode()) + ERR_FAIL_COND_MSG(p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN, "MOUSE_MODE_CONFINED is not supported for the HTML5 platform."); + if (p_mode == get_mouse_mode()) { return; + } if (p_mode == MOUSE_MODE_VISIBLE) { godot_js_display_cursor_set_visible(1); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index aaf69580b70..d0008583799 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -759,7 +759,7 @@ static void _mouseDownEvent(NSEvent *event, int index, int mask, bool pressed) { return; } - if (OS_OSX::singleton->mouse_mode == OS::MOUSE_MODE_CONFINED) { + if (OS_OSX::singleton->mouse_mode == OS::MOUSE_MODE_CONFINED || OS_OSX::singleton->mouse_mode == OS::MOUSE_MODE_CONFINED_HIDDEN) { // Discard late events if (([event timestamp]) < OS_OSX::singleton->last_warp) { return; @@ -2189,7 +2189,7 @@ void OS_OSX::warp_mouse_position(const Point2 &p_to) { CGEventSourceSetLocalEventsSuppressionInterval(lEventRef, 0.0); CGAssociateMouseAndMouseCursorPosition(false); CGWarpMouseCursorPosition(lMouseWarpPos); - if (mouse_mode != MOUSE_MODE_CONFINED) { + if (mouse_mode != MOUSE_MODE_CONFINED && mouse_mode != MOUSE_MODE_CONFINED_HIDDEN) { CGAssociateMouseAndMouseCursorPosition(true); } } @@ -3469,7 +3469,12 @@ void OS_OSX::set_mouse_mode(MouseMode p_mode) { } else if (p_mode == MOUSE_MODE_CONFINED) { CGDisplayShowCursor(kCGDirectMainDisplay); CGAssociateMouseAndMouseCursorPosition(false); - } else { + } else if (p_mode == MOUSE_MODE_CONFINED_HIDDEN) { + if (mouse_mode == MOUSE_MODE_VISIBLE || mouse_mode == MOUSE_MODE_CONFINED) { + CGDisplayHideCursor(kCGDirectMainDisplay); + } + CGAssociateMouseAndMouseCursorPosition(false); + } else { // MOUSE_MODE_VISIBLE CGDisplayShowCursor(kCGDirectMainDisplay); CGAssociateMouseAndMouseCursorPosition(true); } diff --git a/platform/uwp/app.cpp b/platform/uwp/app.cpp index a00ef77eb21..7f93e9763d5 100644 --- a/platform/uwp/app.cpp +++ b/platform/uwp/app.cpp @@ -342,8 +342,9 @@ void App::OnPointerMoved(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Co os->input_event(screen_drag); } else { // In case the mouse grabbed, MouseMoved will handle this - if (os->get_mouse_mode() == OS::MouseMode::MOUSE_MODE_CAPTURED) + if (os->get_mouse_mode() == OS::MouseMode::MOUSE_MODE_CAPTURED) { return; + } Ref mouse_motion; mouse_motion.instance(); @@ -360,8 +361,9 @@ void App::OnPointerMoved(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Co void App::OnMouseMoved(MouseDevice ^ mouse_device, MouseEventArgs ^ args) { // In case the mouse isn't grabbed, PointerMoved will handle this - if (os->get_mouse_mode() != OS::MouseMode::MOUSE_MODE_CAPTURED) + if (os->get_mouse_mode() != OS::MouseMode::MOUSE_MODE_CAPTURED) { return; + } Windows::Foundation::Point pos; pos.X = last_mouse_pos.X + args->MouseDelta.X; diff --git a/platform/uwp/os_uwp.cpp b/platform/uwp/os_uwp.cpp index 26494af9b78..465d437379f 100644 --- a/platform/uwp/os_uwp.cpp +++ b/platform/uwp/os_uwp.cpp @@ -435,14 +435,12 @@ void OS_UWP::ManagedType::on_gyroscope_reading_changed(Gyrometer ^ sender, Gyrom void OS_UWP::set_mouse_mode(MouseMode p_mode) { if (p_mode == MouseMode::MOUSE_MODE_CAPTURED) { CoreWindow::GetForCurrentThread()->SetPointerCapture(); - } else { CoreWindow::GetForCurrentThread()->ReleasePointerCapture(); } - if (p_mode == MouseMode::MOUSE_MODE_CAPTURED || p_mode == MouseMode::MOUSE_MODE_HIDDEN) { + if (p_mode == MouseMode::MOUSE_MODE_HIDDEN || p_mode == MouseMode::MOUSE_MODE_CAPTURED || p_mode == MouseMode::MOUSE_MODE_CONFINED_HIDDEN) { CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; - } else { CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); } diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index aa9a0352ecd..6779644e3ed 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -497,8 +497,9 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) ScreenToClient(hWnd, &coords); // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode. - if (!window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED) + if (!window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED) { break; + } Ref mm; mm.instance(); @@ -628,8 +629,9 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode. - if (!window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED) + if (!window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED) { break; + } Ref mm; mm.instance(); @@ -730,8 +732,9 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } // Don't calculate relative mouse movement if we don't have focus in CAPTURED mode. - if (!window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED) + if (!window_has_focus && mouse_mode == MOUSE_MODE_CAPTURED) { break; + } Ref mm; mm.instance(); @@ -1111,7 +1114,7 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } break; case WM_SETCURSOR: { if (LOWORD(lParam) == HTCLIENT) { - if (window_has_focus && (mouse_mode == MOUSE_MODE_HIDDEN || mouse_mode == MOUSE_MODE_CAPTURED)) { + if (window_has_focus && (mouse_mode == MOUSE_MODE_HIDDEN || mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN)) { //Hide the cursor if (hCursor == NULL) { hCursor = SetCursor(NULL); @@ -1810,7 +1813,8 @@ void OS_Windows::set_mouse_mode(MouseMode p_mode) { } void OS_Windows::_set_mouse_mode_impl(MouseMode p_mode) { - if (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED) { + if (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_CONFINED || p_mode == MOUSE_MODE_CONFINED_HIDDEN) { + // Mouse is grabbed (captured or confined). RECT clipRect; GetClientRect(hWnd, &clipRect); ClientToScreen(hWnd, (POINT *)&clipRect.left); @@ -1824,11 +1828,12 @@ void OS_Windows::_set_mouse_mode_impl(MouseMode p_mode) { SetCapture(hWnd); } } else { + // Mouse is free to move around (not captured or confined). ReleaseCapture(); ClipCursor(NULL); } - if (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_HIDDEN) { + if (p_mode == MOUSE_MODE_CAPTURED || p_mode == MOUSE_MODE_HIDDEN || p_mode == MOUSE_MODE_CONFINED_HIDDEN) { if (hCursor == NULL) { hCursor = SetCursor(NULL); } else { @@ -2058,7 +2063,7 @@ void OS_Windows::set_window_position(const Point2 &p_position) { MoveWindow(hWnd, p_position.x, p_position.y, r.right - r.left, r.bottom - r.top, TRUE); // Don't let the mouse leave the window when moved - if (mouse_mode == MOUSE_MODE_CONFINED) { + if (mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { RECT rect; GetClientRect(hWnd, &rect); ClientToScreen(hWnd, (POINT *)&rect.left); @@ -2139,7 +2144,7 @@ void OS_Windows::set_window_size(const Size2 p_size) { MoveWindow(hWnd, rect.left, rect.top, w, h, TRUE); // Don't let the mouse leave the window when resizing to a smaller resolution - if (mouse_mode == MOUSE_MODE_CONFINED) { + if (mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { RECT crect; GetClientRect(hWnd, &crect); ClientToScreen(hWnd, (POINT *)&crect.left); diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 47a1cde9402..835eb66a201 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -924,7 +924,7 @@ void OS_X11::set_mouse_mode(MouseMode p_mode) { return; } - if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED) { + if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { XUngrabPointer(x11_display, CurrentTime); } @@ -939,7 +939,7 @@ void OS_X11::set_mouse_mode(MouseMode p_mode) { mouse_mode = p_mode; - if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED) { + if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { //flush pending motion events flush_mouse_motion(); @@ -2525,7 +2525,7 @@ void OS_X11::process_xevents() { do_mouse_warp = false; // Is the current mouse mode one where it needs to be grabbed. - bool mouse_mode_grab = mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED; + bool mouse_mode_grab = mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN; xi.pressure = 0; xi.tilt = Vector2(); @@ -2760,7 +2760,7 @@ void OS_X11::process_xevents() { // Show and update the cursor if confined and the window regained focus. if (mouse_mode == MOUSE_MODE_CONFINED) { XUndefineCursor(x11_display, x11_window); - } else if (mouse_mode == MOUSE_MODE_CAPTURED) { // or re-hide it in captured mode + } else if (mouse_mode == MOUSE_MODE_CAPTURED || mouse_mode == MOUSE_MODE_CONFINED_HIDDEN) { // or re-hide it in captured mode XDefineCursor(x11_display, x11_window, null_cursor); }