From 0572b7d2371a0b256baaab7620a2ca3256c60e80 Mon Sep 17 00:00:00 2001 From: jwendt <wendt@vr.rwth-aachen.de> Date: Thu, 26 Jul 2018 16:46:12 +0200 Subject: [PATCH] Add mouse weel scroll signal #463 --- library/phx/input/mouse.cpp | 7 +++++++ library/phx/input/mouse.hpp | 4 +++- tests/src/test_mouse.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/library/phx/input/mouse.cpp b/library/phx/input/mouse.cpp index f575e1b3..dca39bfa 100644 --- a/library/phx/input/mouse.cpp +++ b/library/phx/input/mouse.cpp @@ -36,6 +36,8 @@ void Mouse::OnSDLEvent(const SDL_Event& event) { if (event.type == SDL_MOUSEMOTION) { move_signal_(static_cast<int>(event.motion.xrel), static_cast<int>(event.motion.yrel)); + } else if (event.type == SDL_MOUSEWHEEL) { + wheel_signal_(static_cast<int>(event.wheel.y)); } else if (event.type == SDL_MOUSEBUTTONDOWN) { event_type = BUTTON_PRESSED; } else if (event.type == SDL_MOUSEBUTTONUP) { @@ -75,4 +77,9 @@ boost::signals2::connection Mouse::RegisterMoveSignal( return move_signal_.connect(callback); } +boost::signals2::connection Mouse::RegisterWheelSignal( + const std::function<void(int)>& callback) { + return wheel_signal_.connect(callback); +} + } // namespace phx diff --git a/library/phx/input/mouse.hpp b/library/phx/input/mouse.hpp index 500ffd35..76af966f 100644 --- a/library/phx/input/mouse.hpp +++ b/library/phx/input/mouse.hpp @@ -31,7 +31,6 @@ #include "boost/signals2/signal.hpp" #include "phx/input/sdl_device.hpp" -#include "phx/suppress_warnings.hpp" #include "phx/export.hpp" @@ -57,10 +56,13 @@ class PHOENIX_EXPORT Mouse : public SDLDevice { const std::function<void(ButtonId, ButtonEvent)>& callback); boost::signals2::connection RegisterMoveSignal( const std::function<void(int, int)>& callback); + boost::signals2::connection RegisterWheelSignal( + const std::function<void(int)>& callback); private: boost::signals2::signal<void(ButtonId, ButtonEvent)> button_signal_; boost::signals2::signal<void(int, int)> move_signal_; + boost::signals2::signal<void(int)> wheel_signal_; }; } // namespace phx diff --git a/tests/src/test_mouse.cpp b/tests/src/test_mouse.cpp index b395803e..c13e5286 100644 --- a/tests/src/test_mouse.cpp +++ b/tests/src/test_mouse.cpp @@ -97,5 +97,29 @@ SCENARIO("Mouse signals received events.", "[phx][phx::Mouse]") { } } } + + WHEN("a mouse scroll signal is registered") { + int y_delta_received = 0; + mouse.RegisterWheelSignal( + [&y_delta_received](int y_delta) { y_delta_received = y_delta; }); + WHEN("A mouse scroll event is fired by SDL.") { + SDL_Event mouse_event; + mouse_event.type = SDL_MOUSEWHEEL; + mouse_event.wheel.y = 3; + auto first_call = std::make_shared<bool>(true); + ALLOW_CALL(sdl_mock.Get(), SDL_PollEvent(_)) + .SIDE_EFFECT(*_1 = mouse_event) + .SIDE_EFFECT(*first_call = false) + .WITH(*first_call == true) + .RETURN(true); + ALLOW_CALL(sdl_mock.Get(), SDL_PollEvent(_)) + .WITH(*first_call == false) + .RETURN(false); + THEN("I should receive a signal.") { + mouse.Update(); + REQUIRE(y_delta_received == 3); + } + } + } } } -- GitLab