1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

complement keybinding form

This commit is contained in:
y5nw 2025-05-25 00:33:52 +02:00
parent c9e28d39e9
commit 77142181f2

View file

@ -7,12 +7,27 @@ local function get_formspec(dialogdata)
readable_name = ("%s (%s)"):format(fgettext(dialogdata.setting.readable_name), name)
end
local value = dialogdata.value
local selection = dialogdata.selection
local fs = {
"formspec_version[4]",
"size[6,2]", -- size element; height can only be determined later
"size[6,9]",
("label[0.5,0.8;%s]"):format(readable_name),
("button[0.5,5.7;5,0.8;btn_add;%s]"):format(fgettext("Add keybinding")),
("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")),
("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")),
}
local cells = {}
for _, key in ipairs(value) do
table.insert(cells, core.formspec_escape(key))
end
table.insert(fs, ("textlist[0.5,1.3;5,4;keylist;%s;%d;false]"):format(table.concat(cells, ","), selection))
return table.concat(fs)
end
@ -21,6 +36,34 @@ local function buttonhandler(self, fields)
if fields.quit or fields.btn_close then
self:delete()
return true
elseif fields.btn_save then
local value = {}
for _, v in ipairs(self.data.value) do
if v ~= "" then -- filter out "empty" keybindings
table.insert(value, v)
end
end
core.settings:set(name, table.concat(value, "|"))
self:delete()
return true
elseif fields.btn_clear then
local selection = self.data.selection
table.remove(self.data.value, selection)
self.data.selection = math.max(1, math.min(selection, #self.data.value))
return true
elseif fields.btn_add then
table.insert(self.data.value, "")
self.data.selection = #self.data.value
return true
elseif fields.btn_bind then
self.data.value[self.data.selection] = fields.btn_bind
return true
elseif fields.keylist then
local event = core.explode_textlist_event(fields.keylist)
if event.type == "CHG" or event.type == "DCL" then
self.data.selection = event.index
return true
end
end
return false
end
@ -67,6 +110,8 @@ function show_change_keybinding_dlg(setting, tabview)
end
dlg.data.setting = setting
dlg.data.value = core.settings:get(setting.name):split("|")
dlg.data.selection = 1
if is_mainmenu then
dlg:set_parent(tabview)