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