]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu.lua
Swap name/password <-> server description
[dragonfireclient.git] / builtin / mainmenu.lua
1 os.setlocale("C", "numeric")
2
3 local scriptpath = engine.get_scriptdir()
4
5 dofile(scriptpath .. DIR_DELIM .. "mainmenu_worldlist.lua")
6 dofile(scriptpath .. DIR_DELIM .. "modmgr.lua")
7 dofile(scriptpath .. DIR_DELIM .. "modstore.lua")
8 dofile(scriptpath .. DIR_DELIM .. "gamemgr.lua")
9
10 local menu = {}
11 local tabbuilder = {}
12 local menubar = {}
13
14 --------------------------------------------------------------------------------
15 function render_favourite(spec,render_details)
16         local text = ""
17         
18         if spec.name ~= nil then
19                 text = text .. fs_escape_string(spec.name:trim())
20                 
21 --              if spec.description ~= nil and
22 --                      fs_escape_string(spec.description):trim() ~= "" then
23 --                      text = text .. " (" .. fs_escape_string(spec.description) .. ")"
24 --              end
25         else
26                 if spec.address ~= nil then
27                         text = text .. spec.address:trim()
28                 end
29         end
30         
31         if not render_details then
32                 return text
33         end
34         
35         local details = ""
36         if spec.password == true then
37                 details = details .. "*"
38         else
39                 details = details .. "_"
40         end
41         
42         if spec.creative then
43                 details = details .. "C"
44         else
45                 details = details .. "_"
46         end
47         
48         if spec.damage then
49                 details = details .. "D"
50         else
51                 details = details .. "_"
52         end
53         
54         if spec.pvp then
55                 details = details .. "P"
56         else
57                 details = details .. "_"
58         end
59         details = details .. "  "
60         
61         return fs_escape_string(details) .. text
62 end
63
64 --------------------------------------------------------------------------------
65 os.tempfolder = function()
66         local filetocheck = os.tmpname()
67         os.remove(filetocheck)
68         
69         local randname = "MTTempModFolder_" .. math.random(0,10000)
70         if DIR_DELIM == "\\" then
71                 local tempfolder = os.getenv("TEMP")
72                 return tempfolder .. filetocheck
73         else
74                 local backstring = filetocheck:reverse()
75                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
76         end
77
78 end
79
80 --------------------------------------------------------------------------------
81 function cleanup_path(temppath)
82         
83         local parts = temppath:split("-")
84         temppath = ""   
85         for i=1,#parts,1 do
86                 if temppath ~= "" then
87                         temppath = temppath .. "_"
88                 end
89                 temppath = temppath .. parts[i]
90         end
91         
92         parts = temppath:split(".")
93         temppath = ""   
94         for i=1,#parts,1 do
95                 if temppath ~= "" then
96                         temppath = temppath .. "_"
97                 end
98                 temppath = temppath .. parts[i]
99         end
100         
101         parts = temppath:split("'")
102         temppath = ""   
103         for i=1,#parts,1 do
104                 if temppath ~= "" then
105                         temppath = temppath .. ""
106                 end
107                 temppath = temppath .. parts[i]
108         end
109         
110         parts = temppath:split(" ")
111         temppath = ""   
112         for i=1,#parts,1 do
113                 if temppath ~= "" then
114                         temppath = temppath
115                 end
116                 temppath = temppath .. parts[i]
117         end
118         
119         return temppath
120 end
121
122 --------------------------------------------------------------------------------
123
124 function menu.set_texture(identifier,gamedetails)
125         local texture_set = false
126         if menu.texturepack ~= nil and gamedetails ~= nil then
127                 local path = menu.basetexturedir .. 
128                                                 gamedetails.id .. "_menu_" .. identifier .. ".png"
129                 
130                 if engine.set_background(identifier,path) then
131                         texture_set = true
132                 end
133         end
134         
135         if not texture_set and gamedetails ~= nil then
136                 local path = gamedetails.path .. DIR_DELIM .."menu" .. 
137                                                                          DIR_DELIM .. identifier .. ".png"
138                 if engine.set_background(identifier,path) then
139                         texture_set = true
140                 end
141         end
142         
143         if not texture_set then
144                 local path = menu.basetexturedir .. DIR_DELIM .."menu_" .. 
145                                                                                 identifier .. ".png"
146                 if engine.set_background(identifier,path) then
147                         texture_set = true
148                 end
149         end
150         
151         if not texture_set then
152                 local path = menu.defaulttexturedir .. DIR_DELIM .."menu_" .. 
153                                                                                 identifier .. ".png"
154                 engine.set_background(identifier,path)
155         end
156 end
157
158 --------------------------------------------------------------------------------
159 function menu.update_gametype()
160         
161         
162         
163         if (menu.game_last_check == nil or
164                 menu.game_last_check ~= menu.last_game) and
165                 tabbuilder.current_tab == "singleplayer" then
166                 
167                 local gamedetails = menu.lastgame()
168                 engine.set_topleft_text(gamedetails.name)
169                 worldlist.set_gamefilter(gamedetails.id)
170                 
171                 --background
172                 local background_set = false
173                 if menu.texturepack ~= nil then
174                         local path_background_texture = menu.basetexturedir .. 
175                                                                                 gamedetails.id .. "_menu_background.png"
176                         
177                         if engine.set_background("background",path_background_texture) then
178                                 background_set = true
179                                 engine.set_clouds(false)
180                         end
181                 end
182                 
183                 if not background_set then
184                         local path_background_texture = gamedetails.path .. DIR_DELIM .."menu" .. 
185                                                                                  DIR_DELIM .. "background.png"
186                         if engine.set_background("background",path_background_texture) then
187                                 background_set = true
188                                 engine.set_clouds(false)
189                         end
190                 end
191                 
192                 if not background_set then
193                         engine.set_clouds(true)
194                 end
195                 
196                 menu.set_texture("overlay",gamedetails)
197                 menu.set_texture("header",gamedetails)
198                 menu.set_texture("footer",gamedetails)
199                 
200                 menu.game_last_check = menu.last_game
201         else
202                 if menu.game_last_check ~= menu.last_game then
203                         menu.game_last_check = menu.last_game
204                         menu.reset_gametype()
205                 end
206         end
207 end
208
209 --------------------------------------------------------------------------------
210 function menu.reset_gametype()
211         worldlist.set_gamefilter(nil)
212         menu.game_last_check = nil
213         
214         local path_background_texture = menu.basetexturedir .. "menu_background.png"
215                         
216         if engine.set_background("background",path_background_texture) then
217                 background_set = true
218                 engine.set_clouds(false)
219         else
220                 engine.set_clouds(true)
221         end 
222
223         menu.set_texture("overlay",nil)
224         menu.set_texture("header",nil)
225         menu.set_texture("footer",nil)
226         engine.set_topleft_text("")
227 end
228
229 --------------------------------------------------------------------------------
230 function get_last_folder(text,count)
231         local parts = text:split(DIR_DELIM)
232         
233         if count == nil then
234                 return parts[#parts]
235         end
236         
237         local retval = ""
238         for i=1,count,1 do
239                 retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
240         end
241         
242         return retval
243 end
244
245 --------------------------------------------------------------------------------
246 function init_globals()
247         --init gamedata
248         gamedata.worldindex = 0
249 end
250
251 --------------------------------------------------------------------------------
252 function identify_filetype(name)
253
254         if name:sub(-3):lower() == "zip" then
255                 return {
256                                 name = name,
257                                 type = "zip"
258                                 }
259         end
260         
261         if name:sub(-6):lower() == "tar.gz" or
262                 name:sub(-3):lower() == "tgz"then
263                 return {
264                                 name = name,
265                                 type = "tgz"
266                                 }
267         end
268         
269         if name:sub(-6):lower() == "tar.bz2" then
270                 return {
271                                 name = name,
272                                 type = "tbz"
273                                 }
274         end
275         
276         if name:sub(-2):lower() == "7z" then
277                 return {
278                                 name = name,
279                                 type = "7z"
280                                 }
281         end
282
283         return {
284                 name = name,
285                 type = "ukn"
286         }
287 end
288
289 --------------------------------------------------------------------------------
290 function update_menu()
291
292         local formspec = "size[12,5.2]"
293         
294         -- handle errors
295         if gamedata.errormessage ~= nil then
296                 formspec = formspec ..
297                         "field[1,2;10,2;;ERROR: " ..
298                         gamedata.errormessage .. 
299                         ";]"..
300                         "button[4.5,4.2;3,0.5;btn_error_confirm;Ok]"
301         else
302                 formspec = formspec .. tabbuilder.gettab()
303         end
304
305         engine.update_formspec(formspec)
306 end
307
308 --------------------------------------------------------------------------------
309 function menu.render_world_list()
310         local retval = ""
311         
312         local current_worldlist = worldlist.get_list()
313         
314         for i,v in ipairs(current_worldlist) do
315                 if retval ~= "" then
316                         retval = retval ..","
317                 end
318                 
319                 retval = retval .. v.name .. 
320                                         " \\[" .. v.gameid .. "\\]"
321         end
322
323         return retval
324 end
325
326 --------------------------------------------------------------------------------
327 function menu.init()
328         --init menu data
329         gamemgr.update_gamelist()
330         
331         menu.last_game  = tonumber(engine.setting_get("main_menu_last_game_idx"))
332         
333         if type(menu.last_game) ~= "number" then
334                 menu.last_game = 1
335         end
336
337         if engine.setting_getbool("public_serverlist") then
338                 menu.favorites = engine.get_favorites("online")
339         else
340                 menu.favorites = engine.get_favorites("local")
341         end
342         
343         
344         menu.defaulttexturedir = engine.get_gamepath() .. DIR_DELIM .. ".." ..
345                                                 DIR_DELIM .. "textures" .. DIR_DELIM .. "base" .. 
346                                                 DIR_DELIM .. "pack" .. DIR_DELIM
347         menu.basetexturedir = menu.defaulttexturedir
348         
349         menu.texturepack = engine.setting_get("texture_path")
350         
351         if menu.texturepack ~= nil then
352                 menu.basetexturedir = menu.texturepack .. DIR_DELIM
353         end
354 end
355
356 --------------------------------------------------------------------------------
357 function menu.lastgame()
358         if menu.last_game > 0 and menu.last_game <= #gamemgr.games then
359                 return gamemgr.games[menu.last_game]
360         end
361         
362         if #gamemgr.games >= 1 then
363                 menu.last_game = 1
364                 return gamemgr.games[menu.last_game]
365         end
366         
367         --error case!!
368         return nil
369 end
370
371 --------------------------------------------------------------------------------
372 function menu.update_last_game()
373
374         local current_world = worldlist.get_raw_world(
375                                                         engine.setting_get("mainmenu_last_selected_world")
376                                                         )
377                                                         
378         if current_world == nil then
379                 return
380         end
381                 
382         for i=1,#gamemgr.games,1 do             
383                 if gamemgr.games[i].id == current_world.gameid then
384                         menu.last_game = i
385                         engine.setting_set("main_menu_last_game_idx",menu.last_game)
386                         break
387                 end
388         end
389 end
390
391 --------------------------------------------------------------------------------
392 function menu.handle_key_up_down(fields,textlist,settingname)
393
394         if fields["key_up"] then
395                 local oldidx = engine.get_textlist_index(textlist)
396                 
397                 if oldidx > 1 then
398                         local newidx = oldidx -1
399                         engine.setting_set(settingname,
400                                 worldlist.get_engine_index(newidx))
401                 end
402         end
403         
404         if fields["key_down"] then
405                 local oldidx = engine.get_textlist_index(textlist)
406                 
407                 if oldidx < worldlist.size() then
408                         local newidx = oldidx + 1
409                         engine.setting_set(settingname,
410                                 worldlist.get_engine_index(newidx))
411                 end
412         end
413 end
414
415
416 --------------------------------------------------------------------------------
417 function menubar.handle_buttons(fields)
418         for i=1,#menubar.buttons,1 do
419                 if fields[menubar.buttons[i].btn_name] ~= nil then
420                         menu.last_game = menubar.buttons[i].index
421                         engine.setting_set("main_menu_last_game_idx",menu.last_game)
422                         menu.update_gametype()
423                 end
424         end
425 end
426
427 --------------------------------------------------------------------------------
428 function menubar.refresh()
429         menubar.formspec = "box[-0.3,5.625;12.4,1.3;000000]" ..
430                                            "box[-0.3,5.6;12.4,0.05;FFFFFF]"
431         menubar.buttons = {}
432
433         local button_base = -0.25
434         
435         local maxbuttons = #gamemgr.games
436         
437         if maxbuttons > 10 then
438                 maxbuttons = 10
439         end
440         
441         for i=1,maxbuttons,1 do
442
443                 local btn_name = "menubar_btn_" .. gamemgr.games[i].id
444                 local buttonpos = button_base + (i-1) * 1.245
445                 if gamemgr.games[i].menuicon_path ~= nil and
446                         gamemgr.games[i].menuicon_path ~= "" then
447
448                         menubar.formspec = menubar.formspec ..
449                                 "image_button[" .. buttonpos ..  ",5.7;1.3,1.3;"  ..
450                                 gamemgr.games[i].menuicon_path .. ";" .. btn_name .. ";;true;false]"
451                 else
452                 
453                         local part1 = gamemgr.games[i].id:sub(1,5)
454                         local part2 = gamemgr.games[i].id:sub(6,10)
455                         local part3 = gamemgr.games[i].id:sub(11)
456                         
457                         local text = part1 .. "\n" .. part2
458                         if part3 ~= nil and
459                                 part3 ~= "" then
460                                 text = text .. "\n" .. part3
461                         end
462                         menubar.formspec = menubar.formspec ..
463                                 "image_button[" .. buttonpos ..  ",5.7;1.3,1.3;;" ..btn_name ..
464                                 ";" .. text .. ";true;true]"
465                 end
466                 
467                 local toadd = {
468                         btn_name = btn_name,
469                         index = i,
470                 }
471                 
472                 table.insert(menubar.buttons,toadd)
473         end
474 end
475
476 --------------------------------------------------------------------------------
477 function tabbuilder.dialog_create_world()
478         local mapgens = {"v6", "v7", "indev", "singlenode", "math"}
479
480         local current_mg = engine.setting_get("mg_name")
481
482         local mglist = ""
483         local selindex = 1
484         local i = 1
485         for k,v in pairs(mapgens) do
486                 if current_mg == v then
487                         selindex = i
488                 end
489                 i = i + 1
490                 mglist = mglist .. v .. ","
491         end
492         mglist = mglist:sub(1, -2)
493
494         local retval = 
495                 "label[2,0;World name]"..
496                 "label[2,1;Mapgen]"..
497                 "field[4.5,0.4;6,0.5;te_world_name;;]" ..
498                 "label[2,2;Game]"..
499                 "button[5,4.5;2.6,0.5;world_create_confirm;Create]" ..
500                 "button[7.5,4.5;2.8,0.5;world_create_cancel;Cancel]" ..
501                 "dropdown[4.2,1;6.3;dd_mapgen;" .. mglist .. ";" .. selindex .. "]" ..
502                 "textlist[4.2,1.9;5.8,2.3;games;" ..
503                 gamemgr.gamelist() ..
504                 ";" .. menu.last_game .. ";true]"
505
506         return retval
507 end
508
509 --------------------------------------------------------------------------------
510 function tabbuilder.dialog_delete_world()
511         return  "label[2,2;Delete World \"" .. worldlist.get_raw_list()[menu.world_to_del].name .. "\"?]"..
512                         "button[3.5,4.2;2.6,0.5;world_delete_confirm;Yes]" ..
513                         "button[6,4.2;2.8,0.5;world_delete_cancel;No]"
514 end
515
516 --------------------------------------------------------------------------------
517 function tabbuilder.gettab()
518         local retval = ""
519         
520         if tabbuilder.show_buttons then
521                 retval = retval .. tabbuilder.tab_header()
522         end
523
524         if tabbuilder.current_tab == "singleplayer" then
525                 retval = retval .. tabbuilder.tab_singleplayer()
526         end
527         
528         if tabbuilder.current_tab == "multiplayer" then
529                 retval = retval .. tabbuilder.tab_multiplayer()
530         end
531
532         if tabbuilder.current_tab == "server" then
533                 retval = retval .. tabbuilder.tab_server()
534         end
535         
536         if tabbuilder.current_tab == "settings" then
537                 retval = retval .. tabbuilder.tab_settings()
538         end
539         
540         if tabbuilder.current_tab == "credits" then
541                 retval = retval .. tabbuilder.tab_credits()
542         end
543         
544         if tabbuilder.current_tab == "dialog_create_world" then
545                 retval = retval .. tabbuilder.dialog_create_world()
546         end
547         
548         if tabbuilder.current_tab == "dialog_delete_world" then
549                 retval = retval .. tabbuilder.dialog_delete_world()
550         end
551         
552         retval = retval .. modmgr.gettab(tabbuilder.current_tab)
553         retval = retval .. gamemgr.gettab(tabbuilder.current_tab)
554         retval = retval .. modstore.gettab(tabbuilder.current_tab)
555
556         return retval
557 end
558
559 --------------------------------------------------------------------------------
560 function tabbuilder.handle_create_world_buttons(fields)
561         
562         if fields["world_create_confirm"] or
563                 fields["key_enter"] then
564                 
565                 local worldname = fields["te_world_name"]
566                 local gameindex = engine.get_textlist_index("games")
567                 
568                 if gameindex > 0 and
569                         worldname ~= "" then
570                         
571                         local message = nil
572                         
573                         if not worldlist.exists(worldname) then
574                                 engine.setting_set("mg_name",fields["dd_mapgen"])
575                                 message = engine.create_world(worldname,gameindex)
576                         else
577                                 message = "A world named \"" .. worldname .. "\" already exists"
578                         end
579                         
580                         if message ~= nil then
581                                 gamedata.errormessage = message
582                         else
583                                 menu.last_game = gameindex
584                                 engine.setting_set("main_menu_last_game_idx",gameindex)
585                                 
586                                 worldlist.refresh()
587                                 engine.setting_set("mainmenu_last_selected_world",
588                                                                         worldlist.engine_index_by_name(worldname))
589                         end
590                 else
591                         gamedata.errormessage = "No worldname given or no game selected"
592                 end
593         end
594         
595         if fields["games"] then
596                 tabbuilder.skipformupdate = true
597                 return
598         end
599         
600         --close dialog
601         tabbuilder.is_dialog = false
602         tabbuilder.show_buttons = true
603         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
604 end
605
606 --------------------------------------------------------------------------------
607 function tabbuilder.handle_delete_world_buttons(fields)
608         
609         if fields["world_delete_confirm"] then
610                 if menu.world_to_del > 0 and 
611                         menu.world_to_del <= #worldlist.get_raw_list() then
612                         engine.delete_world(menu.world_to_del)
613                         menu.world_to_del = 0
614                         worldlist.refresh()
615                 end
616         end
617         
618         tabbuilder.is_dialog = false
619         tabbuilder.show_buttons = true
620         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
621 end
622
623 --------------------------------------------------------------------------------
624 function tabbuilder.handle_multiplayer_buttons(fields)
625         
626         if fields["te_name"] ~= nil then
627                 gamedata.playername = fields["te_name"]
628                 engine.setting_set("name", fields["te_name"])
629         end
630         
631         if fields["favourites"] ~= nil then
632                 local event = explode_textlist_event(fields["favourites"])
633                 if event.typ == "DCL" then
634                         gamedata.address = menu.favorites[event.index].address
635                         gamedata.port = menu.favorites[event.index].port
636                         gamedata.playername             = fields["te_name"]
637                         if fields["te_pwd"] ~= nil then
638                                 gamedata.password               = fields["te_pwd"]
639                         end
640                         gamedata.selected_world = 0
641                         
642                         if menu.favorites ~= nil then
643                                 gamedata.servername = menu.favorites[event.index].name
644                                 gamedata.serverdescription = menu.favorites[event.index].description
645                         end
646                         
647                         if gamedata.address ~= nil and
648                                 gamedata.port ~= nil then
649                                 
650                                 engine.start()
651                         end
652                 end
653                 
654                 if event.typ == "CHG" then
655                         local address = menu.favorites[event.index].address
656                         local port = menu.favorites[event.index].port
657                         
658                         if address ~= nil and
659                                 port ~= nil then
660                                 engine.setting_set("address",address)
661                                 engine.setting_set("port",port)
662                         end
663                         
664                         menu.fav_selected = event.index
665                 end
666                 return
667         end
668         
669         if fields["key_up"] ~= nil or
670                 fields["key_down"] ~= nil then
671                 
672                 local fav_idx = engine.get_textlist_index("favourites")
673                 
674                 if fields["key_up"] ~= nil and fav_idx > 1 then
675                         fav_idx = fav_idx -1
676                 else if fields["key_down"] and fav_idx < #menu.favorites then
677                         fav_idx = fav_idx +1
678                 end end
679                 
680                 local address = menu.favorites[fav_idx].address
681                 local port = menu.favorites[fav_idx].port
682                 
683                 if address ~= nil and
684                         port ~= nil then
685                         engine.setting_set("address",address)
686                         engine.setting_set("port",port)
687                 end
688                 
689                 menu.fav_selected = fav_idx
690                 return
691         end
692         
693         if fields["cb_public_serverlist"] ~= nil then
694                 engine.setting_setbool("public_serverlist",
695                         tabbuilder.tobool(fields["cb_public_serverlist"]))
696                         
697                 if engine.setting_getbool("public_serverlist") then
698                         menu.favorites = engine.get_favorites("online")
699                 else
700                         menu.favorites = engine.get_favorites("local")
701                 end
702                 menu.fav_selected = nil
703                 return
704         end
705
706         if fields["btn_delete_favorite"] ~= nil then
707                 local current_favourite = engine.get_textlist_index("favourites")
708                 engine.delete_favorite(current_favourite)
709                 menu.favorites = engine.get_favorites()
710                 menu.fav_selected = nil
711                 
712                 engine.setting_set("address","")
713                 engine.setting_get("port","")
714                 
715                 return
716         end
717
718         if fields["btn_mp_connect"] ~= nil or
719                 fields["key_enter"] then
720                 
721                 gamedata.playername             = fields["te_name"]
722                 gamedata.password               = fields["te_pwd"]
723                 gamedata.address                = fields["te_address"]
724                 gamedata.port                   = fields["te_port"]
725                 
726                 local fav_idx = engine.get_textlist_index("favourites")
727                 
728                 if fav_idx > 0 and fav_idx <= #menu.favorites and
729                         menu.favorites[fav_idx].address == fields["te_address"] and
730                         menu.favorites[fav_idx].port == fields["te_port"] then
731                         
732                         gamedata.servername                     = menu.favorites[fav_idx].name
733                         gamedata.serverdescription      = menu.favorites[fav_idx].description
734                 else
735                         gamedata.servername = ""
736                         gamedata.serverdescription = ""
737                 end
738
739                 gamedata.selected_world = 0
740                 
741                 engine.start()
742                 return
743         end
744 end
745
746 --------------------------------------------------------------------------------
747 function tabbuilder.handle_server_buttons(fields)
748
749         local world_doubleclick = false
750
751         if fields["srv_worlds"] ~= nil then
752                 local event = explode_textlist_event(fields["srv_worlds"])
753                 
754                 if event.typ == "DCL" then
755                         world_doubleclick = true
756                 end
757                 if event.typ == "CHG" then
758                         engine.setting_set("mainmenu_last_selected_world",
759                                 worldlist.get_engine_index(engine.get_textlist_index("srv_worlds")))
760                 end
761         end
762         
763         menu.handle_key_up_down(fields,"srv_worlds","mainmenu_last_selected_world")
764         
765         if fields["cb_creative_mode"] then
766                 engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
767         end
768         
769         if fields["cb_enable_damage"] then
770                 engine.setting_setbool("enable_damage",tabbuilder.tobool(fields["cb_enable_damage"]))
771         end
772
773         if fields["cb_server_announce"] then
774                 engine.setting_setbool("server_announce",tabbuilder.tobool(fields["cb_server_announce"]))
775         end
776         
777         if fields["start_server"] ~= nil or
778                 world_doubleclick or
779                 fields["key_enter"] then
780                 local selected = engine.get_textlist_index("srv_worlds")
781                 if selected > 0 then
782                         gamedata.playername             = fields["te_playername"]
783                         gamedata.password               = fields["te_passwd"]
784                         gamedata.port                   = fields["te_serverport"]
785                         gamedata.address                = ""
786                         gamedata.selected_world = worldlist.get_engine_index(selected)
787                         
788                         menu.update_last_game(gamedata.selected_world)
789                         engine.start()
790                 end
791         end
792         
793         if fields["world_create"] ~= nil then
794                 tabbuilder.current_tab = "dialog_create_world"
795                 tabbuilder.is_dialog = true
796                 tabbuilder.show_buttons = false
797         end
798         
799         if fields["world_delete"] ~= nil then
800                 local selected = engine.get_textlist_index("srv_worlds")
801                 if selected > 0 and
802                         selected <= worldlist.size() then
803                         local world = worldlist.get_list()[selected]
804                         if world ~= nil and
805                                 world.name ~= nil and
806                                 world.name ~= "" then
807                                 menu.world_to_del = worldlist.get_engine_index(selected)
808                                 tabbuilder.current_tab = "dialog_delete_world"
809                                 tabbuilder.is_dialog = true
810                                 tabbuilder.show_buttons = false
811                         else
812                                 menu.world_to_del = 0
813                         end
814                 end
815         end
816         
817         if fields["world_configure"] ~= nil then
818                 selected = engine.get_textlist_index("srv_worlds")
819                 if selected > 0 then
820                         modmgr.world_config_selected_world = worldlist.get_engine_index(selected)
821                         if modmgr.init_worldconfig() then
822                                 tabbuilder.current_tab = "dialog_configure_world"
823                                 tabbuilder.is_dialog = true
824                                 tabbuilder.show_buttons = false
825                         end
826                 end
827         end
828 end
829
830 --------------------------------------------------------------------------------
831 function tabbuilder.tobool(text)
832         if text == "true" then
833                 return true
834         else
835                 return false
836         end
837 end
838
839 --------------------------------------------------------------------------------
840 function tabbuilder.handle_settings_buttons(fields)
841         if fields["cb_fancy_trees"] then
842                 engine.setting_setbool("new_style_leaves",tabbuilder.tobool(fields["cb_fancy_trees"]))
843         end
844                 
845         if fields["cb_smooth_lighting"] then
846                 engine.setting_setbool("smooth_lighting",tabbuilder.tobool(fields["cb_smooth_lighting"]))
847         end
848         if fields["cb_3d_clouds"] then
849                 engine.setting_setbool("enable_3d_clouds",tabbuilder.tobool(fields["cb_3d_clouds"]))
850         end
851         if fields["cb_opaque_water"] then
852                 engine.setting_setbool("opaque_water",tabbuilder.tobool(fields["cb_opaque_water"]))
853         end
854                         
855         if fields["cb_mipmapping"] then
856                 engine.setting_setbool("mip_map",tabbuilder.tobool(fields["cb_mipmapping"]))
857         end
858         if fields["cb_anisotrophic"] then
859                 engine.setting_setbool("anisotropic_filter",tabbuilder.tobool(fields["cb_anisotrophic"]))
860         end
861         if fields["cb_bilinear"] then
862                 engine.setting_setbool("bilinear_filter",tabbuilder.tobool(fields["cb_bilinear"]))
863         end
864         if fields["cb_trilinear"] then
865                 engine.setting_setbool("trilinear_filter",tabbuilder.tobool(fields["cb_trilinear"]))
866         end
867                         
868         if fields["cb_shaders"] then
869                 engine.setting_setbool("enable_shaders",tabbuilder.tobool(fields["cb_shaders"]))
870         end
871         if fields["cb_pre_ivis"] then
872                 engine.setting_setbool("preload_item_visuals",tabbuilder.tobool(fields["cb_pre_ivis"]))
873         end
874         if fields["cb_particles"] then
875                 engine.setting_setbool("enable_particles",tabbuilder.tobool(fields["cb_particles"]))
876         end
877         if fields["cb_finite_liquid"] then
878                 engine.setting_setbool("liquid_finite",tabbuilder.tobool(fields["cb_finite_liquid"]))
879         end
880
881         if fields["btn_change_keys"] ~= nil then
882                 engine.show_keys_menu()
883         end
884 end
885
886 --------------------------------------------------------------------------------
887 function tabbuilder.handle_singleplayer_buttons(fields)
888
889         local world_doubleclick = false
890
891         if fields["sp_worlds"] ~= nil then
892                 local event = explode_textlist_event(fields["sp_worlds"])
893                 
894                 if event.typ == "DCL" then
895                         world_doubleclick = true
896                 end
897                 
898                 if event.typ == "CHG" then
899                         engine.setting_set("mainmenu_last_selected_world",
900                                 worldlist.get_engine_index(engine.get_textlist_index("sp_worlds")))
901                 end
902         end
903         
904         menu.handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world")
905         
906         if fields["cb_creative_mode"] then
907                 engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
908         end
909         
910         if fields["cb_enable_damage"] then
911                 engine.setting_setbool("enable_damage",tabbuilder.tobool(fields["cb_enable_damage"]))
912         end
913
914         if fields["play"] ~= nil or
915                 world_doubleclick or
916                 fields["key_enter"] then
917                 local selected = engine.get_textlist_index("sp_worlds")
918                 if selected > 0 then
919                         gamedata.selected_world = worldlist.get_engine_index(selected)
920                         gamedata.singleplayer   = true
921                         
922                         menu.update_last_game(gamedata.selected_world)
923                         
924                         engine.start()
925                 end
926         end
927         
928         if fields["world_create"] ~= nil then
929                 tabbuilder.current_tab = "dialog_create_world"
930                 tabbuilder.is_dialog = true
931                 tabbuilder.show_buttons = false
932         end
933         
934         if fields["world_delete"] ~= nil then
935                 local selected = engine.get_textlist_index("sp_worlds")
936                 if selected > 0 and
937                         selected <= worldlist.size() then
938                         local world = worldlist.get_list()[selected]
939                         if world ~= nil and
940                                 world.name ~= nil and
941                                 world.name ~= "" then
942                                 menu.world_to_del = worldlist.get_engine_index(selected)
943                                 tabbuilder.current_tab = "dialog_delete_world"
944                                 tabbuilder.is_dialog = true
945                                 tabbuilder.show_buttons = false
946                         else
947                                 menu.world_to_del = 0
948                         end
949                 end
950         end
951         
952         if fields["world_configure"] ~= nil then
953                 selected = engine.get_textlist_index("sp_worlds")
954                 if selected > 0 then
955                         modmgr.world_config_selected_world = worldlist.get_engine_index(selected)
956                         if modmgr.init_worldconfig() then
957                                 tabbuilder.current_tab = "dialog_configure_world"
958                                 tabbuilder.is_dialog = true
959                                 tabbuilder.show_buttons = false
960                         end
961                 end
962         end
963 end
964
965 --------------------------------------------------------------------------------
966 function tabbuilder.tab_header()
967
968         if tabbuilder.last_tab_index == nil then
969                 tabbuilder.last_tab_index = 1
970         end
971         
972         local toadd = ""
973         
974         for i=1,#tabbuilder.current_buttons,1 do
975                 
976                 if toadd ~= "" then
977                         toadd = toadd .. ","
978                 end
979                 
980                 toadd = toadd .. tabbuilder.current_buttons[i].caption
981         end
982         return "tabheader[-0.3,-0.99;main_tab;" .. toadd ..";" .. tabbuilder.last_tab_index .. ";true;false]"
983 end
984
985 --------------------------------------------------------------------------------
986 function tabbuilder.handle_tab_buttons(fields)
987
988         if fields["main_tab"] then
989                 local index = tonumber(fields["main_tab"])
990                 tabbuilder.last_tab_index = index
991                 tabbuilder.current_tab = tabbuilder.current_buttons[index].name
992                 
993                 engine.setting_set("main_menu_tab",tabbuilder.current_tab)
994         end
995         
996         --handle tab changes
997         if tabbuilder.current_tab ~= tabbuilder.old_tab then
998                 if tabbuilder.current_tab ~= "singleplayer" then
999                         menu.reset_gametype()
1000                 end
1001         end
1002         
1003         if tabbuilder.current_tab == "singleplayer" then
1004                 menu.update_gametype()
1005         end
1006         
1007         tabbuilder.old_tab = tabbuilder.current_tab
1008 end
1009
1010 --------------------------------------------------------------------------------
1011 function tabbuilder.init()
1012         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
1013         
1014         if tabbuilder.current_tab == nil or
1015                 tabbuilder.current_tab == "" then
1016                 tabbuilder.current_tab = "singleplayer"
1017                 engine.setting_set("main_menu_tab",tabbuilder.current_tab)
1018         end
1019         
1020         
1021         --initialize tab buttons
1022         tabbuilder.last_tab = nil
1023         tabbuilder.show_buttons = true
1024         
1025         tabbuilder.current_buttons = {}
1026         table.insert(tabbuilder.current_buttons,{name="singleplayer", caption="Singleplayer"})
1027         table.insert(tabbuilder.current_buttons,{name="multiplayer", caption="Client"})
1028         table.insert(tabbuilder.current_buttons,{name="server", caption="Server"})
1029         table.insert(tabbuilder.current_buttons,{name="settings", caption="Settings"})
1030         
1031         if engine.setting_getbool("main_menu_game_mgr") then
1032                 table.insert(tabbuilder.current_buttons,{name="game_mgr", caption="Games"})
1033         end
1034         
1035         if engine.setting_getbool("main_menu_mod_mgr") then
1036                 table.insert(tabbuilder.current_buttons,{name="mod_mgr", caption="Mods"})
1037         end
1038         table.insert(tabbuilder.current_buttons,{name="credits", caption="Credits"})
1039         
1040         
1041         for i=1,#tabbuilder.current_buttons,1 do
1042                 if tabbuilder.current_buttons[i].name == tabbuilder.current_tab then
1043                         tabbuilder.last_tab_index = i
1044                 end
1045         end
1046         
1047         menu.update_gametype()
1048 end
1049
1050 --------------------------------------------------------------------------------
1051 function tabbuilder.tab_multiplayer()
1052
1053         local retval =
1054                 "vertlabel[0,-0.25;CLIENT]" ..
1055                 "label[1,-0.25;Favorites:]"..
1056                 "label[1,4.25;Address/Port]"..
1057                 "label[9,2.75;Name/Password]" ..
1058                 "field[1.25,5.25;5.5,0.5;te_address;;" ..engine.setting_get("address") .."]" ..
1059                 "field[6.75,5.25;2.25,0.5;te_port;;" ..engine.setting_get("port") .."]" ..
1060                 "checkbox[1,3.6;cb_public_serverlist;Public Serverlist;" ..
1061                 dump(engine.setting_getbool("public_serverlist")) .. "]"
1062                 
1063         if not engine.setting_getbool("public_serverlist") then
1064                 retval = retval .. 
1065                 "button[6.45,3.95;2.25,0.5;btn_delete_favorite;Delete]"
1066         end
1067         
1068         retval = retval ..
1069                 "button[9,4.95;2.5,0.5;btn_mp_connect;Connect]" ..
1070                 "field[9.3,3.75;2.5,0.5;te_name;;" ..engine.setting_get("name") .."]" ..
1071                 "pwdfield[9.3,4.5;2.5,0.5;te_pwd;]" ..
1072                 "textarea[9.3,0.25;2.5,2.75;;"
1073         if menu.fav_selected ~= nil and 
1074                 menu.favorites[menu.fav_selected].description ~= nil then
1075                 retval = retval .. 
1076                         fs_escape_string(menu.favorites[menu.fav_selected].description,true)
1077         end
1078         
1079         retval = retval .. 
1080                 ";]" ..
1081                 "textlist[1,0.35;7.5,3.35;favourites;"
1082
1083         local render_details = engine.setting_getbool("public_serverlist")
1084
1085         if #menu.favorites > 0 then
1086                 retval = retval .. render_favourite(menu.favorites[1],render_details)
1087                 
1088                 for i=2,#menu.favorites,1 do
1089                         retval = retval .. "," .. render_favourite(menu.favorites[i],render_details)
1090                 end
1091         end
1092
1093         if menu.fav_selected ~= nil then
1094                 retval = retval .. ";" .. menu.fav_selected .. "]"
1095         else
1096                 retval = retval .. ";0]"
1097         end
1098
1099         return retval
1100 end
1101
1102 --------------------------------------------------------------------------------
1103 function tabbuilder.tab_server()
1104
1105         local index = worldlist.get_current_index(
1106                                 tonumber(engine.setting_get("mainmenu_last_selected_world"))
1107                                 )
1108         
1109         local retval = 
1110                 "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
1111                 "button[6.5,4.15;2.8,0.5;world_create;New]" ..
1112                 "button[9.2,4.15;2.55,0.5;world_configure;Configure]" ..
1113                 "button[8.5,4.9;3.25,0.5;start_server;Start Game]" ..
1114                 "label[4,-0.25;Select World:]"..
1115                 "vertlabel[0,-0.25;START SERVER]" ..
1116                 "checkbox[0.5,0.25;cb_creative_mode;Creative Mode;" ..
1117                 dump(engine.setting_getbool("creative_mode")) .. "]"..
1118                 "checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
1119                 dump(engine.setting_getbool("enable_damage")) .. "]"..
1120                 "checkbox[0.5,1.15;cb_server_announce;Public;" ..
1121                 dump(engine.setting_getbool("server_announce")) .. "]"..
1122                 "field[0.8,3.2;3,0.5;te_playername;Name;" ..
1123                 engine.setting_get("name") .. "]" ..
1124                 "pwdfield[0.8,4.2;3,0.5;te_passwd;Password]" ..
1125                 "field[0.8,5.2;3,0.5;te_serverport;Server Port;30000]" ..
1126                 "textlist[4,0.25;7.5,3.7;srv_worlds;" ..
1127                 menu.render_world_list() ..
1128                 ";" .. index .. "]"
1129                 
1130         return retval
1131 end
1132
1133 --------------------------------------------------------------------------------
1134 function tabbuilder.tab_settings()
1135         return  "vertlabel[0,0;SETTINGS]" ..
1136                         "checkbox[1,0.75;cb_fancy_trees;Fancy trees;"           .. dump(engine.setting_getbool("new_style_leaves"))     .. "]"..
1137                         "checkbox[1,1.25;cb_smooth_lighting;Smooth Lighting;".. dump(engine.setting_getbool("smooth_lighting")) .. "]"..
1138                         "checkbox[1,1.75;cb_3d_clouds;3D Clouds;"                       .. dump(engine.setting_getbool("enable_3d_clouds"))     .. "]"..
1139                         "checkbox[1,2.25;cb_opaque_water;Opaque Water;"                 .. dump(engine.setting_getbool("opaque_water"))         .. "]"..
1140                         
1141                         "checkbox[4,0.75;cb_mipmapping;Mip-Mapping;"            .. dump(engine.setting_getbool("mip_map"))                      .. "]"..
1142                         "checkbox[4,1.25;cb_anisotrophic;Anisotropic Filtering;".. dump(engine.setting_getbool("anisotropic_filter"))   .. "]"..
1143                         "checkbox[4,1.75;cb_bilinear;Bi-Linear Filtering;"      .. dump(engine.setting_getbool("bilinear_filter"))      .. "]"..
1144                         "checkbox[4,2.25;cb_trilinear;Tri-Linear Filtering;"    .. dump(engine.setting_getbool("trilinear_filter"))     .. "]"..
1145                         
1146                         "checkbox[7.5,0.75;cb_shaders;Shaders;"                         .. dump(engine.setting_getbool("enable_shaders"))               .. "]"..
1147                         "checkbox[7.5,1.25;cb_pre_ivis;Preload item visuals;".. dump(engine.setting_getbool("preload_item_visuals"))    .. "]"..
1148                         "checkbox[7.5,1.75;cb_particles;Enable Particles;"      .. dump(engine.setting_getbool("enable_particles"))     .. "]"..
1149                         "checkbox[7.5,2.25;cb_finite_liquid;Finite Liquid;"     .. dump(engine.setting_getbool("liquid_finite"))                .. "]"..
1150                         
1151                         "button[1,3.75;2.25,0.5;btn_change_keys;Change keys]"
1152 end
1153
1154 --------------------------------------------------------------------------------
1155 function tabbuilder.tab_singleplayer()
1156         
1157         local index = worldlist.get_current_index(
1158                                 tonumber(engine.setting_get("mainmenu_last_selected_world"))
1159                                 )
1160
1161         return  "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
1162                         "button[6.5,4.15;2.8,0.5;world_create;New]" ..
1163                         "button[9.2,4.15;2.55,0.5;world_configure;Configure]" ..
1164                         "button[8.5,4.95;3.25,0.5;play;Play]" ..
1165                         "label[4,-0.25;Select World:]"..
1166                         "vertlabel[0,-0.25;SINGLE PLAYER]" ..
1167                         "checkbox[0.5,0.25;cb_creative_mode;Creative Mode;" ..
1168                         dump(engine.setting_getbool("creative_mode")) .. "]"..
1169                         "checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
1170                         dump(engine.setting_getbool("enable_damage")) .. "]"..
1171                         "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
1172                         menu.render_world_list() ..
1173                         ";" .. index .. "]" ..
1174                         menubar.formspec
1175 end
1176
1177 --------------------------------------------------------------------------------
1178 function tabbuilder.tab_credits()
1179         return  "vertlabel[0,-0.5;CREDITS]" ..
1180                         "label[0.5,3;Minetest " .. engine.get_version() .. "]" ..
1181                         "label[0.5,3.3;http://minetest.net]" .. 
1182                         "image[0.5,1;" .. menu.defaulttexturedir .. "logo.png]" ..
1183                         "textlist[3.5,-0.25;8.5,5.8;list_credits;" ..
1184                         "#FFFF00Core Developers," ..
1185                         "Perttu Ahola (celeron55) <celeron55@gmail.com>,"..
1186                         "Ryan Kwolek (kwolekr) <kwolekr@minetest.net>,"..
1187                         "PilzAdam <pilzadam@minetest.net>," ..
1188                         "IIya Zhuravlev (thexyz) <xyz@minetest.net>,"..
1189                         "Lisa Milne (darkrose) <lisa@ltmnet.com>,"..
1190                         "Maciej Kasatkin (RealBadAngel) <mk@realbadangel.pl>,"..
1191                         "proller <proler@gmail.com>,"..
1192                         "sfan5 <sfan5@live.de>,"..
1193                         "kahrl <kahrl@gmx.net>,"..
1194                         ","..
1195                         "#FFFF00Active Contributors," ..
1196                         "sapier,"..
1197                         "Vanessa Ezekowitz (VanessaE) <vanessaezekowitz@gmail.com>,"..
1198                         "Jurgen Doser (doserj) <jurgen.doser@gmail.com>,"..
1199                         "Jeija <jeija@mesecons.net>,"..
1200                         "MirceaKitsune <mirceakitsune@gmail.com>,"..
1201                         "ShadowNinja,"..
1202                         "dannydark <the_skeleton_of_a_child@yahoo.co.uk>,"..
1203                         "0gb.us <0gb.us@0gb.us>,"..
1204                         "," ..
1205                         "#FFFF00Previous Contributors," ..
1206                         "Guiseppe Bilotta (Oblomov) <guiseppe.bilotta@gmail.com>,"..
1207                         "Jonathan Neuschafer <j.neuschaefer@gmx.net>,"..
1208                         "Nils Dagsson Moskopp (erlehmann) <nils@dieweltistgarnichtso.net>,"..
1209                         "Constantin Wenger (SpeedProg) <constantin.wenger@googlemail.com>,"..
1210                         "matttpt <matttpt@gmail.com>,"..
1211                         "JacobF <queatz@gmail.com>,"..
1212                         ";0;true]"
1213 end
1214
1215 --------------------------------------------------------------------------------
1216 function tabbuilder.checkretval(retval)
1217
1218         if retval ~= nil then
1219                 if retval.current_tab ~= nil then
1220                         tabbuilder.current_tab = retval.current_tab
1221                 end
1222                 
1223                 if retval.is_dialog ~= nil then
1224                         tabbuilder.is_dialog = retval.is_dialog
1225                 end
1226                 
1227                 if retval.show_buttons ~= nil then
1228                         tabbuilder.show_buttons = retval.show_buttons
1229                 end
1230                 
1231                 if retval.skipformupdate ~= nil then
1232                         tabbuilder.skipformupdate = retval.skipformupdate
1233                 end
1234         end
1235 end
1236
1237 --------------------------------------------------------------------------------
1238 --------------------------------------------------------------------------------
1239 -- initialize callbacks
1240 --------------------------------------------------------------------------------
1241 --------------------------------------------------------------------------------
1242 engine.button_handler = function(fields)
1243         --print("Buttonhandler: tab: " .. tabbuilder.current_tab .. " fields: " .. dump(fields))
1244         
1245         if fields["btn_error_confirm"] then
1246                 gamedata.errormessage = nil
1247         end
1248         
1249         local retval = modmgr.handle_buttons(tabbuilder.current_tab,fields)
1250         tabbuilder.checkretval(retval)
1251         
1252         retval = gamemgr.handle_buttons(tabbuilder.current_tab,fields)
1253         tabbuilder.checkretval(retval)
1254         
1255         retval = modstore.handle_buttons(tabbuilder.current_tab,fields)
1256         tabbuilder.checkretval(retval)
1257         
1258         if tabbuilder.current_tab == "dialog_create_world" then
1259                 tabbuilder.handle_create_world_buttons(fields)
1260         end
1261         
1262         if tabbuilder.current_tab == "dialog_delete_world" then
1263                 tabbuilder.handle_delete_world_buttons(fields)
1264         end
1265         
1266         if tabbuilder.current_tab == "singleplayer" then
1267                 tabbuilder.handle_singleplayer_buttons(fields)
1268         end
1269         
1270         if tabbuilder.current_tab == "multiplayer" then
1271                 tabbuilder.handle_multiplayer_buttons(fields)
1272         end
1273         
1274         if tabbuilder.current_tab == "settings" then
1275                 tabbuilder.handle_settings_buttons(fields)
1276         end
1277         
1278         if tabbuilder.current_tab == "server" then
1279                 tabbuilder.handle_server_buttons(fields)
1280         end
1281         
1282         --tab buttons
1283         tabbuilder.handle_tab_buttons(fields)
1284         
1285         --menubar buttons
1286         menubar.handle_buttons(fields)
1287         
1288         if not tabbuilder.skipformupdate then
1289                 --update menu
1290                 update_menu()
1291         else
1292                 tabbuilder.skipformupdate = false
1293         end
1294 end
1295
1296 --------------------------------------------------------------------------------
1297 engine.event_handler = function(event)
1298         if event == "MenuQuit" then
1299                 if tabbuilder.is_dialog then
1300                         tabbuilder.is_dialog = false
1301                         tabbuilder.show_buttons = true
1302                         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
1303                         update_menu()
1304                 else
1305                         engine.close()
1306                 end
1307         end
1308 end
1309
1310 --------------------------------------------------------------------------------
1311 --------------------------------------------------------------------------------
1312 -- menu startup
1313 --------------------------------------------------------------------------------
1314 --------------------------------------------------------------------------------
1315 init_globals()
1316 worldlist.init()
1317 menu.init()
1318 tabbuilder.init()
1319 menubar.refresh()
1320 modstore.init()
1321
1322 update_menu()