lxgui
Loading...
Searching...
No Matches
gui_key_binder.hpp
1#ifndef LXGUI_GUI_KEY_BINDER_HPP
2#define LXGUI_GUI_KEY_BINDER_HPP
3
4#include "lxgui/input_keys.hpp"
5#include "lxgui/lxgui.hpp"
6#include "lxgui/utils_signal.hpp"
7
8#include <lxgui/extern_sol2_protected_function.hpp>
9#include <string>
10#include <vector>
11
12namespace lxgui::gui {
13
14class event_emitter;
15
18 using signal_type = utils::signal<void()>;
19
20public:
23
25 key_binder() = default;
26
27 // Non-copiable, non-movable.
28 key_binder(const key_binder&) = delete;
30 key_binder& operator=(const key_binder&) = delete;
32
44 register_key_binding(std::string_view name, sol::protected_function lua_function);
45
56 utils::connection register_key_binding(std::string_view name, function_type function);
57
67 void set_key_binding(std::string_view name, std::string_view key_name);
68
77 void set_key_binding(
78 std::string_view name,
79 input::key key_id,
80 bool shift_is_pressed,
81 bool ctrl_is_pressed,
82 bool alt_is_pressed);
83
88 void remove_key_binding(std::string_view name);
89
99 bool on_key_down(
100 input::key key_id, bool shift_is_pressed, bool ctrl_is_pressed, bool alt_is_pressed);
101
102private:
103 struct key_binding {
104 std::string name;
105
107 bool shift_is_pressed = false;
108 bool ctrl_is_pressed = false;
109 bool alt_is_pressed = false;
110
111 signal_type signal;
112 };
113
114 key_binding* find_binding_(
115 input::key key_id, bool shift_is_pressed, bool ctrl_is_pressed, bool alt_is_pressed);
116
117 std::vector<key_binding> key_bindings_;
118};
119
120} // namespace lxgui::gui
121
122#endif
Binds global actions to key presses.
key_binder & operator=(const key_binder &)=delete
void set_key_binding(std::string_view name, std::string_view key_name)
Binds an action to a key.
key_binder(const key_binder &)=delete
key_binder & operator=(key_binder &&)=delete
signal_type::function_type function_type
Type of a keybinding callback.
utils::connection register_key_binding(std::string_view name, sol::protected_function lua_function)
Registers an action as a possible key binding.
void remove_key_binding(std::string_view name)
Unbinds an action.
key_binder()=default
Constructor.
bool on_key_down(input::key key_id, bool shift_is_pressed, bool ctrl_is_pressed, bool alt_is_pressed)
Called when a key is pressed.
key_binder(key_binder &&)=delete
Object representing the connection between a slot and a signal.
std::function< void() > function_type
Type of the callable function stored in a slot. Can use any function/delegate type here,...