Skip to content

Commit 92d4a73

Browse files
Rui914kwolekr
Rui914
authored andcommittedApr 8, 2016
Mainmenu: Refactor tab UI code
- Use local variables for tabs in place of globals - Merge together if statements where possible - Replace manual table searching code with indexof where possible
1 parent 27ee8d8 commit 92d4a73

11 files changed

+70
-80
lines changed
 

‎builtin/init.lua

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if core.print then
1212
-- Override native print and use
1313
-- terminal if that's turned on
1414
function print(...)
15-
local n, t = select("#", ...), { ... }
15+
local n, t = select("#", ...), {...}
1616
for i = 1, n do
1717
t[i] = tostring(t[i])
1818
end
@@ -25,26 +25,26 @@ os.setlocale("C", "numeric")
2525
minetest = core
2626

2727
-- Load other files
28-
local scriptdir = core.get_builtin_path()..DIR_DELIM
29-
local gamepath = scriptdir.."game"..DIR_DELIM
30-
local commonpath = scriptdir.."common"..DIR_DELIM
31-
local asyncpath = scriptdir.."async"..DIR_DELIM
28+
local scriptdir = core.get_builtin_path() .. DIR_DELIM
29+
local gamepath = scriptdir .. "game" .. DIR_DELIM
30+
local commonpath = scriptdir .. "common" .. DIR_DELIM
31+
local asyncpath = scriptdir .. "async" .. DIR_DELIM
3232

33-
dofile(commonpath.."strict.lua")
34-
dofile(commonpath.."serialize.lua")
35-
dofile(commonpath.."misc_helpers.lua")
33+
dofile(commonpath .. "strict.lua")
34+
dofile(commonpath .. "serialize.lua")
35+
dofile(commonpath .. "misc_helpers.lua")
3636

3737
if INIT == "game" then
38-
dofile(gamepath.."init.lua")
38+
dofile(gamepath .. "init.lua")
3939
elseif INIT == "mainmenu" then
4040
local mainmenuscript = core.setting_get("main_menu_script")
4141
if mainmenuscript ~= nil and mainmenuscript ~= "" then
4242
dofile(mainmenuscript)
4343
else
44-
dofile(core.get_mainmenu_path()..DIR_DELIM.."init.lua")
44+
dofile(core.get_mainmenu_path() .. DIR_DELIM .. "init.lua")
4545
end
4646
elseif INIT == "async" then
47-
dofile(asyncpath.."init.lua")
47+
dofile(asyncpath .. "init.lua")
4848
else
4949
error(("Unrecognized builtin initialization type %s!"):format(tostring(INIT)))
5050
end

‎builtin/mainmenu/init.lua

+45-55
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,29 @@ dofile(menupath .. DIR_DELIM .. "common.lua")
3737
dofile(menupath .. DIR_DELIM .. "gamemgr.lua")
3838
dofile(menupath .. DIR_DELIM .. "modmgr.lua")
3939
dofile(menupath .. DIR_DELIM .. "store.lua")
40+
dofile(menupath .. DIR_DELIM .. "textures.lua")
41+
4042
dofile(menupath .. DIR_DELIM .. "dlg_config_world.lua")
41-
dofile(menupath .. DIR_DELIM .. "tab_credits.lua")
42-
dofile(menupath .. DIR_DELIM .. "tab_mods.lua")
43-
dofile(menupath .. DIR_DELIM .. "tab_settings.lua")
4443
dofile(menupath .. DIR_DELIM .. "dlg_settings_advanced.lua")
4544
if PLATFORM ~= "Android" then
4645
dofile(menupath .. DIR_DELIM .. "dlg_create_world.lua")
4746
dofile(menupath .. DIR_DELIM .. "dlg_delete_mod.lua")
4847
dofile(menupath .. DIR_DELIM .. "dlg_delete_world.lua")
4948
dofile(menupath .. DIR_DELIM .. "dlg_rename_modpack.lua")
50-
dofile(menupath .. DIR_DELIM .. "tab_multiplayer.lua")
51-
dofile(menupath .. DIR_DELIM .. "tab_server.lua")
52-
dofile(menupath .. DIR_DELIM .. "tab_singleplayer.lua")
53-
dofile(menupath .. DIR_DELIM .. "tab_texturepacks.lua")
54-
dofile(menupath .. DIR_DELIM .. "textures.lua")
49+
end
50+
51+
local tabs = {}
52+
53+
tabs.settings = dofile(menupath .. DIR_DELIM .. "tab_settings.lua")
54+
tabs.mods = dofile(menupath .. DIR_DELIM .. "tab_mods.lua")
55+
tabs.credits = dofile(menupath .. DIR_DELIM .. "tab_credits.lua")
56+
if PLATFORM == "Android" then
57+
tabs.simple_main = dofile(menupath .. DIR_DELIM .. "tab_simple_main.lua")
5558
else
56-
dofile(menupath .. DIR_DELIM .. "tab_simple_main.lua")
59+
tabs.singleplayer = dofile(menupath .. DIR_DELIM .. "tab_singleplayer.lua")
60+
tabs.multiplayer = dofile(menupath .. DIR_DELIM .. "tab_multiplayer.lua")
61+
tabs.server = dofile(menupath .. DIR_DELIM .. "tab_server.lua")
62+
tabs.texturepacks = dofile(menupath .. DIR_DELIM .. "tab_texturepacks.lua")
5763
end
5864

5965
--------------------------------------------------------------------------------
@@ -69,8 +75,19 @@ local function init_globals()
6975
-- Init gamedata
7076
gamedata.worldindex = 0
7177

78+
if PLATFORM == "Android" then
79+
local world_list = core.get_worlds()
80+
local world_index = table.indexof(world_list, "singleplayerworld")
7281

73-
if PLATFORM ~= "Android" then
82+
if world_index == -1 then
83+
core.create_world("singleplayerworld", 1)
84+
85+
world_list = core.get_worlds()
86+
world_index = table.indexof(world_list, "singleplayerworld")
87+
end
88+
89+
gamedata.worldindex = world_index
90+
else
7491
menudata.worldlist = filterlist.create(
7592
core.get_worlds,
7693
compare_worlds,
@@ -89,71 +106,44 @@ local function init_globals()
89106

90107
if not core.setting_get("menu_last_game") then
91108
local default_game = core.setting_get("default_game") or "minetest"
92-
core.setting_set("menu_last_game", default_game )
109+
core.setting_set("menu_last_game", default_game)
93110
end
94111

95112
mm_texture.init()
96-
else
97-
local world_list = core.get_worlds()
98-
99-
local found_singleplayerworld = false
100-
101-
for i,world in pairs(world_list) do
102-
if world.name == "singleplayerworld" then
103-
found_singleplayerworld = true
104-
gamedata.worldindex = i
105-
break
106-
end
107-
end
108-
109-
if not found_singleplayerworld then
110-
core.create_world("singleplayerworld", 1)
111-
112-
local world_list = core.get_worlds()
113-
114-
for i,world in pairs(world_list) do
115-
if world.name == "singleplayerworld" then
116-
gamedata.worldindex = i
117-
break
118-
end
119-
end
120-
end
121113
end
122114

123115
-- Create main tabview
124-
local tv_main = tabview_create("maintab",{x=12,y=5.2},{x=0,y=0})
125-
if PLATFORM ~= "Android" then
126-
tv_main:set_autosave_tab(true)
127-
end
128-
if PLATFORM ~= "Android" then
129-
tv_main:add(tab_singleplayer)
130-
tv_main:add(tab_multiplayer)
131-
tv_main:add(tab_server)
116+
local tv_main = tabview_create("maintab", {x = 12, y = 5.2}, {x = 0, y = 0})
117+
118+
if PLATFORM == "Android" then
119+
tv_main:add(tabs.simple_main)
120+
tv_main:add(tabs.settings)
132121
else
133-
tv_main:add(tab_simple_main)
134-
end
135-
tv_main:add(tab_settings)
136-
if PLATFORM ~= "Android" then
137-
tv_main:add(tab_texturepacks)
122+
tv_main:set_autosave_tab(true)
123+
tv_main:add(tabs.singleplayer)
124+
tv_main:add(tabs.multiplayer)
125+
tv_main:add(tabs.server)
126+
tv_main:add(tabs.settings)
127+
tv_main:add(tabs.texturepacks)
138128
end
139-
tv_main:add(tab_mods)
140-
tv_main:add(tab_credits)
141129

142-
tv_main:set_global_event_handler(main_event_handler)
130+
tv_main:add(tabs.mods)
131+
tv_main:add(tabs.credits)
143132

133+
tv_main:set_global_event_handler(main_event_handler)
144134
tv_main:set_fixed_size(false)
145135

146-
if not (PLATFORM == "Android") then
136+
if PLATFORM ~= "Android" then
147137
tv_main:set_tab(core.setting_get("maintab_LAST"))
148138
end
149139
ui.set_default("maintab")
150140
tv_main:show()
151141

152142
-- Create modstore ui
153143
if PLATFORM == "Android" then
154-
modstore.init({x=12, y=6}, 3, 2)
144+
modstore.init({x = 12, y = 6}, 3, 2)
155145
else
156-
modstore.init({x=12, y=8}, 4, 3)
146+
modstore.init({x = 12, y = 8}, 4, 3)
157147
end
158148

159149
ui.update()

‎builtin/mainmenu/init_simple.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
-- helper file to be able to debug the simple menu on PC
22
-- without messing around with actual menu code!
3-
PLATFORM="Android"
3+
PLATFORM = "Android"
44
dofile("builtin/mainmenu/init.lua")

‎builtin/mainmenu/tab_credits.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ local previous_contributors = {
6969
"Zefram <zefram@fysh.org>",
7070
}
7171

72-
tab_credits = {
72+
return {
7373
name = "credits",
7474
caption = fgettext("Credits"),
7575
cbf_formspec = function(tabview, name, tabdata)

‎builtin/mainmenu/tab_mods.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ local function handle_buttons(tabview, fields, tabname, tabdata)
163163
end
164164

165165
--------------------------------------------------------------------------------
166-
tab_mods = {
166+
return {
167167
name = "mods",
168168
caption = fgettext("Mods"),
169169
cbf_formspec = get_formspec,

‎builtin/mainmenu/tab_multiplayer.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ local function on_change(type,old_tab,new_tab)
256256
end
257257

258258
--------------------------------------------------------------------------------
259-
tab_multiplayer = {
259+
return {
260260
name = "multiplayer",
261261
caption = fgettext("Client"),
262262
cbf_formspec = get_formspec,
263263
cbf_button_handler = main_button_handler,
264264
on_change = on_change
265-
}
265+
}

‎builtin/mainmenu/tab_server.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ local function main_button_handler(this, fields, name, tabdata)
186186
end
187187

188188
--------------------------------------------------------------------------------
189-
tab_server = {
189+
return {
190190
name = "server",
191191
caption = fgettext("Server"),
192192
cbf_formspec = get_formspec,
193193
cbf_button_handler = main_button_handler,
194194
on_change = nil
195-
}
195+
}

‎builtin/mainmenu/tab_settings.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ local function handle_settings_buttons(this, fields, tabname, tabdata)
384384
return ddhandled
385385
end
386386

387-
tab_settings = {
387+
return {
388388
name = "settings",
389389
caption = fgettext("Settings"),
390390
cbf_formspec = formspec,

‎builtin/mainmenu/tab_simple_main.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ local function on_activate(type,old_tab,new_tab)
196196
end
197197

198198
--------------------------------------------------------------------------------
199-
tab_simple_main = {
199+
return {
200200
name = "main",
201201
caption = fgettext("Main"),
202202
cbf_formspec = get_formspec,
203203
cbf_button_handler = main_button_handler,
204204
on_change = on_activate
205-
}
205+
}

‎builtin/mainmenu/tab_singleplayer.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ local function on_change(type, old_tab, new_tab)
241241
end
242242

243243
--------------------------------------------------------------------------------
244-
tab_singleplayer = {
244+
return {
245245
name = "singleplayer",
246246
caption = fgettext("Singleplayer"),
247247
cbf_formspec = get_formspec,
248248
cbf_button_handler = main_button_handler,
249249
on_change = on_change
250-
}
250+
}

‎builtin/mainmenu/tab_texturepacks.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ local function main_button_handler(tabview, fields, name, tabdata)
123123
end
124124

125125
--------------------------------------------------------------------------------
126-
tab_texturepacks = {
126+
return {
127127
name = "texturepacks",
128128
caption = fgettext("Texturepacks"),
129129
cbf_formspec = get_formspec,
130130
cbf_button_handler = main_button_handler,
131131
on_change = nil
132-
}
132+
}

0 commit comments

Comments
 (0)
Please sign in to comment.