From da0a8db704f610994d665dfd7c080b49476ce76d Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Wed, 13 Sep 2017 00:43:56 -0500 Subject: BACKENDS: Also hide mouse cursor outside game area when an engine has hidden the cursor The only reason we show the system cursor outside the game area is to show users where their mouse is when the window is resized and the mouse is outside the game area. If the game cannot be interacted with, then the mouse also does not need to be shown in the black areas. --- backends/graphics/opengl/opengl-graphics.cpp | 14 +--------- backends/graphics/opengl/opengl-graphics.h | 6 ----- backends/graphics/sdl/sdl-graphics.cpp | 30 +++++++++++++++++----- backends/graphics/sdl/sdl-graphics.h | 2 ++ .../graphics/surfacesdl/surfacesdl-graphics.cpp | 15 ++--------- backends/graphics/surfacesdl/surfacesdl-graphics.h | 2 -- backends/graphics/windowed.h | 17 ++++++++++++ 7 files changed, 46 insertions(+), 40 deletions(-) diff --git a/backends/graphics/opengl/opengl-graphics.cpp b/backends/graphics/opengl/opengl-graphics.cpp index d98dcda599..f3ea372b2a 100644 --- a/backends/graphics/opengl/opengl-graphics.cpp +++ b/backends/graphics/opengl/opengl-graphics.cpp @@ -58,7 +58,7 @@ OpenGLGraphicsManager::OpenGLGraphicsManager() _cursor(nullptr), _cursorHotspotX(0), _cursorHotspotY(0), _cursorHotspotXScaled(0), _cursorHotspotYScaled(0), _cursorWidthScaled(0), _cursorHeightScaled(0), - _cursorKeyColor(0), _cursorVisible(false), _cursorDontScale(false), _cursorPaletteEnabled(false) + _cursorKeyColor(0), _cursorDontScale(false), _cursorPaletteEnabled(false) #ifdef USE_OSD , _osdMessageChangeRequest(false), _osdMessageAlpha(0), _osdMessageFadeStartTime(0), _osdMessageSurface(nullptr), _osdIconSurface(nullptr) @@ -547,18 +547,6 @@ void OpenGLGraphicsManager::grabOverlay(void *buf, int pitch) const { } } -bool OpenGLGraphicsManager::showMouse(bool visible) { - // In case the mouse cursor visibility changed we need to redraw the whole - // screen even when nothing else changed. - if (_cursorVisible != visible) { - _cursorNeedsRedraw = true; - } - - bool last = _cursorVisible; - _cursorVisible = visible; - return last; -} - namespace { template void applyColorKey(DstPixel *dst, const SrcPixel *src, uint w, uint h, uint dstPitch, uint srcPitch, SrcPixel keyColor, DstPixel alphaMask) { diff --git a/backends/graphics/opengl/opengl-graphics.h b/backends/graphics/opengl/opengl-graphics.h index 92bb988a5e..df968aa67e 100644 --- a/backends/graphics/opengl/opengl-graphics.h +++ b/backends/graphics/opengl/opengl-graphics.h @@ -107,7 +107,6 @@ public: virtual void clearOverlay() override; virtual void grabOverlay(void *buf, int pitch) const override; - virtual bool showMouse(bool visible) override; virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) override; virtual void setCursorPalette(const byte *colors, uint start, uint num) override; @@ -395,11 +394,6 @@ protected: */ uint32 _cursorKeyColor; - /** - * Whether the cursor is actually visible. - */ - bool _cursorVisible; - /** * Whether no cursor scaling should be applied. */ diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp index ea947c1073..6ce0bc7bac 100644 --- a/backends/graphics/sdl/sdl-graphics.cpp +++ b/backends/graphics/sdl/sdl-graphics.cpp @@ -148,18 +148,36 @@ void SdlGraphicsManager::initSizeHint(const Graphics::ModeList &modes) { #endif } +bool SdlGraphicsManager::showMouse(const bool visible) { + if (visible == _cursorVisible) { + return visible; + } + + int showCursor = SDL_DISABLE; + if (visible) { + // _cursorX and _cursorY are currently always clipped to the active + // area, so we need to ask SDL where the system's mouse cursor is + // instead + int x, y; + SDL_GetMouseState(&x, &y); + if (!_activeArea.drawRect.contains(Common::Point(x, y))) { + showCursor = SDL_ENABLE; + } + } + SDL_ShowCursor(showCursor); + + return WindowedGraphicsManager::showMouse(visible); +} + void SdlGraphicsManager::notifyMousePosition(Common::Point &mouse) { - int showCursor; - if (_activeArea.drawRect.contains(mouse)) { - showCursor = SDL_DISABLE; - } else { + int showCursor = SDL_DISABLE; + if (!_activeArea.drawRect.contains(mouse)) { mouse.x = CLIP(mouse.x, _activeArea.drawRect.left, _activeArea.drawRect.right - 1); mouse.y = CLIP(mouse.y, _activeArea.drawRect.top, _activeArea.drawRect.bottom - 1); if (_window->mouseIsGrabbed()) { setSystemMousePosition(mouse.x, mouse.y); - showCursor = SDL_DISABLE; - } else { + } else if (_cursorVisible) { showCursor = SDL_ENABLE; } } diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index 886070aedb..772bc88637 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -87,6 +87,8 @@ public: */ virtual void notifyMousePosition(Common::Point &mouse); + virtual bool showMouse(const bool visible) override; + /** * A (subset) of the graphic manager's state. This is used when switching * between different SDL graphic managers at runtime. diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index 52104ffb7a..f610922cc2 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -145,7 +145,7 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou #endif _overlayscreen(0), _tmpscreen2(0), _scalerProc(0), _screenChangeCount(0), - _mouseVisible(false), _mouseData(0), _mouseSurface(0), + _mouseData(0), _mouseSurface(0), _mouseOrigSurface(0), _cursorDontScale(false), _cursorPaletteDisabled(true), _currentShakePos(0), _newShakePos(0), _paletteDirtyStart(0), _paletteDirtyEnd(0), @@ -1863,17 +1863,6 @@ void SurfaceSdlGraphicsManager::copyRectToOverlay(const void *buf, int pitch, in #pragma mark --- Mouse --- #pragma mark - -bool SurfaceSdlGraphicsManager::showMouse(bool visible) { - if (_mouseVisible == visible) - return visible; - - bool last = _mouseVisible; - _mouseVisible = visible; - _cursorNeedsRedraw = true; - - return last; -} - void SurfaceSdlGraphicsManager::setMouseCursor(const void *buf, uint w, uint h, int hotspot_x, int hotspot_y, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) { #ifdef USE_RGB_COLOR if (!format) @@ -2123,7 +2112,7 @@ void SurfaceSdlGraphicsManager::undrawMouse() { } void SurfaceSdlGraphicsManager::drawMouse() { - if (!_mouseVisible || !_mouseSurface) { + if (!_cursorVisible || !_mouseSurface) { _mouseBackup.x = _mouseBackup.y = _mouseBackup.w = _mouseBackup.h = 0; return; } diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h index c1e49b953a..0ed8d79296 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.h +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h @@ -129,7 +129,6 @@ public: virtual int16 getOverlayHeight() const override { return _videoMode.overlayHeight; } virtual int16 getOverlayWidth() const override { return _videoMode.overlayWidth; } - virtual bool showMouse(bool visible) override; virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale = false, const Graphics::PixelFormat *format = NULL) override; virtual void setCursorPalette(const byte *colors, uint start, uint num) override; @@ -318,7 +317,6 @@ protected: { } }; - bool _mouseVisible; byte *_mouseData; SDL_Rect _mouseBackup; MousePos _mouseCurState; diff --git a/backends/graphics/windowed.h b/backends/graphics/windowed.h index b3e5b832b3..52bb96c2cf 100644 --- a/backends/graphics/windowed.h +++ b/backends/graphics/windowed.h @@ -36,6 +36,7 @@ public: _windowHeight(0), _overlayVisible(false), _forceRedraw(false), + _cursorVisible(false), _cursorX(0), _cursorY(0), _cursorNeedsRedraw(false) {} @@ -182,6 +183,17 @@ protected: */ virtual void setSystemMousePosition(const int x, const int y) = 0; + virtual bool showMouse(const bool visible) override { + if (_cursorVisible == visible) { + return visible; + } + + const bool last = _cursorVisible; + _cursorVisible = visible; + _cursorNeedsRedraw = true; + return last; + } + /** * Move ("warp") the mouse cursor to the specified position. * @@ -281,6 +293,11 @@ protected: */ bool _forceRedraw; + /** + * Whether the cursor is actually visible. + */ + bool _cursorVisible; + /** * Whether the mouse cursor needs to be redrawn on the next frame. */ -- cgit v1.2.3