R-Type  2
Doom but in better
Loading...
Searching...
No Matches
EventManager.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** EventManager.cpp
6*/
7
14#include <iostream>
16
18 :EntityNode(entityId), _mouse(entityId), _mapper(entityId)
19{
20};
21
23
25{
26 return _mouse.getPositionX();
27}
28
30{
31 return _mouse.getPositionY();
32}
33
35{
36 return _mouse.getMousePosition();
37}
38
43
45{
46 processEvents(window, screen);
47}
48
50{
51 _mouse.update(mouse);
52}
53
55{
56 _mouse.update(copy.getMouseInfo());
57 _keys = copy.getKeys();
58}
59
60const std::vector<GUI::ECS::Systems::Key> GUI::ECS::Systems::EventManager::getKeys() const
61{
62 return _keys;
63}
64
65const std::string GUI::ECS::Systems::EventManager::getInfo(const unsigned int indent) const
66{
67
68 std::string indentation = "";
69 for (unsigned int i = 0; i < indent; ++i) {
70 indentation += "\t";
71 }
72 std::string result = indentation + "Event Manager:\n";
73 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
74 result += indentation + "- ResetDelay: " + Recoded::myToString(_resetDelay) + "\n";
75 result += indentation + "- Reset Index: " + Recoded::myToString(_resetIndex) + "\n";
76 result += indentation + "- Mouse info: {\n" + _mouse.getInfo(indent + 1) + indentation + "}\n";
77 result += indentation + "- Key Mapper: {\n" + _mapper.getInfo(indent + 1) + indentation + "}\n";
78 result += indentation + "- keys: {\n";
79 for (unsigned int i = 0; i < _keys.size(); i++) {
80 result += indentation + "\t" + Recoded::myToString(i) + ": " + _mapper.stringKey(_keys[i]) + "\n";
81 }
82 result += indentation + "}\n";
83 return result;
84}
85
86
88{
89 return _mouse.isMouseInFocus();
90}
91
93{
94 return _mouse.isMouseLeftButtonClicked();
95}
96
98{
99 return _mouse.isMouseRightButtonClicked();
100}
101
103{
104 for (const auto &keyNodes : _keys) {
105 if (keyNodes == key) {
106 return true;
107 }
108 }
109 return false;
110}
111
113{
114 if (_resetIndex >= _resetDelay) {
115 _keys.clear();
116 _mouse.clear();
117 _resetIndex = 0;
118 } else {
119 _resetIndex++;
120 }
121}
122
123
125{
126 PRETTY_DEBUG << "Setting the counter to the reset delay value" << std::endl;
127 _resetIndex = _resetDelay;
128 PRETTY_DEBUG << "Calling the clearEvents function" << std::endl;
129 clearEvents();
130 PRETTY_DEBUG << "The events have been flushed" << std::endl;
131}
132
134{
135 int counter = 0;
136 clearEvents();
137 std::any eventCapsule = windowItem.pollEvent();
138 while (eventCapsule.has_value()) {
139 sf::Event event = std::any_cast<sf::Event>(eventCapsule);
140 if (event.is<sf::Event::Closed>()) {
141 PRETTY_INFO << "The window's cross has been clicked." << std::endl;
142 windowItem.close();
143 } else if (const auto *keyPressed = event.getIf<sf::Event::KeyPressed>()) {
144 sf::Keyboard::Scancode code = keyPressed->scancode;
145 PRETTY_DEBUG << "A key was pressed, it's code is: '" << _mapper.stringKey(code) << "'." << std::endl;
146 if (code == sf::Keyboard::Scancode::Escape) {
147 PRETTY_INFO << "The escape key was pressed." << std::endl;
148 if (currentScreen == ActiveScreen::MENU) {
149 windowItem.close();
150 } else {
151 _keys.push_back(_mapper.mapKey(code));
152 }
153 } else {
154 _keys.push_back(_mapper.mapKey(code));
155 }
156 } else if (
157 event.is<sf::Event::MouseButtonPressed>() || event.is<sf::Event::MouseMoved>() ||
158 event.is<sf::Event::MouseWheelScrolled>() || event.is<sf::Event::TouchBegan>() ||
159 event.is<sf::Event::TouchEnded>() || event.is<sf::Event::TouchMoved>()
160 ) {
161 PRETTY_DEBUG << "Begin processing the mouse Event." << std::endl;
162 _mouse.update(eventCapsule);
163 PRETTY_DEBUG << "End processing the mouse Event." << std::endl;
164 } else {
165 counter += 1;
166 // PRETTY_WARNING << "Event type not supported by this program." << std::endl;
167 }
168 eventCapsule = windowItem.pollEvent();
169 }
170}
171
172
174{
175 if (this != &copy) {
176 update(copy);
177 }
178 return *this;
179};
180
181std::ostream &GUI::ECS::Systems::operator<<(std::ostream &os, const GUI::ECS::Systems::EventManager &item)
182{
183 os << item.getInfo();
184 return os;
185}
ActiveScreen
This is the enum class in charge of the window switcher code. This enum allows the mainloop of the pr...
Header file for the EventManager class, responsible for managing input events.
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
Manages input events such as mouse movements, key presses, and window interactions.
const bool isLeftButtonClicked() const
Checks if the left mouse button is clicked.
const std::pair< int, int > getMousePosition() const
Retrieves the current position of the mouse as a pair (x, y).
const bool isRightButtonClicked() const
Checks if the right mouse button is clicked.
const bool isKeyPressed(const GUI::ECS::Systems::Key &key) const
Checks if a specific key is currently pressed.
EventManager & operator=(const GUI::ECS::Systems::EventManager &copy)
Assignment operator for the EventManager.
void clearEvents()
Clears all currently stored events if the counter has reached the delay, otherwise,...
const float getPositionY() const
Retrieves the current Y position of the mouse.
const std::vector< GUI::ECS::Systems::Key > getKeys() const
Retrieves all currently pressed keys.
const bool isMouseInFocus() const
Checks if the mouse is in focus within the window.
const GUI::ECS::Systems::MouseInfo getMouseInfo() const
Retrieves the current state of the mouse.
const float getPositionX() const
Retrieves the current X position of the mouse.
void flushEvents()
Forcefully clear all the stored events.
~EventManager()
Destroys the EventManager instance.
EventManager(const std::uint32_t entityId=0)
Constructs an EventManager with an optional entity ID.
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 ...
void update(GUI::ECS::Systems::Window &window, const ActiveScreen &screen=ActiveScreen::UNKNOWN)
Updates the EventManager with events from a window.
void processEvents(GUI::ECS::Systems::Window &window, const ActiveScreen &currentScreen=ActiveScreen::UNKNOWN)
Processes events from the specified window.
Manages an SFML-based graphical window and handles rendering of ECS components.
Definition Window.hpp:67
void close()
Closes the window.
Definition Window.cpp:90
std::any pollEvent()
Polls for the next event in the window's event queue.
Definition Window.cpp:95
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