]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/tab_simple_main.lua
Various grammar improvements (#7769)
[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.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;]"
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                 image_column(fgettext("Ping"), "") .. ",padding=0.25;" ..
47                 "color,span=3;" ..
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;" ..
54                 "color,span=1;" ..
55                 "text,padding=1]" ..                  -- name
56                 "table[-0.05,0;9.2,2.75;favourites;"
57
58         if #menudata.favorites > 0 then
59                 local favs = core.get_favorites("local")
60                 if #favs > 0 then
61                         for i = 1, #favs do
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))
67                                         end
68                                 end
69                                 if favs[i].address ~= menudata.favorites[i].address then
70                                         table.insert(menudata.favorites, i, favs[i])
71                                 end
72                         end
73                 end
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))
77                 end
78         end
79
80         if tabdata.fav_selected then
81                 retval = retval .. ";" .. tabdata.fav_selected .. "]"
82         else
83                 retval = retval .. ";0]"
84         end
85
86         -- separator
87         retval = retval .. "box[-0.28,3.75;12.4,0.1;#FFFFFF]"
88
89         -- checkboxes
90         retval = retval ..
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")) .. "]"
95         -- buttons
96         retval = retval ..
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") .. "]"
99
100         return retval
101 end
102
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
108                 core.start()
109                 return true
110         end
111
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
116                                 gamedata.fav = false
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
122
123                                 for i = 1, #favs do
124                                         if fav.address == favs[i].address and
125                                                         fav.port == favs[i].port then
126                                                 gamedata.fav = true
127                                         end
128                                 end
129
130                                 if address and port then
131                                         core.settings:set("address", address)
132                                         core.settings:set("remote_port", port)
133                                 end
134                                 tabdata.fav_selected = event.row
135                         end
136                         return true
137                 end
138         end
139
140         if fields.btn_delete_favorite then
141                 local current_favourite = core.get_table_index("favourites")
142                 if not current_favourite then return end
143
144                 core.delete_favorite(current_favourite)
145                 asyncOnlineFavourites()
146                 tabdata.fav_selected = nil
147
148                 core.settings:set("address", "")
149                 core.settings:set("remote_port", "30000")
150                 return true
151         end
152
153         if fields.cb_creative then
154                 core.settings:set("creative_mode", fields.cb_creative)
155                 return true
156         end
157
158         if fields.cb_damage then
159                 core.settings:set("enable_damage", fields.cb_damage)
160                 return true
161         end
162
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")
169
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
176
177                         if menudata.favorites_is_public and
178                                         not is_server_protocol_compat_or_error(
179                                                 fav.proto_min, fav.proto_max) then
180                                 return true
181                         end
182                 else
183                         gamedata.servername        = ""
184                         gamedata.serverdescription = ""
185                 end
186
187                 gamedata.selected_world = 0
188
189                 core.settings:set("address", fields.te_address)
190                 core.settings:set("remote_port", fields.te_port)
191                         
192                 core.start()            
193                 return true             
194         end
195
196         if fields.btn_config_sp_world then
197                 local configdialog = create_configure_world_dlg(1)
198                 if configdialog then
199                         configdialog:set_parent(tabview)
200                         tabview:hide()
201                         configdialog:show()
202                 end
203                 return true
204         end
205 end
206
207 --------------------------------------------------------------------------------
208 local function on_activate(type,old_tab,new_tab)
209         if type == "LEAVE" then return end
210         asyncOnlineFavourites()
211 end
212
213 --------------------------------------------------------------------------------
214 return {
215         name = "main",
216         caption = fgettext("Main"),
217         cbf_formspec = get_formspec,
218         cbf_button_handler = main_button_handler,
219         on_change = on_activate
220 }