a773a491240a804431300901ea036232d4f1ab62
[oweals/minetest.git] / builtin / mainmenu / tab_simple_main.lua
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 --------------------------------------------------------------------------------
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]
24
25         local retval =
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;]"
35
36
37         if tabdata.fav_selected and fav_selected then
38                 if gamedata.fav then
39                         retval = retval .. "button[7.7,2.6;2.3,1.5;btn_delete_favorite;" ..
40                                 fgettext("Del. Favorite") .. "]"
41                 end
42         end
43
44         retval = retval .. "tablecolumns[" ..
45                 image_column(fgettext("Favorite"), "favorite") .. ";" ..
46                 "color,span=3;" ..
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;" ..
53                 "color,span=1;" ..
54                 "text,padding=1]" ..                  -- name
55                 "table[-0.05,0;9.2,2.75;favourites;"
56
57         if #menudata.favorites > 0 then
58                 local favs = core.get_favorites("local")
59                 if #favs > 0 then
60                         for i = 1, #favs do
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))
66                                         end
67                                 end
68                                 if favs[i].address ~= menudata.favorites[i].address then
69                                         table.insert(menudata.favorites, i, favs[i])
70                                 end
71                         end
72                 end
73                 retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0))
74                 for i = 2, #menudata.favorites do
75                         retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs))
76                 end
77         end
78
79         if tabdata.fav_selected then
80                 retval = retval .. ";" .. tabdata.fav_selected .. "]"
81         else
82                 retval = retval .. ";0]"
83         end
84
85         -- separator
86         retval = retval .. "box[-0.28,3.75;12.4,0.1;#FFFFFF]"
87
88         -- checkboxes
89         retval = retval ..
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")) .. "]"
94         -- buttons
95         retval = retval ..
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") .. "]"
98
99         return retval
100 end
101
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
107                 core.start()
108                 return true
109         end
110
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
115                                 gamedata.fav = false
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
121
122                                 for i = 1, #favs do
123                                         if fav.address == favs[i].address and
124                                                         fav.port == favs[i].port then
125                                                 gamedata.fav = true
126                                         end
127                                 end
128
129                                 if address and port then
130                                         core.setting_set("address", address)
131                                         core.setting_set("remote_port", port)
132                                 end
133                                 tabdata.fav_selected = event.row
134                         end
135                         return true
136                 end
137         end
138
139         if fields.btn_delete_favorite then
140                 local current_favourite = core.get_table_index("favourites")
141                 if not current_favourite then return end
142
143                 core.delete_favorite(current_favourite)
144                 asyncOnlineFavourites()
145                 tabdata.fav_selected = nil
146
147                 core.setting_set("address", "")
148                 core.setting_set("remote_port", "30000")
149                 return true
150         end
151
152         if fields.cb_creative then
153                 core.setting_set("creative_mode", fields.cb_creative)
154                 return true
155         end
156
157         if fields.cb_damage then
158                 core.setting_set("enable_damage", fields.cb_damage)
159                 return true
160         end
161
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")
168
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
175
176                         if menudata.favorites_is_public and
177                                         not is_server_protocol_compat_or_error(
178                                                 fav.proto_min, fav.proto_max) then
179                                 return true
180                         end
181                 else
182                         gamedata.servername        = ""
183                         gamedata.serverdescription = ""
184                 end
185
186                 gamedata.selected_world = 0
187
188                 core.setting_set("address", fields.te_address)
189                 core.setting_set("remote_port", fields.te_port)
190
191                 core.start()
192                 return true
193         end
194
195         if fields.btn_config_sp_world then
196                 local configdialog = create_configure_world_dlg(1)
197                 if configdialog then
198                         configdialog:set_parent(tabview)
199                         tabview:hide()
200                         configdialog:show()
201                 end
202                 return true
203         end
204 end
205
206 --------------------------------------------------------------------------------
207 local function on_activate(type,old_tab,new_tab)
208         if type == "LEAVE" then return end
209         asyncOnlineFavourites()
210 end
211
212 --------------------------------------------------------------------------------
213 return {
214         name = "main",
215         caption = fgettext("Main"),
216         cbf_formspec = get_formspec,
217         cbf_button_handler = main_button_handler,
218         on_change = on_activate
219 }