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.
18 local function current_game()
19 local last_game_id = core.setting_get("menu_last_game")
20 local game, index = gamemgr.find_by_gameid(last_game_id)
25 local function singleplayer_refresh_gamebar()
27 local old_bar = ui.find_by_name("game_button_bar")
29 if old_bar ~= nil then
33 local function game_buttonbar_button_handler(fields)
34 for key,value in pairs(fields) do
35 for j=1,#gamemgr.games,1 do
36 if ("game_btnbar_" .. gamemgr.games[j].id == key) then
37 mm_texture.update("singleplayer", gamemgr.games[j])
38 core.set_topleft_text(gamemgr.games[j].name)
39 core.setting_set("menu_last_game",gamemgr.games[j].id)
40 menudata.worldlist:set_filtercriteria(gamemgr.games[j].id)
41 local index = filterlist.get_current_index(menudata.worldlist,
42 tonumber(core.setting_get("mainmenu_last_selected_world")))
43 if not index or index < 1 then
44 local selected = core.get_textlist_index("sp_worlds")
45 if selected ~= nil and selected < #menudata.worldlist:get_list() then
48 index = #menudata.worldlist:get_list()
51 menu_worldmt_legacy(index)
58 local btnbar = buttonbar_create("game_button_bar",
59 game_buttonbar_button_handler,
60 {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
62 for i=1,#gamemgr.games,1 do
63 local btn_name = "game_btnbar_" .. gamemgr.games[i].id
67 local tooltip = core.formspec_escape(gamemgr.games[i].name)
69 if gamemgr.games[i].menuicon_path ~= nil and
70 gamemgr.games[i].menuicon_path ~= "" then
71 image = core.formspec_escape(gamemgr.games[i].menuicon_path)
74 local part1 = gamemgr.games[i].id:sub(1,5)
75 local part2 = gamemgr.games[i].id:sub(6,10)
76 local part3 = gamemgr.games[i].id:sub(11)
78 text = part1 .. "\n" .. part2
81 text = text .. "\n" .. part3
84 btnbar:add_button(btn_name, text, image, tooltip)
88 local function get_formspec(tabview, name, tabdata)
91 local index = filterlist.get_current_index(menudata.worldlist,
92 tonumber(core.setting_get("mainmenu_last_selected_world"))
96 "button[4,4.15;2.6,0.5;world_delete;".. fgettext("Delete") .. "]" ..
97 "button[6.5,4.15;2.8,0.5;world_create;".. fgettext("New") .. "]" ..
98 "button[9.2,4.15;2.55,0.5;world_configure;".. fgettext("Configure") .. "]" ..
99 "button[8.5,5;3.25,0.5;play;".. fgettext("Play") .. "]" ..
100 "label[4,-0.25;".. fgettext("Select World:") .. "]"..
101 "checkbox[0.25,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
102 dump(core.setting_getbool("creative_mode")) .. "]"..
103 "checkbox[0.25,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
104 dump(core.setting_getbool("enable_damage")) .. "]"..
105 "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
106 menu_render_worldlist() ..
111 local function main_button_handler(this, fields, name, tabdata)
113 assert(name == "singleplayer")
115 local world_doubleclick = false
117 if fields["sp_worlds"] ~= nil then
118 local event = core.explode_textlist_event(fields["sp_worlds"])
119 local selected = core.get_textlist_index("sp_worlds")
121 menu_worldmt_legacy(selected)
123 if event.type == "DCL" then
124 world_doubleclick = true
127 if event.type == "CHG" and selected ~= nil then
128 core.setting_set("mainmenu_last_selected_world",
129 menudata.worldlist:get_raw_index(selected))
134 if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
138 if fields["cb_creative_mode"] then
139 core.setting_set("creative_mode", fields["cb_creative_mode"])
140 local selected = core.get_textlist_index("sp_worlds")
141 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
146 if fields["cb_enable_damage"] then
147 core.setting_set("enable_damage", fields["cb_enable_damage"])
148 local selected = core.get_textlist_index("sp_worlds")
149 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
154 if fields["play"] ~= nil or
156 fields["key_enter"] then
157 local selected = core.get_textlist_index("sp_worlds")
158 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
160 if selected ~= nil and gamedata.selected_world ~= 0 then
161 gamedata.singleplayer = true
164 gamedata.errormessage =
165 fgettext("No world created or selected!")
170 if fields["world_create"] ~= nil then
171 local create_world_dlg = create_create_world_dlg(true)
172 create_world_dlg:set_parent(this)
174 create_world_dlg:show()
175 mm_texture.update("singleplayer",current_game())
179 if fields["world_delete"] ~= nil then
180 local selected = core.get_textlist_index("sp_worlds")
181 if selected ~= nil and
182 selected <= menudata.worldlist:size() then
183 local world = menudata.worldlist:get_list()[selected]
185 world.name ~= nil and
186 world.name ~= "" then
187 local index = menudata.worldlist:get_raw_index(selected)
188 local delete_world_dlg = create_delete_world_dlg(world.name,index)
189 delete_world_dlg:set_parent(this)
191 delete_world_dlg:show()
192 mm_texture.update("singleplayer",current_game())
199 if fields["world_configure"] ~= nil then
200 local selected = core.get_textlist_index("sp_worlds")
201 if selected ~= nil then
203 create_configure_world_dlg(
204 menudata.worldlist:get_raw_index(selected))
206 if (configdialog ~= nil) then
207 configdialog:set_parent(this)
210 mm_texture.update("singleplayer",current_game())
218 local function on_change(type, old_tab, new_tab)
219 local buttonbar = ui.find_by_name("game_button_bar")
221 if ( buttonbar == nil ) then
222 singleplayer_refresh_gamebar()
223 buttonbar = ui.find_by_name("game_button_bar")
226 if (type == "ENTER") then
227 local game = current_game()
230 menudata.worldlist:set_filtercriteria(game.id)
231 core.set_topleft_text(game.name)
232 mm_texture.update("singleplayer",game)
236 menudata.worldlist:set_filtercriteria(nil)
238 core.set_topleft_text("")
239 mm_texture.update(new_tab,nil)
243 --------------------------------------------------------------------------------
245 name = "singleplayer",
246 caption = fgettext("Singleplayer"),
247 cbf_formspec = get_formspec,
248 cbf_button_handler = main_button_handler,
249 on_change = on_change