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.settings:get("address")) .."]" ..
29 "field[5.75,3.35;2.25,0.5;te_port;;" ..
30 core.formspec_escape(core.settings: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.settings: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") .. ";" ..
46 image_column(fgettext("Ping"), "") .. ",padding=0.25;" ..
48 "text,align=right;" .. -- clients
49 "text,align=center,padding=0.25;" .. -- "/"
50 "text,align=right,padding=0.25;" .. -- clients_max
51 image_column(fgettext("Creative mode"), "creative") .. ",padding=1;" ..
52 image_column(fgettext("Damage enabled"), "damage") .. ",padding=0.25;" ..
53 image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" ..
55 "text,padding=1]" .. -- name
56 "table[-0.05,0;9.2,2.75;favourites;"
58 if #menudata.favorites > 0 then
59 local favs = core.get_favorites("local")
62 for j = 1, #menudata.favorites do
63 if menudata.favorites[j].address == favs[i].address and
64 menudata.favorites[j].port == favs[i].port then
65 table.insert(menudata.favorites, i,
66 table.remove(menudata.favorites, j))
69 if favs[i].address ~= menudata.favorites[i].address then
70 table.insert(menudata.favorites, i, favs[i])
74 retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0))
75 for i = 2, #menudata.favorites do
76 retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs))
80 if tabdata.fav_selected then
81 retval = retval .. ";" .. tabdata.fav_selected .. "]"
83 retval = retval .. ";0]"
87 retval = retval .. "box[-0.28,3.75;12.4,0.1;#FFFFFF]"
91 "checkbox[8.0,3.9;cb_creative;".. fgettext("Creative Mode") .. ";" ..
92 dump(core.settings:get_bool("creative_mode")) .. "]"..
93 "checkbox[8.0,4.4;cb_damage;".. fgettext("Enable Damage") .. ";" ..
94 dump(core.settings:get_bool("enable_damage")) .. "]"
97 "button[0,3.7;8,1.5;btn_start_singleplayer;" .. fgettext("Start Singleplayer") .. "]" ..
98 "button[0,4.5;8,1.5;btn_config_sp_world;" .. fgettext("Config mods") .. "]"
103 --------------------------------------------------------------------------------
104 local function main_button_handler(tabview, fields, name, tabdata)
105 if fields.btn_start_singleplayer then
106 gamedata.selected_world = gamedata.worldindex
107 gamedata.singleplayer = true
112 if fields.favourites then
113 local event = core.explode_table_event(fields.favourites)
114 if event.type == "CHG" then
115 if event.row <= #menudata.favorites then
117 local favs = core.get_favorites("local")
118 local fav = menudata.favorites[event.row]
119 local address = fav.address
120 local port = fav.port
121 gamedata.serverdescription = fav.description
124 if fav.address == favs[i].address and
125 fav.port == favs[i].port then
130 if address and port then
131 core.settings:set("address", address)
132 core.settings:set("remote_port", port)
134 tabdata.fav_selected = event.row
140 if fields.btn_delete_favorite then
141 local current_favourite = core.get_table_index("favourites")
142 if not current_favourite then return end
144 core.delete_favorite(current_favourite)
145 asyncOnlineFavourites()
146 tabdata.fav_selected = nil
148 core.settings:set("address", "")
149 core.settings:set("remote_port", "30000")
153 if fields.cb_creative then
154 core.settings:set("creative_mode", fields.cb_creative)
158 if fields.cb_damage then
159 core.settings:set("enable_damage", fields.cb_damage)
163 if fields.btn_mp_connect or fields.key_enter then
164 gamedata.playername = fields.te_name
165 gamedata.password = fields.te_pwd
166 gamedata.address = fields.te_address
167 gamedata.port = fields.te_port
168 local fav_idx = core.get_textlist_index("favourites")
170 if fav_idx and fav_idx <= #menudata.favorites and
171 menudata.favorites[fav_idx].address == fields.te_address and
172 menudata.favorites[fav_idx].port == fields.te_port then
173 local fav = menudata.favorites[fav_idx]
174 gamedata.servername = fav.name
175 gamedata.serverdescription = fav.description
177 if menudata.favorites_is_public and
178 not is_server_protocol_compat_or_error(
179 fav.proto_min, fav.proto_max) then
183 gamedata.servername = ""
184 gamedata.serverdescription = ""
187 gamedata.selected_world = 0
189 core.settings:set("address", fields.te_address)
190 core.settings:set("remote_port", fields.te_port)
196 if fields.btn_config_sp_world then
197 local configdialog = create_configure_world_dlg(1)
199 configdialog:set_parent(tabview)
207 --------------------------------------------------------------------------------
208 local function on_activate(type,old_tab,new_tab)
209 if type == "LEAVE" then return end
210 asyncOnlineFavourites()
213 --------------------------------------------------------------------------------
216 caption = fgettext("Main"),
217 cbf_formspec = get_formspec,
218 cbf_button_handler = main_button_handler,
219 on_change = on_activate