tab_local.lua 9.8 KB

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