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

Move keybinding settings to (Lua-based) setting menu (#15791)

This commit is contained in:
y5nw 2025-04-20 20:20:49 +02:00 committed by GitHub
parent c1d2124102
commit 23bfb2db72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 591 additions and 782 deletions

11
builtin/common/menu.lua Normal file
View file

@ -0,0 +1,11 @@
-- Luanti
-- SPDX-License-Identifier: LGPL-2.1-or-later
-- These colors are used by the main menu and the settings menu
mt_color_grey = "#AAAAAA"
mt_color_blue = "#6389FF"
mt_color_lightblue = "#99CCFF"
mt_color_green = "#72FF63"
mt_color_dark_green = "#25C191"
mt_color_orange = "#FF8800"
mt_color_red = "#FF3300"

View file

@ -37,6 +37,7 @@ local make = {}
-- * `fs` is a string for the formspec.
-- Components should be relative to `0,0`, and not exceed `avail_w` or the returned `used_height`.
-- * `used_height` is the space used by components in `fs`.
-- * `spacing`: (Optional) the vertical margin to be added before the component (default 0.25)
-- * `on_submit = function(self, fields, parent)`:
-- * `fields`: submitted formspec fields
-- * `parent`: the fstk element for the settings UI, use to show dialogs
@ -442,6 +443,59 @@ local function make_noise_params(setting)
}
end
function make.key(setting)
local btn_bind = "bind_" .. setting.name
local btn_clear = "unbind_" .. setting.name
local function add_conflict_warnings(fs, height)
local value = core.settings:get(setting.name)
if value == "" then
return height
end
for _, o in ipairs(core.full_settingtypes) do
if o.type == "key" and o.name ~= setting.name and core.are_keycodes_equal(core.settings:get(o.name), value) then
table.insert(fs, ("label[0,%f;%s]"):format(height + 0.3,
core.colorize(mt_color_orange, fgettext([[Conflicts with "$1"]], fgettext(o.readable_name)))))
height = height + 0.6
end
end
return height
end
return {
info_text = setting.comment,
setting = setting,
spacing = 0.1,
get_formspec = function(self, avail_w)
self.resettable = core.settings:has(setting.name)
local btn_bind_width = math.max(2.5, avail_w/2)
local value = core.settings:get(setting.name)
local fs = {
("label[0,0.4;%s]"):format(get_label(setting)),
("button_key[%f,0;%f,0.8;%s;%s]"):format(
btn_bind_width, btn_bind_width-0.8,
btn_bind, core.formspec_escape(value)),
("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")),
}
local height = 0.8
height = add_conflict_warnings(fs, height)
return table.concat(fs), height
end,
on_submit = function(self, fields)
if fields[btn_bind] then
core.settings:set(setting.name, fields[btn_bind])
return true
elseif fields[btn_clear] then
core.settings:set(setting.name, "")
return true
end
end,
}
end
if INIT == "pause_menu" then
-- Making the noise parameter dialog work in the pause menu settings would
-- require porting "FSTK" (at least the dialog API) from the mainmenu formspec

View file

@ -22,7 +22,6 @@ local component_funcs = dofile(path .. "components.lua")
local shadows_component = dofile(path .. "shadows_component.lua")
local loaded = false
local full_settings
local info_icon_path = core.formspec_escape(defaulttexturedir .. "settings_info.png")
local reset_icon_path = core.formspec_escape(defaulttexturedir .. "settings_reset.png")
local all_pages = {}
@ -32,7 +31,7 @@ local filtered_page_by_id = page_by_id
local function get_setting_info(name)
for _, entry in ipairs(full_settings) do
for _, entry in ipairs(core.full_settingtypes) do
if entry.type ~= "category" and entry.name == name then
return entry
end
@ -70,7 +69,7 @@ local function load_settingtypes()
end
end
for _, entry in ipairs(full_settings) do
for _, entry in ipairs(core.full_settingtypes) do
if entry.type == "category" then
if entry.level == 0 then
section = entry.name
@ -104,24 +103,7 @@ local function load()
end
loaded = true
full_settings = settingtypes.parse_config_file(false, true)
local change_keys = {
query_text = "Controls",
requires = {
keyboard_mouse = true,
},
context = "client",
get_formspec = function(self, avail_w)
local btn_w = math.min(avail_w, 3)
return ("button[0,0;%f,0.8;btn_change_keys;%s]"):format(btn_w, fgettext("Controls")), 0.8
end,
on_submit = function(self, fields)
if fields.btn_change_keys then
core.show_keys_menu()
end
end,
}
core.full_settingtypes = settingtypes.parse_config_file(false, true)
local touchscreen_layout = {
query_text = "Touchscreen layout",
@ -166,7 +148,6 @@ local function load()
load_settingtypes()
table.insert(page_by_id.controls_keyboard_and_mouse.content, 1, change_keys)
-- insert after "touch_controls"
table.insert(page_by_id.controls_touchscreen.content, 2, touchscreen_layout)
do
@ -665,7 +646,13 @@ local function get_formspec(dialogdata)
fs[#fs + 1] = "container_end[]"
if used_h > 0 then
y = y + used_h + 0.25
local spacing = 0.25
local next_comp = dialogdata.components[i + 1]
if next_comp and next_comp.spacing then
spacing = next_comp.spacing
end
y = y + used_h + spacing
end
end

View file

@ -249,9 +249,9 @@ local function parse_setting_line(settings, line, read_all, base_level, allow_se
if not default then
return "Invalid string setting"
end
if setting_type == "key" and not read_all then
-- ignore key type if read_all is false
return
if setting_type == "key" then
requires.keyboard_mouse = true
end
table.insert(settings, {