[Scummvm-git-logs] scummvm master -> d968d22eb124a9f422d5c5ebd401135cdd41033f
aquadran
aquadran at gmail.com
Wed Oct 14 06:03:34 UTC 2020
This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .
Summary:
d968d22eb1 SDL: Refactor grabbing and locking the mouse (#2522)
Commit: d968d22eb124a9f422d5c5ebd401135cdd41033f
https://github.com/scummvm/scummvm/commit/d968d22eb124a9f422d5c5ebd401135cdd41033f
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2020-10-14T08:03:31+02:00
Commit Message:
SDL: Refactor grabbing and locking the mouse (#2522)
Changed paths:
backends/graphics/downscalesdl/downscalesdl-graphics.cpp
backends/graphics/sdl/sdl-graphics.cpp
backends/graphics3d/sdl/sdl-graphics3d.cpp
backends/platform/sdl/sdl-window.cpp
backends/platform/sdl/sdl-window.h
diff --git a/backends/graphics/downscalesdl/downscalesdl-graphics.cpp b/backends/graphics/downscalesdl/downscalesdl-graphics.cpp
index 8bba04249d..2413d3f1a7 100644
--- a/backends/graphics/downscalesdl/downscalesdl-graphics.cpp
+++ b/backends/graphics/downscalesdl/downscalesdl-graphics.cpp
@@ -110,7 +110,7 @@ void DownscaleSdlGraphicsManager::initSize(uint w, uint h, const Graphics::Pixel
if (w > 320 || h > 240) {
setGraphicsMode(GFX_HALF);
setGraphicsModeIntern();
- _window->toggleMouseGrab();
+ _window->grabMouse(!getWindow()->mouseIsGrabbed());
}
_transactionDetails.sizeChanged = true;
diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp
index cd22a61234..710e942657 100644
--- a/backends/graphics/sdl/sdl-graphics.cpp
+++ b/backends/graphics/sdl/sdl-graphics.cpp
@@ -335,7 +335,7 @@ bool SdlGraphicsManager::notifyEvent(const Common::Event &event) {
switch ((CustomEventAction) event.customType) {
case kActionToggleMouseCapture:
- getWindow()->toggleMouseGrab();
+ getWindow()->grabMouse(!getWindow()->mouseIsGrabbed());
return true;
case kActionToggleFullscreen:
diff --git a/backends/graphics3d/sdl/sdl-graphics3d.cpp b/backends/graphics3d/sdl/sdl-graphics3d.cpp
index 3b6455c7b0..fc09af3f5c 100644
--- a/backends/graphics3d/sdl/sdl-graphics3d.cpp
+++ b/backends/graphics3d/sdl/sdl-graphics3d.cpp
@@ -106,7 +106,7 @@ bool SdlGraphics3dManager::notifyEvent(const Common::Event &event) {
switch ((CustomEventAction) event.customType) {
case kActionToggleMouseCapture:
- getWindow()->toggleMouseGrab();
+ getWindow()->grabMouse(!getWindow()->mouseIsGrabbed());
return true;
case kActionToggleFullscreen:
diff --git a/backends/platform/sdl/sdl-window.cpp b/backends/platform/sdl/sdl-window.cpp
index 8e7efb6eaa..2f8f4ea7f7 100644
--- a/backends/platform/sdl/sdl-window.cpp
+++ b/backends/platform/sdl/sdl-window.cpp
@@ -34,8 +34,10 @@ static const uint32 fullscreenMask = SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_
SdlWindow::SdlWindow()
#if SDL_VERSION_ATLEAST(2, 0, 0)
- : _window(nullptr), _inputGrabState(false), _windowCaption("ScummVM"),
+ : _window(nullptr), _windowCaption("ScummVM"),
_lastFlags(0), _lastX(SDL_WINDOWPOS_UNDEFINED), _lastY(SDL_WINDOWPOS_UNDEFINED)
+#else
+ : _inputGrabState(false), _inputLockState(false)
#endif
{
@@ -145,34 +147,35 @@ void SdlWindow::setWindowCaption(const Common::String &caption) {
#endif
}
-void SdlWindow::toggleMouseGrab() {
+void SdlWindow::grabMouse(bool grab) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (_window) {
- _inputGrabState = SDL_GetWindowGrab(_window) == SDL_FALSE;
- SDL_SetWindowGrab(_window, _inputGrabState ? SDL_TRUE : SDL_FALSE);
+ SDL_SetWindowGrab(_window, grab ? SDL_TRUE : SDL_FALSE);
}
#else
- if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) {
- SDL_WM_GrabInput(SDL_GRAB_ON);
+ if (grab) {
_inputGrabState = true;
+ SDL_WM_GrabInput(SDL_GRAB_ON);
} else {
- SDL_WM_GrabInput(SDL_GRAB_OFF);
_inputGrabState = false;
+ if (!_inputLockState)
+ SDL_WM_GrabInput(SDL_GRAB_OFF);
}
#endif
}
bool SdlWindow::lockMouse(bool lock) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
- if (lock)
- SDL_SetRelativeMouseMode(SDL_TRUE);
- else
- SDL_SetRelativeMouseMode(SDL_FALSE);
+ SDL_SetRelativeMouseMode(lock ? SDL_TRUE : SDL_FALSE);
#else
- if (lock)
+ if (lock) {
+ _inputLockState = true;
SDL_WM_GrabInput(SDL_GRAB_ON);
- else
- SDL_WM_GrabInput(SDL_GRAB_OFF);
+ } else {
+ _inputLockState = false;
+ if (!_inputGrabState)
+ SDL_WM_GrabInput(SDL_GRAB_OFF);
+ }
#endif
return true;
}
@@ -262,7 +265,7 @@ SDL_Surface *copySDLSurface(SDL_Surface *src) {
}
bool SdlWindow::createOrUpdateWindow(int width, int height, uint32 flags) {
- if (_inputGrabState) {
+ if (mouseIsGrabbed()) {
flags |= SDL_WINDOW_INPUT_GRABBED;
}
diff --git a/backends/platform/sdl/sdl-window.h b/backends/platform/sdl/sdl-window.h
index 598f18ffb8..ab5374c3c4 100644
--- a/backends/platform/sdl/sdl-window.h
+++ b/backends/platform/sdl/sdl-window.h
@@ -46,10 +46,10 @@ public:
void setWindowCaption(const Common::String &caption);
/**
- * Toggle mouse grab state. This decides whether the cursor can leave the
- * window or not.
+ * Grab or ungrab the mouse cursor. This decides whether the cursor can leave
+ * the window or not.
*/
- void toggleMouseGrab();
+ void grabMouse(bool grab);
/**
* Lock or unlock the mouse cursor within the window.
@@ -92,13 +92,21 @@ public:
if (_window) {
return SDL_GetWindowGrab(_window) == SDL_TRUE;
}
-#endif
+ return false;
+#else
return _inputGrabState;
+#endif
}
-private:
- bool _inputGrabState;
+ bool mouseIsLocked() const {
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+ return SDL_GetRelativeMouseMode() == SDL_TRUE;
+#else
+ return _inputLockState;
+#endif
+ }
+private:
Common::Rect _desktopRes;
#if SDL_VERSION_ATLEAST(2, 0, 0)
@@ -137,6 +145,8 @@ private:
int _lastX, _lastY;
Common::String _windowCaption;
+#else
+ bool _inputGrabState, _inputLockState;
#endif
};
More information about the Scummvm-git-logs
mailing list