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