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