diff --git a/MainWindow.cpp b/MainWindow.cpp index 70b051c..e6d39ab 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -389,6 +389,19 @@ void MainWindow::reconfigure() noexcept { _renderer.resize(buffer_size()); } +void MainWindow::scroll(MouseWheelAxis axis, float distance) noexcept { + switch (axis) { + case MouseWheelAxis::Vertical: + ImGui::GetIO().AddMouseWheelEvent(0, -distance); + break; + case MouseWheelAxis::Horizontal: + ImGui::GetIO().AddMouseWheelEvent(-distance, 0); + break; + default: + break; + } +} + void MainWindow::text(const std::string& str) const noexcept { ImGui::GetIO().AddInputCharactersUTF8(str.c_str()); } diff --git a/MainWindow.hpp b/MainWindow.hpp index 754b026..0d6781b 100644 --- a/MainWindow.hpp +++ b/MainWindow.hpp @@ -13,6 +13,7 @@ class MainWindow final : public Window { virtual void pointer_click(uint32_t button, wl_pointer_button_state state) noexcept override; virtual void pointer_motion(float x, float y) noexcept override; virtual void reconfigure() noexcept override; + virtual void scroll(MouseWheelAxis axis, float distance) noexcept override; virtual void text(const std::string& str) const noexcept override; void render(); diff --git a/wayland/MouseWheelAxis.hpp b/wayland/MouseWheelAxis.hpp new file mode 100644 index 0000000..2be9c4b --- /dev/null +++ b/wayland/MouseWheelAxis.hpp @@ -0,0 +1,6 @@ +#pragma once + +enum class MouseWheelAxis { + Vertical, + Horizontal +}; diff --git a/wayland/Pointer.cpp b/wayland/Pointer.cpp index 3ffa4c8..8d8eea9 100644 --- a/wayland/Pointer.cpp +++ b/wayland/Pointer.cpp @@ -40,7 +40,19 @@ Pointer::Pointer(Seat& seat) self._focus->pointer_click(button, static_cast(state)); } }, - .axis = [](void *, wl_pointer *, uint32_t, uint32_t, wl_fixed_t) noexcept {}, + .axis = [](void * data, wl_pointer *, uint32_t, uint32_t axis, wl_fixed_t value) noexcept { + auto& self = *static_cast(data); + if (self._focus) { + const auto distance = static_cast(wl_fixed_to_double(value)); + switch (axis) { + case WL_POINTER_AXIS_VERTICAL_SCROLL: + self._focus->scroll(MouseWheelAxis::Vertical, distance); + break; + case WL_POINTER_AXIS_HORIZONTAL_SCROLL: + self._focus->scroll(MouseWheelAxis::Horizontal, distance); + } + } + }, .frame = [](void *, wl_pointer *) noexcept {}, .axis_source = [](void *, wl_pointer *, uint32_t) noexcept {}, .axis_stop = [](void *, wl_pointer *, uint32_t, uint32_t) noexcept {}, diff --git a/wayland/Window.hpp b/wayland/Window.hpp index d6f3ee2..6e174d3 100644 --- a/wayland/Window.hpp +++ b/wayland/Window.hpp @@ -1,5 +1,6 @@ #pragma once +#include "MouseWheelAxis.hpp" #include "WaylandPointer.hpp" #include "cursor/CursorBase.hpp" @@ -25,6 +26,7 @@ class Window { virtual void pointer_click(uint32_t button, wl_pointer_button_state state) noexcept = 0; virtual void pointer_motion(float x, float y) noexcept = 0; virtual void reconfigure() noexcept = 0; + virtual void scroll(MouseWheelAxis axis, float distance) noexcept = 0; virtual void text(const std::string& str) const noexcept = 0; void register_cursor(CursorBase *cursor);