ECSTASY
All in the name
Loading...
Searching...
No Matches
Mouse.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTASY_INTEGRATION_EVENT_INPUTS_MOUSE_HPP_
13#define ECSTASY_INTEGRATION_EVENT_INPUTS_MOUSE_HPP_
14
15#include <array>
16#include <iostream>
17#include <utility>
18
21
23{
30 class Mouse : public IResource {
31 public:
32 // LCOV_EXCL_START
33
35
36 // LCOV_EXCL_STOP
37#ifdef _DOXYGEN_
39 enum class Button {
40 Left,
41 Right,
42 Middle,
43 Extra1,
44 Extra2,
45 Extra3,
46
47 Count
48 };
49#endif
50
52 enum class Wheel {
54 Vertical,
55
56 Count
57 };
58
65 Mouse() noexcept : _buttons({false}), _x(0), _y(0){};
66
73 virtual ~Mouse() noexcept = default;
74
87 [[nodiscard]] constexpr bool isButtonDown(Button button) const
88 {
89 assertButtonValid(button);
90 return _buttons[static_cast<size_t>(button)];
91 }
92
105 [[nodiscard]] constexpr bool isButtonUp(Button button) const
106 {
107 return !isButtonDown(button);
108 }
109
123 constexpr void setButtonState(Button button, bool down)
124 {
125 assertButtonValid(button);
126 _buttons[static_cast<size_t>(button)] = down;
127 }
128
137 [[nodiscard]] constexpr int getX() const noexcept
138 {
139 return _x;
140 }
141
152 constexpr void setX(int x) noexcept
153 {
154 _x = x;
155 }
156
165 [[nodiscard]] constexpr int getY() const noexcept
166 {
167 return _y;
168 }
169
180 constexpr void setY(int y) noexcept
181 {
182 _y = y;
183 }
184
193 [[nodiscard]] constexpr std::pair<int, int> getPosition() noexcept
194 {
195 return std::make_pair(_x, _y);
196 }
197
209 constexpr void setPosition(int x, int y) noexcept
210 {
211 _x = x;
212 _y = y;
213 }
214
225 constexpr static void assertButtonValid(Button button)
226 {
227 if (static_cast<std::size_t>(button) >= static_cast<std::size_t>(Button::Count)) [[unlikely]]
228 throw std::invalid_argument("Invalid mouse button");
229 }
230
231 private:
232 // Mouse buttons state
234 // Mouse X position
235 int _x;
236 // Mouse Y position
237 int _y;
238 };
239} // namespace ecstasy::integration::event
240
241#endif /* !ECSTASY_INTEGRATION_EVENT_INPUTS_MOUSE_HPP_ */
Base class of all registry resources.
Create a serializable enumeration type.
#define SERIALIZABLE_ENUM(NAME, STARTING_AT,...)
Create a serializable enumeration type.
Base class of all registry resources.
Definition IResource.hpp:33
Current mouse state.
Definition Mouse.hpp:30
constexpr void setX(int x) noexcept
Update the mouse x position.
Definition Mouse.hpp:152
@ Horizontal
Horizontal mouse wheel (common)
constexpr void setY(int y) noexcept
Update the mouse y position.
Definition Mouse.hpp:180
constexpr std::pair< int, int > getPosition() noexcept
Get the mouse position on the window.
Definition Mouse.hpp:193
static constexpr void assertButtonValid(Button button)
Assert the validity of a mouse button.
Definition Mouse.hpp:225
constexpr void setButtonState(Button button, bool down)
Update a given button state.
Definition Mouse.hpp:123
std::array< bool, static_cast< int >(Button::Count)> _buttons
Definition Mouse.hpp:233
virtual ~Mouse() noexcept=default
Destroy the Mouse resource.
constexpr bool isButtonUp(Button button) const
Check whether a mouse button is up.
Definition Mouse.hpp:105
constexpr int getY() const noexcept
Get the y position of the mouse on the window.
Definition Mouse.hpp:165
Mouse() noexcept
Construct a new Mouse resource.
Definition Mouse.hpp:65
constexpr bool isButtonDown(Button button) const
Check whether a mouse button is down.
Definition Mouse.hpp:87
constexpr void setPosition(int x, int y) noexcept
Set the mouse position.
Definition Mouse.hpp:209
@ Extra1
The first extra mouse button.
@ Extra2
The second extra mouse button.
@ Middle
The middle (wheel) mouse button.
@ Extra3
The third extra mouse button.
@ Count
Keep last – the total number of mouse buttons.
constexpr int getX() const noexcept
Get the X position of the mouse on the window.
Definition Mouse.hpp:137
T make_pair(T... args)
Event integration.
Definition Event.hpp:25