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 "../common.h"
5#include <glm/glm.hpp>
6#include <string>
7#include <vector>
8#include <unordered_map>
9#include <functional>
10
11#include "datatypes.h"
12#include "event_type.h"
13
14namespace oe::core
15{
22 {
23 public:
24
33 void addEvent(const EventType type, const EventData& data) noexcept;
34
40 void processEvents(const bool clean_processed = true) noexcept;
41
47 const EventList& getProcessedEvents() const noexcept
48 {
49 return _events;
50 }
51
55 bool isKeyPressed(const key_code_t& key, const bool& strict = false) const noexcept;
56
60 bool hasKeyChanged(const key_code_t& key) const noexcept;
61
65 bool isKeyJustPressed(const key_code_t& key) const noexcept
66 {
67 return isKeyPressed(key) && hasKeyChanged(key);
68 }
69
73 bool isClicked(const key_code_t& key, const bool& strict = false) const noexcept;
74
78 bool isHold(const key_code_t& key) const noexcept;
79
83 void resetAllStates() noexcept;
84
88 bool isEscapePressed() const noexcept;
89
93 bool isCtrlPressed() const noexcept;
94
98 bool isAltPressed() const noexcept;
99
103 bool isShiftPressed() const noexcept;
104
108 bool isSpacePressed() const noexcept;
109
115 bool isSuperPressed() const noexcept;
116
117 const glm::vec2& getMouseScroll() const noexcept
118 {
119 return _mouse_scroll;
120 }
121
122 void setMouseScroll(const glm::vec2& scroll) noexcept
123 {
124 _mouse_scroll = scroll;
125 }
126
127 const glm::vec2& getCursorPos() const noexcept
128 {
129 return _cursor_pos;
130 }
131
132 const glm::vec2& getCursorOffset() const noexcept
133 {
134 return _cursor_offset;
135 }
136
140 void forceState(const key_code_t& key, const int& state, const bool& isMouse = false) noexcept;
141
148 void setCursorPos(const glm::vec2& cursor_pos) noexcept
149 {
150 _last_cursor_pos = cursor_pos;
151 _cursor_pos = cursor_pos;
152 _cursor_offset = {};
153 }
154
155 private:
156 EventList _events;
157 EventList _raw_events;
158
159 std::unordered_map<key_code_t, int> _key_status;
160 std::unordered_map<key_code_t, int> _real_key_status;
161
162 std::unordered_map<key_code_t, bool> _switched_key_status;
163
164 std::unordered_map<key_code_t, int> _mouse_buttons_status;
165 std::unordered_map<key_code_t, int> _real_mouse_buttons_status;
166
167 glm::vec2 _mouse_scroll = glm::vec2(0.0);
168 glm::vec2 _cursor_pos = glm::vec2(0.0);
169
170 glm::vec2 _last_cursor_pos = glm::vec2(0.0);
171 glm::vec2 _cursor_offset = glm::vec2(0.0);
172 bool _first_cursor_move = true;
173 };
174}
175
176#endif
Event handler.
Definition event_handler.h:22
void setCursorPos(const glm::vec2 &cursor_pos) noexcept
Definition event_handler.h:148
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:65
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:47
bool isAltPressed() const noexcept
Check if any of ALT keys are pressed.
Core functionality (windows, event handler, logger, ...)
Definition cursor.h:8
EventType
Definition event_type.h:9
Definition event_type.h:201