From 4cf68c29fcef884f52f95da1e45aebc9c95d355f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 30 Jan 2020 14:01:28 +0100 Subject: [PATCH] [wip] Platform: implement mouse capturing for SDL and GLFW. TODO: GLFW has it together with normal/hidden/locked in setCursor() and those options are mutually exclusive, so maybe do it that way instead of setMouseCaptured() like in case of SDL? TODO: while SDL reports move event coordinates outside of the window, GLFW docs in the branch seem to indicate that the mouse position won't change -- would this mean the mouse move events won't get reported? --- src/Magnum/Platform/GlfwApplication.h | 4 ++++ src/Magnum/Platform/Sdl2Application.h | 20 +++++++++++++++++++ .../Platform/Test/Sdl2ApplicationTest.cpp | 5 ++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index 994cc60b9..404ab6e85 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -625,6 +625,10 @@ class GlfwApplication { glfwSetCursorPos(_window, Double(position.x()), Double(position.y())); } + /** @todo GLFW_CURSOR_CAPTURED, currently only in a branch: + https://github.com/glfw/glfw/issues/58 + https://github.com/glfw/glfw/compare/captured-cursor-mode-r2 */ + private: /** @copydoc Sdl2Application::mousePressEvent() */ virtual void mousePressEvent(MouseEvent& event); diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 0cd2f5596..a5f6d0108 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -967,6 +967,26 @@ class Sdl2Application { void warpCursor(const Vector2i& position) { SDL_WarpMouseInWindow(_window, position.x(), position.y()); } + + /** + * @brief Whether the mouse is captured + * @m_since_latest + * + * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". + */ + bool isMouseCaptured() { + return SDL_GetWindowFlags(_window) & SDL_WINDOW_MOUSE_CAPTURE; + } + + /** + * @brief Capture mouse + * @m_since_latest + * + * @note Not available in @ref CORRADE_TARGET_EMSCRIPTEN "Emscripten". + */ + void setMouseCaptured(bool enabled) { + SDL_CaptureMouse(enabled ? SDL_TRUE : SDL_FALSE); + } #endif #ifdef MAGNUM_BUILD_DEPRECATED diff --git a/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp b/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp index f81605217..e0afea40e 100644 --- a/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp +++ b/src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp @@ -97,7 +97,10 @@ struct Sdl2ApplicationTest: Platform::Application { setWindowTitle("This is a UTF-8 Window Titleā„¢!"); } #ifndef CORRADE_TARGET_EMSCRIPTEN - else if(event.key() == KeyEvent::Key::S) { + else if(event.key() == KeyEvent::Key::C) { + Debug{} << "toggling mouse capture to" << !isMouseCaptured(); + setMouseCaptured(!isMouseCaptured()); + } else if(event.key() == KeyEvent::Key::S) { Debug{} << "setting window size, which should trigger a viewport event"; setWindowSize(Vector2i{300, 200}); } else if(event.key() == KeyEvent::Key::W) {