Get settings GUI implemented and functional

This commit is contained in:
teknomunk 2024-06-28 07:09:04 -05:00
parent 0c258b3680
commit 26b16b412e
2 changed files with 64 additions and 12 deletions

View file

@ -2,26 +2,69 @@ local modname = "vl_tuning"
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
local S = minetest.get_translator(modname) local S = minetest.get_translator(modname)
local F = function(f) return minetest.formspec_escape(S(f)) end local F = function(f) return minetest.formspec_escape(S(f)) end
local FE = minetest.formspec_escape
local mod = vl_tuning
local function bool_to_string(value)
if value then return "true" end
return "false"
end
local function formspec_for_setting(y, name)
local setting = mod.registered_settings[name]
if not setting then return "" end
local setting_type = setting.setting_type
local fs = {}
table.insert(fs, "label[0,"..(y+0.15)..";"..FE(name).."]")
table.insert(fs, "hypertext[0.15,"..(y+0.25)..";14.85,0.65;;"..FE("<style color=black>"..(setting.description or "").."</style>").."]")
if setting_type == "bool" then
table.insert(fs, "checkbox[17,"..(y+0.15)..";"..FE(name)..";;"..bool_to_string(setting[1]).."]")
elseif setting_type == "number" then
table.insert(fs, "field[15,"..y..";2.5,0.75;"..FE(name)..";;"..string.format("%.4g", setting[1]).."]")
table.insert(fs, "field_close_on_enter["..FE(name)..";false]")
elseif setting_type == "string" then
end
return table.concat(fs)
end
function vl_tuning.show_formspec(player_name, tab) function vl_tuning.show_formspec(player_name, tab)
if not tab then tab = 1 end if not tab then tab = "1" end
local gamerules = {}
local settings = {} local settings = {}
local y = 0.5
for name,_ in pairs(vl_tuning.registered_settings) do for name,_ in pairs(vl_tuning.registered_settings) do
if name:sub(0,#"gamerule:") == "gamerule:" then if name:sub(0,#"gamerule:") == "gamerule:" then
table.insert(gamerules, name) if tab == "1" then
table.insert(settings, formspec_for_setting(y,name))
y = y + 1
end
else else
table.insert(settings, name) if tab == "2" then
table.insert(settings, formspec_for_setting(y,name))
y = y + 1
end
end end
end end
local formspec =
"formspec_version[4]".. local formspec = table.concat({
"size[25,15,true]".. "formspec_version[4]",
"size[20,10.5,true]",
"tabheader[0,0;tab;".. "tabheader[0,0;tab;"..
F("Game Rules")..",".. F("Game Rules")..","..
F("Settings").. F("Settings")..
";"..tab..";false;false]" ";"..tab..";false;false]",
"field[0,0;0,0;old_tab;;"..tab.."]",
"scroll_container[1,0.5;18,9.25;settings;vertical;]",
table.concat(settings),
"scroll_container_end[]",
"scrollbaroptions[min=0;max="..tostring(10 * math.max(#settings - 9, 0))..";smallstep=1;largestep=1]",
"scrollbar[18.75,0.75;0.75,9.25;vertical;settings;0]",
})
minetest.show_formspec(player_name, "vl_tuning:settings", formspec) minetest.show_formspec(player_name, "vl_tuning:settings", formspec)
end end
@ -33,16 +76,24 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
fields = fields, fields = fields,
formname = formname, formname = formname,
})) }))
if fields.quit then
return for k,value in pairs(fields) do
local setting = mod.registered_settings[k]
if setting then
setting:set(value)
end
end end
vl_tuning.show_formspec(player:get_player_name(), fields.tab)
if fields.quit or (not fields.tab or fields.old_tab == fields.tab) then return end
minetest.log("Seting settings formspec")
mod.show_formspec(player:get_player_name(), fields.tab)
end) end)
minetest.register_chatcommand("settings",{ minetest.register_chatcommand("settings",{
func = function(player_name, param) func = function(player_name, param)
dofile(modpath.."/gui.lua") dofile(modpath.."/gui.lua")
vl_tuning.show_formspec(player_name) mod.show_formspec(player_name)
end end
}) })

View file

@ -66,6 +66,7 @@ function mod.setting(setting, setting_type, def )
tunable = table.copy(def) tunable = table.copy(def)
tunable.setting = setting tunable.setting = setting
tunable.type = tunable_types[setting_type] tunable.type = tunable_types[setting_type]
tunable.setting_type = setting_type
tunable[1] = tunable.default tunable[1] = tunable.default
setmetatable(tunable, {__index=tunable_class}) setmetatable(tunable, {__index=tunable_class})