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 ~= nil 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"] ~= nil then
96 gamedata.playername = fields["te_name"]
97 core.setting_set("name", fields["te_name"])
100 if fields["favourites"] ~= nil 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
111 gamedata.address = fav.address
112 gamedata.port = fav.port
113 gamedata.playername = fields["te_name"]
114 if fields["te_pwd"] ~= nil then
115 gamedata.password = fields["te_pwd"]
117 gamedata.selected_world = 0
119 gamedata.servername = fav.name
120 gamedata.serverdescription = fav.description
122 if gamedata.address ~= nil and
123 gamedata.port ~= nil then
124 core.setting_set("address",gamedata.address)
125 core.setting_set("remote_port",gamedata.port)
132 if event.type == "CHG" then
133 if event.row <= #menudata.favorites then
135 local favs = core.get_favorites("local")
136 local address = fav.address
137 local port = fav.port
138 gamedata.serverdescription = fav.description
141 if fav.address == favs[i].address and
142 fav.port == favs[i].port then
147 if address ~= nil and
149 core.setting_set("address",address)
150 core.setting_set("remote_port",port)
153 tabdata.fav_selected = event.row
160 if fields["key_up"] ~= nil or
161 fields["key_down"] ~= nil then
163 local fav_idx = core.get_table_index("favourites")
165 if fav_idx ~= nil then
166 if fields["key_up"] ~= nil and fav_idx > 1 then
168 else if fields["key_down"] and fav_idx < #menudata.favorites then
175 if menudata.favorites == nil or
176 menudata.favorites[fav_idx] == nil then
177 tabdata.fav_selected = 0
181 local address = menudata.favorites[fav_idx].address
182 local port = menudata.favorites[fav_idx].port
184 if address ~= nil and
186 core.setting_set("address",address)
187 core.setting_set("remote_port",port)
190 tabdata.fav_selected = fav_idx
194 if fields.btn_delete_favorite then
195 local current_favourite = core.get_table_index("favourites")
196 if not current_favourite then return end
198 core.delete_favorite(current_favourite)
199 asyncOnlineFavourites()
200 tabdata.fav_selected = nil
202 core.setting_set("address", "")
203 core.setting_set("remote_port", "30000")
207 if (fields["btn_mp_connect"] ~= nil or
208 fields["key_enter"] ~= nil) and fields["te_address"] ~= nil and
209 fields["te_port"] ~= nil then
211 gamedata.playername = fields["te_name"]
212 gamedata.password = fields["te_pwd"]
213 gamedata.address = fields["te_address"]
214 gamedata.port = fields["te_port"]
216 local fav_idx = core.get_table_index("favourites")
218 if fav_idx ~= nil and fav_idx <= #menudata.favorites and
219 menudata.favorites[fav_idx].address == fields["te_address"] and
220 menudata.favorites[fav_idx].port == fields["te_port"] then
222 local fav = menudata.favorites[fav_idx]
223 gamedata.servername = fav.name
224 gamedata.serverdescription = fav.description
226 if menudata.favorites_is_public and
227 not is_server_protocol_compat_or_error(
228 fav.proto_min, fav.proto_max) then
232 gamedata.servername = ""
233 gamedata.serverdescription = ""
236 gamedata.selected_world = 0
238 core.setting_set("address", fields["te_address"])
239 core.setting_set("remote_port",fields["te_port"])
247 local function on_change(type, old_tab, new_tab)
248 if type == "LEAVE" then return end
249 asyncOnlineFavourites()
252 --------------------------------------------------------------------------------
254 name = "multiplayer",
255 caption = fgettext("Client"),
256 cbf_formspec = get_formspec,
257 cbf_button_handler = main_button_handler,
258 on_change = on_change