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 if enable_gamebar then
22 function current_game()
23 local last_game_id = core.settings:get("menu_last_game")
24 local game, index = pkgmgr.find_by_gameid(last_game_id)
29 function singleplayer_refresh_gamebar()
31 local old_bar = ui.find_by_name("game_button_bar")
33 if old_bar ~= nil then
37 local function game_buttonbar_button_handler(fields)
38 for key,value in pairs(fields) do
39 for j=1,#pkgmgr.games,1 do
40 if ("game_btnbar_" .. pkgmgr.games[j].id == key) then
41 mm_texture.update("singleplayer", pkgmgr.games[j])
42 core.set_topleft_text(pkgmgr.games[j].name)
43 core.settings:set("menu_last_game",pkgmgr.games[j].id)
44 menudata.worldlist:set_filtercriteria(pkgmgr.games[j].id)
45 local index = filterlist.get_current_index(menudata.worldlist,
46 tonumber(core.settings:get("mainmenu_last_selected_world")))
47 if not index or index < 1 then
48 local selected = core.get_textlist_index("sp_worlds")
49 if selected ~= nil and selected < #menudata.worldlist:get_list() then
52 index = #menudata.worldlist:get_list()
55 menu_worldmt_legacy(index)
62 local btnbar = buttonbar_create("game_button_bar",
63 game_buttonbar_button_handler,
64 {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
66 for i=1,#pkgmgr.games,1 do
67 local btn_name = "game_btnbar_" .. pkgmgr.games[i].id
71 local tooltip = core.formspec_escape(pkgmgr.games[i].name)
73 if pkgmgr.games[i].menuicon_path ~= nil and
74 pkgmgr.games[i].menuicon_path ~= "" then
75 image = core.formspec_escape(pkgmgr.games[i].menuicon_path)
78 local part1 = pkgmgr.games[i].id:sub(1,5)
79 local part2 = pkgmgr.games[i].id:sub(6,10)
80 local part3 = pkgmgr.games[i].id:sub(11)
82 text = part1 .. "\n" .. part2
85 text = text .. "\n" .. part3
88 btnbar:add_button(btn_name, text, image, tooltip)
92 function current_game()
97 local function get_formspec(tabview, name, tabdata)
100 local index = filterlist.get_current_index(menudata.worldlist,
101 tonumber(core.settings:get("mainmenu_last_selected_world"))
105 "button[4,3.95;2.6,1;world_delete;".. fgettext("Delete") .. "]" ..
106 "button[6.5,3.95;2.8,1;world_configure;".. fgettext("Configure") .. "]" ..
107 "button[9.2,3.95;2.5,1;world_create;".. fgettext("New") .. "]" ..
108 "label[4,-0.25;".. fgettext("Select World:") .. "]"..
109 "checkbox[0.25,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
110 dump(core.settings:get_bool("creative_mode")) .. "]"..
111 "checkbox[0.25,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
112 dump(core.settings:get_bool("enable_damage")) .. "]"..
113 "checkbox[0.25,1.15;cb_server;".. fgettext("Host Server") ..";" ..
114 dump(core.settings:get_bool("enable_server")) .. "]" ..
115 "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
116 menu_render_worldlist() ..
119 if core.settings:get_bool("enable_server") then
121 "button[8.5,4.8;3.2,1;play;".. fgettext("Host Game") .. "]" ..
122 "checkbox[0.25,1.6;cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
123 dump(core.settings:get_bool("server_announce")) .. "]" ..
124 "label[0.25,2.2;" .. fgettext("Name/Password") .. "]" ..
125 "field[0.55,3.2;3.5,0.5;te_playername;;" ..
126 core.formspec_escape(core.settings:get("name")) .. "]" ..
127 "pwdfield[0.55,4;3.5,0.5;te_passwd;]"
129 local bind_addr = core.settings:get("bind_address")
130 if bind_addr ~= nil and bind_addr ~= "" then
132 "field[0.55,5.2;2.25,0.5;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
133 core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
134 "field[2.8,5.2;1.25,0.5;te_serverport;" .. fgettext("Port") .. ";" ..
135 core.formspec_escape(core.settings:get("port")) .. "]"
138 "field[0.55,5.2;3.5,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
139 core.formspec_escape(core.settings:get("port")) .. "]"
143 "button[8.5,4.8;3.2,1;play;".. fgettext("Play Game") .. "]"
149 local function main_button_handler(this, fields, name, tabdata)
151 assert(name == "local")
153 local world_doubleclick = false
155 if fields["sp_worlds"] ~= nil then
156 local event = core.explode_textlist_event(fields["sp_worlds"])
157 local selected = core.get_textlist_index("sp_worlds")
159 menu_worldmt_legacy(selected)
161 if event.type == "DCL" then
162 world_doubleclick = true
165 if event.type == "CHG" and selected ~= nil then
166 core.settings:set("mainmenu_last_selected_world",
167 menudata.worldlist:get_raw_index(selected))
172 if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
176 if fields["cb_creative_mode"] then
177 core.settings:set("creative_mode", fields["cb_creative_mode"])
178 local selected = core.get_textlist_index("sp_worlds")
179 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
184 if fields["cb_enable_damage"] then
185 core.settings:set("enable_damage", fields["cb_enable_damage"])
186 local selected = core.get_textlist_index("sp_worlds")
187 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
192 if fields["cb_server"] then
193 core.settings:set("enable_server", fields["cb_server"])
198 if fields["cb_server_announce"] then
199 core.settings:set("server_announce", fields["cb_server_announce"])
200 local selected = core.get_textlist_index("srv_worlds")
201 menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
206 if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
207 local selected = core.get_textlist_index("sp_worlds")
208 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
210 if core.settings:get_bool("enable_server") then
211 if selected ~= nil and gamedata.selected_world ~= 0 then
212 gamedata.playername = fields["te_playername"]
213 gamedata.password = fields["te_passwd"]
214 gamedata.port = fields["te_serverport"]
215 gamedata.address = ""
217 core.settings:set("port",gamedata.port)
218 if fields["te_serveraddr"] ~= nil then
219 core.settings:set("bind_address",fields["te_serveraddr"])
223 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
225 local game, index = pkgmgr.find_by_gameid(world.gameid)
226 core.settings:set("menu_last_game", game.id)
231 gamedata.errormessage =
232 fgettext("No world created or selected!")
235 if selected ~= nil and gamedata.selected_world ~= 0 then
236 gamedata.singleplayer = true
239 gamedata.errormessage =
240 fgettext("No world created or selected!")
246 if fields["world_create"] ~= nil then
247 local create_world_dlg = create_create_world_dlg(true)
248 create_world_dlg:set_parent(this)
250 create_world_dlg:show()
251 mm_texture.update("singleplayer", current_game())
255 if fields["world_delete"] ~= nil then
256 local selected = core.get_textlist_index("sp_worlds")
257 if selected ~= nil and
258 selected <= menudata.worldlist:size() then
259 local world = menudata.worldlist:get_list()[selected]
261 world.name ~= nil and
262 world.name ~= "" then
263 local index = menudata.worldlist:get_raw_index(selected)
264 local delete_world_dlg = create_delete_world_dlg(world.name,index)
265 delete_world_dlg:set_parent(this)
267 delete_world_dlg:show()
268 mm_texture.update("singleplayer",current_game())
275 if fields["world_configure"] ~= nil then
276 local selected = core.get_textlist_index("sp_worlds")
277 if selected ~= nil then
279 create_configure_world_dlg(
280 menudata.worldlist:get_raw_index(selected))
282 if (configdialog ~= nil) then
283 configdialog:set_parent(this)
286 mm_texture.update("singleplayer",current_game())
295 if enable_gamebar then
296 function on_change(type, old_tab, new_tab)
297 local buttonbar = ui.find_by_name("game_button_bar")
299 if ( buttonbar == nil ) then
300 singleplayer_refresh_gamebar()
301 buttonbar = ui.find_by_name("game_button_bar")
304 if (type == "ENTER") then
305 local game = current_game()
308 menudata.worldlist:set_filtercriteria(game.id)
309 core.set_topleft_text(game.name)
310 mm_texture.update("singleplayer",game)
314 menudata.worldlist:set_filtercriteria(nil)
316 core.set_topleft_text("")
317 mm_texture.update(new_tab,nil)
322 --------------------------------------------------------------------------------
325 caption = fgettext("Start Game"),
326 cbf_formspec = get_formspec,
327 cbf_button_handler = main_button_handler,
328 on_change = on_change