tab_simple_main.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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[0,3.0;".. fgettext("Address/Port") .. "]"..
  23. "label[8,0.5;".. fgettext("Name/Password") .. "]" ..
  24. "field[0.25,3.25;5.5,0.5;te_address;;" ..core.setting_get("address") .."]" ..
  25. "field[5.75,3.25;2.25,0.5;te_port;;" ..core.setting_get("remote_port") .."]" ..
  26. "checkbox[8,-0.25;cb_public_serverlist;".. fgettext("Public Serverlist") .. ";" ..
  27. render_details .. "]"
  28. retval = retval ..
  29. "button[8,2.5;4,1.5;btn_mp_connect;".. fgettext("Connect") .. "]" ..
  30. "field[8.75,1.5;3.5,0.5;te_name;;" ..core.setting_get("name") .."]" ..
  31. "pwdfield[8.75,2.3;3.5,0.5;te_pwd;]"
  32. --favourites
  33. retval = retval ..
  34. "textlist[-0.05,0.0;7.55,2.75;favourites;"
  35. if #menudata.favorites > 0 then
  36. retval = retval .. render_favorite(menudata.favorites[1],render_details)
  37. for i=2,#menudata.favorites,1 do
  38. retval = retval .. "," .. render_favorite(menudata.favorites[i],render_details)
  39. end
  40. end
  41. if tabdata.fav_selected ~= nil then
  42. retval = retval .. ";" .. tabdata.fav_selected .. "]"
  43. else
  44. retval = retval .. ";0]"
  45. end
  46. -- separator
  47. retval = retval ..
  48. "box[-0.3,3.75;12.4,0.1;#FFFFFF]"
  49. -- checkboxes
  50. retval = retval ..
  51. "checkbox[1.0,3.9;cb_creative;".. fgettext("Creative Mode") .. ";" ..
  52. dump(core.setting_getbool("creative_mode")) .. "]"..
  53. "checkbox[5.0,3.9;cb_damage;".. fgettext("Enable Damage") .. ";" ..
  54. dump(core.setting_getbool("enable_damage")) .. "]" ..
  55. "checkbox[8,3.9;cb_fly_mode;".. fgettext("Fly mode") .. ";" ..
  56. dump(core.setting_getbool("free_move")) .. "]"
  57. -- buttons
  58. retval = retval ..
  59. "button[3.0,4.5;6,1.5;btn_start_singleplayer;" .. fgettext("Start Singleplayer") .. "]"
  60. return retval
  61. end
  62. --------------------------------------------------------------------------------
  63. local function main_button_handler(tabview, fields, name, tabdata)
  64. if fields["btn_start_singleplayer"] then
  65. gamedata.selected_world = gamedata.worldindex
  66. gamedata.singleplayer = true
  67. core.start()
  68. end
  69. if fields["favourites"] ~= nil then
  70. local event = core.explode_textlist_event(fields["favourites"])
  71. if event.type == "CHG" then
  72. if event.index <= #maintab_favorites then
  73. local address = maintab_favorites[event.index].address
  74. local port = maintab_favorites[event.index].port
  75. if address ~= nil and
  76. port ~= nil then
  77. core.setting_set("address",address)
  78. core.setting_set("remote_port",port)
  79. end
  80. tabdata.fav_selected = event.index
  81. end
  82. end
  83. return
  84. end
  85. if fields["cb_public_serverlist"] ~= nil then
  86. core.setting_set("public_serverlist", fields["cb_public_serverlist"])
  87. if core.setting_getbool("public_serverlist") then
  88. asyncOnlineFavourites()
  89. else
  90. maintab_favorites = core.get_favorites("local")
  91. end
  92. return
  93. end
  94. if fields["cb_creative"] then
  95. core.setting_set("creative_mode", fields["cb_creative"])
  96. end
  97. if fields["cb_damage"] then
  98. core.setting_set("enable_damage", fields["cb_damage"])
  99. end
  100. if fields["cb_fly_mode"] then
  101. core.setting_set("free_move", fields["cb_fly_mode"])
  102. end
  103. if fields["btn_mp_connect"] ~= nil or
  104. fields["key_enter"] ~= nil then
  105. gamedata.playername = fields["te_name"]
  106. gamedata.password = fields["te_pwd"]
  107. gamedata.address = fields["te_address"]
  108. gamedata.port = fields["te_port"]
  109. local fav_idx = core.get_textlist_index("favourites")
  110. if fav_idx ~= nil and fav_idx <= #menudata.favorites and
  111. menudata.favorites[fav_idx].address == fields["te_address"] and
  112. menudata.favorites[fav_idx].port == fields["te_port"] then
  113. gamedata.servername = menudata.favorites[fav_idx].name
  114. gamedata.serverdescription = menudata.favorites[fav_idx].description
  115. else
  116. gamedata.servername = ""
  117. gamedata.serverdescription = ""
  118. end
  119. gamedata.selected_world = 0
  120. core.setting_set("address",fields["te_address"])
  121. core.setting_set("remote_port",fields["te_port"])
  122. core.start()
  123. return
  124. end
  125. end
  126. --------------------------------------------------------------------------------
  127. local function on_activate(type,old_tab,new_tab)
  128. if type == "LEAVE" then
  129. return
  130. end
  131. if core.setting_getbool("public_serverlist") then
  132. asyncOnlineFavourites()
  133. else
  134. menudata.favorites = core.get_favorites("local")
  135. end
  136. end
  137. --------------------------------------------------------------------------------
  138. tab_simple_main = {
  139. name = "main",
  140. caption = fgettext("Main"),
  141. cbf_formspec = get_formspec,
  142. cbf_button_handler = main_button_handler,
  143. on_change = on_activate
  144. }