tab_local.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 enable_gamebar = PLATFORM ~= "Android"
  18. local current_game, singleplayer_refresh_gamebar
  19. local valid_disabled_settings = {
  20. ["enable_damage"]=true,
  21. ["creative_mode"]=true,
  22. ["enable_server"]=true,
  23. }
  24. if enable_gamebar then
  25. -- Currently chosen game in gamebar for theming and filtering
  26. function current_game()
  27. local last_game_id = core.settings:get("menu_last_game")
  28. local game = pkgmgr.find_by_gameid(last_game_id)
  29. return game
  30. end
  31. function singleplayer_refresh_gamebar()
  32. local old_bar = ui.find_by_name("game_button_bar")
  33. if old_bar ~= nil then
  34. old_bar:delete()
  35. end
  36. local function game_buttonbar_button_handler(fields)
  37. if fields.game_open_cdb then
  38. local maintab = ui.find_by_name("maintab")
  39. local dlg = create_store_dlg("game")
  40. dlg:set_parent(maintab)
  41. maintab:hide()
  42. dlg:show()
  43. return true
  44. end
  45. for key,value in pairs(fields) do
  46. for j=1,#pkgmgr.games,1 do
  47. if ("game_btnbar_" .. pkgmgr.games[j].id == key) then
  48. mm_texture.update("singleplayer", pkgmgr.games[j])
  49. core.set_topleft_text(pkgmgr.games[j].name)
  50. core.settings:set("menu_last_game",pkgmgr.games[j].id)
  51. menudata.worldlist:set_filtercriteria(pkgmgr.games[j].id)
  52. local index = filterlist.get_current_index(menudata.worldlist,
  53. tonumber(core.settings:get("mainmenu_last_selected_world")))
  54. if not index or index < 1 then
  55. local selected = core.get_textlist_index("sp_worlds")
  56. if selected ~= nil and selected < #menudata.worldlist:get_list() then
  57. index = selected
  58. else
  59. index = #menudata.worldlist:get_list()
  60. end
  61. end
  62. menu_worldmt_legacy(index)
  63. return true
  64. end
  65. end
  66. end
  67. end
  68. local btnbar = buttonbar_create("game_button_bar",
  69. game_buttonbar_button_handler,
  70. {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15})
  71. for i=1,#pkgmgr.games,1 do
  72. local btn_name = "game_btnbar_" .. pkgmgr.games[i].id
  73. local image = nil
  74. local text = nil
  75. local tooltip = core.formspec_escape(pkgmgr.games[i].name)
  76. if pkgmgr.games[i].menuicon_path ~= nil and
  77. pkgmgr.games[i].menuicon_path ~= "" then
  78. image = core.formspec_escape(pkgmgr.games[i].menuicon_path)
  79. else
  80. local part1 = pkgmgr.games[i].id:sub(1,5)
  81. local part2 = pkgmgr.games[i].id:sub(6,10)
  82. local part3 = pkgmgr.games[i].id:sub(11)
  83. text = part1 .. "\n" .. part2
  84. if part3 ~= nil and
  85. part3 ~= "" then
  86. text = text .. "\n" .. part3
  87. end
  88. end
  89. btnbar:add_button(btn_name, text, image, tooltip)
  90. end
  91. local plus_image = core.formspec_escape(defaulttexturedir .. "plus.png")
  92. btnbar:add_button("game_open_cdb", "", plus_image, fgettext("Install games from ContentDB"))
  93. end
  94. else
  95. -- Currently chosen game in gamebar: no gamebar -> no "current" game
  96. function current_game()
  97. return nil
  98. end
  99. end
  100. local function get_disabled_settings(game)
  101. if not game then
  102. return {}
  103. end
  104. local gameconfig = Settings(game.path .. "/game.conf")
  105. local disabled_settings = {}
  106. if gameconfig then
  107. local disabled_settings_str = (gameconfig:get("disabled_settings") or ""):split()
  108. for _, value in pairs(disabled_settings_str) do
  109. local state = false
  110. value = value:trim()
  111. if string.sub(value, 1, 1) == "!" then
  112. state = true
  113. value = string.sub(value, 2)
  114. end
  115. if valid_disabled_settings[value] then
  116. disabled_settings[value] = state
  117. else
  118. core.log("error", "Invalid disabled setting in game.conf: "..tostring(value))
  119. end
  120. end
  121. end
  122. return disabled_settings
  123. end
  124. local function get_formspec(tabview, name, tabdata)
  125. local retval = ""
  126. local index = filterlist.get_current_index(menudata.worldlist,
  127. tonumber(core.settings:get("mainmenu_last_selected_world")))
  128. local list = menudata.worldlist:get_list()
  129. local world = list and index and list[index]
  130. local gameid = world and world.gameid
  131. local game = gameid and pkgmgr.find_by_gameid(gameid)
  132. local disabled_settings = get_disabled_settings(game)
  133. local creative, damage, host = "", "", ""
  134. -- Y offsets for game settings checkboxes
  135. local y = -0.2
  136. local yo = 0.45
  137. if disabled_settings["creative_mode"] == nil then
  138. creative = "checkbox[0,"..y..";cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
  139. dump(core.settings:get_bool("creative_mode")) .. "]"
  140. y = y + yo
  141. end
  142. if disabled_settings["enable_damage"] == nil then
  143. damage = "checkbox[0,"..y..";cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
  144. dump(core.settings:get_bool("enable_damage")) .. "]"
  145. y = y + yo
  146. end
  147. if disabled_settings["enable_server"] == nil then
  148. host = "checkbox[0,"..y..";cb_server;".. fgettext("Host Server") ..";" ..
  149. dump(core.settings:get_bool("enable_server")) .. "]"
  150. y = y + yo
  151. end
  152. retval = retval ..
  153. "button[3.9,3.8;2.8,1;world_delete;".. fgettext("Delete") .. "]" ..
  154. "button[6.55,3.8;2.8,1;world_configure;".. fgettext("Select Mods") .. "]" ..
  155. "button[9.2,3.8;2.8,1;world_create;".. fgettext("New") .. "]" ..
  156. "label[3.9,-0.05;".. fgettext("Select World:") .. "]"..
  157. creative ..
  158. damage ..
  159. host ..
  160. "textlist[3.9,0.4;7.9,3.45;sp_worlds;" ..
  161. menu_render_worldlist() ..
  162. ";" .. index .. "]"
  163. if core.settings:get_bool("enable_server") and disabled_settings["enable_server"] == nil then
  164. retval = retval ..
  165. "button[7.9,4.75;4.1,1;play;".. fgettext("Host Game") .. "]" ..
  166. "checkbox[0,"..y..";cb_server_announce;" .. fgettext("Announce Server") .. ";" ..
  167. dump(core.settings:get_bool("server_announce")) .. "]" ..
  168. "field[0.3,2.85;3.8,0.5;te_playername;" .. fgettext("Name") .. ";" ..
  169. core.formspec_escape(core.settings:get("name")) .. "]" ..
  170. "pwdfield[0.3,4.05;3.8,0.5;te_passwd;" .. fgettext("Password") .. "]"
  171. local bind_addr = core.settings:get("bind_address")
  172. if bind_addr ~= nil and bind_addr ~= "" then
  173. retval = retval ..
  174. "field[0.3,5.25;2.5,0.5;te_serveraddr;" .. fgettext("Bind Address") .. ";" ..
  175. core.formspec_escape(core.settings:get("bind_address")) .. "]" ..
  176. "field[2.85,5.25;1.25,0.5;te_serverport;" .. fgettext("Port") .. ";" ..
  177. core.formspec_escape(core.settings:get("port")) .. "]"
  178. else
  179. retval = retval ..
  180. "field[0.3,5.25;3.8,0.5;te_serverport;" .. fgettext("Server Port") .. ";" ..
  181. core.formspec_escape(core.settings:get("port")) .. "]"
  182. end
  183. else
  184. retval = retval ..
  185. "button[7.9,4.75;4.1,1;play;" .. fgettext("Play Game") .. "]"
  186. end
  187. return retval
  188. end
  189. local function main_button_handler(this, fields, name, tabdata)
  190. assert(name == "local")
  191. local world_doubleclick = false
  192. if fields["sp_worlds"] ~= nil then
  193. local event = core.explode_textlist_event(fields["sp_worlds"])
  194. local selected = core.get_textlist_index("sp_worlds")
  195. menu_worldmt_legacy(selected)
  196. if event.type == "DCL" then
  197. world_doubleclick = true
  198. end
  199. if event.type == "CHG" and selected ~= nil then
  200. core.settings:set("mainmenu_last_selected_world",
  201. menudata.worldlist:get_raw_index(selected))
  202. return true
  203. end
  204. end
  205. if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
  206. return true
  207. end
  208. if fields["cb_creative_mode"] then
  209. core.settings:set("creative_mode", fields["cb_creative_mode"])
  210. local selected = core.get_textlist_index("sp_worlds")
  211. menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
  212. return true
  213. end
  214. if fields["cb_enable_damage"] then
  215. core.settings:set("enable_damage", fields["cb_enable_damage"])
  216. local selected = core.get_textlist_index("sp_worlds")
  217. menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
  218. return true
  219. end
  220. if fields["cb_server"] then
  221. core.settings:set("enable_server", fields["cb_server"])
  222. return true
  223. end
  224. if fields["cb_server_announce"] then
  225. core.settings:set("server_announce", fields["cb_server_announce"])
  226. local selected = core.get_textlist_index("srv_worlds")
  227. menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
  228. return true
  229. end
  230. if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
  231. local selected = core.get_textlist_index("sp_worlds")
  232. gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
  233. if selected == nil or gamedata.selected_world == 0 then
  234. gamedata.errormessage =
  235. fgettext("No world created or selected!")
  236. return true
  237. end
  238. -- Update last game
  239. local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
  240. local game_obj
  241. if world then
  242. game_obj = pkgmgr.find_by_gameid(world.gameid)
  243. core.settings:set("menu_last_game", game_obj.id)
  244. end
  245. local disabled_settings = get_disabled_settings(game_obj)
  246. for k, _ in pairs(valid_disabled_settings) do
  247. local v = disabled_settings[k]
  248. if v ~= nil then
  249. if k == "enable_server" and v == true then
  250. error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
  251. end
  252. core.settings:set_bool(k, disabled_settings[k])
  253. end
  254. end
  255. if core.settings:get_bool("enable_server") then
  256. gamedata.playername = fields["te_playername"]
  257. gamedata.password = fields["te_passwd"]
  258. gamedata.port = fields["te_serverport"]
  259. gamedata.address = ""
  260. core.settings:set("port",gamedata.port)
  261. if fields["te_serveraddr"] ~= nil then
  262. core.settings:set("bind_address",fields["te_serveraddr"])
  263. end
  264. else
  265. gamedata.singleplayer = true
  266. end
  267. core.start()
  268. return true
  269. end
  270. if fields["world_create"] ~= nil then
  271. local create_world_dlg = create_create_world_dlg(true)
  272. create_world_dlg:set_parent(this)
  273. this:hide()
  274. create_world_dlg:show()
  275. mm_texture.update("singleplayer", current_game())
  276. return true
  277. end
  278. if fields["world_delete"] ~= nil then
  279. local selected = core.get_textlist_index("sp_worlds")
  280. if selected ~= nil and
  281. selected <= menudata.worldlist:size() then
  282. local world = menudata.worldlist:get_list()[selected]
  283. if world ~= nil and
  284. world.name ~= nil and
  285. world.name ~= "" then
  286. local index = menudata.worldlist:get_raw_index(selected)
  287. local delete_world_dlg = create_delete_world_dlg(world.name,index)
  288. delete_world_dlg:set_parent(this)
  289. this:hide()
  290. delete_world_dlg:show()
  291. mm_texture.update("singleplayer",current_game())
  292. end
  293. end
  294. return true
  295. end
  296. if fields["world_configure"] ~= nil then
  297. local selected = core.get_textlist_index("sp_worlds")
  298. if selected ~= nil then
  299. local configdialog =
  300. create_configure_world_dlg(
  301. menudata.worldlist:get_raw_index(selected))
  302. if (configdialog ~= nil) then
  303. configdialog:set_parent(this)
  304. this:hide()
  305. configdialog:show()
  306. mm_texture.update("singleplayer",current_game())
  307. end
  308. end
  309. return true
  310. end
  311. end
  312. local on_change
  313. if enable_gamebar then
  314. function on_change(type, old_tab, new_tab)
  315. if (type == "ENTER") then
  316. local game = current_game()
  317. if game then
  318. menudata.worldlist:set_filtercriteria(game.id)
  319. core.set_topleft_text(game.name)
  320. mm_texture.update("singleplayer",game)
  321. end
  322. singleplayer_refresh_gamebar()
  323. ui.find_by_name("game_button_bar"):show()
  324. else
  325. menudata.worldlist:set_filtercriteria(nil)
  326. local gamebar = ui.find_by_name("game_button_bar")
  327. if gamebar then
  328. gamebar:hide()
  329. end
  330. core.set_topleft_text("")
  331. mm_texture.update(new_tab,nil)
  332. end
  333. end
  334. end
  335. --------------------------------------------------------------------------------
  336. return {
  337. name = "local",
  338. caption = fgettext("Start Game"),
  339. cbf_formspec = get_formspec,
  340. cbf_button_handler = main_button_handler,
  341. on_change = on_change
  342. }