Skip to content

Commit

Permalink
SDL: Use SDL_SetWindowMouseRect to confine the mouse area
Browse files Browse the repository at this point in the history
  • Loading branch information
ccawley2011 authored and sev- committed Nov 19, 2021
1 parent 0b011e9 commit c6836c9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions backends/graphics/sdl/sdl-graphics.cpp
Expand Up @@ -282,6 +282,10 @@ void SdlGraphicsManager::setSystemMousePosition(const int x, const int y) {
}
}

void SdlGraphicsManager::notifyActiveAreaChanged() {
_window->setMouseRect(_activeArea.drawRect);
}

void SdlGraphicsManager::handleResizeImpl(const int width, const int height) {
_forceRedraw = true;
}
Expand Down
2 changes: 2 additions & 0 deletions backends/graphics/sdl/sdl-graphics.h
Expand Up @@ -182,6 +182,8 @@ class SdlGraphicsManager : virtual public WindowedGraphicsManager, public Common

void setSystemMousePosition(const int x, const int y) override;

void notifyActiveAreaChanged() override;

void handleResizeImpl(const int width, const int height) override;

#if SDL_VERSION_ATLEAST(2, 0, 0)
Expand Down
8 changes: 8 additions & 0 deletions backends/graphics/windowed.h
Expand Up @@ -63,6 +63,7 @@ class WindowedGraphicsManager : virtual public GraphicsManager {
_activeArea.height = getOverlayHeight();
_overlayVisible = true;
_forceRedraw = true;
notifyActiveAreaChanged();
}

void hideOverlay() override {
Expand All @@ -74,6 +75,7 @@ class WindowedGraphicsManager : virtual public GraphicsManager {
_activeArea.height = getHeight();
_overlayVisible = false;
_forceRedraw = true;
notifyActiveAreaChanged();
}

bool isOverlayVisible() const override { return _overlayVisible; }
Expand Down Expand Up @@ -211,6 +213,7 @@ class WindowedGraphicsManager : virtual public GraphicsManager {
_activeArea.width = getWidth();
_activeArea.height = getHeight();
}
notifyActiveAreaChanged();
}

/**
Expand All @@ -222,6 +225,11 @@ class WindowedGraphicsManager : virtual public GraphicsManager {
*/
virtual void setSystemMousePosition(const int x, const int y) = 0;

/**
* Called whenever the active area has changed.
*/
virtual void notifyActiveAreaChanged() {}

bool showMouse(bool visible) override {
if (_cursorVisible == visible) {
return visible;
Expand Down
20 changes: 20 additions & 0 deletions backends/platform/sdl/sdl-window.cpp
Expand Up @@ -39,6 +39,7 @@ SdlWindow::SdlWindow() :
#endif
_inputGrabState(false), _inputLockState(false)
{
memset(&grabRect, 0, sizeof(grabRect));

#if SDL_VERSION_ATLEAST(2, 0, 0)
#elif SDL_VERSION_ATLEAST(1, 2, 10)
Expand Down Expand Up @@ -150,6 +151,9 @@ void SdlWindow::grabMouse(bool grab) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
if (_window) {
SDL_SetWindowGrab(_window, grab ? SDL_TRUE : SDL_FALSE);
#if SDL_VERSION_ATLEAST(2, 0, 17)
SDL_SetWindowMouseRect(_window, grab ? &grabRect : NULL);
#endif
}
_inputGrabState = grab;
#else
Expand All @@ -164,6 +168,19 @@ void SdlWindow::grabMouse(bool grab) {
#endif
}

void SdlWindow::setMouseRect(const Common::Rect &rect) {
grabRect.x = rect.left;
grabRect.y = rect.top;
grabRect.w = rect.width();
grabRect.h = rect.height();

#if SDL_VERSION_ATLEAST(2, 0, 17)
if (_inputGrabState || _lastFlags & fullscreenMask) {
SDL_SetWindowMouseRect(_window, &grabRect);
}
#endif
}

bool SdlWindow::lockMouse(bool lock) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_SetRelativeMouseMode(lock ? SDL_TRUE : SDL_FALSE);
Expand Down Expand Up @@ -397,6 +414,9 @@ bool SdlWindow::createOrUpdateWindow(int width, int height, uint32 flags) {

const bool shouldGrab = (flags & SDL_WINDOW_INPUT_GRABBED) || fullscreenFlags;
SDL_SetWindowGrab(_window, shouldGrab ? SDL_TRUE : SDL_FALSE);
#if SDL_VERSION_ATLEAST(2, 0, 17)
SDL_SetWindowMouseRect(_window, shouldGrab ? &grabRect : NULL);
#endif

if (!_window) {
return false;
Expand Down
6 changes: 6 additions & 0 deletions backends/platform/sdl/sdl-window.h
Expand Up @@ -51,6 +51,11 @@ class SdlWindow {
*/
void grabMouse(bool grab);

/**
* Specify the area of the window to confine the mouse cursor.
*/
void setMouseRect(const Common::Rect &rect);

/**
* Lock or unlock the mouse cursor within the window.
*/
Expand Down Expand Up @@ -124,6 +129,7 @@ class SdlWindow {
private:
Common::Rect _desktopRes;
bool _inputGrabState, _inputLockState;
SDL_Rect grabRect;

protected:
void getDisplayDpi(float *dpi, float *defaultDpi) const;
Expand Down

0 comments on commit c6836c9

Please sign in to comment.