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