Skip to content

Commit

Permalink
Support mouse wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
HildarTheDorf committed Nov 27, 2024
1 parent d7bec4e commit 3119a44
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
13 changes: 13 additions & 0 deletions MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
1 change: 1 addition & 0 deletions MainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions wayland/MouseWheelAxis.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

enum class MouseWheelAxis {
Vertical,
Horizontal
};
14 changes: 13 additions & 1 deletion wayland/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ Pointer::Pointer(Seat& seat)
self._focus->pointer_click(button, static_cast<wl_pointer_button_state>(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<Pointer *>(data);
if (self._focus) {
const auto distance = static_cast<float>(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 {},
Expand Down
2 changes: 2 additions & 0 deletions wayland/Window.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "MouseWheelAxis.hpp"
#include "WaylandPointer.hpp"
#include "cursor/CursorBase.hpp"

Expand All @@ -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);
Expand Down

0 comments on commit 3119a44

Please sign in to comment.