From 26b16b412efdeb2bc66791ea8c7092c3260d380b Mon Sep 17 00:00:00 2001 From: teknomunk Date: Fri, 28 Jun 2024 07:09:04 -0500 Subject: [PATCH] Get settings GUI implemented and functional --- mods/CORE/vl_tuning/gui.lua | 75 ++++++++++++++++++++++++++++++------ mods/CORE/vl_tuning/init.lua | 1 + 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/mods/CORE/vl_tuning/gui.lua b/mods/CORE/vl_tuning/gui.lua index 778b9e442..d515b33a0 100644 --- a/mods/CORE/vl_tuning/gui.lua +++ b/mods/CORE/vl_tuning/gui.lua @@ -2,26 +2,69 @@ local modname = "vl_tuning" local modpath = minetest.get_modpath(modname) local S = minetest.get_translator(modname) 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("").."]") + + 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) - if not tab then tab = 1 end + if not tab then tab = "1" end - local gamerules = {} local settings = {} + local y = 0.5 for name,_ in pairs(vl_tuning.registered_settings) do 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 - table.insert(settings, name) + if tab == "2" then + table.insert(settings, formspec_for_setting(y,name)) + y = y + 1 + end end end - local formspec = - "formspec_version[4]".. - "size[25,15,true]".. + + local formspec = table.concat({ + "formspec_version[4]", + "size[20,10.5,true]", "tabheader[0,0;tab;".. F("Game Rules")..",".. 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) end @@ -33,16 +76,24 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) fields = fields, 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 - 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) minetest.register_chatcommand("settings",{ func = function(player_name, param) dofile(modpath.."/gui.lua") - vl_tuning.show_formspec(player_name) + mod.show_formspec(player_name) end }) diff --git a/mods/CORE/vl_tuning/init.lua b/mods/CORE/vl_tuning/init.lua index fe3210798..a200f580d 100644 --- a/mods/CORE/vl_tuning/init.lua +++ b/mods/CORE/vl_tuning/init.lua @@ -66,6 +66,7 @@ function mod.setting(setting, setting_type, def ) tunable = table.copy(def) tunable.setting = setting tunable.type = tunable_types[setting_type] + tunable.setting_type = setting_type tunable[1] = tunable.default setmetatable(tunable, {__index=tunable_class})