tab_singleplayer.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.setting_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.setting_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.setting_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.65}, "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.setting_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. "button[8.5,4.95;3.25,0.5;play;".. fgettext("Play") .. "]" ..
  85. "label[4,-0.25;".. fgettext("Select World:") .. "]"..
  86. "checkbox[0.25,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" ..
  87. dump(core.setting_getbool("creative_mode")) .. "]"..
  88. "checkbox[0.25,0.7;cb_enable_damage;".. fgettext("Enable Damage") .. ";" ..
  89. dump(core.setting_getbool("enable_damage")) .. "]"..
  90. "textlist[4,0.25;7.5,3.7;sp_worlds;" ..
  91. menu_render_worldlist() ..
  92. ";" .. index .. "]"
  93. return retval
  94. end
  95. local function main_button_handler(this, fields, name, tabdata)
  96. assert(name == "singleplayer")
  97. local world_doubleclick = false
  98. if fields["sp_worlds"] ~= nil then
  99. local event = core.explode_textlist_event(fields["sp_worlds"])
  100. local selected = core.get_textlist_index("sp_worlds")
  101. menu_worldmt_legacy(selected)
  102. if event.type == "DCL" then
  103. world_doubleclick = true
  104. end
  105. if event.type == "CHG" and selected ~= nil then
  106. core.setting_set("mainmenu_last_selected_world",
  107. menudata.worldlist:get_raw_index(selected))
  108. return true
  109. end
  110. end
  111. if menu_handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world") then
  112. return true
  113. end
  114. if fields["cb_creative_mode"] then
  115. core.setting_set("creative_mode", fields["cb_creative_mode"])
  116. local selected = core.get_textlist_index("sp_worlds")
  117. menu_worldmt(selected, "creative_mode", fields["cb_creative_mode"])
  118. return true
  119. end
  120. if fields["cb_enable_damage"] then
  121. core.setting_set("enable_damage", fields["cb_enable_damage"])
  122. local selected = core.get_textlist_index("sp_worlds")
  123. menu_worldmt(selected, "enable_damage", fields["cb_enable_damage"])
  124. return true
  125. end
  126. if fields["play"] ~= nil or
  127. world_doubleclick or
  128. fields["key_enter"] then
  129. local selected = core.get_textlist_index("sp_worlds")
  130. gamedata.selected_world = menudata.worldlist:get_raw_index(selected)
  131. if selected ~= nil and gamedata.selected_world ~= 0 then
  132. gamedata.singleplayer = true
  133. core.start()
  134. else
  135. gamedata.errormessage =
  136. fgettext("No world created or selected!")
  137. end
  138. return true
  139. end
  140. if fields["world_create"] ~= nil then
  141. local create_world_dlg = create_create_world_dlg(true)
  142. create_world_dlg:set_parent(this)
  143. this:hide()
  144. create_world_dlg:show()
  145. mm_texture.update("singleplayer",current_game())
  146. return true
  147. end
  148. if fields["world_delete"] ~= nil then
  149. local selected = core.get_textlist_index("sp_worlds")
  150. if selected ~= nil and
  151. selected <= menudata.worldlist:size() then
  152. local world = menudata.worldlist:get_list()[selected]
  153. if world ~= nil and
  154. world.name ~= nil and
  155. world.name ~= "" then
  156. local index = menudata.worldlist:get_raw_index(selected)
  157. local delete_world_dlg = create_delete_world_dlg(world.name,index)
  158. delete_world_dlg:set_parent(this)
  159. this:hide()
  160. delete_world_dlg:show()
  161. mm_texture.update("singleplayer",current_game())
  162. end
  163. end
  164. return true
  165. end
  166. if fields["world_configure"] ~= nil then
  167. local selected = core.get_textlist_index("sp_worlds")
  168. if selected ~= nil then
  169. local configdialog =
  170. create_configure_world_dlg(
  171. menudata.worldlist:get_raw_index(selected))
  172. if (configdialog ~= nil) then
  173. configdialog:set_parent(this)
  174. this:hide()
  175. configdialog:show()
  176. mm_texture.update("singleplayer",current_game())
  177. end
  178. end
  179. return true
  180. end
  181. end
  182. local function on_change(type, old_tab, new_tab)
  183. local buttonbar = ui.find_by_name("game_button_bar")
  184. if ( buttonbar == nil ) then
  185. singleplayer_refresh_gamebar()
  186. buttonbar = ui.find_by_name("game_button_bar")
  187. end
  188. if (type == "ENTER") then
  189. local game = current_game()
  190. if game then
  191. menudata.worldlist:set_filtercriteria(game.id)
  192. core.set_topleft_text(game.name)
  193. mm_texture.update("singleplayer",game)
  194. end
  195. buttonbar:show()
  196. else
  197. menudata.worldlist:set_filtercriteria(nil)
  198. buttonbar:hide()
  199. core.set_topleft_text("")
  200. mm_texture.update(new_tab,nil)
  201. end
  202. end
  203. --------------------------------------------------------------------------------
  204. return {
  205. name = "singleplayer",
  206. caption = fgettext("Singleplayer"),
  207. cbf_formspec = get_formspec,
  208. cbf_button_handler = main_button_handler,
  209. on_change = on_change
  210. }