]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/mainmenu.lua
c8b0e4b872b356a8272e52d9feb66ec060738648
[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 menu.favorites ~= nil then
648                                 gamedata.servername = menu.favorites[event.index].name
649                                 gamedata.serverdescription = menu.favorites[event.index].description
650                         end
651                         
652                         if gamedata.address ~= nil and
653                                 gamedata.port ~= nil then
654                                 
655                                 engine.start()
656                         end
657                 end
658                 
659                 if event.typ == "CHG" then
660                         local address = menu.favorites[event.index].address
661                         local port = menu.favorites[event.index].port
662                         
663                         if address ~= nil and
664                                 port ~= nil then
665                                 engine.setting_set("address",address)
666                                 engine.setting_set("port",port)
667                         end
668                         
669                         menu.fav_selected = event.index
670                 end
671                 return
672         end
673         
674         if fields["key_up"] ~= nil or
675                 fields["key_down"] ~= nil then
676                 
677                 local fav_idx = engine.get_textlist_index("favourites")
678                 
679                 if fields["key_up"] ~= nil and fav_idx > 1 then
680                         fav_idx = fav_idx -1
681                 else if fields["key_down"] and fav_idx < #menu.favorites then
682                         fav_idx = fav_idx +1
683                 end end
684                 
685                 local address = menu.favorites[fav_idx].address
686                 local port = menu.favorites[fav_idx].port
687                 
688                 if address ~= nil and
689                         port ~= nil then
690                         engine.setting_set("address",address)
691                         engine.setting_set("port",port)
692                 end
693                 
694                 menu.fav_selected = fav_idx
695                 return
696         end
697         
698         if fields["cb_public_serverlist"] ~= nil then
699                 engine.setting_setbool("public_serverlist",
700                         tabbuilder.tobool(fields["cb_public_serverlist"]))
701                         
702                 if engine.setting_getbool("public_serverlist") then
703                         menu.favorites = engine.get_favorites("online")
704                 else
705                         menu.favorites = engine.get_favorites("local")
706                 end
707                 menu.fav_selected = nil
708                 return
709         end
710
711         if fields["btn_delete_favorite"] ~= nil then
712                 local current_favourite = engine.get_textlist_index("favourites")
713                 engine.delete_favorite(current_favourite)
714                 menu.favorites = engine.get_favorites()
715                 menu.fav_selected = nil
716                 
717                 engine.setting_set("address","")
718                 engine.setting_get("port","")
719                 
720                 return
721         end
722
723         if fields["btn_mp_connect"] ~= nil or
724                 fields["key_enter"] then
725                 
726                 gamedata.playername             = fields["te_name"]
727                 gamedata.password               = fields["te_pwd"]
728                 gamedata.address                = fields["te_address"]
729                 gamedata.port                   = fields["te_port"]
730                 
731                 local fav_idx = engine.get_textlist_index("favourites")
732                 
733                 if fav_idx > 0 and fav_idx <= #menu.favorites and
734                         menu.favorites[fav_idx].address == fields["te_address"] and
735                         menu.favorites[fav_idx].port == fields["te_port"] then
736                         
737                         gamedata.servername                     = menu.favorites[fav_idx].name
738                         gamedata.serverdescription      = menu.favorites[fav_idx].description
739                 else
740                         gamedata.servername = ""
741                         gamedata.serverdescription = ""
742                 end
743
744                 gamedata.selected_world = 0
745                 
746                 engine.start()
747                 return
748         end
749 end
750
751 --------------------------------------------------------------------------------
752 function tabbuilder.handle_server_buttons(fields)
753
754         local world_doubleclick = false
755
756         if fields["srv_worlds"] ~= nil then
757                 local event = explode_textlist_event(fields["srv_worlds"])
758                 
759                 if event.typ == "DCL" then
760                         world_doubleclick = true
761                 end
762                 if event.typ == "CHG" then
763                         engine.setting_set("mainmenu_last_selected_world",
764                                 worldlist.get_engine_index(engine.get_textlist_index("srv_worlds")))
765                 end
766         end
767         
768         menu.handle_key_up_down(fields,"srv_worlds","mainmenu_last_selected_world")
769         
770         if fields["cb_creative_mode"] then
771                 engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
772         end
773         
774         if fields["cb_enable_damage"] then
775                 engine.setting_setbool("enable_damage",tabbuilder.tobool(fields["cb_enable_damage"]))
776         end
777
778         if fields["cb_server_announce"] then
779                 engine.setting_setbool("server_announce",tabbuilder.tobool(fields["cb_server_announce"]))
780         end
781         
782         if fields["start_server"] ~= nil or
783                 world_doubleclick or
784                 fields["key_enter"] then
785                 local selected = engine.get_textlist_index("srv_worlds")
786                 if selected > 0 then
787                         gamedata.playername             = fields["te_playername"]
788                         gamedata.password               = fields["te_passwd"]
789                         gamedata.port                   = fields["te_serverport"]
790                         gamedata.address                = ""
791                         gamedata.selected_world = worldlist.get_engine_index(selected)
792                         
793                         menu.update_last_game(gamedata.selected_world)
794                         engine.start()
795                 end
796         end
797         
798         if fields["world_create"] ~= nil then
799                 tabbuilder.current_tab = "dialog_create_world"
800                 tabbuilder.is_dialog = true
801                 tabbuilder.show_buttons = false
802         end
803         
804         if fields["world_delete"] ~= nil then
805                 local selected = engine.get_textlist_index("srv_worlds")
806                 if selected > 0 and
807                         selected <= worldlist.size() then
808                         local world = worldlist.get_list()[selected]
809                         if world ~= nil and
810                                 world.name ~= nil and
811                                 world.name ~= "" then
812                                 menu.world_to_del = worldlist.get_engine_index(selected)
813                                 tabbuilder.current_tab = "dialog_delete_world"
814                                 tabbuilder.is_dialog = true
815                                 tabbuilder.show_buttons = false
816                         else
817                                 menu.world_to_del = 0
818                         end
819                 end
820         end
821         
822         if fields["world_configure"] ~= nil then
823                 selected = engine.get_textlist_index("srv_worlds")
824                 if selected > 0 then
825                         modmgr.world_config_selected_world = worldlist.get_engine_index(selected)
826                         if modmgr.init_worldconfig() then
827                                 tabbuilder.current_tab = "dialog_configure_world"
828                                 tabbuilder.is_dialog = true
829                                 tabbuilder.show_buttons = false
830                         end
831                 end
832         end
833 end
834
835 --------------------------------------------------------------------------------
836 function tabbuilder.tobool(text)
837         if text == "true" then
838                 return true
839         else
840                 return false
841         end
842 end
843
844 --------------------------------------------------------------------------------
845 function tabbuilder.handle_settings_buttons(fields)
846         if fields["cb_fancy_trees"] then
847                 engine.setting_setbool("new_style_leaves",tabbuilder.tobool(fields["cb_fancy_trees"]))
848         end
849                 
850         if fields["cb_smooth_lighting"] then
851                 engine.setting_setbool("smooth_lighting",tabbuilder.tobool(fields["cb_smooth_lighting"]))
852         end
853         if fields["cb_3d_clouds"] then
854                 engine.setting_setbool("enable_3d_clouds",tabbuilder.tobool(fields["cb_3d_clouds"]))
855         end
856         if fields["cb_opaque_water"] then
857                 engine.setting_setbool("opaque_water",tabbuilder.tobool(fields["cb_opaque_water"]))
858         end
859                         
860         if fields["cb_mipmapping"] then
861                 engine.setting_setbool("mip_map",tabbuilder.tobool(fields["cb_mipmapping"]))
862         end
863         if fields["cb_anisotrophic"] then
864                 engine.setting_setbool("anisotropic_filter",tabbuilder.tobool(fields["cb_anisotrophic"]))
865         end
866         if fields["cb_bilinear"] then
867                 engine.setting_setbool("bilinear_filter",tabbuilder.tobool(fields["cb_bilinear"]))
868         end
869         if fields["cb_trilinear"] then
870                 engine.setting_setbool("trilinear_filter",tabbuilder.tobool(fields["cb_trilinear"]))
871         end
872                         
873         if fields["cb_shaders"] then
874                 engine.setting_setbool("enable_shaders",tabbuilder.tobool(fields["cb_shaders"]))
875         end
876         if fields["cb_pre_ivis"] then
877                 engine.setting_setbool("preload_item_visuals",tabbuilder.tobool(fields["cb_pre_ivis"]))
878         end
879         if fields["cb_particles"] then
880                 engine.setting_setbool("enable_particles",tabbuilder.tobool(fields["cb_particles"]))
881         end
882         if fields["cb_finite_liquid"] then
883                 engine.setting_setbool("liquid_finite",tabbuilder.tobool(fields["cb_finite_liquid"]))
884         end
885
886         if fields["btn_change_keys"] ~= nil then
887                 engine.show_keys_menu()
888         end
889 end
890
891 --------------------------------------------------------------------------------
892 function tabbuilder.handle_singleplayer_buttons(fields)
893
894         local world_doubleclick = false
895
896         if fields["sp_worlds"] ~= nil then
897                 local event = explode_textlist_event(fields["sp_worlds"])
898                 
899                 if event.typ == "DCL" then
900                         world_doubleclick = true
901                 end
902                 
903                 if event.typ == "CHG" then
904                         engine.setting_set("mainmenu_last_selected_world",
905                                 worldlist.get_engine_index(engine.get_textlist_index("sp_worlds")))
906                 end
907         end
908         
909         menu.handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world")
910         
911         if fields["cb_creative_mode"] then
912                 engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
913         end
914         
915         if fields["cb_enable_damage"] then
916                 engine.setting_setbool("enable_damage",tabbuilder.tobool(fields["cb_enable_damage"]))
917         end
918
919         if fields["play"] ~= nil or
920                 world_doubleclick or
921                 fields["key_enter"] then
922                 local selected = engine.get_textlist_index("sp_worlds")
923                 if selected > 0 then
924                         gamedata.selected_world = worldlist.get_engine_index(selected)
925                         gamedata.singleplayer   = true
926                         
927                         menu.update_last_game(gamedata.selected_world)
928                         
929                         engine.start()
930                 end
931         end
932         
933         if fields["world_create"] ~= nil then
934                 tabbuilder.current_tab = "dialog_create_world"
935                 tabbuilder.is_dialog = true
936                 tabbuilder.show_buttons = false
937         end
938         
939         if fields["world_delete"] ~= nil then
940                 local selected = engine.get_textlist_index("sp_worlds")
941                 if selected > 0 and
942                         selected <= worldlist.size() then
943                         local world = worldlist.get_list()[selected]
944                         if world ~= nil and
945                                 world.name ~= nil and
946                                 world.name ~= "" then
947                                 menu.world_to_del = worldlist.get_engine_index(selected)
948                                 tabbuilder.current_tab = "dialog_delete_world"
949                                 tabbuilder.is_dialog = true
950                                 tabbuilder.show_buttons = false
951                         else
952                                 menu.world_to_del = 0
953                         end
954                 end
955         end
956         
957         if fields["world_configure"] ~= nil then
958                 selected = engine.get_textlist_index("sp_worlds")
959                 if selected > 0 then
960                         modmgr.world_config_selected_world = worldlist.get_engine_index(selected)
961                         if modmgr.init_worldconfig() then
962                                 tabbuilder.current_tab = "dialog_configure_world"
963                                 tabbuilder.is_dialog = true
964                                 tabbuilder.show_buttons = false
965                         end
966                 end
967         end
968 end
969
970 --------------------------------------------------------------------------------
971 function tabbuilder.tab_header()
972
973         if tabbuilder.last_tab_index == nil then
974                 tabbuilder.last_tab_index = 1
975         end
976         
977         local toadd = ""
978         
979         for i=1,#tabbuilder.current_buttons,1 do
980                 
981                 if toadd ~= "" then
982                         toadd = toadd .. ","
983                 end
984                 
985                 toadd = toadd .. tabbuilder.current_buttons[i].caption
986         end
987         return "tabheader[-0.3,-0.99;main_tab;" .. toadd ..";" .. tabbuilder.last_tab_index .. ";true;false]"
988 end
989
990 --------------------------------------------------------------------------------
991 function tabbuilder.handle_tab_buttons(fields)
992
993         if fields["main_tab"] then
994                 local index = tonumber(fields["main_tab"])
995                 tabbuilder.last_tab_index = index
996                 tabbuilder.current_tab = tabbuilder.current_buttons[index].name
997                 
998                 engine.setting_set("main_menu_tab",tabbuilder.current_tab)
999         end
1000         
1001         --handle tab changes
1002         if tabbuilder.current_tab ~= tabbuilder.old_tab then
1003                 if tabbuilder.current_tab ~= "singleplayer" then
1004                         menu.reset_gametype()
1005                 end
1006         end
1007         
1008         if tabbuilder.current_tab == "singleplayer" then
1009                 menu.update_gametype()
1010         end
1011         
1012         tabbuilder.old_tab = tabbuilder.current_tab
1013 end
1014
1015 --------------------------------------------------------------------------------
1016 function tabbuilder.init()
1017         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
1018         
1019         if tabbuilder.current_tab == nil or
1020                 tabbuilder.current_tab == "" then
1021                 tabbuilder.current_tab = "singleplayer"
1022                 engine.setting_set("main_menu_tab",tabbuilder.current_tab)
1023         end
1024         
1025         
1026         --initialize tab buttons
1027         tabbuilder.last_tab = nil
1028         tabbuilder.show_buttons = true
1029         
1030         tabbuilder.current_buttons = {}
1031         table.insert(tabbuilder.current_buttons,{name="singleplayer", caption="Singleplayer"})
1032         table.insert(tabbuilder.current_buttons,{name="multiplayer", caption="Client"})
1033         table.insert(tabbuilder.current_buttons,{name="server", caption="Server"})
1034         table.insert(tabbuilder.current_buttons,{name="settings", caption="Settings"})
1035         
1036         if engine.setting_getbool("main_menu_game_mgr") then
1037                 table.insert(tabbuilder.current_buttons,{name="game_mgr", caption="Games"})
1038         end
1039         
1040         if engine.setting_getbool("main_menu_mod_mgr") then
1041                 table.insert(tabbuilder.current_buttons,{name="mod_mgr", caption="Mods"})
1042         end
1043         table.insert(tabbuilder.current_buttons,{name="credits", caption="Credits"})
1044         
1045         
1046         for i=1,#tabbuilder.current_buttons,1 do
1047                 if tabbuilder.current_buttons[i].name == tabbuilder.current_tab then
1048                         tabbuilder.last_tab_index = i
1049                 end
1050         end
1051         
1052         menu.update_gametype()
1053 end
1054
1055 --------------------------------------------------------------------------------
1056 function tabbuilder.tab_multiplayer()
1057
1058         local retval =
1059                 "vertlabel[0,-0.25;CLIENT]" ..
1060                 "label[1,-0.25;Favorites:]"..
1061                 "label[1,4.25;Address/Port]"..
1062                 "label[9,0;Name/Password]" ..
1063                 "field[1.25,5.25;5.5,0.5;te_address;;" ..engine.setting_get("address") .."]" ..
1064                 "field[6.75,5.25;2.25,0.5;te_port;;" ..engine.setting_get("port") .."]" ..
1065                 "checkbox[1,3.6;cb_public_serverlist;Public Serverlist;" ..
1066                 dump(engine.setting_getbool("public_serverlist")) .. "]"
1067                 
1068         if not engine.setting_getbool("public_serverlist") then
1069                 retval = retval .. 
1070                 "button[6.45,3.95;2.25,0.5;btn_delete_favorite;Delete]"
1071         end
1072         
1073         retval = retval ..
1074                 "button[9,4.95;2.5,0.5;btn_mp_connect;Connect]" ..
1075                 "field[9.25,1;2.5,0.5;te_name;;" ..engine.setting_get("name") .."]" ..
1076                 "pwdfield[9.25,1.75;2.5,0.5;te_pwd;]" ..
1077                 "textarea[9.25,2.25;2.5,2.75;;"
1078         if menu.fav_selected ~= nil and 
1079                 menu.favorites[menu.fav_selected].description ~= nil then
1080                 retval = retval .. 
1081                         fs_escape_string(menu.favorites[menu.fav_selected].description,true)
1082         end
1083         
1084         retval = retval .. 
1085                 ";]" ..
1086                 "textlist[1,0.35;7.5,3.35;favourites;"
1087
1088         local render_details = engine.setting_getbool("public_serverlist")
1089
1090         if #menu.favorites > 0 then
1091                 retval = retval .. render_favourite(menu.favorites[1],render_details)
1092                 
1093                 for i=2,#menu.favorites,1 do
1094                         retval = retval .. "," .. render_favourite(menu.favorites[i],render_details)
1095                 end
1096         end
1097
1098         if menu.fav_selected ~= nil then
1099                 retval = retval .. ";" .. menu.fav_selected .. "]"
1100         else
1101                 retval = retval .. ";0]"
1102         end
1103
1104         return retval
1105 end
1106
1107 --------------------------------------------------------------------------------
1108 function tabbuilder.tab_server()
1109
1110         local index = worldlist.get_current_index(
1111                                 tonumber(engine.setting_get("mainmenu_last_selected_world"))
1112                                 )
1113         
1114         local retval = 
1115                 "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
1116                 "button[6.5,4.15;2.8,0.5;world_create;New]" ..
1117                 "button[9.2,4.15;2.55,0.5;world_configure;Configure]" ..
1118                 "button[8.5,4.9;3.25,0.5;start_server;Start Game]" ..
1119                 "label[4,-0.25;Select World:]"..
1120                 "vertlabel[0,-0.25;START SERVER]" ..
1121                 "checkbox[0.5,0.25;cb_creative_mode;Creative Mode;" ..
1122                 dump(engine.setting_getbool("creative_mode")) .. "]"..
1123                 "checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
1124                 dump(engine.setting_getbool("enable_damage")) .. "]"..
1125                 "checkbox[0.5,1.15;cb_server_announce;Public;" ..
1126                 dump(engine.setting_getbool("server_announce")) .. "]"..
1127                 "field[0.8,3.2;3,0.5;te_playername;Name;" ..
1128                 engine.setting_get("name") .. "]" ..
1129                 "pwdfield[0.8,4.2;3,0.5;te_passwd;Password]" ..
1130                 "field[0.8,5.2;3,0.5;te_serverport;Server Port;30000]" ..
1131                 "textlist[4,0.25;7.5,3.7;srv_worlds;" ..
1132                 menu.render_world_list() ..
1133                 ";" .. index .. "]"
1134                 
1135         return retval
1136 end
1137
1138 --------------------------------------------------------------------------------
1139 function tabbuilder.tab_settings()
1140         return  "vertlabel[0,0;SETTINGS]" ..
1141                         "checkbox[1,0.75;cb_fancy_trees;Fancy trees;"           .. dump(engine.setting_getbool("new_style_leaves"))     .. "]"..
1142                         "checkbox[1,1.25;cb_smooth_lighting;Smooth Lighting;".. dump(engine.setting_getbool("smooth_lighting")) .. "]"..
1143                         "checkbox[1,1.75;cb_3d_clouds;3D Clouds;"                       .. dump(engine.setting_getbool("enable_3d_clouds"))     .. "]"..
1144                         "checkbox[1,2.25;cb_opaque_water;Opaque Water;"                 .. dump(engine.setting_getbool("opaque_water"))         .. "]"..
1145                         
1146                         "checkbox[4,0.75;cb_mipmapping;Mip-Mapping;"            .. dump(engine.setting_getbool("mip_map"))                      .. "]"..
1147                         "checkbox[4,1.25;cb_anisotrophic;Anisotropic Filtering;".. dump(engine.setting_getbool("anisotropic_filter"))   .. "]"..
1148                         "checkbox[4,1.75;cb_bilinear;Bi-Linear Filtering;"      .. dump(engine.setting_getbool("bilinear_filter"))      .. "]"..
1149                         "checkbox[4,2.25;cb_trilinear;Tri-Linear Filtering;"    .. dump(engine.setting_getbool("trilinear_filter"))     .. "]"..
1150                         
1151                         "checkbox[7.5,0.75;cb_shaders;Shaders;"                         .. dump(engine.setting_getbool("enable_shaders"))               .. "]"..
1152                         "checkbox[7.5,1.25;cb_pre_ivis;Preload item visuals;".. dump(engine.setting_getbool("preload_item_visuals"))    .. "]"..
1153                         "checkbox[7.5,1.75;cb_particles;Enable Particles;"      .. dump(engine.setting_getbool("enable_particles"))     .. "]"..
1154                         "checkbox[7.5,2.25;cb_finite_liquid;Finite Liquid;"     .. dump(engine.setting_getbool("liquid_finite"))                .. "]"..
1155                         
1156                         "button[1,3.75;2.25,0.5;btn_change_keys;Change keys]"
1157 end
1158
1159 --------------------------------------------------------------------------------
1160 function tabbuilder.tab_singleplayer()
1161         
1162         local index = worldlist.get_current_index(
1163                                 tonumber(engine.setting_get("mainmenu_last_selected_world"))
1164                                 )
1165
1166         return  "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
1167                         "button[6.5,4.15;2.8,0.5;world_create;New]" ..
1168                         "button[9.2,4.15;2.55,0.5;world_configure;Configure]" ..
1169                         "button[8.5,4.95;3.25,0.5;play;Play]" ..
1170                         "label[4,-0.25;Select World:]"..
1171                         "vertlabel[0,-0.25;SINGLE PLAYER]" ..
1172                         "checkbox[0.5,0.25;cb_creative_mode;Creative Mode;" ..
1173                         dump(engine.setting_getbool("creative_mode")) .. "]"..
1174                         "checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
1175                         dump(engine.setting_getbool("enable_damage")) .. "]"..
1176                         "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
1177                         menu.render_world_list() ..
1178                         ";" .. index .. "]" ..
1179                         menubar.formspec
1180 end
1181
1182 --------------------------------------------------------------------------------
1183 function tabbuilder.tab_credits()
1184         return  "vertlabel[0,-0.5;CREDITS]" ..
1185                         "label[0.5,3;Minetest " .. engine.get_version() .. "]" ..
1186                         "label[0.5,3.3;http://minetest.net]" .. 
1187                         "image[0.5,1;" .. menu.defaulttexturedir .. "logo.png]" ..
1188                         "textlist[3.5,-0.25;8.5,5.8;list_credits;" ..
1189                         "#FFFF00Core Developers," ..
1190                         "Perttu Ahola (celeron55) <celeron55@gmail.com>,"..
1191                         "Ryan Kwolek (kwolekr) <kwolekr@minetest.net>,"..
1192                         "PilzAdam <pilzadam@minetest.net>," ..
1193                         "IIya Zhuravlev (thexyz) <xyz@minetest.net>,"..
1194                         "Lisa Milne (darkrose) <lisa@ltmnet.com>,"..
1195                         "Maciej Kasatkin (RealBadAngel) <mk@realbadangel.pl>,"..
1196                         "proller <proler@gmail.com>,"..
1197                         "sfan5 <sfan5@live.de>,"..
1198                         "kahrl <kahrl@gmx.net>,"..
1199                         ","..
1200                         "#FFFF00Active Contributors," ..
1201                         "sapier,"..
1202                         "Vanessa Ezekowitz (VanessaE) <vanessaezekowitz@gmail.com>,"..
1203                         "Jurgen Doser (doserj) <jurgen.doser@gmail.com>,"..
1204                         "Jeija <jeija@mesecons.net>,"..
1205                         "MirceaKitsune <mirceakitsune@gmail.com>,"..
1206                         "ShadowNinja,"..
1207                         "dannydark <the_skeleton_of_a_child@yahoo.co.uk>,"..
1208                         "0gb.us <0gb.us@0gb.us>,"..
1209                         "," ..
1210                         "#FFFF00Previous Contributors," ..
1211                         "Guiseppe Bilotta (Oblomov) <guiseppe.bilotta@gmail.com>,"..
1212                         "Jonathan Neuschafer <j.neuschaefer@gmx.net>,"..
1213                         "Nils Dagsson Moskopp (erlehmann) <nils@dieweltistgarnichtso.net>,"..
1214                         "Constantin Wenger (SpeedProg) <constantin.wenger@googlemail.com>,"..
1215                         "matttpt <matttpt@gmail.com>,"..
1216                         "JacobF <queatz@gmail.com>,"..
1217                         ";0;true]"
1218 end
1219
1220 --------------------------------------------------------------------------------
1221 function tabbuilder.checkretval(retval)
1222
1223         if retval ~= nil then
1224                 if retval.current_tab ~= nil then
1225                         tabbuilder.current_tab = retval.current_tab
1226                 end
1227                 
1228                 if retval.is_dialog ~= nil then
1229                         tabbuilder.is_dialog = retval.is_dialog
1230                 end
1231                 
1232                 if retval.show_buttons ~= nil then
1233                         tabbuilder.show_buttons = retval.show_buttons
1234                 end
1235                 
1236                 if retval.skipformupdate ~= nil then
1237                         tabbuilder.skipformupdate = retval.skipformupdate
1238                 end
1239         end
1240 end
1241
1242 --------------------------------------------------------------------------------
1243 --------------------------------------------------------------------------------
1244 -- initialize callbacks
1245 --------------------------------------------------------------------------------
1246 --------------------------------------------------------------------------------
1247 engine.button_handler = function(fields)
1248         --print("Buttonhandler: tab: " .. tabbuilder.current_tab .. " fields: " .. dump(fields))
1249         
1250         if fields["btn_error_confirm"] then
1251                 gamedata.errormessage = nil
1252         end
1253         
1254         local retval = modmgr.handle_buttons(tabbuilder.current_tab,fields)
1255         tabbuilder.checkretval(retval)
1256         
1257         retval = gamemgr.handle_buttons(tabbuilder.current_tab,fields)
1258         tabbuilder.checkretval(retval)
1259         
1260         retval = modstore.handle_buttons(tabbuilder.current_tab,fields)
1261         tabbuilder.checkretval(retval)
1262         
1263         if tabbuilder.current_tab == "dialog_create_world" then
1264                 tabbuilder.handle_create_world_buttons(fields)
1265         end
1266         
1267         if tabbuilder.current_tab == "dialog_delete_world" then
1268                 tabbuilder.handle_delete_world_buttons(fields)
1269         end
1270         
1271         if tabbuilder.current_tab == "singleplayer" then
1272                 tabbuilder.handle_singleplayer_buttons(fields)
1273         end
1274         
1275         if tabbuilder.current_tab == "multiplayer" then
1276                 tabbuilder.handle_multiplayer_buttons(fields)
1277         end
1278         
1279         if tabbuilder.current_tab == "settings" then
1280                 tabbuilder.handle_settings_buttons(fields)
1281         end
1282         
1283         if tabbuilder.current_tab == "server" then
1284                 tabbuilder.handle_server_buttons(fields)
1285         end
1286         
1287         --tab buttons
1288         tabbuilder.handle_tab_buttons(fields)
1289         
1290         --menubar buttons
1291         menubar.handle_buttons(fields)
1292         
1293         if not tabbuilder.skipformupdate then
1294                 --update menu
1295                 update_menu()
1296         else
1297                 tabbuilder.skipformupdate = false
1298         end
1299 end
1300
1301 --------------------------------------------------------------------------------
1302 engine.event_handler = function(event)
1303         if event == "MenuQuit" then
1304                 if tabbuilder.is_dialog then
1305                         tabbuilder.is_dialog = false
1306                         tabbuilder.show_buttons = true
1307                         tabbuilder.current_tab = engine.setting_get("main_menu_tab")
1308                         update_menu()
1309                 else
1310                         engine.close()
1311                 end
1312         end
1313 end
1314
1315 --------------------------------------------------------------------------------
1316 --------------------------------------------------------------------------------
1317 -- menu startup
1318 --------------------------------------------------------------------------------
1319 --------------------------------------------------------------------------------
1320 init_globals()
1321 worldlist.init()
1322 menu.init()
1323 tabbuilder.init()
1324 menubar.refresh()
1325 modstore.init()
1326
1327 update_menu()