-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse_event.h
56 lines (49 loc) · 1.86 KB
/
mouse_event.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Created by piotr on 02.02.25.
//
#pragma once
#include "types.h"
#include <format>
#include <SDL3/SDL.h>
namespace Witcher {
enum class MouseButton {
Unknown,
Left,
Middle,
Right,
};
class MouseEvent {
SDL_MouseButtonEvent event_{};
MouseButton button_{};
public:
MouseEvent() = default;
explicit MouseEvent(SDL_MouseButtonEvent const& event) : event_(event) {
if (event_.button == SDL_BUTTON_LEFT)
button_ = MouseButton::Left;
else if (event_.button == SDL_BUTTON_RIGHT)
button_ = MouseButton::Right;
else if (event_.button == SDL_BUTTON_MIDDLE)
button_ = MouseButton::Middle;
else
button_ = MouseButton::Unknown;
}
[[nodiscard]] std::pair<f32, f32> pos() const noexcept { return {event_.x, event_.y}; }
[[nodiscard]] uint clicks() const noexcept { return event_.clicks; }
[[nodiscard]] u64 timestamp () const noexcept { return event_.timestamp / 1'000'000; }
[[nodiscard]] MouseButton button() const noexcept { return button_; }
static std::string button(MouseButton const button) noexcept {
if (button == MouseButton::Left) return "Left";
if (button == MouseButton::Middle) return "Middle";
if (button == MouseButton::Right) return "Right";
return "Unknown";
}
};
}
template <>
struct std::formatter<Witcher::MouseEvent> : std::formatter<std::string> {
auto format(Witcher::MouseEvent const& e, format_context& ctx) const {
auto const [x, y] = e.pos();
return formatter<string>::format(std::format("MouseEvent [pos: ({}, {}), button: {}, clicks: {}, timestamp: {}]",
x, y, Witcher::MouseEvent::button(e.button()), e.clicks(), e.timestamp()), ctx);
}
};