Clean up and improve mainmenu theme / game theme code (#13885)

This commit is contained in:
Gregor Parzefall 2023-10-18 20:18:50 +02:00 committed by GitHub
parent 62eb6cfed0
commit b1dec37adb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 96 additions and 80 deletions

@ -1177,8 +1177,12 @@ end
function store.handle_events(event) function store.handle_events(event)
if event == "DialogShow" then if event == "DialogShow" then
-- On mobile, don't show the "MINETEST" header behind the dialog.
mm_game_theme.set_engine(TOUCHSCREEN_GUI)
-- If the store is already loaded, auto-install packages here. -- If the store is already loaded, auto-install packages here.
do_auto_install() do_auto_install()
return true return true
end end

@ -41,8 +41,6 @@ function check_reinstall_mtg()
return return
end end
mm_game_theme.reset()
local maintab = ui.find_by_name("maintab") local maintab = ui.find_by_name("maintab")
local dlg = create_reinstall_mtg_dlg() local dlg = create_reinstall_mtg_dlg()
@ -96,7 +94,10 @@ local function buttonhandler(this, fields)
end end
local function eventhandler(event) local function eventhandler(event)
if event == "MenuQuit" then if event == "DialogShow" then
mm_game_theme.set_engine()
return true
elseif event == "MenuQuit" then
-- Don't allow closing the dialog with ESC, but still allow exiting -- Don't allow closing the dialog with ESC, but still allow exiting
-- Minetest. -- Minetest.
core.close() core.close()

@ -71,6 +71,15 @@ local function version_info_buttonhandler(this, fields)
return false return false
end end
local function version_info_eventhandler(event)
if event == "DialogShow" then
mm_game_theme.set_engine()
return true
end
return false
end
local function create_version_info_dlg(new_version, url) local function create_version_info_dlg(new_version, url)
assert(type(new_version) == "string") assert(type(new_version) == "string")
assert(type(url) == "string") assert(type(url) == "string")
@ -78,7 +87,7 @@ local function create_version_info_dlg(new_version, url)
local retval = dialog_create("version_info", local retval = dialog_create("version_info",
version_info_formspec, version_info_formspec,
version_info_buttonhandler, version_info_buttonhandler,
nil) version_info_eventhandler)
retval.data.new_version = new_version retval.data.new_version = new_version
retval.data.url = url retval.data.url = url

@ -20,10 +20,6 @@ mm_game_theme = {}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.init() function mm_game_theme.init()
mm_game_theme.defaulttexturedir = core.get_texturepath_share() .. DIR_DELIM .. "base" ..
DIR_DELIM .. "pack" .. DIR_DELIM
mm_game_theme.basetexturedir = mm_game_theme.defaulttexturedir
mm_game_theme.texturepack = core.settings:get("texture_path") mm_game_theme.texturepack = core.settings:get("texture_path")
mm_game_theme.gameid = nil mm_game_theme.gameid = nil
@ -32,35 +28,27 @@ function mm_game_theme.init()
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.update(tab,gamedetails) function mm_game_theme.set_engine(hide_decorations)
if tab ~= "singleplayer" then
mm_game_theme.reset()
return
end
if gamedetails == nil then
return
end
mm_game_theme.update_game(gamedetails)
end
--------------------------------------------------------------------------------
function mm_game_theme.reset()
mm_game_theme.gameid = nil mm_game_theme.gameid = nil
mm_game_theme.stop_music()
core.set_topleft_text("")
local have_bg = false local have_bg = false
local have_overlay = mm_game_theme.set_generic("overlay") local have_overlay = mm_game_theme.set_engine_single("overlay")
if not have_overlay then if not have_overlay then
have_bg = mm_game_theme.set_generic("background") have_bg = mm_game_theme.set_engine_single("background")
end end
mm_game_theme.clear("header") mm_game_theme.clear_single("header")
mm_game_theme.clear("footer") mm_game_theme.clear_single("footer")
core.set_clouds(false) core.set_clouds(false)
mm_game_theme.set_generic("footer") if not hide_decorations then
mm_game_theme.set_generic("header") mm_game_theme.set_engine_single("header")
mm_game_theme.set_engine_single("footer")
end
if not have_bg then if not have_bg then
if core.settings:get_bool("menu_clouds") then if core.settings:get_bool("menu_clouds") then
@ -69,51 +57,50 @@ function mm_game_theme.reset()
mm_game_theme.set_dirt_bg() mm_game_theme.set_dirt_bg()
end end
end end
if mm_game_theme.music_handle ~= nil then
core.sound_stop(mm_game_theme.music_handle)
end
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.update_game(gamedetails) function mm_game_theme.set_game(gamedetails)
assert(gamedetails ~= nil)
if mm_game_theme.gameid == gamedetails.id then if mm_game_theme.gameid == gamedetails.id then
return return
end end
mm_game_theme.gameid = gamedetails.id
mm_game_theme.set_music(gamedetails)
core.set_topleft_text(gamedetails.name)
local have_bg = false local have_bg = false
local have_overlay = mm_game_theme.set_game("overlay",gamedetails) local have_overlay = mm_game_theme.set_game_single("overlay", gamedetails)
if not have_overlay then if not have_overlay then
have_bg = mm_game_theme.set_game("background",gamedetails) have_bg = mm_game_theme.set_game_single("background", gamedetails)
end end
mm_game_theme.clear("header") mm_game_theme.clear_single("header")
mm_game_theme.clear("footer") mm_game_theme.clear_single("footer")
core.set_clouds(false) core.set_clouds(false)
if not have_bg then mm_game_theme.set_game_single("header", gamedetails)
mm_game_theme.set_game_single("footer", gamedetails)
if not have_bg then
if core.settings:get_bool("menu_clouds") then if core.settings:get_bool("menu_clouds") then
core.set_clouds(true) core.set_clouds(true)
else else
mm_game_theme.set_dirt_bg() mm_game_theme.set_dirt_bg()
end end
end end
mm_game_theme.set_game("footer",gamedetails)
mm_game_theme.set_game("header",gamedetails)
mm_game_theme.gameid = gamedetails.id
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.clear(identifier) function mm_game_theme.clear_single(identifier)
core.set_background(identifier,"") core.set_background(identifier,"")
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.set_generic(identifier) function mm_game_theme.set_engine_single(identifier)
--try texture pack first --try texture pack first
if mm_game_theme.texturepack ~= nil then if mm_game_theme.texturepack ~= nil then
local path = mm_game_theme.texturepack .. DIR_DELIM .."menu_" .. local path = mm_game_theme.texturepack .. DIR_DELIM .."menu_" ..
@ -123,25 +110,17 @@ function mm_game_theme.set_generic(identifier)
end end
end end
if mm_game_theme.defaulttexturedir ~= nil then local path = defaulttexturedir .. DIR_DELIM .. "menu_" .. identifier .. ".png"
local path = mm_game_theme.defaulttexturedir .. DIR_DELIM .."menu_" .. if core.set_background(identifier, path) then
identifier .. ".png" return true
if core.set_background(identifier,path) then
return true
end
end end
return false return false
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.set_game(identifier, gamedetails) function mm_game_theme.set_game_single(identifier, gamedetails)
assert(gamedetails ~= nil)
if gamedetails == nil then
return false
end
mm_game_theme.set_music(gamedetails)
if mm_game_theme.texturepack ~= nil then if mm_game_theme.texturepack ~= nil then
local path = mm_game_theme.texturepack .. DIR_DELIM .. local path = mm_game_theme.texturepack .. DIR_DELIM ..
@ -194,10 +173,18 @@ function mm_game_theme.set_dirt_bg()
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function mm_game_theme.set_music(gamedetails) function mm_game_theme.stop_music()
if mm_game_theme.music_handle ~= nil then if mm_game_theme.music_handle ~= nil then
core.sound_stop(mm_game_theme.music_handle) core.sound_stop(mm_game_theme.music_handle)
end end
end
--------------------------------------------------------------------------------
function mm_game_theme.set_music(gamedetails)
mm_game_theme.stop_music()
assert(gamedetails ~= nil)
local music_path = gamedetails.path .. DIR_DELIM .. "menu" .. DIR_DELIM .. "theme" local music_path = gamedetails.path .. DIR_DELIM .. "menu" .. DIR_DELIM .. "theme"
mm_game_theme.music_handle = core.sound_play(music_path, true) mm_game_theme.music_handle = core.sound_play(music_path, true)
end end

@ -89,7 +89,7 @@ local function init_globals()
menudata.worldlist:set_sortmode("alphabetic") menudata.worldlist:set_sortmode("alphabetic")
mm_game_theme.init() mm_game_theme.init()
mm_game_theme.reset() mm_game_theme.set_engine() -- This is just a fallback.
-- Create main tabview -- Create main tabview
local tv_main = tabview_create("maintab", {x = 15.5, y = 7.1}, {x = 0, y = 0}) local tv_main = tabview_create("maintab", {x = 15.5, y = 7.1}, {x = 0, y = 0})

@ -692,8 +692,19 @@ local function buttonhandler(this, fields)
end end
local function eventhandler(event)
if event == "DialogShow" then
-- Don't show the "MINETEST" header behind the dialog.
mm_game_theme.set_engine(true)
return true
end
return false
end
function create_settings_dlg() function create_settings_dlg()
local dlg = dialog_create("dlg_settings", get_formspec, buttonhandler, nil) local dlg = dialog_create("dlg_settings", get_formspec, buttonhandler, eventhandler)
dlg.data.page_id = update_filtered_pages("") dlg.data.page_id = update_filtered_pages("")

@ -122,6 +122,7 @@ end
return { return {
name = "about", name = "about",
caption = fgettext("About"), caption = fgettext("About"),
cbf_formspec = function(tabview, name, tabdata) cbf_formspec = function(tabview, name, tabdata)
local logofile = defaulttexturedir .. "logo.png" local logofile = defaulttexturedir .. "logo.png"
local version = core.get_version() local version = core.get_version()
@ -196,6 +197,7 @@ return {
return fs return fs
end, end,
cbf_button_handler = function(this, fields, name, tabdata) cbf_button_handler = function(this, fields, name, tabdata)
if fields.homepage then if fields.homepage then
core.open_url("https://www.minetest.net") core.open_url("https://www.minetest.net")
@ -210,4 +212,10 @@ return {
core.open_dir(core.get_user_path()) core.open_dir(core.get_user_path())
end end
end, end,
on_change = function(type)
if type == "ENTER" then
mm_game_theme.set_engine()
end
end,
} }

@ -46,6 +46,7 @@ end
local function on_change(type) local function on_change(type)
if type == "ENTER" then if type == "ENTER" then
mm_game_theme.set_engine()
update_packages() update_packages()
end end
end end
@ -171,7 +172,7 @@ local function handle_doubleclick(pkg)
packages = nil packages = nil
mm_game_theme.init() mm_game_theme.init()
mm_game_theme.reset() mm_game_theme.set_engine()
end end
end end
@ -225,7 +226,7 @@ local function handle_buttons(tabview, fields, tabname, tabdata)
packages = nil packages = nil
mm_game_theme.init() mm_game_theme.init()
mm_game_theme.reset() mm_game_theme.set_engine()
return true return true
end end

@ -53,11 +53,10 @@ end
-- Apply menu changes from given game -- Apply menu changes from given game
function apply_game(game) function apply_game(game)
core.set_topleft_text(game.name)
core.settings:set("menu_last_game", game.id) core.settings:set("menu_last_game", game.id)
menudata.worldlist:set_filtercriteria(game.id) menudata.worldlist:set_filtercriteria(game.id)
mm_game_theme.update("singleplayer", game) -- this refreshes the formspec mm_game_theme.set_game(game)
local index = filterlist.get_current_index(menudata.worldlist, local index = filterlist.get_current_index(menudata.worldlist,
tonumber(core.settings:get("mainmenu_last_selected_world"))) tonumber(core.settings:get("mainmenu_last_selected_world")))
@ -396,7 +395,6 @@ local function main_button_handler(this, fields, name, tabdata)
create_world_dlg:set_parent(this) create_world_dlg:set_parent(this)
this:hide() this:hide()
create_world_dlg:show() create_world_dlg:show()
mm_game_theme.update("singleplayer", current_game())
return true return true
end end
@ -413,7 +411,6 @@ local function main_button_handler(this, fields, name, tabdata)
delete_world_dlg:set_parent(this) delete_world_dlg:set_parent(this)
this:hide() this:hide()
delete_world_dlg:show() delete_world_dlg:show()
mm_game_theme.update("singleplayer",current_game())
end end
end end
@ -431,7 +428,6 @@ local function main_button_handler(this, fields, name, tabdata)
configdialog:set_parent(this) configdialog:set_parent(this)
this:hide() this:hide()
configdialog:show() configdialog:show()
mm_game_theme.update("singleplayer",current_game())
end end
end end
@ -439,27 +435,24 @@ local function main_button_handler(this, fields, name, tabdata)
end end
end end
local function on_change(type, old_tab, new_tab) local function on_change(type)
if (type == "ENTER") then if type == "ENTER" then
local game = current_game() local game = current_game()
if game then if game then
apply_game(game) apply_game(game)
else
mm_game_theme.set_engine()
end end
if singleplayer_refresh_gamebar() then if singleplayer_refresh_gamebar() then
ui.find_by_name("game_button_bar"):show() ui.find_by_name("game_button_bar"):show()
end end
else elseif type == "LEAVE" then
menudata.worldlist:set_filtercriteria(nil) menudata.worldlist:set_filtercriteria(nil)
local gamebar = ui.find_by_name("game_button_bar") local gamebar = ui.find_by_name("game_button_bar")
if gamebar then if gamebar then
gamebar:hide() gamebar:hide()
end end
core.set_topleft_text("")
-- If new_tab is nil, a dialog is being shown; avoid resetting the theme
if new_tab then
mm_game_theme.update(new_tab,nil)
end
end end
end end

@ -416,9 +416,11 @@ local function main_button_handler(tabview, fields, name, tabdata)
return false return false
end end
local function on_change(type, old_tab, new_tab) local function on_change(type)
if type == "LEAVE" then return end if type == "ENTER" then
serverlistmgr.sync() mm_game_theme.set_engine()
serverlistmgr.sync()
end
end end
return { return {