tab_local.lua 12 KB

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