]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/tab_local.lua
Fix Enter key after creating a new world (#12997)
[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 current_game, singleplayer_refresh_gamebar
20 local valid_disabled_settings = {
21         ["enable_damage"]=true,
22         ["creative_mode"]=true,
23         ["enable_server"]=true,
24 }
25
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)
30
31         return game
32 end
33
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)
39
40         mm_game_theme.update("singleplayer", game) -- this refreshes the formspec
41
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
47                         index = selected
48                 else
49                         index = #menudata.worldlist:get_list()
50                 end
51         end
52         menu_worldmt_legacy(index)
53 end
54
55 function singleplayer_refresh_gamebar()
56
57         local old_bar = ui.find_by_name("game_button_bar")
58         if old_bar ~= nil then
59                 old_bar:delete()
60         end
61
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)
67                         maintab:hide()
68                         dlg:show()
69                         return true
70                 end
71
72                 for _, game in ipairs(pkgmgr.games) do
73                         if fields["game_btnbar_" .. game.id] then
74                                 apply_game(game)
75                                 return true
76                         end
77                 end
78         end
79
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})
83
84         for _, game in ipairs(pkgmgr.games) do
85                 local btn_name = "game_btnbar_" .. game.id
86
87                 local image = nil
88                 local text = nil
89                 local tooltip = core.formspec_escape(game.title)
90
91                 if (game.menuicon_path or "") ~= "" then
92                         image = core.formspec_escape(game.menuicon_path)
93                 else
94                         local part1 = game.id:sub(1,5)
95                         local part2 = game.id:sub(6,10)
96                         local part3 = game.id:sub(11)
97
98                         text = part1 .. "\n" .. part2
99                         if part3 ~= "" then
100                                 text = text .. "\n" .. part3
101                         end
102                 end
103                 btnbar:add_button(btn_name, text, image, tooltip)
104         end
105
106         local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
107         btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
108 end
109
110 local function get_disabled_settings(game)
111         if not game then
112                 return {}
113         end
114
115         local gameconfig = Settings(game.path .. "/game.conf")
116         local disabled_settings = {}
117         if gameconfig then
118                 local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
119                 for _, value in pairs(disabled_settings_str) do
120                         local state = false
121                         value = value:trim()
122                         if string.sub(value, 1, 1) == "!" then
123                                 state = true
124                                 value = string.sub(value, 2)
125                         end
126                         if valid_disabled_settings[value] then
127                                 disabled_settings[value] = state
128                         else
129                                 core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
130                         end
131                 end
132         end
133         return disabled_settings
134 end
135
136 local function get_formspec(tabview, name, tabdata)
137         local retval = ""
138
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]
143         local game
144         if world then
145                 game = pkgmgr.find_by_gameid(world.gameid)
146         else
147                 game = current_game()
148         end
149         local disabled_settings = get_disabled_settings(game)
150
151         local creative, damage, host = "", "", ""
152
153         -- Y offsets for game settings checkboxes
154         local y = -0.2
155         local yo = 0.45
156
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")) .. "]"
160                 y = y + yo
161         end
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")) .. "]"
165                 y = y + yo
166         end
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")) .. "]"
170                 y = y + yo
171         end
172
173         retval = retval ..
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:") .. "]"..
178                         creative ..
179                         damage ..
180                         host ..
181                         "textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
182                         menu_render_worldlist() ..
183                         ";" .. index .. "]"
184
185         if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
186                 retval = retval ..
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") .. "]"
193
194                 local bind_addr = core.settings:get("bind_address")
195                 if bind_addr ~= nil and bind_addr ~= "" then
196                         retval = retval ..
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")) .. "]"
201                 else
202                         retval = retval ..
203                                 "field[0.3,5.25;3.8,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
204                                 core.formspec_escape(core.settings:get("port")) .. "]"
205                 end
206         else
207                 retval = retval ..
208                                 "button[7.9,4.75;4.1,1;play;" .. fgettext("Play Game") .. "]"
209         end
210
211         return retval
212 end
213
214 local function main_button_handler(this, fields, name, tabdata)
215
216         assert(name == "local")
217
218         if this.dlg_create_world_closed_at == nil then
219                 this.dlg_create_world_closed_at = 0
220         end
221
222         local world_doubleclick = false
223
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")
227
228                 menu_worldmt_legacy(selected)
229
230                 if event.type == "DCL" then
231                         world_doubleclick = true
232                 end
233
234                 if event.type == "CHG" and selected ~= nil then
235                         core.settings:set("mainmenu_last_selected_world",
236                                 menudata.worldlist:get_raw_index(selected))
237                         return true
238                 end
239         end
240
241         if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
242                 return true
243         end
244
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"])
249
250                 return true
251         end
252
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"])
257
258                 return true
259         end
260
261         if fields["cb_server"] then
262                 core.settings:set("enable_server", fields["cb_server"])
263
264                 return true
265         end
266
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"])
271
272                 return true
273         end
274
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
279                         return true
280                 end
281
282                 local selected = core.get_textlist_index("sp_worlds")
283                 gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
284
285                 if selected == nil or gamedata.selected_world == 0 then
286                         gamedata.errormessage =
287                                         fgettext("No world created or selected!")
288                         return true
289                 end
290
291                 -- Update last game
292                 local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
293                 local game_obj
294                 if world then
295                         game_obj = pkgmgr.find_by_gameid(world.gameid)
296                         core.settings:set("menu_last_game", game_obj.id)
297                 end
298
299                 local disabled_settings = get_disabled_settings(game_obj)
300                 for k, _ in pairs(valid_disabled_settings) do
301                         local v = disabled_settings[k]
302                         if v ~= nil then
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.")
305                                 end
306                                 core.settings:set_bool(k, disabled_settings[k])
307                         end
308                 end
309
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    = ""
315
316                         core.settings:set("port",gamedata.port)
317                         if fields["te_serveraddr"] ~= nil then
318                                 core.settings:set("bind_address",fields["te_serveraddr"])
319                         end
320                 else
321                         gamedata.singleplayer = true
322                 end
323
324                 core.start()
325                 return true
326         end
327
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)
332                 this:hide()
333                 create_world_dlg:show()
334                 mm_game_theme.update("singleplayer", current_game())
335                 return true
336         end
337
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]
343                         if world ~= nil and
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)
349                                 this:hide()
350                                 delete_world_dlg:show()
351                                 mm_game_theme.update("singleplayer",current_game())
352                         end
353                 end
354
355                 return true
356         end
357
358         if fields["world_configure"] ~= nil then
359                 local selected = core.get_textlist_index("sp_worlds")
360                 if selected ~= nil then
361                         local configdialog =
362                                 create_configure_world_dlg(
363                                                 menudata.worldlist:get_raw_index(selected))
364
365                         if (configdialog ~= nil) then
366                                 configdialog:set_parent(this)
367                                 this:hide()
368                                 configdialog:show()
369                                 mm_game_theme.update("singleplayer",current_game())
370                         end
371                 end
372
373                 return true
374         end
375 end
376
377 local function on_change(type, old_tab, new_tab)
378         if (type == "ENTER") then
379                 local game = current_game()
380                 if game then
381                         apply_game(game)
382                 end
383
384                 singleplayer_refresh_gamebar()
385                 ui.find_by_name("game_button_bar"):show()
386         else
387                 menudata.worldlist:set_filtercriteria(nil)
388                 local gamebar = ui.find_by_name("game_button_bar")
389                 if gamebar then
390                         gamebar:hide()
391                 end
392                 core.set_topleft_text("")
393                 -- If new_tab is nil, a dialog is being shown; avoid resetting the theme
394                 if new_tab then
395                         mm_game_theme.update(new_tab,nil)
396                 end
397         end
398 end
399
400 --------------------------------------------------------------------------------
401 return {
402         name = "local",
403         caption = fgettext("Start Game"),
404         cbf_formspec = get_formspec,
405         cbf_button_handler = main_button_handler,
406         on_change = on_change
407 }