1 #include "lxgui/gui_key_binder.hpp"
3 #include "lxgui/gui_event_emitter.hpp"
4 #include "lxgui/gui_out.hpp"
5 #include "lxgui/input_dispatcher.hpp"
6 #include "lxgui/utils_std.hpp"
7 #include "lxgui/utils_string.hpp"
9 #include <lxgui/extern_sol2_state.hpp>
15 auto function = [lua_function = std::move(lua_function)]() {
17 auto result = lua_function();
20 if (!result.valid()) {
30 utils::find_if(key_bindings_, [&](
const auto& binding) {
return binding.name == name; });
32 if (iter == key_bindings_.end()) {
33 gui::out <<
gui::error <<
"key_binder: a binding already exists with name '" << name <<
"'."
39 binding.name = std::string(name);
40 auto connection = binding.signal.connect(std::move(
function));
41 key_bindings_.push_back(std::move(binding));
47 std::string_view name,
49 bool shift_is_pressed,
51 bool alt_is_pressed) {
53 utils::find_if(key_bindings_, [&](
const auto& binding) {
return binding.name == name; });
55 if (iter == key_bindings_.end()) {
56 gui::out <<
gui::error <<
"key_binder: no binding with name '" << name <<
"'." << std::endl;
60 iter->key_id = key_id;
61 iter->shift_is_pressed =
63 iter->ctrl_is_pressed =
65 iter->alt_is_pressed =
70 bool shift_is_pressed =
false;
71 bool ctrl_is_pressed =
false;
72 bool alt_is_pressed =
false;
74 const auto tokens = utils::cut(key_name,
"-");
75 for (
auto token : tokens) {
77 shift_is_pressed =
true;
78 else if (token ==
"Ctrl")
79 ctrl_is_pressed =
true;
80 else if (token ==
"Alt")
81 alt_is_pressed =
true;
91 utils::find_if(key_bindings_, [&](
const auto& binding) {
return binding.name == name; });
93 if (iter == key_bindings_.end()) {
94 gui::out <<
gui::error <<
"key_binder: no binding with name '" << name <<
"'." << std::endl;
98 key_bindings_.erase(iter);
101 key_binder::key_binding* key_binder::find_binding_(
102 input::key key_id,
bool shift_is_pressed,
bool ctrl_is_pressed,
bool alt_is_pressed) {
103 auto iter =
utils::find_if(key_bindings_, [&](
const auto& binding) {
104 return binding.key_id == key_id && binding.shift_is_pressed == shift_is_pressed &&
105 binding.ctrl_is_pressed == ctrl_is_pressed &&
106 binding.alt_is_pressed == alt_is_pressed;
109 if (iter == key_bindings_.end())
116 input::key key_id,
bool shift_is_pressed,
bool ctrl_is_pressed,
bool alt_is_pressed) {
117 auto* binding = find_binding_(key_id, shift_is_pressed, ctrl_is_pressed, alt_is_pressed);
123 }
catch (
const std::exception& e) {
124 throw std::runtime_error(
"Bound action: " + binding->name +
": " + std::string(e.what()));
Exception to be thrown by GUI code.
void set_key_binding(std::string_view name, std::string_view key_name)
Binds an action to a key.
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.
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.
Object representing the connection between a slot and a signal.
auto find_if(C &v, T &&f)