tab_local.lua 9.6 KB

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