Fix forgotten favourite list image update of simple menu
[oweals/minetest.git] / builtin / mainmenu / common.lua
1 --Minetest
2 --Copyright (C) 2014 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 -- Global menu data
19 ---------------------------------------------------------------------------------
20 menudata = {}
21
22 --------------------------------------------------------------------------------
23 -- Menu helper functions
24 --------------------------------------------------------------------------------
25
26 --------------------------------------------------------------------------------
27 local function render_client_count(n)
28         if n > 99 then
29                 return '99+'
30         elseif n >= 0 then
31                 return tostring(n)
32         else
33                 return '?'
34         end
35 end
36
37 --------------------------------------------------------------------------------
38 function image_column(tooltip, flagname)
39         return "image," ..
40                 "tooltip=" .. core.formspec_escape(tooltip) .. "," ..
41                 "0=" .. core.formspec_escape(defaulttexturedir .. "blank.png") .. "," ..
42                 "1=" .. core.formspec_escape(defaulttexturedir .. "server_flags_" .. flagname .. ".png")
43 end
44
45 --------------------------------------------------------------------------------
46 function render_favorite(spec,render_details)
47         local text = ""
48
49         if spec.name ~= nil then
50                 text = text .. core.formspec_escape(spec.name:trim())
51
52 --              if spec.description ~= nil and
53 --                      core.formspec_escape(spec.description):trim() ~= "" then
54 --                      text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
55 --              end
56         else
57                 if spec.address ~= nil then
58                         text = text .. spec.address:trim()
59
60                         if spec.port ~= nil then
61                                 text = text .. ":" .. spec.port
62                         end
63                 end
64         end
65
66         if not render_details then
67                 return text
68         end
69
70         local details = ""
71
72         if spec.clients ~= nil and spec.clients_max ~= nil then
73                 local clients_color = ''
74                 local clients_percent = 100 * spec.clients / spec.clients_max
75
76                 -- Choose a color depending on how many clients are connected
77                 -- (relatively to clients_max)
78                 if spec.clients == 0 then
79                         clients_color = ''        -- 0 players: default/white
80                 elseif spec.clients == spec.clients_max then
81                         clients_color = '#dd5b5b' -- full server: red (darker)
82                 elseif clients_percent <= 60 then
83                         clients_color = '#a1e587' -- 0-60%: green
84                 elseif clients_percent <= 90 then
85                         clients_color = '#ffdc97' -- 60-90%: yellow
86                 else
87                         clients_color = '#ffba97' -- 90-100%: orange
88                 end
89
90                 details = details ..
91                                 clients_color .. ',' ..
92                                 render_client_count(spec.clients) .. ',' ..
93                                 '/,' ..
94                                 render_client_count(spec.clients_max) .. ','
95         else
96                 details = details .. ',?,/,?,'
97         end
98
99         if spec.creative then
100                 details = details .. "1,"
101         else
102                 details = details .. "0,"
103         end
104
105         if spec.damage then
106                 details = details .. "1,"
107         else
108                 details = details .. "0,"
109         end
110
111         if spec.pvp then
112                 details = details .. "1,"
113         else
114                 details = details .. "0,"
115         end
116
117         return details .. text
118 end
119
120 --------------------------------------------------------------------------------
121 os.tempfolder = function()
122         if core.setting_get("TMPFolder") then
123                 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
124         end
125
126         local filetocheck = os.tmpname()
127         os.remove(filetocheck)
128
129         local randname = "MTTempModFolder_" .. math.random(0,10000)
130         if DIR_DELIM == "\\" then
131                 local tempfolder = os.getenv("TEMP")
132                 return tempfolder .. filetocheck
133         else
134                 local backstring = filetocheck:reverse()
135                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
136         end
137
138 end
139
140 --------------------------------------------------------------------------------
141 function menu_render_worldlist()
142         local retval = ""
143
144         local current_worldlist = menudata.worldlist:get_list()
145
146         for i,v in ipairs(current_worldlist) do
147                 if retval ~= "" then
148                         retval = retval ..","
149                 end
150
151                 retval = retval .. core.formspec_escape(v.name) ..
152                                         " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
153         end
154
155         return retval
156 end
157
158 --------------------------------------------------------------------------------
159 function menu_handle_key_up_down(fields,textlist,settingname)
160
161         if fields["key_up"] then
162                 local oldidx = core.get_textlist_index(textlist)
163
164                 if oldidx ~= nil and oldidx > 1 then
165                         local newidx = oldidx -1
166                         core.setting_set(settingname,
167                                 menudata.worldlist:get_raw_index(newidx))
168                 end
169                 return true
170         end
171
172         if fields["key_down"] then
173                 local oldidx = core.get_textlist_index(textlist)
174
175                 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
176                         local newidx = oldidx + 1
177                         core.setting_set(settingname,
178                                 menudata.worldlist:get_raw_index(newidx))
179                 end
180                 
181                 return true
182         end
183         
184         return false
185 end
186
187 --------------------------------------------------------------------------------
188 function asyncOnlineFavourites()
189
190         menudata.favorites = {}
191         core.handle_async(
192                 function(param)
193                         return core.get_favorites("online")
194                 end,
195                 nil,
196                 function(result)
197                         if core.setting_getbool("public_serverlist") then
198                                 menudata.favorites = result
199                                 core.event_handler("Refresh")
200                         end
201                 end
202                 )
203 end
204
205 --------------------------------------------------------------------------------
206 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
207         local textlines = core.splittext(text,textlen)
208         
209         local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
210                                                                 .. width .. "," .. height .. ";"
211                                                                 .. tl_name .. ";"
212         
213         for i=1, #textlines, 1 do
214                 textlines[i] = textlines[i]:gsub("\r","")
215                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
216         end
217         
218         retval = retval .. ";0;"
219         
220         if transparency then
221                 retval = retval .. "true"
222         end
223         
224         retval = retval .. "]"
225
226         return retval
227 end