tab_simple_main.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. --Minetest
  2. --Copyright (C) 2013 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. --------------------------------------------------------------------------------
  18. local function get_formspec(tabview, name, tabdata)
  19. local retval = ""
  20. local render_details = dump(core.setting_getbool("public_serverlist"))
  21. retval = retval ..
  22. "label[8,0.5;".. fgettext("Name/Password") .. "]" ..
  23. "field[0.25,3.25;5.5,0.5;te_address;;" ..
  24. core.formspec_escape(core.setting_get("address")) .."]" ..
  25. "field[5.75,3.25;2.25,0.5;te_port;;" ..
  26. core.formspec_escape(core.setting_get("remote_port")) .."]" ..
  27. "checkbox[8,-0.25;cb_public_serverlist;".. fgettext("Public Serverlist") .. ";" ..
  28. render_details .. "]"
  29. retval = retval ..
  30. "button[8,2.5;4,1.5;btn_mp_connect;".. fgettext("Connect") .. "]" ..
  31. "field[8.75,1.5;3.5,0.5;te_name;;" ..
  32. core.formspec_escape(core.setting_get("name")) .."]" ..
  33. "pwdfield[8.75,2.3;3.5,0.5;te_pwd;]"
  34. if render_details then
  35. retval = retval .. "tablecolumns[" ..
  36. "color,span=3;" ..
  37. "text,align=right;" .. -- clients
  38. "text,align=center,padding=0.25;" .. -- "/"
  39. "text,align=right,padding=0.25;" .. -- clients_max
  40. image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
  41. image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
  42. image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
  43. "color,span=1;" ..
  44. "text,padding=1]" -- name
  45. else
  46. retval = retval .. "tablecolumns[text]"
  47. end
  48. retval = retval ..
  49. "table[-0.05,0;7.55,2.75;favourites;"
  50. if #menudata.favorites > 0 then
  51. retval = retval .. render_favorite(menudata.favorites[1],render_details)
  52. for i=2,#menudata.favorites,1 do
  53. retval = retval .. "," .. render_favorite(menudata.favorites[i],render_details)
  54. end
  55. end
  56. if tabdata.fav_selected ~= nil then
  57. retval = retval .. ";" .. tabdata.fav_selected .. "]"
  58. else
  59. retval = retval .. ";0]"
  60. end
  61. -- separator
  62. retval = retval ..
  63. "box[-0.28,3.75;12.4,0.1;#FFFFFF]"
  64. -- checkboxes
  65. retval = retval ..
  66. "checkbox[8.0,3.9;cb_creative;".. fgettext("Creative Mode") .. ";" ..
  67. dump(core.setting_getbool("creative_mode")) .. "]"..
  68. "checkbox[8.0,4.4;cb_damage;".. fgettext("Enable Damage") .. ";" ..
  69. dump(core.setting_getbool("enable_damage")) .. "]"
  70. -- buttons
  71. retval = retval ..
  72. "button[0,3.7;8,1.5;btn_start_singleplayer;" .. fgettext("Start Singleplayer") .. "]" ..
  73. "button[0,4.5;8,1.5;btn_config_sp_world;" .. fgettext("Config mods") .. "]"
  74. return retval
  75. end
  76. --------------------------------------------------------------------------------
  77. local function main_button_handler(tabview, fields, name, tabdata)
  78. if fields["btn_start_singleplayer"] then
  79. gamedata.selected_world = gamedata.worldindex
  80. gamedata.singleplayer = true
  81. core.start()
  82. return true
  83. end
  84. if fields["favourites"] ~= nil then
  85. local event = core.explode_table_event(fields["favourites"])
  86. if event.type == "CHG" then
  87. if event.row <= #menudata.favorites then
  88. local address = menudata.favorites[event.row].address
  89. local port = menudata.favorites[event.row].port
  90. if address ~= nil and
  91. port ~= nil then
  92. core.setting_set("address",address)
  93. core.setting_set("remote_port",port)
  94. end
  95. tabdata.fav_selected = event.row
  96. end
  97. end
  98. return true
  99. end
  100. if fields["cb_public_serverlist"] ~= nil then
  101. core.setting_set("public_serverlist", fields["cb_public_serverlist"])
  102. if core.setting_getbool("public_serverlist") then
  103. asyncOnlineFavourites()
  104. else
  105. menudata.favorites = core.get_favorites("local")
  106. end
  107. return true
  108. end
  109. if fields["cb_creative"] then
  110. core.setting_set("creative_mode", fields["cb_creative"])
  111. return true
  112. end
  113. if fields["cb_damage"] then
  114. core.setting_set("enable_damage", fields["cb_damage"])
  115. return true
  116. end
  117. if fields["btn_mp_connect"] ~= nil or
  118. fields["key_enter"] ~= nil then
  119. gamedata.playername = fields["te_name"]
  120. gamedata.password = fields["te_pwd"]
  121. gamedata.address = fields["te_address"]
  122. gamedata.port = fields["te_port"]
  123. local fav_idx = core.get_textlist_index("favourites")
  124. if fav_idx ~= nil and fav_idx <= #menudata.favorites and
  125. menudata.favorites[fav_idx].address == fields["te_address"] and
  126. menudata.favorites[fav_idx].port == fields["te_port"] then
  127. gamedata.servername = menudata.favorites[fav_idx].name
  128. gamedata.serverdescription = menudata.favorites[fav_idx].description
  129. if not is_server_protocol_compat_or_error(menudata.favorites[fav_idx].proto_min,
  130. menudata.favorites[fav_idx].proto_max) then
  131. return true
  132. end
  133. else
  134. gamedata.servername = ""
  135. gamedata.serverdescription = ""
  136. end
  137. gamedata.selected_world = 0
  138. core.setting_set("address",fields["te_address"])
  139. core.setting_set("remote_port",fields["te_port"])
  140. core.start()
  141. return true
  142. end
  143. if fields["btn_config_sp_world"] ~= nil then
  144. local configdialog = create_configure_world_dlg(1)
  145. if (configdialog ~= nil) then
  146. configdialog:set_parent(tabview)
  147. tabview:hide()
  148. configdialog:show()
  149. end
  150. return true
  151. end
  152. end
  153. --------------------------------------------------------------------------------
  154. local function on_activate(type,old_tab,new_tab)
  155. if type == "LEAVE" then
  156. return
  157. end
  158. if core.setting_getbool("public_serverlist") then
  159. asyncOnlineFavourites()
  160. else
  161. menudata.favorites = core.get_favorites("local")
  162. end
  163. end
  164. --------------------------------------------------------------------------------
  165. tab_simple_main = {
  166. name = "main",
  167. caption = fgettext("Main"),
  168. cbf_formspec = get_formspec,
  169. cbf_button_handler = main_button_handler,
  170. on_change = on_activate
  171. }