1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

allow adding keybindings from setting menu

This commit is contained in:
y5nw 2025-05-25 01:11:19 +02:00
parent 8eba9ffa2c
commit 9a9fced642
2 changed files with 9 additions and 21 deletions

View file

@ -431,9 +431,7 @@ local function make_noise_params(setting)
end
function make.key(setting)
local btn_bind = "bind_" .. setting.name
local btn_edit = "edit_" .. setting.name
local btn_clear = "unbind_" .. setting.name
local function add_conflict_warnings(fs, height)
local value = core.settings:get(setting.name):split("|")
for _, o in ipairs(core.full_settingtypes) do
@ -457,33 +455,22 @@ function make.key(setting)
local value = core.settings:get(setting.name):split("|")
local fs = {
("label[0,0.4;%s]"):format(get_label(setting)),
("image_button[%f,0;0.8,0.8;%s;%s;]"):format(avail_w - 0.8,
core.formspec_escape(defaulttexturedir .. "clear.png"), btn_clear),
("tooltip[%s;%s]"):format(btn_clear, fgettext("Remove keybinding")),
}
if #value < 2 then
table.insert(fs, ("button_key[%f,0;%f,0.8;%s;%s]"):format(
btn_bind_width, btn_bind_width-0.8,
btn_bind, core.formspec_escape(value[1] or "")))
else
table.insert(fs, ("button[%f,0;%f,0.8;%s;%s]"):format(
btn_bind_width, btn_bind_width-0.8,
btn_edit, fgettext("Edit")))
local edit_label = fgettext("Edit")
if #value <= 1 then
edit_label = core.formspec_escape(core.get_keycode_name(value[1] or ""))
end
table.insert(fs, ("button[%f,0;%f,0.8;%s;%s]"):format(
btn_bind_width, btn_bind_width,
btn_edit, edit_label))
local height = 0.8
height = add_conflict_warnings(fs, height)
return table.concat(fs), height
end,
on_submit = function(self, fields, tabview)
if fields[btn_bind] then
core.settings:set(setting.name, fields[btn_bind])
return true
elseif fields[btn_edit] then
if fields[btn_edit] then
return show_change_keybinding_dlg(setting, tabview)
elseif fields[btn_clear] then
core.settings:set(setting.name, "")
return true
end
end,
}

View file

@ -18,6 +18,7 @@ local function get_formspec(dialogdata)
("button_key[0.5,6.7;4.2,0.8;btn_bind;%s]"):format(core.formspec_escape(value[selection] or "")),
("image_button[4.6,6.7;0.8,0.8;%s;btn_clear;]"):format(
core.formspec_escape(defaulttexturedir .. "clear.png")),
("tooltip[btn_clear;%s]"):format(fgettext("Remove keybinding")),
("button[3.1,7.7;2.4,0.8;btn_close;%s]"):format(fgettext("Cancel")),
("button[0.5,7.7;2.4,0.8;btn_save;%s]"):format(fgettext("Save")),
}
@ -31,7 +32,7 @@ local function get_formspec(dialogdata)
and has_keybinding_conflict(core.settings:get(o.name):split("|"), key) then
prefix = mt_color_orange
if idx == selection then
warning = core.colorize(mt_color_orange, fgettext([[Conflicts with "$1"]], fgettext(o.readable_name)))
warning = core.colorize(mt_color_orange, fgettext([[Conflicts with "$1"]], fgettext_ne(o.readable_name)))
end
break
end