From 47ade4ef5329bd62fc2ce40cf78b0c589b2e141a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 25 Aug 2018 19:15:09 +0200 Subject: [PATCH] Platform: make it possible to get scroll position in Glfw and Sdl2App. --- doc/changelog.dox | 2 ++ src/Magnum/Platform/GlfwApplication.cpp | 10 ++++++++++ src/Magnum/Platform/GlfwApplication.h | 8 ++++++++ src/Magnum/Platform/Sdl2Application.cpp | 7 +++++++ src/Magnum/Platform/Sdl2Application.h | 11 ++++++++++- 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 5c893bf37..957206ec2 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -98,6 +98,8 @@ See also: @ref Platform::Sdl2Application::setContainerCssClass() - Implemented @ref Platform::GlfwApplication::MouseMoveEvent::buttons() for feature parity with @ref Platform::Sdl2Application +- Added @ref Platform::Sdl2Application::MouseScrollEvent::position() and + @ref Platform::GlfwApplication::MouseScrollEvent::position() - Added @ref Platform::Sdl2Application::GLConfiguration::setColorBufferSize() "GLConfiguration::setColorBufferSize()", @ref Platform::Sdl2Application::GLConfiguration::setDepthBufferSize() "GLConfiguration::setDepthBufferSize()", @ref Platform::Sdl2Application::GLConfiguration::setStencilBufferSize() "GLConfiguration::setStencilBufferSize()", diff --git a/src/Magnum/Platform/GlfwApplication.cpp b/src/Magnum/Platform/GlfwApplication.cpp index f3b33d5a6..0ce6f6b98 100644 --- a/src/Magnum/Platform/GlfwApplication.cpp +++ b/src/Magnum/Platform/GlfwApplication.cpp @@ -465,6 +465,16 @@ auto GlfwApplication::MouseMoveEvent::modifiers() -> Modifiers { return *_modifiers; } +Vector2i GlfwApplication::MouseScrollEvent::position() { + if(!_position) { + Vector2d position; + glfwGetCursorPos(_window, &position.x(), &position.y()); + _position = Vector2i{position}; + } + + return *_position; +} + auto GlfwApplication::MouseScrollEvent::modifiers() -> Modifiers { if(!_modifiers) _modifiers = currentGlfwModifiers(_window); return *_modifiers; diff --git a/src/Magnum/Platform/GlfwApplication.h b/src/Magnum/Platform/GlfwApplication.h index 816c94da8..29fd9d294 100644 --- a/src/Magnum/Platform/GlfwApplication.h +++ b/src/Magnum/Platform/GlfwApplication.h @@ -1342,6 +1342,13 @@ class GlfwApplication::MouseScrollEvent: public GlfwApplication::InputEvent { /** @brief Scroll offset */ Vector2 offset() const { return _offset; } + /** + * @brief Position + * + * Lazily populated on first request. + */ + Vector2i position(); + /** * @brief Modifiers * @@ -1354,6 +1361,7 @@ class GlfwApplication::MouseScrollEvent: public GlfwApplication::InputEvent { GLFWwindow* _window; const Vector2 _offset; + Containers::Optional _position; Containers::Optional _modifiers; }; diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index e6cf7ecc5..c1220144e 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -879,6 +879,13 @@ Sdl2Application::InputEvent::Modifiers Sdl2Application::MouseMoveEvent::modifier return _modifiers = fixedModifiers(Uint16(SDL_GetModState())); } +Vector2i Sdl2Application::MouseScrollEvent::position() { + if(_positionLoaded) return _position; + _positionLoaded = true; + SDL_GetMouseState(&_position.x(), &_position.y()); + return _position; +} + Sdl2Application::InputEvent::Modifiers Sdl2Application::MouseScrollEvent::modifiers() { if(_modifiersLoaded) return _modifiers; _modifiersLoaded = true; diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 229513578..2c316aa4e 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -2035,6 +2035,13 @@ class Sdl2Application::MouseScrollEvent: public Sdl2Application::InputEvent { /** @brief Scroll offset */ Vector2 offset() const { return _offset; } + /** + * @brief Position + * + * Lazily populated on first request. + */ + Vector2i position(); + /** * @brief Modifiers * @@ -2043,10 +2050,12 @@ class Sdl2Application::MouseScrollEvent: public Sdl2Application::InputEvent { Modifiers modifiers(); private: - explicit MouseScrollEvent(const Vector2& offset): _offset{offset}, _modifiersLoaded{false} {} + explicit MouseScrollEvent(const Vector2& offset): _offset{offset}, _positionLoaded{false}, _modifiersLoaded{false} {} const Vector2 _offset; + bool _positionLoaded; bool _modifiersLoaded; + Vector2i _position; Modifiers _modifiers; };