2 --Copyright (C) 2014 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[7.75,-0.15;" .. fgettext("Address / Port") .. "]" ..
27 "label[7.75,1.05;" .. fgettext("Name / Password") .. "]" ..
28 "field[8,0.75;3.3,0.5;te_address;;" ..
29 core.formspec_escape(core.setting_get("address")) .. "]" ..
30 "field[11.15,0.75;1.4,0.5;te_port;;" ..
31 core.formspec_escape(core.setting_get("remote_port")) .. "]" ..
32 "button[10.1,4.9;2,0.5;btn_mp_connect;" .. fgettext("Connect") .. "]" ..
33 "field[8,1.95;2.95,0.5;te_name;;" ..
34 core.formspec_escape(core.setting_get("name")) .. "]" ..
35 "pwdfield[10.78,1.95;1.77,0.5;te_pwd;]" ..
36 "box[7.73,2.35;4.3,2.28;#999999]"
38 if tabdata.fav_selected and fav_selected then
40 retval = retval .. "button[7.85,4.9;2.3,0.5;btn_delete_favorite;" ..
41 fgettext("Del. Favorite") .. "]"
43 if fav_selected.description then
44 retval = retval .. "textarea[8.1,2.4;4.26,2.6;;" ..
45 core.formspec_escape((gamedata.serverdescription or ""), true) .. ";]"
50 retval = retval .. "tablecolumns[" ..
51 image_column(fgettext("Favorite"), "favorite") .. ";" ..
53 "text,align=right;" .. -- clients
54 "text,align=center,padding=0.25;" .. -- "/"
55 "text,align=right,padding=0.25;" .. -- clients_max
56 image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
57 image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
58 image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
61 "table[-0.15,-0.1;7.75,5.5;favourites;"
63 if #menudata.favorites > 0 then
64 local favs = core.get_favorites("local")
67 for j = 1, #menudata.favorites do
68 if menudata.favorites[j].address == favs[i].address and
69 menudata.favorites[j].port == favs[i].port then
70 table.insert(menudata.favorites, i, table.remove(menudata.favorites, j))
73 if favs[i].address ~= menudata.favorites[i].address then
74 table.insert(menudata.favorites, i, favs[i])
78 retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0))
79 for i = 2, #menudata.favorites do
80 retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs))
84 if tabdata.fav_selected then
85 retval = retval .. ";" .. tabdata.fav_selected .. "]"
87 retval = retval .. ";0]"
93 --------------------------------------------------------------------------------
94 local function main_button_handler(tabview, fields, name, tabdata)
95 if fields.te_name then
96 gamedata.playername = fields.te_name
97 core.setting_set("name", fields.te_name)
100 if fields.favourites then
101 local event = core.explode_table_event(fields.favourites)
102 local fav = menudata.favorites[event.row]
104 if event.type == "DCL" then
105 if event.row <= #menudata.favorites then
106 if menudata.favorites_is_public and
107 not is_server_protocol_compat_or_error(
108 fav.proto_min, fav.proto_max) then
112 gamedata.address = fav.address
113 gamedata.port = fav.port
114 gamedata.playername = fields.te_name
115 gamedata.selected_world = 0
117 if fields.te_pwd then
118 gamedata.password = fields.te_pwd
121 gamedata.servername = fav.name
122 gamedata.serverdescription = fav.description
124 if gamedata.address and gamedata.port then
125 core.setting_set("address", gamedata.address)
126 core.setting_set("remote_port", gamedata.port)
133 if event.type == "CHG" then
134 if event.row <= #menudata.favorites then
136 local favs = core.get_favorites("local")
137 local address = fav.address
138 local port = fav.port
139 gamedata.serverdescription = fav.description
142 if fav.address == favs[i].address and
143 fav.port == favs[i].port then
148 if address and port then
149 core.setting_set("address", address)
150 core.setting_set("remote_port", port)
152 tabdata.fav_selected = event.row
158 if fields.key_up or fields.key_down then
159 local fav_idx = core.get_table_index("favourites")
160 local fav = menudata.favorites[fav_idx]
163 if fields.key_up and fav_idx > 1 then
164 fav_idx = fav_idx - 1
165 elseif fields.key_down and fav_idx < #menudata.favorites then
166 fav_idx = fav_idx + 1
172 if not menudata.favorites or not fav then
173 tabdata.fav_selected = 0
177 local address = fav.address
178 local port = fav.port
180 if address and port then
181 core.setting_set("address", address)
182 core.setting_set("remote_port", port)
185 tabdata.fav_selected = fav_idx
189 if fields.btn_delete_favorite then
190 local current_favourite = core.get_table_index("favourites")
191 if not current_favourite then return end
193 core.delete_favorite(current_favourite)
194 asyncOnlineFavourites()
195 tabdata.fav_selected = nil
197 core.setting_set("address", "")
198 core.setting_set("remote_port", "30000")
202 if (fields.btn_mp_connect or fields.key_enter) and fields.te_address and fields.te_port then
203 gamedata.playername = fields.te_name
204 gamedata.password = fields.te_pwd
205 gamedata.address = fields.te_address
206 gamedata.port = fields.te_port
207 gamedata.selected_world = 0
208 local fav_idx = core.get_table_index("favourites")
209 local fav = menudata.favorites[fav_idx]
211 if fav_idx and fav_idx <= #menudata.favorites and
212 fav.address == fields.te_address and
213 fav.port == fields.te_port then
215 gamedata.servername = fav.name
216 gamedata.serverdescription = fav.description
218 if menudata.favorites_is_public and
219 not is_server_protocol_compat_or_error(
220 fav.proto_min, fav.proto_max) then
224 gamedata.servername = ""
225 gamedata.serverdescription = ""
228 core.setting_set("address", fields.te_address)
229 core.setting_set("remote_port", fields.te_port)
237 local function on_change(type, old_tab, new_tab)
238 if type == "LEAVE" then return end
239 asyncOnlineFavourites()
242 --------------------------------------------------------------------------------
244 name = "multiplayer",
245 caption = fgettext("Client"),
246 cbf_formspec = get_formspec,
247 cbf_button_handler = main_button_handler,
248 on_change = on_change