diff --git a/src/delegates.hpp b/src/delegates.hpp index f583f951d..abd489ca2 100644 --- a/src/delegates.hpp +++ b/src/delegates.hpp @@ -8,7 +8,7 @@ using runnable = std::function; template using supplier = std::function; template using consumer = std::function; -using KeyCallback = std::function; +using InputCallback = std::function; // data sources using wstringsupplier = std::function; diff --git a/src/logic/scripting/lua/libs/libinput.cpp b/src/logic/scripting/lua/libs/libinput.cpp index 68469bb23..352da415a 100644 --- a/src/logic/scripting/lua/libs/libinput.cpp +++ b/src/logic/scripting/lua/libs/libinput.cpp @@ -44,10 +44,13 @@ static int l_add_callback(lua::State* L) { auto& input = engine->getInput(); if (pos != std::string::npos) { - std::string prefix = bindname.substr(0, pos); + auto prefix = std::string_view(bindname).substr(0, pos); if (prefix == "key") { auto key = input_util::keycode_from(bindname.substr(pos + 1)); handler = input.addKeyCallback(key, actual_callback); + } else if (prefix == "mouse") { + auto mouse = input_util::mousecode_from(bindname.substr(pos + 1)); + handler = input.addMouseCallback(mouse, actual_callback); } } diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 6457911b0..d1ca02473 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -263,7 +263,7 @@ runnable lua::create_runnable(State* L) { }; } -KeyCallback lua::create_simple_handler(State* L) { +InputCallback lua::create_simple_handler(State* L) { auto funcptr = create_lambda_handler(L); return [=]() -> bool { if (!get_from_registry(L, LAMBDAS_TABLE, *funcptr, false)) { diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 61b56cef3..690fcb32d 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -665,7 +665,7 @@ namespace lua { } runnable create_runnable(lua::State*); - KeyCallback create_simple_handler(lua::State*); + InputCallback create_simple_handler(lua::State*); scripting::common_func create_lambda(lua::State*); scripting::common_func create_lambda_nothrow(lua::State*); diff --git a/src/window/detail/GLFWWindow.cpp b/src/window/detail/GLFWWindow.cpp index ca168a064..08b39bd07 100644 --- a/src/window/detail/GLFWWindow.cpp +++ b/src/window/detail/GLFWWindow.cpp @@ -216,10 +216,17 @@ public: return bindings; } - ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override { + ObserverHandler addKeyCallback(Keycode key, InputCallback callback) override { return keyCallbacks[key].add(std::move(callback)); } + ObserverHandler addMouseCallback(Mousecode button, InputCallback callback) override { + return addKeyCallback( + static_cast(MOUSE_KEYS_OFFSET + static_cast(button)), + std::move(callback) + ); + } + const std::vector& getPressedKeys() const override { return pressedKeys; } diff --git a/src/window/input.hpp b/src/window/input.hpp index 59d663672..839eeca96 100644 --- a/src/window/input.hpp +++ b/src/window/input.hpp @@ -288,12 +288,13 @@ public: virtual const Bindings& getBindings() const = 0; - virtual ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) = 0; + virtual ObserverHandler addKeyCallback(Keycode key, InputCallback callback) = 0; + virtual ObserverHandler addMouseCallback(Mousecode button, InputCallback callback) = 0; virtual const std::vector& getPressedKeys() const = 0; virtual const std::vector& getCodepoints() const = 0; - ObserverHandler addCallback(const std::string& name, KeyCallback callback) { + ObserverHandler addCallback(const std::string& name, InputCallback callback) { return getBindings().require(name).onactived.add(callback); } };