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 current_game, singleplayer_refresh_gamebar
20 local valid_disabled_settings = {
21 ["enable_damage"]=true,
22 ["creative_mode"]=true,
23 ["enable_server"]=true,
26 -- Currently chosen game in gamebar for theming and filtering
27 function current_game()
28 local last_game_id = core.settings:get("menu_last_game")
29 local game = pkgmgr.find_by_gameid(last_game_id)
34 -- Apply menu changes from given game
35 function apply_game(game)
36 core.set_topleft_text(game.name)
37 core.settings:set("menu_last_game", game.id)
38 menudata.worldlist:set_filtercriteria(game.id)
40 mm_game_theme.update("singleplayer", game) -- this refreshes the formspec
42 local index = filterlist.get_current_index(menudata.worldlist,
43 tonumber(core.settings:get("mainmenu_last_selected_world")))
44 if not index or index < 1 then
45 local selected = core.get_textlist_index("sp_worlds")
46 if selected ~= nil and selected < #menudata.worldlist:get_list() then
49 index = #menudata.worldlist:get_list()
52 menu_worldmt_legacy(index)
55 function singleplayer_refresh_gamebar()
57 local old_bar = ui.find_by_name("game_button_bar")
58 if old_bar ~= nil then
62 local function game_buttonbar_button_handler(fields)
63 if fields.game_open_cdb then
64 local maintab = ui.find_by_name("maintab")
65 local dlg = create_store_dlg("game")
66 dlg:set_parent(maintab)
72 for _, game in ipairs(pkgmgr.games) do
73 if fields["game_btnbar_" .. game.id] then
80 local btnbar = buttonbar_create("game_button_bar",
81 game_buttonbar_button_handler,
82 {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
84 for _, game in ipairs(pkgmgr.games) do
85 local btn_name = "game_btnbar_" .. game.id
89 local tooltip = core.formspec_escape(game.title)
91 if (game.menuicon_path or "") ~= "" then
92 image = core.formspec_escape(game.menuicon_path)
94 local part1 = game.id:sub(1,5)
95 local part2 = game.id:sub(6,10)
96 local part3 = game.id:sub(11)
98 text = part1 .. "\n" .. part2
100 text = text .. "\n" .. part3
103 btnbar:add_button(btn_name, text, image, tooltip)
106 local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
107 btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
110 local function get_disabled_settings(game)
115 local gameconfig = Settings(game.path .. "/game.conf")
116 local disabled_settings = {}
118 local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
119 for _, value in pairs(disabled_settings_str) do
122 if string.sub(value, 1, 1) == "!" then
124 value = string.sub(value, 2)
126 if valid_disabled_settings[value] then
127 disabled_settings[value] = state
129 core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
133 return disabled_settings
136 local function get_formspec(tabview, name, tabdata)
139 local index = filterlist.get_current_index(menudata.worldlist,
140 tonumber(core.settings:get("mainmenu_last_selected_world")))
141 local list = menudata.worldlist:get_list()
142 local world = list and index and list[index]
145 game = pkgmgr.find_by_gameid(world.gameid)
147 game = current_game()
149 local disabled_settings = get_disabled_settings(game)
151 local creative, damage, host = "", "", ""
153 -- Y offsets for game settings checkboxes
157 if disabled_settings["creative_mode"] == nil then
158 creative = "checkbox[0,"..y..";cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
159 dump(core.settings:get_bool("creative_mode")) .. "]"
162 if disabled_settings["enable_damage"] == nil then
163 damage = "checkbox[0,"..y..";cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
164 dump(core.settings:get_bool("enable_damage")) .. "]"
167 if disabled_settings["enable_server"] == nil then
168 host = "checkbox[0,"..y..";cb_server;".. fgettext("Host Server") ..";" ..
169 dump(core.settings:get_bool("enable_server")) .. "]"
174 "button[3.9,3.8;2.8,1;world_delete;".. fgettext("Delete") .. "]" ..
175 "button[6.55,3.8;2.8,1;world_configure;".. fgettext("Select Mods") .. "]" ..
176 "button[9.2,3.8;2.8,1;world_create;".. fgettext("New") .. "]" ..
177 "label[3.9,-0.05;".. fgettext("Select World:") .. "]"..
181 "textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
182 menu_render_worldlist() ..
185 if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
187 "button[7.9,4.75;4.1,1;play;".. fgettext("Host Game") .. "]" ..
188 "checkbox[0,"..y..";cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
189 dump(core.settings:get_bool("server_announce")) .. "]" ..
190 "field[0.3,2.85;3.8,0.5;te_playername;" .. fgettext("Name") .. ";" ..
191 core.formspec_escape(core.settings:get("name")) .. "]" ..
192 "pwdfield[0.3,4.05;3.8,0.5;te_passwd;" .. fgettext("Password") .. "]"
194 local bind_addr = core.settings:get("bind_address")
195 if bind_addr ~= nil and bind_addr ~= "" then
197 "field[0.3,5.25;2.5,0.5;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
198 core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
199 "field[2.85,5.25;1.25,0.5;te_serverport;" .. fgettext("Port") .. ";" ..
200 core.formspec_escape(core.settings:get("port")) .. "]"
203 "field[0.3,5.25;3.8,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
204 core.formspec_escape(core.settings:get("port")) .. "]"
208 "button[7.9,4.75;4.1,1;play;" .. fgettext("Play Game") .. "]"
214 local function main_button_handler(this, fields, name, tabdata)
216 assert(name == "local")
218 if this.dlg_create_world_closed_at == nil then
219 this.dlg_create_world_closed_at = 0
222 local world_doubleclick = false
224 if fields["sp_worlds"] ~= nil then
225 local event = core.explode_textlist_event(fields["sp_worlds"])
226 local selected = core.get_textlist_index("sp_worlds")
228 menu_worldmt_legacy(selected)
230 if event.type == "DCL" then
231 world_doubleclick = true
234 if event.type == "CHG" and selected ~= nil then
235 core.settings:set("mainmenu_last_selected_world",
236 menudata.worldlist:get_raw_index(selected))
241 if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
245 if fields["cb_creative_mode"] then
246 core.settings:set("creative_mode", fields["cb_creative_mode"])
247 local selected = core.get_textlist_index("sp_worlds")
248 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
253 if fields["cb_enable_damage"] then
254 core.settings:set("enable_damage", fields["cb_enable_damage"])
255 local selected = core.get_textlist_index("sp_worlds")
256 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
261 if fields["cb_server"] then
262 core.settings:set("enable_server", fields["cb_server"])
267 if fields["cb_server_announce"] then
268 core.settings:set("server_announce", fields["cb_server_announce"])
269 local selected = core.get_textlist_index("srv_worlds")
270 menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
275 if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
276 local enter_key_duration = core.get_us_time() - this.dlg_create_world_closed_at
277 if world_doubleclick and enter_key_duration <= 200000 then -- 200 ms
278 this.dlg_create_world_closed_at = 0
282 local selected = core.get_textlist_index("sp_worlds")
283 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
285 if selected == nil or gamedata.selected_world == 0 then
286 gamedata.errormessage =
287 fgettext("No world created or selected!")
292 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
295 game_obj = pkgmgr.find_by_gameid(world.gameid)
296 core.settings:set("menu_last_game", game_obj.id)
299 local disabled_settings = get_disabled_settings(game_obj)
300 for k, _ in pairs(valid_disabled_settings) do
301 local v = disabled_settings[k]
303 if k == "enable_server" and v == true then
304 error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
306 core.settings:set_bool(k, disabled_settings[k])
310 if core.settings:get_bool("enable_server") then
311 gamedata.playername = fields["te_playername"]
312 gamedata.password = fields["te_passwd"]
313 gamedata.port = fields["te_serverport"]
314 gamedata.address = ""
316 core.settings:set("port",gamedata.port)
317 if fields["te_serveraddr"] ~= nil then
318 core.settings:set("bind_address",fields["te_serveraddr"])
321 gamedata.singleplayer = true
328 if fields["world_create"] ~= nil then
329 this.dlg_create_world_closed_at = 0
330 local create_world_dlg = create_create_world_dlg()
331 create_world_dlg:set_parent(this)
333 create_world_dlg:show()
334 mm_game_theme.update("singleplayer", current_game())
338 if fields["world_delete"] ~= nil then
339 local selected = core.get_textlist_index("sp_worlds")
340 if selected ~= nil and
341 selected <= menudata.worldlist:size() then
342 local world = menudata.worldlist:get_list()[selected]
344 world.name ~= nil and
345 world.name ~= "" then
346 local index = menudata.worldlist:get_raw_index(selected)
347 local delete_world_dlg = create_delete_world_dlg(world.name,index)
348 delete_world_dlg:set_parent(this)
350 delete_world_dlg:show()
351 mm_game_theme.update("singleplayer",current_game())
358 if fields["world_configure"] ~= nil then
359 local selected = core.get_textlist_index("sp_worlds")
360 if selected ~= nil then
362 create_configure_world_dlg(
363 menudata.worldlist:get_raw_index(selected))
365 if (configdialog ~= nil) then
366 configdialog:set_parent(this)
369 mm_game_theme.update("singleplayer",current_game())
377 local function on_change(type, old_tab, new_tab)
378 if (type == "ENTER") then
379 local game = current_game()
384 singleplayer_refresh_gamebar()
385 ui.find_by_name("game_button_bar"):show()
387 menudata.worldlist:set_filtercriteria(nil)
388 local gamebar = ui.find_by_name("game_button_bar")
392 core.set_topleft_text("")
393 -- If new_tab is nil, a dialog is being shown; avoid resetting the theme
395 mm_game_theme.update(new_tab,nil)
400 --------------------------------------------------------------------------------
403 caption = fgettext("Start Game"),
404 cbf_formspec = get_formspec,
405 cbf_button_handler = main_button_handler,
406 on_change = on_change