Brenta Engine 1.0
Loading...
Searching...
No Matches
engine_input.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "engine.hpp"
28
29using namespace Brenta;
30using namespace Brenta::Types;
31using namespace Brenta::Utils;
32
33std::unordered_map<int, std::function<void()>> Input::keyboardCallbacks;
34std::unordered_map<std::string, std::function<void(double, double)>>
35 Input::mouseCallbacks;
36
44
45void Input::AddKeyboardCallback(int key, std::function<void()> callback)
46{
47 Input::keyboardCallbacks[key] = callback;
48 INFO("Added callback for key: " + std::to_string(key));
49}
50
52{
53 if (Input::keyboardCallbacks.find(key) == Input::keyboardCallbacks.end())
54 {
55 ERROR("No callback found for key: ", key);
56 return;
57 }
58
59 Input::keyboardCallbacks.erase(key);
60 INFO("Removed callback for key: ", key);
61}
62
63void Input::KeyCallback(GLFWwindow *window, int key, int scancode, int action,
64 int mods)
65{
66 if (action == GLFW_PRESS)
67 {
68 if (Input::keyboardCallbacks.find(key)
69 != Input::keyboardCallbacks.end())
70 {
71 Input::keyboardCallbacks.at(key)();
72 }
73 }
74}
75
76void Input::AddMousePosCallback(MouseCallbackName callback_name,
77 std::function<void(double, double)> callback)
78{
79 Input::mouseCallbacks[callback_name] = callback;
80 INFO("Added callback for mouse: ", callback_name);
81}
82
83void Input::RemoveMousePosCallback(MouseCallbackName callback_name)
84{
85 if (Input::mouseCallbacks.find(callback_name)
86 == Input::mouseCallbacks.end())
87 {
88 ERROR("No callback found for mouse: ", callback_name);
89 return;
90 }
91
92 Input::mouseCallbacks.erase(callback_name);
93 INFO("Removed callback for mouse: ", callback_name);
94}
95
96void Input::MousePosCallback(GLFWwindow *window, double xpos, double ypos)
97{
98 for (auto &callback : Input::mouseCallbacks)
99 {
100 callback.second(xpos, ypos);
101 }
102}
static void AddKeyboardCallback(int key, std::function< void()> callback)
Add a keyboard callback.
static void MousePosCallback(GLFWwindow *window, double xpos, double ypos)
Mouse position callback.
static void RemoveKeyboardCallback(int key)
Remove a keyboard callback.
static void Init()
Initialize the input system.
static void RemoveMousePosCallback(Types::MouseCallbackName callback_name)
Remove a mouse position callback.
static void AddMousePosCallback(Types::MouseCallbackName name, std::function< void(double, double)> callback)
Add a mouse position callback.
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
Keyboard callback.
static void SetMousePosCallback(GLFWcursorposfun callback)
Set the mouse position callback.
Definition screen.cpp:176
static void SetKeyCallback(GLFWkeyfun callback)
Set the key callback.
Definition screen.cpp:171