diff --git a/library/phx/input/mouse.cpp b/library/phx/input/mouse.cpp index f575e1b3dcd5568b19ef7b75a8a1d4e8fd42cee7..dca39bfabe84074197d3dbe2994dcd189be9f4d5 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 500ffd35ec021075409c2d0fb0acadbdbe95a9bd..76af966f539508e57d4f730cbdf081c96f1d4892 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 b395803e58d310b301f9bb33e93d054e370543e8..c13e52861ba38aff397f1aded949aa2ff35f3535 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); + } + } + } } }