Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
InputManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <GLFW/glfw3.h>
4#include <glm/vec2.hpp>
5
6#include <functional>
7#include <vector>
8
10#include "FunctionContainer.hpp"
11#include "core/Core.hpp"
12#include "resource/Window.hpp"
13
14namespace Input::Resource {
19 public:
20 explicit InputManager(Engine::Core &core) : _core(core)
21 {
22 _keyCallbacks = std::make_shared<KeyCallbackContainer>();
23 _charCallbacks = std::make_shared<CharCallbackContainer>();
24 _charModsCallbacks = std::make_shared<CharModsCallbackContainer>();
25 _mouseButtonCallbacks = std::make_shared<MouseButtonCallbackContainer>();
26 _cursorPosCallbacks = std::make_shared<CursorPosCallbackContainer>();
27 _cursorEnterCallbacks = std::make_shared<CursorEnterCallbackContainer>();
28 _scrollCallbacks = std::make_shared<ScrollCallbackContainer>();
29 _dropCallbacks = std::make_shared<DropCallbackContainer>();
30 }
31 ~InputManager() = default;
32
40 template <typename TCallable> inline FunctionUtils::FunctionID RegisterKeyCallback(TCallable callback)
41 {
43 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, int, int, int, int>> keyCallback =
44 std::make_unique<KeyCallback>(callback);
45
46 return _keyCallbacks->AddFunction(std::move(keyCallback));
47 }
48
56 template <typename TCallable> inline FunctionUtils::FunctionID RegisterCharCallback(TCallable callback)
57 {
59 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, unsigned int>> charCallback =
60 std::make_unique<CharCallback>(callback);
61
62 return _charCallbacks->AddFunction(std::move(charCallback));
63 }
64
72 template <typename TCallable> inline FunctionUtils::FunctionID RegisterCharModsCallback(TCallable callback)
73 {
75 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, unsigned int, int>> charModsCallback =
76 std::make_unique<CharModsCallback>(callback);
77
78 return _charModsCallbacks->AddFunction(std::move(charModsCallback));
79 }
80
88 template <typename TCallable> inline FunctionUtils::FunctionID RegisterMouseButtonCallback(TCallable callback)
89 {
91 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, int, int, int>> mouseButtonCallback =
92 std::make_unique<MouseButtonCallback>(callback);
93
94 return _mouseButtonCallbacks->AddFunction(std::move(mouseButtonCallback));
95 }
96
104 template <typename TCallable> inline FunctionUtils::FunctionID RegisterCursorPosCallback(TCallable callback)
105 {
107 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, double, double>> cursorPosCallback =
108 std::make_unique<CursorPosCallback>(callback);
109
110 return _cursorPosCallbacks->AddFunction(std::move(cursorPosCallback));
111 }
112
120 template <typename TCallable> inline FunctionUtils::FunctionID RegisterCursorEnterCallback(TCallable callback)
121 {
123 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, int>> cursorEnterCallback =
124 std::make_unique<CursorEnterCallback>(callback);
125
126 return _cursorEnterCallbacks->AddFunction(std::move(cursorEnterCallback));
127 }
128
136 template <typename TCallable> inline FunctionUtils::FunctionID RegisterScrollCallback(TCallable callback)
137 {
139 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, double, double>> scrollCallback =
140 std::make_unique<ScrollCallback>(callback);
141
142 return _scrollCallbacks->AddFunction(std::move(scrollCallback));
143 }
144
152 template <typename TCallable> inline FunctionUtils::FunctionID RegisterDropCallback(TCallable callback)
153 {
155 std::unique_ptr<FunctionUtils::BaseFunction<void, Engine::Core &, int, const char **>> dropCallback =
156 std::make_unique<DropCallback>(callback);
157
158 return _dropCallbacks->AddFunction(std::move(dropCallback));
159 }
160
172 inline void CallKeyCallbacks(Engine::Core &core, int key, int scancode, int action, int mods)
173 {
174 for (auto &callback : _keyCallbacks->GetFunctions())
175 {
176 callback->Call(core, key, scancode, action, mods);
177 }
178 }
179
188 inline void CallCharCallbacks(Engine::Core &core, unsigned int codepoint)
189 {
190 for (auto &callback : _charCallbacks->GetFunctions())
191 {
192 callback->Call(core, codepoint);
193 }
194 }
195
205 inline void CallCharModsCallbacks(Engine::Core &core, unsigned int codepoint, int mods) const
206 {
207 for (auto &callback : _charModsCallbacks->GetFunctions())
208 {
209 callback->Call(core, codepoint, mods);
210 }
211 }
212
223 inline void CallMouseButtonCallbacks(Engine::Core &core, int button, int action, int mods) const
224 {
225 for (auto &callback : _mouseButtonCallbacks->GetFunctions())
226 {
227 callback->Call(core, button, action, mods);
228 }
229 }
230
240 inline void CallCursorPosCallbacks(Engine::Core &core, double xpos, double ypos) const
241 {
242 for (auto &callback : _cursorPosCallbacks->GetFunctions())
243 {
244 callback->Call(core, xpos, ypos);
245 }
246 }
247
256 inline void CallCursorEnterCallbacks(Engine::Core &core, int entered) const
257 {
258 for (auto &callback : _cursorEnterCallbacks->GetFunctions())
259 {
260 callback->Call(core, entered);
261 }
262 }
263
273 inline void CallScrollCallbacks(Engine::Core &core, double xoffset, double yoffset) const
274 {
275 for (auto &callback : _scrollCallbacks->GetFunctions())
276 {
277 callback->Call(core, xoffset, yoffset);
278 }
279 }
280
290 inline void CallDropCallbacks(Engine::Core &core, int count, const char **paths) const
291 {
292 for (auto &callback : _dropCallbacks->GetFunctions())
293 {
294 callback->Call(core, count, paths);
295 }
296 }
297
304 inline bool DeleteKeyCallback(FunctionUtils::FunctionID id) { return _keyCallbacks->DeleteFunction(id) != nullptr; }
305
313 {
314 return _charCallbacks->DeleteFunction(id) != nullptr;
315 }
316
324 {
325 return _charModsCallbacks->DeleteFunction(id) != nullptr;
326 }
327
335 {
336 return _mouseButtonCallbacks->DeleteFunction(id) != nullptr;
337 }
338
346 {
347 return _cursorPosCallbacks->DeleteFunction(id) != nullptr;
348 }
349
357 {
358 return _cursorEnterCallbacks->DeleteFunction(id) != nullptr;
359 }
360
368 {
369 return _scrollCallbacks->DeleteFunction(id) != nullptr;
370 }
371
379 {
380 return _dropCallbacks->DeleteFunction(id) != nullptr;
381 }
382
383 inline bool IsKeyPressed(int key) noexcept
384 {
385 return glfwGetKey(_core.GetResource<Window::Resource::Window>().GetGLFWWindow(), key) == GLFW_PRESS;
386 }
387
388 private:
391 std::shared_ptr<KeyCallbackContainer> _keyCallbacks;
393 std::shared_ptr<CharCallbackContainer> _charCallbacks;
395 std::shared_ptr<CharModsCallbackContainer> _charModsCallbacks;
397 std::shared_ptr<MouseButtonCallbackContainer> _mouseButtonCallbacks;
399 std::shared_ptr<CursorPosCallbackContainer> _cursorPosCallbacks;
401 std::shared_ptr<CursorEnterCallbackContainer> _cursorEnterCallbacks;
403 std::shared_ptr<ScrollCallbackContainer> _scrollCallbacks;
405 std::shared_ptr<DropCallbackContainer> _dropCallbacks;
406};
407} // namespace Input::Resource
static void DropCallback(GLFWwindow *window, int count, const char **paths)
Definition BindCallbacksToGLFW.cpp:69
static void CursorPosCallback(GLFWwindow *window, double xpos, double ypos)
Definition BindCallbacksToGLFW.cpp:48
static void MouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
Definition BindCallbacksToGLFW.cpp:41
static void ScrollCallback(GLFWwindow *window, double xoffset, double yoffset)
Definition BindCallbacksToGLFW.cpp:62
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition BindCallbacksToGLFW.cpp:20
static void CharModsCallback(GLFWwindow *window, unsigned int codepoint, int mods)
Definition BindCallbacksToGLFW.cpp:34
static void CharCallback(GLFWwindow *window, unsigned int codepoint)
Definition BindCallbacksToGLFW.cpp:27
static void CursorEnterCallback(GLFWwindow *window, int entered)
Definition BindCallbacksToGLFW.cpp:55
static int count
Definition SystemSetTest.cpp:7
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
Abstract class that holds a callable object, to be later stored in a container.
Definition CallableFunction.hpp:13
Container for functions, allowing for dynamic storage and invocation.
Definition FunctionContainer.hpp:14
void CallCharModsCallbacks(Engine::Core &core, unsigned int codepoint, int mods) const
Call the char mods callbacks.
Definition InputManager.hpp:205
FunctionUtils::FunctionID RegisterCursorPosCallback(TCallable callback)
Register a cursor position callback.
Definition InputManager.hpp:104
std::shared_ptr< CharCallbackContainer > _charCallbacks
Definition InputManager.hpp:393
FunctionUtils::FunctionID RegisterCharCallback(TCallable callback)
Register a char callback.
Definition InputManager.hpp:56
FunctionUtils::FunctionContainer< void, Engine::Core &, int, const char ** > DropCallbackContainer
Definition InputManager.hpp:404
bool IsKeyPressed(int key) noexcept
Definition InputManager.hpp:383
FunctionUtils::FunctionID RegisterScrollCallback(TCallable callback)
Register a scroll callback.
Definition InputManager.hpp:136
void CallCharCallbacks(Engine::Core &core, unsigned int codepoint)
Call the char callbacks.
Definition InputManager.hpp:188
void CallScrollCallbacks(Engine::Core &core, double xoffset, double yoffset) const
Call the scroll callbacks.
Definition InputManager.hpp:273
std::shared_ptr< DropCallbackContainer > _dropCallbacks
Definition InputManager.hpp:405
FunctionUtils::FunctionContainer< void, Engine::Core &, double, double > CursorPosCallbackContainer
Definition InputManager.hpp:398
void CallDropCallbacks(Engine::Core &core, int count, const char **paths) const
Call the drop callbacks.
Definition InputManager.hpp:290
void CallKeyCallbacks(Engine::Core &core, int key, int scancode, int action, int mods)
Call the key callbacks.
Definition InputManager.hpp:172
Engine::Core & _core
Definition InputManager.hpp:389
bool DeleteCursorEnterCallback(FunctionUtils::FunctionID id)
Delete a cursor enter callback.
Definition InputManager.hpp:356
FunctionUtils::FunctionContainer< void, Engine::Core &, int > CursorEnterCallbackContainer
Definition InputManager.hpp:400
void CallCursorEnterCallbacks(Engine::Core &core, int entered) const
Call the cursor enter callbacks.
Definition InputManager.hpp:256
void CallMouseButtonCallbacks(Engine::Core &core, int button, int action, int mods) const
Call the mouse button callbacks.
Definition InputManager.hpp:223
bool DeleteCharModsCallback(FunctionUtils::FunctionID id)
Delete a char mods callback.
Definition InputManager.hpp:323
FunctionUtils::FunctionContainer< void, Engine::Core &, unsigned int, int > CharModsCallbackContainer
Definition InputManager.hpp:394
FunctionUtils::FunctionContainer< void, Engine::Core &, unsigned int > CharCallbackContainer
Definition InputManager.hpp:392
FunctionUtils::FunctionID RegisterCursorEnterCallback(TCallable callback)
Register a cursor enter callback.
Definition InputManager.hpp:120
FunctionUtils::FunctionID RegisterMouseButtonCallback(TCallable callback)
Register a mouse button callback.
Definition InputManager.hpp:88
std::shared_ptr< KeyCallbackContainer > _keyCallbacks
Definition InputManager.hpp:391
std::shared_ptr< CharModsCallbackContainer > _charModsCallbacks
Definition InputManager.hpp:395
FunctionUtils::FunctionID RegisterDropCallback(TCallable callback)
Register a drop callback.
Definition InputManager.hpp:152
std::shared_ptr< CursorPosCallbackContainer > _cursorPosCallbacks
Definition InputManager.hpp:399
bool DeleteCharCallback(FunctionUtils::FunctionID id)
Delete a char callback.
Definition InputManager.hpp:312
bool DeleteScrollCallback(FunctionUtils::FunctionID id)
Delete a scroll callback.
Definition InputManager.hpp:367
FunctionUtils::FunctionID RegisterKeyCallback(TCallable callback)
Register a key callback.
Definition InputManager.hpp:40
bool DeleteKeyCallback(FunctionUtils::FunctionID id)
Delete a key callback.
Definition InputManager.hpp:304
FunctionUtils::FunctionContainer< void, Engine::Core &, int, int, int, int > KeyCallbackContainer
Definition InputManager.hpp:390
std::shared_ptr< ScrollCallbackContainer > _scrollCallbacks
Definition InputManager.hpp:403
bool DeleteCursorPosCallback(FunctionUtils::FunctionID id)
Delete a cursor position callback.
Definition InputManager.hpp:345
FunctionUtils::FunctionID RegisterCharModsCallback(TCallable callback)
Register a char mods callback.
Definition InputManager.hpp:72
std::shared_ptr< CursorEnterCallbackContainer > _cursorEnterCallbacks
Definition InputManager.hpp:401
bool DeleteDropCallback(FunctionUtils::FunctionID id)
Delete a drop callback.
Definition InputManager.hpp:378
FunctionUtils::FunctionContainer< void, Engine::Core &, double, double > ScrollCallbackContainer
Definition InputManager.hpp:402
FunctionUtils::FunctionContainer< void, Engine::Core &, int, int, int > MouseButtonCallbackContainer
Definition InputManager.hpp:396
bool DeleteMouseButtonCallback(FunctionUtils::FunctionID id)
Delete a mouse button callback.
Definition InputManager.hpp:334
void CallCursorPosCallbacks(Engine::Core &core, double xpos, double ypos) const
Call the cursor position callbacks.
Definition InputManager.hpp:240
InputManager(Engine::Core &core)
Definition InputManager.hpp:20
std::shared_ptr< MouseButtonCallbackContainer > _mouseButtonCallbacks
Definition InputManager.hpp:397
Definition Window.hpp:25
GLFWwindow * GetGLFWWindow() const
Get a pointer to the GLFW window.
Definition Window.hpp:59
std::size_t FunctionID
FunctionID class to represent a unique identifier for functions.
Definition FunctionID.hpp:9
Definition InputManager.hpp:14