Replace settings tab with button

This commit is contained in:
rubenwardy 2023-08-28 16:56:32 +01:00 committed by Gregor Parzefall
parent 798b9eae4a
commit 48ab1835da
3 changed files with 58 additions and 31 deletions

@ -66,8 +66,8 @@ local function get_formspec(self)
local content, prepend = tab.get_formspec(self, tab.name, tab.tabdata, tab.tabsize)
if self.parent == nil and not prepend then
local tsize = tab.tabsize or { width = self.width, height = self.height }
if self.parent == nil and not prepend then
prepend = string.format("size[%f,%f,%s]", tsize.width, tsize.height,
dump(self.fixed_size))
@ -76,7 +76,28 @@ local function get_formspec(self)
end
end
local formspec = (prepend or "") .. self:tab_header() .. content
local end_button_size = 0.75
local tab_header_size = { width = tsize.width, height = 0.85 }
if self.end_button then
tab_header_size.width = tab_header_size.width - end_button_size - 0.1
end
local formspec = (prepend or "") .. self:tab_header(tab_header_size) .. content
if self.end_button then
formspec = formspec ..
("style[%s;noclip=true;border=false]"):format(self.end_button.name) ..
("tooltip[%s;%s]"):format(self.end_button.name, self.end_button.label) ..
("image_button[%f,%f;%f,%f;%s;%s;]"):format(
self.width - end_button_size,
(-tab_header_size.height - end_button_size) / 2,
end_button_size,
end_button_size,
core.formspec_escape(self.end_button.icon),
self.end_button.name)
end
return formspec
end
@ -91,6 +112,10 @@ local function handle_buttons(self,fields)
return true
end
if self.end_button and fields[self.end_button.name] then
return self.end_button.on_click(self)
end
if self.glb_btn_handler ~= nil and
self.glb_btn_handler(self, fields) then
return true
@ -126,8 +151,7 @@ end
--------------------------------------------------------------------------------
local function tab_header(self)
local function tab_header(self, size)
local toadd = ""
for i=1,#self.tablist,1 do
@ -138,8 +162,8 @@ local function tab_header(self)
toadd = toadd .. self.tablist[i].caption
end
return string.format("tabheader[%f,%f;%s;%s;%i;true;false]",
self.header_x, self.header_y, self.name, toadd, self.last_tab_index);
return string.format("tabheader[%f,%f;%f,%f;%s;%s;%i;true;false]",
self.header_x, self.header_y, size.width, size.height, self.name, toadd, self.last_tab_index)
end
--------------------------------------------------------------------------------
@ -230,6 +254,8 @@ local tabview_metatable = {
function(self,handler) self.glb_evt_handler = handler end,
set_fixed_size =
function(self,state) self.fixed_size = state end,
set_end_button =
function(self, v) self.end_button = v end,
tab_header = tab_header,
handle_tab_buttons = handle_tab_buttons
}

@ -51,30 +51,13 @@ dofile(menupath .. DIR_DELIM .. "dlg_register.lua")
dofile(menupath .. DIR_DELIM .. "dlg_rename_modpack.lua")
dofile(menupath .. DIR_DELIM .. "dlg_version_info.lua")
local tabs = {}
tabs.settings = {
name = "settings",
caption = fgettext("Settings"),
cbf_formspec = function()
return "button[0.1,0.1;3,0.8;open_settings;" .. fgettext("Open Settings") .. "]"
end,
cbf_button_handler = function(tabview, fields)
if fields.open_settings then
local dlg = create_settings_dlg()
dlg:set_parent(tabview)
tabview:hide()
dlg:show()
return true
end
end,
local tabs = {
content = dofile(menupath .. DIR_DELIM .. "tab_content.lua"),
about = dofile(menupath .. DIR_DELIM .. "tab_about.lua"),
local_game = dofile(menupath .. DIR_DELIM .. "tab_local.lua"),
play_online = dofile(menupath .. DIR_DELIM .. "tab_online.lua")
}
tabs.content = dofile(menupath .. DIR_DELIM .. "tab_content.lua")
tabs.about = dofile(menupath .. DIR_DELIM .. "tab_about.lua")
tabs.local_game = dofile(menupath .. DIR_DELIM .. "tab_local.lua")
tabs.play_online = dofile(menupath .. DIR_DELIM .. "tab_online.lua")
--------------------------------------------------------------------------------
local function main_event_handler(tabview, event)
if event == "MenuQuit" then
@ -121,7 +104,6 @@ local function init_globals()
tv_main:add(tabs.local_game)
tv_main:add(tabs.play_online)
tv_main:add(tabs.content)
tv_main:add(tabs.settings)
tv_main:add(tabs.about)
tv_main:set_global_event_handler(main_event_handler)
@ -132,6 +114,19 @@ local function init_globals()
tv_main:set_tab(last_tab)
end
tv_main:set_end_button({
icon = defaulttexturedir .. "settings_btn.png",
label = fgettext("Settings"),
name = "open_settings",
on_click = function(tabview)
local dlg = create_settings_dlg()
dlg:set_parent(tabview)
tabview:hide()
dlg:show()
return true
end,
})
-- In case the folder of the last selected game has been deleted,
-- display "Minetest" as a header
if tv_main.current_tab == "local" and not game then

@ -85,6 +85,12 @@ methods:
^ delete tabview
- set_fixed_size(state)
^ true/false set to fixed size, variable size
- set_end_button(info)
^ info is a table with:
* name: button name
* label: tooltip text
* icon: path to icon
* on_click(tabview): callback function
File: fst/dialog.lua
---------------------