tab_local.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. local current_game, singleplayer_refresh_gamebar
  18. local valid_disabled_settings = {
  19. ["enable_damage"]=true,
  20. ["creative_mode"]=true,
  21. ["enable_server"]=true,
  22. }
  23. -- Name and port stored to persist when updating the formspec
  24. local current_name = core.settings:get("name")
  25. local current_port = core.settings:get("port")
  26. -- Currently chosen game in gamebar for theming and filtering
  27. function current_game()
  28. local gameid = core.settings:get("menu_last_game")
  29. local game = gameid and pkgmgr.find_by_gameid(gameid)
  30. -- Fall back to first game installed if one exists.
  31. if not game and #pkgmgr.games > 0 then
  32. -- If devtest is the first game in the list and there is another
  33. -- game available, pick the other game instead.
  34. local picked_game
  35. if pkgmgr.games[1].id == "devtest" and #pkgmgr.games > 1 then
  36. picked_game = 2
  37. else
  38. picked_game = 1
  39. end
  40. game = pkgmgr.games[picked_game]
  41. gameid = game.id
  42. core.settings:set("menu_last_game", gameid)
  43. end
  44. return game
  45. end
  46. -- Apply menu changes from given game
  47. function apply_game(game)
  48. core.settings:set("menu_last_game", game.id)
  49. menudata.worldlist:set_filtercriteria(game.id)
  50. mm_game_theme.set_game(game)
  51. local index = filterlist.get_current_index(menudata.worldlist,
  52. tonumber(core.settings:get("mainmenu_last_selected_world")))
  53. if not index or index < 1 then
  54. local selected = core.get_textlist_index("sp_worlds")
  55. if selected ~= nil and selected < #menudata.worldlist:get_list() then
  56. index = selected
  57. else
  58. index = #menudata.worldlist:get_list()
  59. end
  60. end
  61. menu_worldmt_legacy(index)
  62. end
  63. function singleplayer_refresh_gamebar()
  64. local old_bar = ui.find_by_name("game_button_bar")
  65. if old_bar ~= nil then
  66. old_bar:delete()
  67. end
  68. -- Hide gamebar if no games are installed
  69. if #pkgmgr.games == 0 then
  70. return false
  71. end
  72. local function game_buttonbar_button_handler(fields)
  73. for _, game in ipairs(pkgmgr.games) do
  74. if fields["game_btnbar_" .. game.id] then
  75. apply_game(game)
  76. return true
  77. end
  78. end
  79. end
  80. local ENABLE_TOUCH = core.settings:get_bool("enable_touch")
  81. local gamebar_pos_y = MAIN_TAB_H
  82. + TABHEADER_H -- tabheader included in formspec size
  83. + (ENABLE_TOUCH and GAMEBAR_OFFSET_TOUCH or GAMEBAR_OFFSET_DESKTOP)
  84. local btnbar = buttonbar_create(
  85. "game_button_bar",
  86. {x = 0, y = gamebar_pos_y},
  87. {x = MAIN_TAB_W, y = GAMEBAR_H},
  88. "#000000",
  89. game_buttonbar_button_handler)
  90. for _, game in ipairs(pkgmgr.games) do
  91. local btn_name = "game_btnbar_" .. game.id
  92. local image = nil
  93. local text = nil
  94. local tooltip = core.formspec_escape(game.title)
  95. if (game.menuicon_path or "") ~= "" then
  96. image = core.formspec_escape(game.menuicon_path)
  97. else
  98. local part1 = game.id:sub(1,5)
  99. local part2 = game.id:sub(6,10)
  100. local part3 = game.id:sub(11)
  101. text = part1 .. "\n" .. part2
  102. if part3 ~= "" then
  103. text = text .. "\n" .. part3
  104. end
  105. end
  106. btnbar:add_button(btn_name, text, image, tooltip)
  107. end
  108. local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
  109. btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
  110. return true
  111. end
  112. local function get_disabled_settings(game)
  113. if not game then
  114. return {}
  115. end
  116. local gameconfig = Settings(game.path .. "/game.conf")
  117. local disabled_settings = {}
  118. if gameconfig then
  119. local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
  120. for _, value in pairs(disabled_settings_str) do
  121. local state = false
  122. value = value:trim()
  123. if string.sub(value, 1, 1) == "!" then
  124. state = true
  125. value = string.sub(value, 2)
  126. end
  127. if valid_disabled_settings[value] then
  128. disabled_settings[value] = state
  129. else
  130. core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
  131. end
  132. end
  133. end
  134. return disabled_settings
  135. end
  136. local function get_formspec(tabview, name, tabdata)
  137. -- Point the player to ContentDB when no games are found
  138. if #pkgmgr.games == 0 then
  139. local W = tabview.width
  140. local H = tabview.height
  141. local hypertext = "<global valign=middle halign=center size=18>" ..
  142. fgettext_ne("Minetest is a game-creation platform that allows you to play many different games.") .. "\n" ..
  143. fgettext_ne("Minetest doesn't come with a game by default.") .. " " ..
  144. fgettext_ne("You need to install a game before you can create a world.")
  145. local button_y = H * 2/3 - 0.6
  146. return table.concat({
  147. "hypertext[0.375,0;", W - 2*0.375, ",", button_y, ";ht;", core.formspec_escape(hypertext), "]",
  148. "button[5.25,", button_y, ";5,1.2;game_open_cdb;", fgettext("Install a game"), "]"})
  149. end
  150. local retval = ""
  151. local index = filterlist.get_current_index(menudata.worldlist,
  152. tonumber(core.settings:get("mainmenu_last_selected_world")))
  153. local list = menudata.worldlist:get_list()
  154. local world = list and index and list[index]
  155. local game
  156. if world then
  157. game = pkgmgr.find_by_gameid(world.gameid)
  158. else
  159. game = current_game()
  160. end
  161. local disabled_settings = get_disabled_settings(game)
  162. local creative, damage, host = "", "", ""
  163. -- Y offsets for game settings checkboxes
  164. local y = 0.2
  165. local yo = 0.5625
  166. if disabled_settings["creative_mode"] == nil then
  167. creative = "checkbox[0,"..y..";cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
  168. dump(core.settings:get_bool("creative_mode")) .. "]"
  169. y = y + yo
  170. end
  171. if disabled_settings["enable_damage"] == nil then
  172. damage = "checkbox[0,"..y..";cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
  173. dump(core.settings:get_bool("enable_damage")) .. "]"
  174. y = y + yo
  175. end
  176. if disabled_settings["enable_server"] == nil then
  177. host = "checkbox[0,"..y..";cb_server;".. fgettext("Host Server") ..";" ..
  178. dump(core.settings:get_bool("enable_server")) .. "]"
  179. y = y + yo
  180. end
  181. retval = retval ..
  182. "container[5.25,4.875]" ..
  183. "button[0,0;3.225,0.8;world_delete;".. fgettext("Delete") .. "]" ..
  184. "button[3.325,0;3.225,0.8;world_configure;".. fgettext("Select Mods") .. "]" ..
  185. "button[6.65,0;3.225,0.8;world_create;".. fgettext("New") .. "]" ..
  186. "container_end[]" ..
  187. "container[0.375,0.375]" ..
  188. creative ..
  189. damage ..
  190. host ..
  191. "container_end[]" ..
  192. "container[5.25,0.375]" ..
  193. "label[0,0.2;".. fgettext("Select World:") .. "]"..
  194. "textlist[0,0.5;9.875,3.9;sp_worlds;" ..
  195. menu_render_worldlist() ..
  196. ";" .. index .. "]" ..
  197. "container_end[]"
  198. if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
  199. retval = retval ..
  200. "button[10.1875,5.925;4.9375,0.8;play;".. fgettext("Host Game") .. "]" ..
  201. "container[0.375,0.375]" ..
  202. "checkbox[0,"..y..";cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
  203. dump(core.settings:get_bool("server_announce")) .. "]"
  204. -- Reset y so that the text fields always start at the same position,
  205. -- regardless of whether some of the checkboxes are hidden.
  206. y = 0.2 + 4 * yo + 0.35
  207. retval = retval .. "field[0," .. y .. ";4.5,0.75;te_playername;" .. fgettext("Name") .. ";" ..
  208. core.formspec_escape(current_name) .. "]"
  209. y = y + 1.15 + 0.25
  210. retval = retval .. "pwdfield[0," .. y .. ";4.5,0.75;te_passwd;" .. fgettext("Password") .. "]"
  211. y = y + 1.15 + 0.25
  212. local bind_addr = core.settings:get("bind_address")
  213. if bind_addr ~= nil and bind_addr ~= "" then
  214. retval = retval ..
  215. "field[0," .. y .. ";3,0.75;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
  216. core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
  217. "field[3.25," .. y .. ";1.25,0.75;te_serverport;" .. fgettext("Port") .. ";" ..
  218. core.formspec_escape(current_port) .. "]"
  219. else
  220. retval = retval ..
  221. "field[0," .. y .. ";4.5,0.75;te_serverport;" .. fgettext("Server Port") .. ";" ..
  222. core.formspec_escape(current_port) .. "]"
  223. end
  224. retval = retval .. "container_end[]"
  225. else
  226. retval = retval ..
  227. "button[10.1875,5.925;4.9375,0.8;play;" .. fgettext("Play Game") .. "]"
  228. end
  229. return retval
  230. end
  231. local function main_button_handler(this, fields, name, tabdata)
  232. assert(name == "local")
  233. if fields.game_open_cdb then
  234. local maintab = ui.find_by_name("maintab")
  235. local dlg = create_contentdb_dlg("game")
  236. dlg:set_parent(maintab)
  237. maintab:hide()
  238. dlg:show()
  239. return true
  240. end
  241. if this.dlg_create_world_closed_at == nil then
  242. this.dlg_create_world_closed_at = 0
  243. end
  244. local world_doubleclick = false
  245. if fields["te_playername"] then
  246. current_name = fields["te_playername"]
  247. end
  248. if fields["te_serverport"] then
  249. current_port = fields["te_serverport"]
  250. end
  251. if fields["sp_worlds"] ~= nil then
  252. local event = core.explode_textlist_event(fields["sp_worlds"])
  253. local selected = core.get_textlist_index("sp_worlds")
  254. menu_worldmt_legacy(selected)
  255. if event.type == "DCL" then
  256. world_doubleclick = true
  257. end
  258. if event.type == "CHG" and selected ~= nil then
  259. core.settings:set("mainmenu_last_selected_world",
  260. menudata.worldlist:get_raw_index(selected))
  261. return true
  262. end
  263. end
  264. if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
  265. return true
  266. end
  267. if fields["cb_creative_mode"] then
  268. core.settings:set("creative_mode", fields["cb_creative_mode"])
  269. local selected = core.get_textlist_index("sp_worlds")
  270. menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
  271. return true
  272. end
  273. if fields["cb_enable_damage"] then
  274. core.settings:set("enable_damage", fields["cb_enable_damage"])
  275. local selected = core.get_textlist_index("sp_worlds")
  276. menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
  277. return true
  278. end
  279. if fields["cb_server"] then
  280. core.settings:set("enable_server", fields["cb_server"])
  281. return true
  282. end
  283. if fields["cb_server_announce"] then
  284. core.settings:set("server_announce", fields["cb_server_announce"])
  285. local selected = core.get_textlist_index("srv_worlds")
  286. menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
  287. return true
  288. end
  289. if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
  290. local enter_key_duration = core.get_us_time() - this.dlg_create_world_closed_at
  291. if world_doubleclick and enter_key_duration <= 200000 then -- 200 ms
  292. this.dlg_create_world_closed_at = 0
  293. return true
  294. end
  295. local selected = core.get_textlist_index("sp_worlds")
  296. gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
  297. if selected == nil or gamedata.selected_world == 0 then
  298. gamedata.errormessage =
  299. fgettext_ne("No world created or selected!")
  300. return true
  301. end
  302. -- Update last game
  303. local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
  304. local game_obj
  305. if world then
  306. game_obj = pkgmgr.find_by_gameid(world.gameid)
  307. core.settings:set("menu_last_game", game_obj.id)
  308. end
  309. local disabled_settings = get_disabled_settings(game_obj)
  310. for k, _ in pairs(valid_disabled_settings) do
  311. local v = disabled_settings[k]
  312. if v ~= nil then
  313. if k == "enable_server" and v == true then
  314. error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
  315. end
  316. core.settings:set_bool(k, disabled_settings[k])
  317. end
  318. end
  319. if core.settings:get_bool("enable_server") then
  320. gamedata.playername = fields["te_playername"]
  321. gamedata.password = fields["te_passwd"]
  322. gamedata.port = fields["te_serverport"]
  323. gamedata.address = ""
  324. core.settings:set("port",gamedata.port)
  325. if fields["te_serveraddr"] ~= nil then
  326. core.settings:set("bind_address",fields["te_serveraddr"])
  327. end
  328. else
  329. gamedata.singleplayer = true
  330. end
  331. core.start()
  332. return true
  333. end
  334. if fields["world_create"] ~= nil then
  335. this.dlg_create_world_closed_at = 0
  336. local create_world_dlg = create_create_world_dlg()
  337. create_world_dlg:set_parent(this)
  338. this:hide()
  339. create_world_dlg:show()
  340. return true
  341. end
  342. if fields["world_delete"] ~= nil then
  343. local selected = core.get_textlist_index("sp_worlds")
  344. if selected ~= nil and
  345. selected <= menudata.worldlist:size() then
  346. local world = menudata.worldlist:get_list()[selected]
  347. if world ~= nil and
  348. world.name ~= nil and
  349. world.name ~= "" then
  350. local index = menudata.worldlist:get_raw_index(selected)
  351. local delete_world_dlg = create_delete_world_dlg(world.name,index)
  352. delete_world_dlg:set_parent(this)
  353. this:hide()
  354. delete_world_dlg:show()
  355. end
  356. end
  357. return true
  358. end
  359. if fields["world_configure"] ~= nil then
  360. local selected = core.get_textlist_index("sp_worlds")
  361. if selected ~= nil then
  362. local configdialog =
  363. create_configure_world_dlg(
  364. menudata.worldlist:get_raw_index(selected))
  365. if (configdialog ~= nil) then
  366. configdialog:set_parent(this)
  367. this:hide()
  368. configdialog:show()
  369. end
  370. end
  371. return true
  372. end
  373. end
  374. local function on_change(type)
  375. if type == "ENTER" then
  376. local game = current_game()
  377. if game then
  378. apply_game(game)
  379. else
  380. mm_game_theme.set_engine()
  381. end
  382. if singleplayer_refresh_gamebar() then
  383. ui.find_by_name("game_button_bar"):show()
  384. end
  385. elseif type == "LEAVE" then
  386. menudata.worldlist:set_filtercriteria(nil)
  387. local gamebar = ui.find_by_name("game_button_bar")
  388. if gamebar then
  389. gamebar:hide()
  390. end
  391. end
  392. end
  393. --------------------------------------------------------------------------------
  394. return {
  395. name = "local",
  396. caption = fgettext("Start Game"),
  397. cbf_formspec = get_formspec,
  398. cbf_button_handler = main_button_handler,
  399. on_change = on_change
  400. }