Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
event_handler.h
1#ifndef OE_CORE_EVENT_HANDLER_H
2#define OE_CORE_EVENT_HANDLER_H
3
4#include <glm/glm.hpp>
5#include <unordered_map>
6
7#include "datatypes.h"
8#include "event_type.h"
9
10namespace oe::core
11{
18 {
19 public:
20
29 void addEvent(const EventType type, const EventData& data) noexcept;
30
36 void processEvents(const bool clean_processed = true) noexcept;
37
43 const EventList& getProcessedEvents() const noexcept
44 {
45 return _events;
46 }
47
51 bool isKeyPressed(const key_code_t& key, const bool& strict = false) const noexcept;
52
56 bool hasKeyChanged(const key_code_t& key) const noexcept;
57
61 bool isKeyJustPressed(const key_code_t& key) const noexcept
62 {
63 return isKeyPressed(key) && hasKeyChanged(key);
64 }
65
69 bool isClicked(const key_code_t& key, const bool& strict = false) const noexcept;
70
74 bool isHold(const key_code_t& key) const noexcept;
75
79 void resetAllStates() noexcept;
80
84 bool isEscapePressed() const noexcept;
85
89 bool isCtrlPressed() const noexcept;
90
94 bool isAltPressed() const noexcept;
95
99 bool isShiftPressed() const noexcept;
100
104 bool isSpacePressed() const noexcept;
105
111 bool isSuperPressed() const noexcept;
112
113 const glm::vec2& getMouseScroll() const noexcept
114 {
115 return _mouse_scroll;
116 }
117
118 void setMouseScroll(const glm::vec2& scroll) noexcept
119 {
120 _mouse_scroll = scroll;
121 }
122
123 const glm::vec2& getCursorPos() const noexcept
124 {
125 return _cursor_pos;
126 }
127
128 const glm::vec2& getCursorOffset() const noexcept
129 {
130 return _cursor_offset;
131 }
132
136 void forceState(const key_code_t& key, const int& state, const bool& isMouse = false) noexcept;
137
144 void setCursorPos(const glm::vec2& cursor_pos) noexcept
145 {
146 _last_cursor_pos = cursor_pos;
147 _cursor_pos = cursor_pos;
148 _cursor_offset = {};
149 }
150
151 private:
152 EventList _events;
153 EventList _raw_events;
154
155 std::unordered_map<key_code_t, int> _key_status;
156 std::unordered_map<key_code_t, int> _real_key_status;
157
158 std::unordered_map<key_code_t, bool> _switched_key_status;
159
160 std::unordered_map<key_code_t, int> _mouse_buttons_status;
161 std::unordered_map<key_code_t, int> _real_mouse_buttons_status;
162
163 glm::vec2 _mouse_scroll = glm::vec2(0.0);
164 glm::vec2 _cursor_pos = glm::vec2(0.0);
165
166 glm::vec2 _last_cursor_pos = glm::vec2(0.0);
167 glm::vec2 _cursor_offset = glm::vec2(0.0);
168 bool _first_cursor_move = true;
169 };
170}
171
172#endif
Event handler.
Definition event_handler.h:18
void setCursorPos(const glm::vec2 &cursor_pos) noexcept
Definition event_handler.h:144
bool isShiftPressed() const noexcept
Check if any of SHIFT keys are pressed.
bool isCtrlPressed() const noexcept
Check if any of CTRL keys are pressed.
bool isSuperPressed() const noexcept
Check if the super key is pressed.
bool hasKeyChanged(const key_code_t &key) const noexcept
Check if a keyboard key state changed this frame.
bool isClicked(const key_code_t &key, const bool &strict=false) const noexcept
Check if mouse button is clicked.
void forceState(const key_code_t &key, const int &state, const bool &isMouse=false) noexcept
Force key or mouse button status, ignoring the real status.
bool isEscapePressed() const noexcept
Check if the ESC key is pressed.
bool isKeyJustPressed(const key_code_t &key) const noexcept
Check if a keyboard was just pressed this frame.
Definition event_handler.h:61
void resetAllStates() noexcept
Force all keys / buttons as released.
void addEvent(const EventType type, const EventData &data) noexcept
Add a new event to be processed.
bool isKeyPressed(const key_code_t &key, const bool &strict=false) const noexcept
Check if a keyboard key is pressed.
void processEvents(const bool clean_processed=true) noexcept
Compute EventHandler state from unprocessed events + make the event list available in getProcessedEve...
bool isSpacePressed() const noexcept
Check if the space bar is pressed.
bool isHold(const key_code_t &key) const noexcept
Check if key or mouse button is hold.
const EventList & getProcessedEvents() const noexcept
Get a list of events that happened during the frame.
Definition event_handler.h:43
bool isAltPressed() const noexcept
Check if any of ALT keys are pressed.
Core functionality (windows, event handler, logger, ...)
Definition args.h:10
EventType
Definition event_type.h:10
Definition event_type.h:205