mainmenu.lua 36 KB

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