]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/tab_local.lua
Add buttons to ContentDB in game bar and configure world (#9944)
[minetest.git] / builtin / mainmenu / tab_local.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
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 = pkgmgr.find_by_gameid(last_game_id)
25
26                 return game
27         end
28
29         function singleplayer_refresh_gamebar()
30
31                 local old_bar = ui.find_by_name("game_button_bar")
32
33                 if old_bar ~= nil then
34                         old_bar:delete()
35                 end
36
37                 local function game_buttonbar_button_handler(fields)
38                         if fields.game_open_cdb then
39                                 local maintab = ui.find_by_name("maintab")
40                                 local dlg = create_store_dlg("game")
41                                 dlg:set_parent(maintab)
42                                 maintab:hide()
43                                 dlg:show()
44                                 return true
45                         end
46
47                         for key,value in pairs(fields) do
48                                 for j=1,#pkgmgr.games,1 do
49                                         if ("game_btnbar_" .. pkgmgr.games[j].id == key) then
50                                                 mm_texture.update("singleplayer", pkgmgr.games[j])
51                                                 core.set_topleft_text(pkgmgr.games[j].name)
52                                                 core.settings:set("menu_last_game",pkgmgr.games[j].id)
53                                                 menudata.worldlist:set_filtercriteria(pkgmgr.games[j].id)
54                                                 local index = filterlist.get_current_index(menudata.worldlist,
55                                                         tonumber(core.settings:get("mainmenu_last_selected_world")))
56                                                 if not index or index < 1 then
57                                                         local selected = core.get_textlist_index("sp_worlds")
58                                                         if selected ~= nil and selected < #menudata.worldlist:get_list() then
59                                                                 index = selected
60                                                         else
61                                                                 index = #menudata.worldlist:get_list()
62                                                         end
63                                                 end
64                                                 menu_worldmt_legacy(index)
65                                                 return true
66                                         end
67                                 end
68                         end
69                 end
70
71                 local btnbar = buttonbar_create("game_button_bar",
72                         game_buttonbar_button_handler,
73                         {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
74
75                 for i=1,#pkgmgr.games,1 do
76                         local btn_name = "game_btnbar_" .. pkgmgr.games[i].id
77
78                         local image = nil
79                         local text = nil
80                         local tooltip = core.formspec_escape(pkgmgr.games[i].name)
81
82                         if pkgmgr.games[i].menuicon_path ~= nil and
83                                 pkgmgr.games[i].menuicon_path ~= "" then
84                                 image = core.formspec_escape(pkgmgr.games[i].menuicon_path)
85                         else
86
87                                 local part1 = pkgmgr.games[i].id:sub(1,5)
88                                 local part2 = pkgmgr.games[i].id:sub(6,10)
89                                 local part3 = pkgmgr.games[i].id:sub(11)
90
91                                 text = part1 .. "\n" .. part2
92                                 if part3 ~= nil and
93                                         part3 ~= "" then
94                                         text = text .. "\n" .. part3
95                                 end
96                         end
97                         btnbar:add_button(btn_name, text, image, tooltip)
98                 end
99
100                 local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
101                 btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
102         end
103 else
104         function current_game()
105                 return nil
106         end
107 end
108
109 local function get_formspec(tabview, name, tabdata)
110         local retval = ""
111
112         local index = filterlist.get_current_index(menudata.worldlist,
113                                 tonumber(core.settings:get("mainmenu_last_selected_world"))
114                                 )
115
116         retval = retval ..
117                         "button[4,3.95;2.6,1;world_delete;".. fgettext("Delete") .. "]" ..
118                         "button[6.5,3.95;2.8,1;world_configure;".. fgettext("Configure") .. "]" ..
119                         "button[9.2,3.95;2.5,1;world_create;".. fgettext("New") .. "]" ..
120                         "label[4,-0.25;".. fgettext("Select World:") .. "]"..
121                         "checkbox[0.25,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
122                         dump(core.settings:get_bool("creative_mode")) .. "]"..
123                         "checkbox[0.25,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
124                         dump(core.settings:get_bool("enable_damage")) .. "]"..
125                         "checkbox[0.25,1.15;cb_server;".. fgettext("Host Server") ..";" ..
126                         dump(core.settings:get_bool("enable_server")) .. "]" ..
127                         "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
128                         menu_render_worldlist() ..
129                         ";" .. index .. "]"
130
131         if core.settings:get_bool("enable_server") then
132                 retval = retval ..
133                                 "button[8.5,4.8;3.2,1;play;".. fgettext("Host Game") .. "]" ..
134                                 "checkbox[0.25,1.6;cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
135                                 dump(core.settings:get_bool("server_announce")) .. "]" ..
136                                 "label[0.25,2.2;" .. fgettext("Name/Password") .. "]" ..
137                                 "field[0.55,3.2;3.5,0.5;te_playername;;" ..
138                                 core.formspec_escape(core.settings:get("name")) .. "]" ..
139                                 "pwdfield[0.55,4;3.5,0.5;te_passwd;]"
140
141                 local bind_addr = core.settings:get("bind_address")
142                 if bind_addr ~= nil and bind_addr ~= "" then
143                         retval = retval ..
144                                 "field[0.55,5.2;2.25,0.5;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
145                                 core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
146                                 "field[2.8,5.2;1.25,0.5;te_serverport;" .. fgettext("Port") .. ";" ..
147                                 core.formspec_escape(core.settings:get("port")) .. "]"
148                 else
149                         retval = retval ..
150                                 "field[0.55,5.2;3.5,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
151                                 core.formspec_escape(core.settings:get("port")) .. "]"
152                 end
153         else
154                 retval = retval ..
155                                 "button[8.5,4.8;3.2,1;play;".. fgettext("Play Game") .. "]"
156         end
157
158         return retval
159 end
160
161 local function main_button_handler(this, fields, name, tabdata)
162
163         assert(name == "local")
164
165         local world_doubleclick = false
166
167         if fields["sp_worlds"] ~= nil then
168                 local event = core.explode_textlist_event(fields["sp_worlds"])
169                 local selected = core.get_textlist_index("sp_worlds")
170
171                 menu_worldmt_legacy(selected)
172
173                 if event.type == "DCL" then
174                         world_doubleclick = true
175                 end
176
177                 if event.type == "CHG" and selected ~= nil then
178                         core.settings:set("mainmenu_last_selected_world",
179                                 menudata.worldlist:get_raw_index(selected))
180                         return true
181                 end
182         end
183
184         if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
185                 return true
186         end
187
188         if fields["cb_creative_mode"] then
189                 core.settings:set("creative_mode", fields["cb_creative_mode"])
190                 local selected = core.get_textlist_index("sp_worlds")
191                 menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
192
193                 return true
194         end
195
196         if fields["cb_enable_damage"] then
197                 core.settings:set("enable_damage", fields["cb_enable_damage"])
198                 local selected = core.get_textlist_index("sp_worlds")
199                 menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
200
201                 return true
202         end
203
204         if fields["cb_server"] then
205                 core.settings:set("enable_server", fields["cb_server"])
206
207                 return true
208         end
209
210         if fields["cb_server_announce"] then
211                 core.settings:set("server_announce", fields["cb_server_announce"])
212                 local selected = core.get_textlist_index("srv_worlds")
213                 menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
214
215                 return true
216         end
217
218         if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
219                 local selected = core.get_textlist_index("sp_worlds")
220                 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
221
222                 if selected == nil or gamedata.selected_world == 0 then
223                         gamedata.errormessage =
224                                         fgettext("No world created or selected!")
225                         return true
226                 end
227
228                 -- Update last game
229                 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
230                 if world then
231                         local game = pkgmgr.find_by_gameid(world.gameid)
232                         core.settings:set("menu_last_game", game.id)
233                 end
234
235                 if core.settings:get_bool("enable_server") then
236                         gamedata.playername = fields["te_playername"]
237                         gamedata.password   = fields["te_passwd"]
238                         gamedata.port       = fields["te_serverport"]
239                         gamedata.address    = ""
240
241                         core.settings:set("port",gamedata.port)
242                         if fields["te_serveraddr"] ~= nil then
243                                 core.settings:set("bind_address",fields["te_serveraddr"])
244                         end
245                 else
246                         gamedata.singleplayer = true
247                 end
248
249                 core.start()
250                 return true
251         end
252
253         if fields["world_create"] ~= nil then
254                 local create_world_dlg = create_create_world_dlg(true)
255                 create_world_dlg:set_parent(this)
256                 this:hide()
257                 create_world_dlg:show()
258                 mm_texture.update("singleplayer", current_game())
259                 return true
260         end
261
262         if fields["world_delete"] ~= nil then
263                 local selected = core.get_textlist_index("sp_worlds")
264                 if selected ~= nil and
265                         selected <= menudata.worldlist:size() then
266                         local world = menudata.worldlist:get_list()[selected]
267                         if world ~= nil and
268                                 world.name ~= nil and
269                                 world.name ~= "" then
270                                 local index = menudata.worldlist:get_raw_index(selected)
271                                 local delete_world_dlg = create_delete_world_dlg(world.name,index)
272                                 delete_world_dlg:set_parent(this)
273                                 this:hide()
274                                 delete_world_dlg:show()
275                                 mm_texture.update("singleplayer",current_game())
276                         end
277                 end
278
279                 return true
280         end
281
282         if fields["world_configure"] ~= nil then
283                 local selected = core.get_textlist_index("sp_worlds")
284                 if selected ~= nil then
285                         local configdialog =
286                                 create_configure_world_dlg(
287                                                 menudata.worldlist:get_raw_index(selected))
288
289                         if (configdialog ~= nil) then
290                                 configdialog:set_parent(this)
291                                 this:hide()
292                                 configdialog:show()
293                                 mm_texture.update("singleplayer",current_game())
294                         end
295                 end
296
297                 return true
298         end
299 end
300
301 local on_change
302 if enable_gamebar then
303         function on_change(type, old_tab, new_tab)
304                 if (type == "ENTER") then
305                         local game = current_game()
306
307                         if game then
308                                 menudata.worldlist:set_filtercriteria(game.id)
309                                 core.set_topleft_text(game.name)
310                                 mm_texture.update("singleplayer",game)
311                         end
312
313                         singleplayer_refresh_gamebar()
314                         ui.find_by_name("game_button_bar"):show()
315                 else
316                         menudata.worldlist:set_filtercriteria(nil)
317                         local gamebar = ui.find_by_name("game_button_bar")
318                         if gamebar then
319                                 gamebar:hide()
320                         end
321                         core.set_topleft_text("")
322                         mm_texture.update(new_tab,nil)
323                 end
324         end
325 end
326
327 --------------------------------------------------------------------------------
328 return {
329         name = "local",
330         caption = fgettext("Start Game"),
331         cbf_formspec = get_formspec,
332         cbf_button_handler = main_button_handler,
333         on_change = on_change
334 }