tab_local.lua 10 KB

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