R-Type  2
Doom but in better
Loading...
Searching...
No Matches
MouseInfo.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** MouseInfo.cpp
6*/
7
15
19GUI::ECS::Systems::MouseInfo::MouseInfo(const std::uint32_t entityId)
20 :EntityNode(entityId)
21{
22 _mousePosition.first = 0;
23 _mousePosition.second = 0;
24 _leftButtonClicked = false;
25 _rightButtonClicked = false;
26};
27
32
38void GUI::ECS::Systems::MouseInfo::update(const std::any &eventCapsule)
39{
40 if (!eventCapsule.has_value()) {
41 PRETTY_WARNING << "There is no event to process, skipping code." << std::endl;
42 return;
43 }
44 sf::Event event = std::any_cast<sf::Event>(eventCapsule);
45 if (event.is<sf::Event::MouseButtonPressed>()) {
46 PRETTY_INFO << "MouseInfo, mouse button pressed." << std::endl;
47 const sf::Event::MouseButtonPressed *node = event.getIf<sf::Event::MouseButtonPressed>();
48 if (node->button == sf::Mouse::Button::Left) {
49 PRETTY_SUCCESS << "MouseInfo: Left mouse button clicked." << std::endl;
50 _leftButtonClicked = true;
51 } else if (node->button == sf::Mouse::Button::Right) {
52 PRETTY_SUCCESS << "MouseInfo: Right mouse button clicked." << std::endl;
53 _rightButtonClicked = true;
54 } else if (node->button == sf::Mouse::Button::Middle) {
55 PRETTY_SUCCESS << "MouseInfo: Middle mouse button clicked." << std::endl;
56 _middleButtonClicked = true;
57 } else if (node->button == sf::Mouse::Button::Extra1) {
58 PRETTY_SUCCESS << "MouseInfo: Extra1 mouse button clicked." << std::endl;
59 _extra1ButtonClicked = true;
60 } else if (node->button == sf::Mouse::Button::Extra2) {
61 PRETTY_SUCCESS << "MouseInfo: Extra2 mouse button clicked." << std::endl;
62 _extra2ButtonClicked = true;
63 } else {
64 PRETTY_WARNING << "MouseInfo: Unknown mouse button clicked." << std::endl;
65 }
66 PRETTY_INFO << "MouseInfo, out of the button being pressed." << std::endl;
67 } else if (event.is<sf::Event::MouseButtonReleased>()) {
68 PRETTY_INFO << "MouseInfo, mouse button released." << std::endl;
69 const sf::Event::MouseButtonPressed *node = event.getIf<sf::Event::MouseButtonPressed>();
70 if (node->button == sf::Mouse::Button::Left) {
71 PRETTY_SUCCESS << "MouseInfo: Left mouse button released." << std::endl;
72 _leftButtonClicked = false;
73 } else if (node->button == sf::Mouse::Button::Right) {
74 PRETTY_SUCCESS << "MouseInfo: Right mouse button released." << std::endl;
75 _rightButtonClicked = false;
76 } else if (node->button == sf::Mouse::Button::Middle) {
77 PRETTY_SUCCESS << "MouseInfo: Middle mouse button released." << std::endl;
78 _middleButtonClicked = false;
79 } else if (node->button == sf::Mouse::Button::Extra1) {
80 PRETTY_SUCCESS << "MouseInfo: Extra1 mouse button released." << std::endl;
81 _extra1ButtonClicked = false;
82 } else if (node->button == sf::Mouse::Button::Extra2) {
83 PRETTY_SUCCESS << "MouseInfo: Extra2 mouse button released." << std::endl;
84 _extra2ButtonClicked = false;
85 } else {
86 PRETTY_WARNING << "MouseInfo: Unknown mouse button released." << std::endl;
87 }
88 PRETTY_INFO << "MouseInfo, out of the button being released." << std::endl;
89 } else if (event.is<sf::Event::MouseEntered>()) {
90 PRETTY_INFO << "MouseInfo: the mouse is in focus" << std::endl;
91 _mouseInFocus = true;
92 } else if (event.is<sf::Event::MouseLeft>()) {
93 PRETTY_INFO << "MouseInfo: the mouse is not in focus" << std::endl;
94 _mouseInFocus = false;
95 } else if (event.is<sf::Event::MouseMoved>()) {
96 const sf::Event::MouseMoved *data = event.getIf<sf::Event::MouseMoved>();
97 int posx = data->position.x;
98 int posy = data->position.y;
99 PRETTY_INFO << "MouseInfo: The mouse was moved, it's position is ("
100 << posx << ", " << posy << ")" << std::endl;
101 _mousePosition.first = posx;
102 _mousePosition.second = posy;
103 } else if (event.is<sf::Event::MouseWheelScrolled>()) {
104 const sf::Event::MouseWheelScrolled *data = event.getIf<sf::Event::MouseWheelScrolled>();
105 PRETTY_INFO << "MouseInfo: Update the position of the wheel." << std::endl;
106 _mouseWheel.delta = data->delta;
107 _mouseWheel.wheel = data->wheel;
108 _mouseWheel.position = data->position;
109 } else if (event.is<sf::Event::TouchMoved>()) {
110 const sf::Event::TouchMoved *data = event.getIf<sf::Event::TouchMoved>();
111 int posx = data->position.x;
112 int posy = data->position.y;
113 PRETTY_INFO << "MouseInfo: Touch position (" << posx << ", " << posy << ")" << std::endl;
114 _mousePosition.first = posx;
115 _mousePosition.second = posy;
116 } else if (event.is<sf::Event::TouchBegan>()) {
117 const sf::Event::TouchMoved *data = event.getIf<sf::Event::TouchMoved>();
118 int posx = data->position.x;
119 int posy = data->position.y;
120 PRETTY_INFO << "MouseInfo: Touch (translated as a left click) began at ("
121 << posx << ", " << posy << ")" << std::endl;
122 _leftButtonClicked = true;
123 _mousePosition.first = posx;
124 _mousePosition.second = posy;
125 } else if (event.is<sf::Event::TouchEnded>()) {
126 const sf::Event::TouchMoved *data = event.getIf<sf::Event::TouchMoved>();
127 int posx = data->position.x;
128 int posy = data->position.y;
129 PRETTY_INFO << "MouseInfo: Touch (translated as a left click) began at ("
130 << posx << ", " << posy << ")" << std::endl;
131 _leftButtonClicked = false;
132 _mousePosition.first = posx;
133 _mousePosition.second = posy;
134 } else {
135 PRETTY_WARNING << "MouseInfo: Unknown event type" << std::endl;
136 }
137};
138
145{
146 _mouseWheel.delta = entity.getScrollIndex();
147 _mouseWheel.position = { entity.getPositionX(), entity.getPositionY() };
149 _mouseWheel.wheel = sf::Mouse::Wheel::Vertical;
150 } else {
151 _mouseWheel.wheel = sf::Mouse::Wheel::Horizontal;
152 }
153 _mouseInFocus = entity.isMouseInFocus();
154 _mousePosition = entity.getMousePosition();
155 _leftButtonClicked = entity.isMouseLeftButtonClicked();
156 _middleButtonClicked = entity.isMouseMiddleButtonClicked();
157 _rightButtonClicked = entity.isMouseRightButtonClicked();
158 _extra1ButtonClicked = entity.isMouseExtra1ButtonClicked();
159 _extra2ButtonClicked = entity.isMouseExtra2ButtonClicked();
160};
161
167void GUI::ECS::Systems::MouseInfo::update(const std::pair<int, int> &mousePosition)
168{
169 _mousePosition = mousePosition;
170 PRETTY_SUCCESS << "MouseInfo: Mouse position updated" << std::endl;
171};
172
173
179const std::pair<int, int> GUI::ECS::Systems::MouseInfo::getMousePosition() const
180{
181 return _mousePosition;
182};
183
190{
191 return _mouseInFocus;
192};
193
200{
201 return _leftButtonClicked;
202};
203
210{
211 return _middleButtonClicked;
212};
213
220{
221 return _rightButtonClicked;
222};
223
230{
231 return _extra1ButtonClicked;
232};
233
240{
241 return _extra2ButtonClicked;
242};
243
251{
252 if (_mouseWheel.delta == 0) {
253 return false;
254 }
255 return true;
256}
257
265{
266 if (_mouseWheel.delta > 0 && getScrollDirection() == MouseWheel::Vertical) {
267 return true;
268 }
269 return false;
270}
271
279{
280 if (_mouseWheel.delta < 0 && getScrollDirection() == MouseWheel::Vertical) {
281 return true;
282 }
283 return false;
284}
285
293{
294 if (_mouseWheel.delta > 0 && getScrollDirection() == MouseWheel::Horizontal) {
295 return true;
296 }
297 return false;
298}
299
307{
308 if (_mouseWheel.delta < 0 && getScrollDirection() == MouseWheel::Horizontal) {
309 return true;
310 }
311 return false;
312}
313
320{
321 return _mouseWheel.delta;
322}
323
330{
331 if (_mouseWheel.wheel == sf::Mouse::Wheel::Vertical) {
333 } else {
335 }
336}
337
344{
345 return _mousePosition.first;
346};
347
354{
355 return _mousePosition.second;
356};
357
358
359const std::string GUI::ECS::Systems::MouseInfo::getInfo(const unsigned int indent) const
360{
361
362 std::string indentation = "";
363 for (unsigned int i = 0; i < indent; ++i) {
364 indentation += "\t";
365 }
366 std::string result = indentation + "Mouse Info:\n";
367 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
368 result += indentation + "- Mouse in focus: " + Recoded::myToString(_mouseInFocus) + "\n";
369 result += indentation + "- Mouse left button clicked: " + Recoded::myToString(_leftButtonClicked) + "\n";
370 result += indentation + "- Mouse middle button clicked: " + Recoded::myToString(_middleButtonClicked) + "\n";
371 result += indentation + "- Mouse right button clicked: " + Recoded::myToString(_rightButtonClicked) + "\n";
372 result += indentation + "- Mouse extra1 button clicked: " + Recoded::myToString(_extra1ButtonClicked) + "\n";
373 result += indentation + "- Mouse extra2 button clicked: " + Recoded::myToString(_extra2ButtonClicked) + "\n";
374 result += indentation + "- Mouse position: " + Recoded::myToString(_mousePosition) + "\n";
375 result += indentation + "- Mouse Wheel Scrolled: {\n";
376 result += indentation + "\t- wheel: ";
377 if (_mouseWheel.wheel == sf::Mouse::Wheel::Vertical) {
378 result += "Vertical";
379 } else {
380 result += "Horizontal";
381 }
382 result += "\n";
383 result += indentation + "\t- Delta: " + Recoded::myToString(_mouseWheel.delta) + "\n";
384 result += indentation + "\t- Position: " + Recoded::myToString(std::pair<int, int>({ _mouseWheel.position.x, _mouseWheel.position.y })) + "\n";
385 result += indentation + "}\n";
386 return result;
387}
388
389
394{
395 // _mousePosition.x = 0;
396 // _mousePosition.y = 0;
397 _leftButtonClicked = false;
398 _rightButtonClicked = false;
399 _middleButtonClicked = false;
400 _extra1ButtonClicked = false;
401 _extra2ButtonClicked = false;
402 // _mouseInFocus = false;
403 _mouseInFocus = true;
404 _mouseWheel.delta = 0;
405 _mouseWheel.position.x = 0;
406 _mouseWheel.position.y = 0;
407 _mouseWheel.wheel = {};
408};
409
417{
418 if (this != &copy) {
419 update(copy);
420 }
421 return *this;
422};
423
424std::ostream &GUI::ECS::Systems::operator<<(std::ostream &os, const GUI::ECS::Systems::MouseInfo &item)
425{
426 os << item.getInfo();
427 return os;
428}
#define PRETTY_INFO
Info log with details and colour.
#define PRETTY_WARNING
Warning log with details and colour.
#define PRETTY_SUCCESS
Success log with details and colour.
This is the header file containing the class for the mouse information.
~MouseInfo()
Destroys the MouseInfo object.
Definition MouseInfo.cpp:31
const bool isMouseWheelScrolled() const
Check if the mouse has scrolled.
const float getScrollIndex() const
Function in charge of returning the scroll index of the mouse.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
const bool isMouseExtra2ButtonClicked() const
Checks if the extra2 mouse button is pressed.
const MouseWheel getScrollDirection() const
Function in charge of returning the direction in which the user scrolled.
const int getPositionX() const
Retrieves the x-coordinate of the mouse position.
MouseInfo & operator=(const GUI::ECS::Systems::MouseInfo &copy)
Update the content of the class using the '=' sign overloader.
const std::pair< int, int > getMousePosition() const
Retrieves the current mouse position.
const bool isMouseWheelScrolledDown() const
Check if the mouse has scrolled downwards.
const bool isMouseExtra1ButtonClicked() const
Checks if the extra1 mouse button is pressed.
const bool isMouseWheelScrolledRight() const
Check if the mouse has scrolled to the right.
const int getPositionY() const
Retrieves the y-coordinate of the mouse position.
const bool isMouseWheelScrolledLeft() const
Check if the mouse has scrolled to the left.
void clear()
Resets the internal state of the MouseInfo object.
const bool isMouseLeftButtonClicked() const
Checks if the left mouse button is pressed.
const bool isMouseWheelScrolledUp() const
Check if the mouse has scrolled upwards.
void update(const std::any &event)
Processes and updates the internal state based on a given sf::Event.
Definition MouseInfo.cpp:38
const bool isMouseRightButtonClicked() const
Checks if the right mouse button is pressed.
const bool isMouseInFocus() const
Checks if the mouse is in the window.
const bool isMouseMiddleButtonClicked() const
Checks if the middle mouse button is pressed.
MouseInfo(const std::uint32_t entityId=0)
Constructs a new MouseInfo object with default values.
Definition MouseInfo.cpp:19
MouseWheel
An enum class to allow the user to know which direction the mouse has scrolled.
Definition MouseInfo.hpp:36
@ Vertical
The vertical mouse wheel.
@ Horizontal
The horizontal mouse wheel.
std::ostream & operator<<(std::ostream &os, const Clock &item)
Outputs the clock's info to a stream.
Definition Clock.cpp:73
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223