Make `options` local here.
[oweals/minetest.git] / builtin / mainmenu / init.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
4 --This program is free software; you can redistribute it and/or modify
5 --it under the terms of the GNU Lesser General Public License as published by
6 --the Free Software Foundation; either version 2.1 of the License, or
7 --(at your option) any later version.
8 --
9 --This program is distributed in the hope that it will be useful,
10 --but WITHOUT ANY WARRANTY; without even the implied warranty of
11 --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 --GNU Lesser General Public License for more details.
13 --
14 --You should have received a copy of the GNU Lesser General Public License along
15 --with this program; if not, write to the Free Software Foundation, Inc.,
16 --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 mt_color_grey  = "#AAAAAA"
19 mt_color_blue  = "#0000DD"
20 mt_color_green = "#00DD00"
21 mt_color_dark_green = "#003300"
22
23 --for all other colors ask sfan5 to complete his work!
24
25 local menupath = core.get_mainmenu_path()
26 local basepath = core.get_builtin_path()
27 defaulttexturedir = core.get_texturepath_share() .. DIR_DELIM .. "base" ..
28                                         DIR_DELIM .. "pack" .. DIR_DELIM
29
30 dofile(basepath .. DIR_DELIM .. "common" .. DIR_DELIM .. "async_event.lua")
31 dofile(basepath .. DIR_DELIM .. "common" .. DIR_DELIM .. "filterlist.lua")
32 dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "buttonbar.lua")
33 dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "dialog.lua")
34 dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "tabview.lua")
35 dofile(basepath .. DIR_DELIM .. "fstk" .. DIR_DELIM .. "ui.lua")
36 dofile(menupath .. DIR_DELIM .. "common.lua")
37 dofile(menupath .. DIR_DELIM .. "gamemgr.lua")
38 dofile(menupath .. DIR_DELIM .. "modmgr.lua")
39 dofile(menupath .. DIR_DELIM .. "store.lua")
40 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")
44 dofile(menupath .. DIR_DELIM .. "dlg_settings_advanced.lua")
45 if PLATFORM ~= "Android" then
46         dofile(menupath .. DIR_DELIM .. "dlg_create_world.lua")
47         dofile(menupath .. DIR_DELIM .. "dlg_delete_mod.lua")
48         dofile(menupath .. DIR_DELIM .. "dlg_delete_world.lua")
49         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")
55 else
56         dofile(menupath .. DIR_DELIM .. "tab_simple_main.lua")
57 end
58
59 --------------------------------------------------------------------------------
60 local function main_event_handler(tabview, event)
61         if event == "MenuQuit" then
62                 core.close()
63         end
64         return true
65 end
66
67 --------------------------------------------------------------------------------
68 local function init_globals()
69         -- Init gamedata
70         gamedata.worldindex = 0
71
72
73         if PLATFORM ~= "Android" then
74                 menudata.worldlist = filterlist.create(
75                         core.get_worlds,
76                         compare_worlds,
77                         -- Unique id comparison function
78                         function(element, uid)
79                                 return element.name == uid
80                         end,
81                         -- Filter function
82                         function(element, gameid)
83                                 return element.gameid == gameid
84                         end
85                 )
86
87                 menudata.worldlist:add_sort_mechanism("alphabetic", sort_worlds_alphabetic)
88                 menudata.worldlist:set_sortmode("alphabetic")
89
90                 if not core.setting_get("menu_last_game") then
91                         local default_game = core.setting_get("default_game") or "minetest"
92                         core.setting_set("menu_last_game", default_game )
93                 end
94
95                 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
121         end
122
123         -- 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)
132         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)
138         end
139         tv_main:add(tab_mods)
140         tv_main:add(tab_credits)
141
142         tv_main:set_global_event_handler(main_event_handler)
143
144         tv_main:set_fixed_size(false)
145
146         if not (PLATFORM == "Android") then
147                 tv_main:set_tab(core.setting_get("maintab_LAST"))
148         end
149         ui.set_default("maintab")
150         tv_main:show()
151
152         -- Create modstore ui
153         if PLATFORM == "Android" then
154                 modstore.init({x=12, y=6}, 3, 2)
155         else
156                 modstore.init({x=12, y=8}, 4, 3)
157         end
158
159         ui.update()
160
161         core.sound_play("main_menu", true)
162 end
163
164 init_globals()
165