2 --Copyright (C) 2013 sapier
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.
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.
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.
18 --------------------------------------------------------------------------------
19 local function get_formspec(tabview, name, tabdata)
20 -- Update the cached supported proto info,
21 -- it may have changed after a change by the settings menu.
22 common_update_cached_supp_proto()
23 local fav_selected = menudata.favorites[tabdata.fav_selected]
26 "label[9.5,0;".. fgettext("Name / Password") .. "]" ..
27 "field[0.25,3.35;5.5,0.5;te_address;;" ..
28 core.formspec_escape(core.setting_get("address")) .."]" ..
29 "field[5.75,3.35;2.25,0.5;te_port;;" ..
30 core.formspec_escape(core.setting_get("remote_port")) .."]" ..
31 "button[10,2.6;2,1.5;btn_mp_connect;".. fgettext("Connect") .. "]" ..
32 "field[9.8,1;2.6,0.5;te_name;;" ..
33 core.formspec_escape(core.setting_get("name")) .."]" ..
34 "pwdfield[9.8,2;2.6,0.5;te_pwd;]"
37 if tabdata.fav_selected and fav_selected then
39 retval = retval .. "button[7.7,2.6;2.3,1.5;btn_delete_favorite;" ..
40 fgettext("Del. Favorite") .. "]"
44 retval = retval .. "tablecolumns[" ..
45 image_column(fgettext("Favorite"), "favorite") .. ";" ..
47 "text,align=right;" .. -- clients
48 "text,align=center,padding=0.25;" .. -- "/"
49 "text,align=right,padding=0.25;" .. -- clients_max
50 image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
51 image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
52 image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
54 "text,padding=1]" .. -- name
55 "table[-0.05,0;9.2,2.75;favourites;"
57 if #menudata.favorites > 0 then
58 local favs = core.get_favorites("local")
61 for j = 1, #menudata.favorites do
62 if menudata.favorites[j].address == favs[i].address and
63 menudata.favorites[j].port == favs[i].port then
64 table.insert(menudata.favorites, i,
65 table.remove(menudata.favorites, j))
68 if favs[i].address ~= menudata.favorites[i].address then
69 table.insert(menudata.favorites, i, favs[i])
73 retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0))
74 for i = 2, #menudata.favorites do
75 retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs))
79 if tabdata.fav_selected then
80 retval = retval .. ";" .. tabdata.fav_selected .. "]"
82 retval = retval .. ";0]"
86 retval = retval .. "box[-0.28,3.75;12.4,0.1;#FFFFFF]"
90 "checkbox[8.0,3.9;cb_creative;".. fgettext("Creative Mode") .. ";" ..
91 dump(core.setting_getbool("creative_mode")) .. "]"..
92 "checkbox[8.0,4.4;cb_damage;".. fgettext("Enable Damage") .. ";" ..
93 dump(core.setting_getbool("enable_damage")) .. "]"
96 "button[0,3.7;8,1.5;btn_start_singleplayer;" .. fgettext("Start Singleplayer") .. "]" ..
97 "button[0,4.5;8,1.5;btn_config_sp_world;" .. fgettext("Config mods") .. "]"
102 --------------------------------------------------------------------------------
103 local function main_button_handler(tabview, fields, name, tabdata)
104 if fields.btn_start_singleplayer then
105 gamedata.selected_world = gamedata.worldindex
106 gamedata.singleplayer = true
111 if fields.favourites then
112 local event = core.explode_table_event(fields.favourites)
113 if event.type == "CHG" then
114 if event.row <= #menudata.favorites then
116 local favs = core.get_favorites("local")
117 local fav = menudata.favorites[event.row]
118 local address = fav.address
119 local port = fav.port
120 gamedata.serverdescription = fav.description
123 if fav.address == favs[i].address and
124 fav.port == favs[i].port then
129 if address and port then
130 core.setting_set("address", address)
131 core.setting_set("remote_port", port)
133 tabdata.fav_selected = event.row
139 if fields.btn_delete_favorite then
140 local current_favourite = core.get_table_index("favourites")
141 if not current_favourite then return end
143 core.delete_favorite(current_favourite)
144 asyncOnlineFavourites()
145 tabdata.fav_selected = nil
147 core.setting_set("address", "")
148 core.setting_set("remote_port", "30000")
152 if fields.cb_creative then
153 core.setting_set("creative_mode", fields.cb_creative)
157 if fields.cb_damage then
158 core.setting_set("enable_damage", fields.cb_damage)
162 if fields.btn_mp_connect or fields.key_enter then
163 gamedata.playername = fields.te_name
164 gamedata.password = fields.te_pwd
165 gamedata.address = fields.te_address
166 gamedata.port = fields.te_port
167 local fav_idx = core.get_textlist_index("favourites")
169 if fav_idx and fav_idx <= #menudata.favorites and
170 menudata.favorites[fav_idx].address == fields.te_address and
171 menudata.favorites[fav_idx].port == fields.te_port then
172 local fav = menudata.favorites[fav_idx]
173 gamedata.servername = fav.name
174 gamedata.serverdescription = fav.description
176 if menudata.favorites_is_public and
177 not is_server_protocol_compat_or_error(
178 fav.proto_min, fav.proto_max) then
182 gamedata.servername = ""
183 gamedata.serverdescription = ""
186 gamedata.selected_world = 0
188 core.setting_set("address", fields.te_address)
189 core.setting_set("remote_port", fields.te_port)
195 if fields.btn_config_sp_world then
196 local configdialog = create_configure_world_dlg(1)
198 configdialog:set_parent(tabview)
206 --------------------------------------------------------------------------------
207 local function on_activate(type,old_tab,new_tab)
208 if type == "LEAVE" then return end
209 asyncOnlineFavourites()
212 --------------------------------------------------------------------------------
215 caption = fgettext("Main"),
216 cbf_formspec = get_formspec,
217 cbf_button_handler = main_button_handler,
218 on_change = on_activate