tab_simple_main.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. -- Update the cached supported proto info,
  20. -- it may have changed after a change by the settings menu.
  21. common_update_cached_supp_proto()
  22. local fav_selected = menudata.favorites[tabdata.fav_selected]
  23. local retval =
  24. "label[9.5,0;".. fgettext("Name / Password") .. "]" ..
  25. "field[0.25,3.35;5.5,0.5;te_address;;" ..
  26. core.formspec_escape(core.settings:get("address")) .."]" ..
  27. "field[5.75,3.35;2.25,0.5;te_port;;" ..
  28. core.formspec_escape(core.settings:get("remote_port")) .."]" ..
  29. "button[10,2.6;2,1.5;btn_mp_connect;".. fgettext("Connect") .. "]" ..
  30. "field[9.8,1;2.6,0.5;te_name;;" ..
  31. core.formspec_escape(core.settings:get("name")) .."]" ..
  32. "pwdfield[9.8,2;2.6,0.5;te_pwd;]"
  33. if tabdata.fav_selected and fav_selected then
  34. if gamedata.fav then
  35. retval = retval .. "button[7.7,2.6;2.3,1.5;btn_delete_favorite;" ..
  36. fgettext("Del. Favorite") .. "]"
  37. end
  38. end
  39. retval = retval .. "tablecolumns[" ..
  40. image_column(fgettext("Favorite"), "favorite") .. ";" ..
  41. image_column(fgettext("Ping"), "") .. ",padding=0.25;" ..
  42. "color,span=3;" ..
  43. "text,align=right;" .. -- clients
  44. "text,align=center,padding=0.25;" .. -- "/"
  45. "text,align=right,padding=0.25;" .. -- clients_max
  46. image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
  47. image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
  48. image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
  49. "color,span=1;" ..
  50. "text,padding=1]" .. -- name
  51. "table[-0.05,0;9.2,2.75;favourites;"
  52. if #menudata.favorites > 0 then
  53. local favs = core.get_favorites("local")
  54. if #favs > 0 then
  55. for i = 1, #favs do
  56. for j = 1, #menudata.favorites do
  57. if menudata.favorites[j].address == favs[i].address and
  58. menudata.favorites[j].port == favs[i].port then
  59. table.insert(menudata.favorites, i,
  60. table.remove(menudata.favorites, j))
  61. end
  62. end
  63. if favs[i].address ~= menudata.favorites[i].address then
  64. table.insert(menudata.favorites, i, favs[i])
  65. end
  66. end
  67. end
  68. retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0))
  69. for i = 2, #menudata.favorites do
  70. retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs))
  71. end
  72. end
  73. if tabdata.fav_selected then
  74. retval = retval .. ";" .. tabdata.fav_selected .. "]"
  75. else
  76. retval = retval .. ";0]"
  77. end
  78. -- separator
  79. retval = retval .. "box[-0.28,3.75;12.4,0.1;#FFFFFF]"
  80. -- checkboxes
  81. retval = retval ..
  82. "checkbox[8.0,3.9;cb_creative;".. fgettext("Creative Mode") .. ";" ..
  83. dump(core.settings:get_bool("creative_mode")) .. "]"..
  84. "checkbox[8.0,4.4;cb_damage;".. fgettext("Enable Damage") .. ";" ..
  85. dump(core.settings:get_bool("enable_damage")) .. "]"
  86. -- buttons
  87. retval = retval ..
  88. "button[0,3.7;8,1.5;btn_start_singleplayer;" .. fgettext("Start Singleplayer") .. "]" ..
  89. "button[0,4.5;8,1.5;btn_config_sp_world;" .. fgettext("Config mods") .. "]"
  90. return retval
  91. end
  92. --------------------------------------------------------------------------------
  93. local function main_button_handler(tabview, fields, name, tabdata)
  94. if fields.btn_start_singleplayer then
  95. gamedata.selected_world = gamedata.worldindex
  96. gamedata.singleplayer = true
  97. core.start()
  98. return true
  99. end
  100. if fields.favourites then
  101. local event = core.explode_table_event(fields.favourites)
  102. if event.type == "CHG" then
  103. if event.row <= #menudata.favorites then
  104. gamedata.fav = false
  105. local favs = core.get_favorites("local")
  106. local fav = menudata.favorites[event.row]
  107. local address = fav.address
  108. local port = fav.port
  109. gamedata.serverdescription = fav.description
  110. for i = 1, #favs do
  111. if fav.address == favs[i].address and
  112. fav.port == favs[i].port then
  113. gamedata.fav = true
  114. end
  115. end
  116. if address and port then
  117. core.settings:set("address", address)
  118. core.settings:set("remote_port", port)
  119. end
  120. tabdata.fav_selected = event.row
  121. end
  122. return true
  123. end
  124. end
  125. if fields.btn_delete_favorite then
  126. local current_favourite = core.get_table_index("favourites")
  127. if not current_favourite then return end
  128. core.delete_favorite(current_favourite)
  129. asyncOnlineFavourites()
  130. tabdata.fav_selected = nil
  131. core.settings:set("address", "")
  132. core.settings:set("remote_port", "30000")
  133. return true
  134. end
  135. if fields.cb_creative then
  136. core.settings:set("creative_mode", fields.cb_creative)
  137. return true
  138. end
  139. if fields.cb_damage then
  140. core.settings:set("enable_damage", fields.cb_damage)
  141. return true
  142. end
  143. if fields.btn_mp_connect or fields.key_enter then
  144. gamedata.playername = fields.te_name
  145. gamedata.password = fields.te_pwd
  146. gamedata.address = fields.te_address
  147. gamedata.port = fields.te_port
  148. local fav_idx = core.get_textlist_index("favourites")
  149. if fav_idx and fav_idx <= #menudata.favorites and
  150. menudata.favorites[fav_idx].address == fields.te_address and
  151. menudata.favorites[fav_idx].port == fields.te_port then
  152. local fav = menudata.favorites[fav_idx]
  153. gamedata.servername = fav.name
  154. gamedata.serverdescription = fav.description
  155. if menudata.favorites_is_public and
  156. not is_server_protocol_compat_or_error(
  157. fav.proto_min, fav.proto_max) then
  158. return true
  159. end
  160. else
  161. gamedata.servername = ""
  162. gamedata.serverdescription = ""
  163. end
  164. gamedata.selected_world = 0
  165. core.settings:set("address", fields.te_address)
  166. core.settings:set("remote_port", fields.te_port)
  167. core.start()
  168. return true
  169. end
  170. if fields.btn_config_sp_world then
  171. local configdialog = create_configure_world_dlg(1)
  172. if configdialog then
  173. configdialog:set_parent(tabview)
  174. tabview:hide()
  175. configdialog:show()
  176. end
  177. return true
  178. end
  179. end
  180. --------------------------------------------------------------------------------
  181. local function on_activate(type,old_tab,new_tab)
  182. if type == "LEAVE" then return end
  183. asyncOnlineFavourites()
  184. end
  185. --------------------------------------------------------------------------------
  186. return {
  187. name = "main",
  188. caption = fgettext("Main"),
  189. cbf_formspec = get_formspec,
  190. cbf_button_handler = main_button_handler,
  191. on_change = on_activate
  192. }