mainmenu.lua 41 KB

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