2 --Copyright (C) 2014 sapier
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.
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.
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.
19 local enable_gamebar = PLATFORM ~= "Android"
20 local current_game, singleplayer_refresh_gamebar
21 local valid_disabled_settings = {
22 ["enable_damage"]=true,
23 ["creative_mode"]=true,
24 ["enable_server"]=true,
27 if enable_gamebar then
28 -- Currently chosen game in gamebar for theming and filtering
29 function current_game()
30 local last_game_id = core.settings:get("menu_last_game")
31 local game = pkgmgr.find_by_gameid(last_game_id)
36 -- Apply menu changes from given game
37 function apply_game(game)
38 core.set_topleft_text(game.name)
39 core.settings:set("menu_last_game", game.id)
40 menudata.worldlist:set_filtercriteria(game.id)
42 mm_game_theme.update("singleplayer", game) -- this refreshes the formspec
44 local index = filterlist.get_current_index(menudata.worldlist,
45 tonumber(core.settings:get("mainmenu_last_selected_world")))
46 if not index or index < 1 then
47 local selected = core.get_textlist_index("sp_worlds")
48 if selected ~= nil and selected < #menudata.worldlist:get_list() then
51 index = #menudata.worldlist:get_list()
54 menu_worldmt_legacy(index)
57 function singleplayer_refresh_gamebar()
59 local old_bar = ui.find_by_name("game_button_bar")
60 if old_bar ~= nil then
64 local function game_buttonbar_button_handler(fields)
65 if fields.game_open_cdb then
66 local maintab = ui.find_by_name("maintab")
67 local dlg = create_store_dlg("game")
68 dlg:set_parent(maintab)
74 for _, game in ipairs(pkgmgr.games) do
75 if fields["game_btnbar_" .. game.id] then
82 local btnbar = buttonbar_create("game_button_bar",
83 game_buttonbar_button_handler,
84 {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
86 for _, game in ipairs(pkgmgr.games) do
87 local btn_name = "game_btnbar_" .. game.id
91 local tooltip = core.formspec_escape(game.name)
93 if (game.menuicon_path or "") ~= "" then
94 image = core.formspec_escape(game.menuicon_path)
96 local part1 = game.id:sub(1,5)
97 local part2 = game.id:sub(6,10)
98 local part3 = game.id:sub(11)
100 text = part1 .. "\n" .. part2
102 text = text .. "\n" .. part3
105 btnbar:add_button(btn_name, text, image, tooltip)
108 local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
109 btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
112 -- Currently chosen game in gamebar: no gamebar -> no "current" game
113 function current_game()
118 local function get_disabled_settings(game)
123 local gameconfig = Settings(game.path .. "/game.conf")
124 local disabled_settings = {}
126 local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
127 for _, value in pairs(disabled_settings_str) do
130 if string.sub(value, 1, 1) == "!" then
132 value = string.sub(value, 2)
134 if valid_disabled_settings[value] then
135 disabled_settings[value] = state
137 core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
141 return disabled_settings
144 local function get_formspec(tabview, name, tabdata)
147 local index = filterlist.get_current_index(menudata.worldlist,
148 tonumber(core.settings:get("mainmenu_last_selected_world")))
149 local list = menudata.worldlist:get_list()
150 local world = list and index and list[index]
153 game = pkgmgr.find_by_gameid(world.gameid)
155 game = current_game()
157 local disabled_settings = get_disabled_settings(game)
159 local creative, damage, host = "", "", ""
161 -- Y offsets for game settings checkboxes
165 if disabled_settings["creative_mode"] == nil then
166 creative = "checkbox[0,"..y..";cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
167 dump(core.settings:get_bool("creative_mode")) .. "]"
170 if disabled_settings["enable_damage"] == nil then
171 damage = "checkbox[0,"..y..";cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
172 dump(core.settings:get_bool("enable_damage")) .. "]"
175 if disabled_settings["enable_server"] == nil then
176 host = "checkbox[0,"..y..";cb_server;".. fgettext("Host Server") ..";" ..
177 dump(core.settings:get_bool("enable_server")) .. "]"
182 "button[3.9,3.8;2.8,1;world_delete;".. fgettext("Delete") .. "]" ..
183 "button[6.55,3.8;2.8,1;world_configure;".. fgettext("Select Mods") .. "]" ..
184 "button[9.2,3.8;2.8,1;world_create;".. fgettext("New") .. "]" ..
185 "label[3.9,-0.05;".. fgettext("Select World:") .. "]"..
189 "textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
190 menu_render_worldlist(not enable_gamebar) ..
193 if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
195 "button[7.9,4.75;4.1,1;play;".. fgettext("Host Game") .. "]" ..
196 "checkbox[0,"..y..";cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
197 dump(core.settings:get_bool("server_announce")) .. "]" ..
198 "field[0.3,2.85;3.8,0.5;te_playername;" .. fgettext("Name") .. ";" ..
199 core.formspec_escape(core.settings:get("name")) .. "]" ..
200 "pwdfield[0.3,4.05;3.8,0.5;te_passwd;" .. fgettext("Password") .. "]"
202 local bind_addr = core.settings:get("bind_address")
203 if bind_addr ~= nil and bind_addr ~= "" then
205 "field[0.3,5.25;2.5,0.5;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
206 core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
207 "field[2.85,5.25;1.25,0.5;te_serverport;" .. fgettext("Port") .. ";" ..
208 core.formspec_escape(core.settings:get("port")) .. "]"
211 "field[0.3,5.25;3.8,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
212 core.formspec_escape(core.settings:get("port")) .. "]"
216 "button[7.9,4.75;4.1,1;play;" .. fgettext("Play Game") .. "]"
222 local function main_button_handler(this, fields, name, tabdata)
224 assert(name == "local")
226 local world_doubleclick = false
228 if fields["sp_worlds"] ~= nil then
229 local event = core.explode_textlist_event(fields["sp_worlds"])
230 local selected = core.get_textlist_index("sp_worlds")
232 menu_worldmt_legacy(selected)
234 if event.type == "DCL" then
235 world_doubleclick = true
238 if event.type == "CHG" and selected ~= nil then
239 core.settings:set("mainmenu_last_selected_world",
240 menudata.worldlist:get_raw_index(selected))
245 if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
249 if fields["cb_creative_mode"] then
250 core.settings:set("creative_mode", fields["cb_creative_mode"])
251 local selected = core.get_textlist_index("sp_worlds")
252 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
257 if fields["cb_enable_damage"] then
258 core.settings:set("enable_damage", fields["cb_enable_damage"])
259 local selected = core.get_textlist_index("sp_worlds")
260 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
265 if fields["cb_server"] then
266 core.settings:set("enable_server", fields["cb_server"])
271 if fields["cb_server_announce"] then
272 core.settings:set("server_announce", fields["cb_server_announce"])
273 local selected = core.get_textlist_index("srv_worlds")
274 menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
279 if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
280 local selected = core.get_textlist_index("sp_worlds")
281 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
283 if selected == nil or gamedata.selected_world == 0 then
284 gamedata.errormessage =
285 fgettext("No world created or selected!")
290 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
293 game_obj = pkgmgr.find_by_gameid(world.gameid)
294 core.settings:set("menu_last_game", game_obj.id)
297 local disabled_settings = get_disabled_settings(game_obj)
298 for k, _ in pairs(valid_disabled_settings) do
299 local v = disabled_settings[k]
301 if k == "enable_server" and v == true then
302 error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
304 core.settings:set_bool(k, disabled_settings[k])
308 if core.settings:get_bool("enable_server") then
309 gamedata.playername = fields["te_playername"]
310 gamedata.password = fields["te_passwd"]
311 gamedata.port = fields["te_serverport"]
312 gamedata.address = ""
314 core.settings:set("port",gamedata.port)
315 if fields["te_serveraddr"] ~= nil then
316 core.settings:set("bind_address",fields["te_serveraddr"])
319 gamedata.singleplayer = true
326 if fields["world_create"] ~= nil then
327 local create_world_dlg = create_create_world_dlg(enable_gamebar)
328 create_world_dlg:set_parent(this)
330 create_world_dlg:show()
331 mm_game_theme.update("singleplayer", current_game())
335 if fields["world_delete"] ~= nil then
336 local selected = core.get_textlist_index("sp_worlds")
337 if selected ~= nil and
338 selected <= menudata.worldlist:size() then
339 local world = menudata.worldlist:get_list()[selected]
341 world.name ~= nil and
342 world.name ~= "" then
343 local index = menudata.worldlist:get_raw_index(selected)
344 local delete_world_dlg = create_delete_world_dlg(world.name,index)
345 delete_world_dlg:set_parent(this)
347 delete_world_dlg:show()
348 mm_game_theme.update("singleplayer",current_game())
355 if fields["world_configure"] ~= nil then
356 local selected = core.get_textlist_index("sp_worlds")
357 if selected ~= nil then
359 create_configure_world_dlg(
360 menudata.worldlist:get_raw_index(selected))
362 if (configdialog ~= nil) then
363 configdialog:set_parent(this)
366 mm_game_theme.update("singleplayer",current_game())
375 if enable_gamebar then
376 function on_change(type, old_tab, new_tab)
377 if (type == "ENTER") then
378 local game = current_game()
383 singleplayer_refresh_gamebar()
384 ui.find_by_name("game_button_bar"):show()
386 menudata.worldlist:set_filtercriteria(nil)
387 local gamebar = ui.find_by_name("game_button_bar")
391 core.set_topleft_text("")
392 mm_game_theme.update(new_tab,nil)
397 --------------------------------------------------------------------------------
400 caption = fgettext("Start Game"),
401 cbf_formspec = get_formspec,
402 cbf_button_handler = main_button_handler,
403 on_change = on_change