tab_local.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. if this.dlg_create_world_closed_at == nil then
  188. this.dlg_create_world_closed_at = 0
  189. end
  190. local world_doubleclick = false
  191. if fields["sp_worlds"] ~= nil then
  192. local event = core.explode_textlist_event(fields["sp_worlds"])
  193. local selected = core.get_textlist_index("sp_worlds")
  194. menu_worldmt_legacy(selected)
  195. if event.type == "DCL" then
  196. world_doubleclick = true
  197. end
  198. if event.type == "CHG" and selected ~= nil then
  199. core.settings:set("mainmenu_last_selected_world",
  200. menudata.worldlist:get_raw_index(selected))
  201. return true
  202. end
  203. end
  204. if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
  205. return true
  206. end
  207. if fields["cb_creative_mode"] then
  208. core.settings:set("creative_mode", fields["cb_creative_mode"])
  209. local selected = core.get_textlist_index("sp_worlds")
  210. menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
  211. return true
  212. end
  213. if fields["cb_enable_damage"] then
  214. core.settings:set("enable_damage", fields["cb_enable_damage"])
  215. local selected = core.get_textlist_index("sp_worlds")
  216. menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
  217. return true
  218. end
  219. if fields["cb_server"] then
  220. core.settings:set("enable_server", fields["cb_server"])
  221. return true
  222. end
  223. if fields["cb_server_announce"] then
  224. core.settings:set("server_announce", fields["cb_server_announce"])
  225. local selected = core.get_textlist_index("srv_worlds")
  226. menu_worldmt(selected, "server_announce", fields["cb_server_announce"])
  227. return true
  228. end
  229. if fields["play"] ~= nil or world_doubleclick or fields["key_enter"] then
  230. local enter_key_duration = core.get_us_time() - this.dlg_create_world_closed_at
  231. if world_doubleclick and enter_key_duration <= 200000 then -- 200 ms
  232. this.dlg_create_world_closed_at = 0
  233. return true
  234. end
  235. local selected = core.get_textlist_index("sp_worlds")
  236. gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
  237. if selected == nil or gamedata.selected_world == 0 then
  238. gamedata.errormessage =
  239. fgettext("No world created or selected!")
  240. return true
  241. end
  242. -- Update last game
  243. local world = menudata.worldlist:get_raw_element(gamedata.selected_world)
  244. local game_obj
  245. if world then
  246. game_obj = pkgmgr.find_by_gameid(world.gameid)
  247. core.settings:set("menu_last_game", game_obj.id)
  248. end
  249. local disabled_settings = get_disabled_settings(game_obj)
  250. for k, _ in pairs(valid_disabled_settings) do
  251. local v = disabled_settings[k]
  252. if v ~= nil then
  253. if k == "enable_server" and v == true then
  254. error("Setting 'enable_server' cannot be force-enabled! The game.conf needs to be fixed.")
  255. end
  256. core.settings:set_bool(k, disabled_settings[k])
  257. end
  258. end
  259. if core.settings:get_bool("enable_server") then
  260. gamedata.playername = fields["te_playername"]
  261. gamedata.password = fields["te_passwd"]
  262. gamedata.port = fields["te_serverport"]
  263. gamedata.address = ""
  264. core.settings:set("port",gamedata.port)
  265. if fields["te_serveraddr"] ~= nil then
  266. core.settings:set("bind_address",fields["te_serveraddr"])
  267. end
  268. else
  269. gamedata.singleplayer = true
  270. end
  271. core.start()
  272. return true
  273. end
  274. if fields["world_create"] ~= nil then
  275. this.dlg_create_world_closed_at = 0
  276. local create_world_dlg = create_create_world_dlg()
  277. create_world_dlg:set_parent(this)
  278. this:hide()
  279. create_world_dlg:show()
  280. mm_game_theme.update("singleplayer", current_game())
  281. return true
  282. end
  283. if fields["world_delete"] ~= nil then
  284. local selected = core.get_textlist_index("sp_worlds")
  285. if selected ~= nil and
  286. selected <= menudata.worldlist:size() then
  287. local world = menudata.worldlist:get_list()[selected]
  288. if world ~= nil and
  289. world.name ~= nil and
  290. world.name ~= "" then
  291. local index = menudata.worldlist:get_raw_index(selected)
  292. local delete_world_dlg = create_delete_world_dlg(world.name,index)
  293. delete_world_dlg:set_parent(this)
  294. this:hide()
  295. delete_world_dlg:show()
  296. mm_game_theme.update("singleplayer",current_game())
  297. end
  298. end
  299. return true
  300. end
  301. if fields["world_configure"] ~= nil then
  302. local selected = core.get_textlist_index("sp_worlds")
  303. if selected ~= nil then
  304. local configdialog =
  305. create_configure_world_dlg(
  306. menudata.worldlist:get_raw_index(selected))
  307. if (configdialog ~= nil) then
  308. configdialog:set_parent(this)
  309. this:hide()
  310. configdialog:show()
  311. mm_game_theme.update("singleplayer",current_game())
  312. end
  313. end
  314. return true
  315. end
  316. end
  317. local function on_change(type, old_tab, new_tab)
  318. if (type == "ENTER") then
  319. local game = current_game()
  320. if game then
  321. apply_game(game)
  322. end
  323. singleplayer_refresh_gamebar()
  324. ui.find_by_name("game_button_bar"):show()
  325. else
  326. menudata.worldlist:set_filtercriteria(nil)
  327. local gamebar = ui.find_by_name("game_button_bar")
  328. if gamebar then
  329. gamebar:hide()
  330. end
  331. core.set_topleft_text("")
  332. -- If new_tab is nil, a dialog is being shown; avoid resetting the theme
  333. if new_tab then
  334. mm_game_theme.update(new_tab,nil)
  335. end
  336. end
  337. end
  338. --------------------------------------------------------------------------------
  339. return {
  340. name = "local",
  341. caption = fgettext("Start Game"),
  342. cbf_formspec = get_formspec,
  343. cbf_button_handler = main_button_handler,
  344. on_change = on_change
  345. }