From 26cd7d44e2c181c96d83125012c0d829fc596db4 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Tue, 13 Oct 2020 20:31:10 +0100 Subject: [PATCH] SDL: Implement lockMouse() in the 2D graphics managers (#2517) --- backends/graphics/sdl/sdl-graphics.cpp | 4 ++++ backends/graphics/sdl/sdl-graphics.h | 1 + backends/graphics3d/sdl/sdl-graphics3d.cpp | 14 +------------- backends/platform/sdl/sdl-window.cpp | 15 +++++++++++++++ backends/platform/sdl/sdl-window.h | 5 +++++ 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp index 38cf0dea7e8..cd22a61234c 100644 --- a/backends/graphics/sdl/sdl-graphics.cpp +++ b/backends/graphics/sdl/sdl-graphics.cpp @@ -191,6 +191,10 @@ bool SdlGraphicsManager::showMouse(bool visible) { return WindowedGraphicsManager::showMouse(visible); } +bool SdlGraphicsManager::lockMouse(bool lock) { + return _window->lockMouse(lock); +} + bool SdlGraphicsManager::notifyMousePosition(Common::Point &mouse) { mouse.x = CLIP(mouse.x, 0, _windowWidth - 1); mouse.y = CLIP(mouse.y, 0, _windowHeight - 1); diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index f32c5fc2e1d..3d2baefc7f2 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -95,6 +95,7 @@ public: virtual bool notifyMousePosition(Common::Point &mouse); virtual bool showMouse(bool visible) override; + virtual bool lockMouse(bool lock) override; virtual bool saveScreenshot(const Common::String &filename) const { return false; } void saveScreenshot() override; diff --git a/backends/graphics3d/sdl/sdl-graphics3d.cpp b/backends/graphics3d/sdl/sdl-graphics3d.cpp index c66340b794f..3b6455c7b0c 100644 --- a/backends/graphics3d/sdl/sdl-graphics3d.cpp +++ b/backends/graphics3d/sdl/sdl-graphics3d.cpp @@ -192,21 +192,9 @@ bool SdlGraphics3dManager::showMouse(bool visible) { } bool SdlGraphics3dManager::lockMouse(bool lock) { -#if SDL_VERSION_ATLEAST(2, 0, 0) - if (lock) - SDL_SetRelativeMouseMode(SDL_TRUE); - else - SDL_SetRelativeMouseMode(SDL_FALSE); -#else - if (lock) - SDL_WM_GrabInput(SDL_GRAB_ON); - else - SDL_WM_GrabInput(SDL_GRAB_OFF); -#endif - return true; + return _window->lockMouse(lock); } - bool SdlGraphics3dManager::notifyMousePosition(Common::Point &mouse) { transformMouseCoordinates(mouse); diff --git a/backends/platform/sdl/sdl-window.cpp b/backends/platform/sdl/sdl-window.cpp index 217734bd44d..8e7efb6eaaa 100644 --- a/backends/platform/sdl/sdl-window.cpp +++ b/backends/platform/sdl/sdl-window.cpp @@ -162,6 +162,21 @@ void SdlWindow::toggleMouseGrab() { #endif } +bool SdlWindow::lockMouse(bool lock) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (lock) + SDL_SetRelativeMouseMode(SDL_TRUE); + else + SDL_SetRelativeMouseMode(SDL_FALSE); +#else + if (lock) + SDL_WM_GrabInput(SDL_GRAB_ON); + else + SDL_WM_GrabInput(SDL_GRAB_OFF); +#endif + return true; +} + bool SdlWindow::hasMouseFocus() const { #if SDL_VERSION_ATLEAST(2, 0, 0) if (_window) { diff --git a/backends/platform/sdl/sdl-window.h b/backends/platform/sdl/sdl-window.h index b46ae7015b7..598f18ffb8c 100644 --- a/backends/platform/sdl/sdl-window.h +++ b/backends/platform/sdl/sdl-window.h @@ -51,6 +51,11 @@ public: */ void toggleMouseGrab(); + /** + * Lock or unlock the mouse cursor within the window. + */ + bool lockMouse(bool lock); + /** * Check whether the application has mouse focus. */