From e3e22de2caec61d76856ea53b2b5fcc0ac96793b Mon Sep 17 00:00:00 2001 From: nanahi <130121847+na-na-hi@users.noreply.github.com> Date: Thu, 14 Nov 2024 16:50:43 -0500 Subject: [PATCH] defaults.lua: support scalable mp.add_key_binding() For complex key bindings, the table now contains a new member of the current key scale. mp.add_key_binding() now accepts the scalable flag to make the binding scalable. --- DOCS/man/lua.rst | 11 +++++++++++ player/lua/defaults.lua | 11 +++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst index dc23d35353..d4a4a25027 100644 --- a/DOCS/man/lua.rst +++ b/DOCS/man/lua.rst @@ -323,6 +323,13 @@ The ``mp`` module is preloaded, although it can be loaded manually with If set to ``true``, enables key repeat for this specific binding. This option only makes sense when ``complex`` is not set to ``true``. + ``scalable`` + If set to ``true``, enables key scaling for this specific binding. + This option only makes sense when ``complex`` is set to ``true``. + Note that this has no effect if the key binding is invoked by + ``script-binding`` command, where the scalability of the command + takes precedence. + ``complex`` If set to ``true``, then ``fn`` is called on key down, repeat and up events, with the first argument being a table. This table has the @@ -350,6 +357,10 @@ The ``mp`` module is preloaded, although it can be loaded manually with description of ``script-binding`` command for details (this field is equivalent to the 5th argument). + ``scale`` + The scale of the key, such as the ones produced by ``WHEEL_*`` + keys. The scale is 1 if the key is nonscalable. + Internally, key bindings are dispatched via the ``script-message-to`` or ``script-binding`` input commands and ``mp.register_script_message``. diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 5cff86b1ba..6a353d5a87 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -60,10 +60,10 @@ local function reserve_binding() return "__keybinding" .. tostring(message_id) end -local function dispatch_key_binding(name, state, key_name, key_text) +local function dispatch_key_binding(name, state, key_name, key_text, scale) local fn = dispatch_key_bindings[name] if fn then - fn(name, state, key_name, key_text) + fn(name, state, key_name, key_text, scale) end end @@ -186,6 +186,7 @@ local function add_binding(attrs, key, name, fn, rp) name = reserve_binding() end local repeatable = rp == "repeatable" or rp["repeatable"] + local scalable = rp == "scalable" or rp["scalable"] if rp["forced"] then attrs.forced = true end @@ -200,7 +201,7 @@ local function add_binding(attrs, key, name, fn, rp) ["r"] = "repeat", ["p"] = "press", } - key_cb = function(_, state, key_name, key_text) + key_cb = function(_, state, key_name, key_text, scale) if key_text == "" then key_text = nil end @@ -210,6 +211,7 @@ local function add_binding(attrs, key, name, fn, rp) canceled = state:sub(3, 3) == "c", key_name = key_name, key_text = key_text, + scale = tonumber(scale), }) end msg_cb = function() @@ -235,8 +237,9 @@ local function add_binding(attrs, key, name, fn, rp) end msg_cb = fn end + local prefix = scalable and "" or " nonscalable" if key and #key > 0 then - attrs.bind = key .. " script-binding " .. mp.script_name .. "/" .. name + attrs.bind = key .. prefix .. " script-binding " .. mp.script_name .. "/" .. name end attrs.name = name -- new bindings override old ones (but do not overwrite them)