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.
17 --------------------------------------------------------------------------------
19 ---------------------------------------------------------------------------------
22 --------------------------------------------------------------------------------
23 -- Menu helper functions
24 --------------------------------------------------------------------------------
26 --------------------------------------------------------------------------------
27 local function render_client_count(n)
37 --------------------------------------------------------------------------------
38 function render_favorite(spec,render_details)
41 if spec.name ~= nil then
42 text = text .. core.formspec_escape(spec.name:trim())
44 -- if spec.description ~= nil and
45 -- core.formspec_escape(spec.description):trim() ~= "" then
46 -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
49 if spec.address ~= nil then
50 text = text .. spec.address:trim()
52 if spec.port ~= nil then
53 text = text .. ":" .. spec.port
58 if not render_details then
64 if spec.clients ~= nil and spec.clients_max ~= nil then
65 local clients_color = ''
66 local clients_percent = 100 * spec.clients / spec.clients_max
68 -- Choose a color depending on how many clients are connected
69 -- (relatively to clients_max)
70 if spec.clients == 0 then
71 clients_color = '' -- 0 players: default/white
72 elseif spec.clients == spec.clients_max then
73 clients_color = '#dd5b5b' -- full server: red (darker)
74 elseif clients_percent <= 60 then
75 clients_color = '#a1e587' -- 0-60%: green
76 elseif clients_percent <= 90 then
77 clients_color = '#ffdc97' -- 60-90%: yellow
79 clients_color = '#ffba97' -- 90-100%: orange
83 clients_color .. ',' ..
84 render_client_count(spec.clients) .. ',' ..
86 render_client_count(spec.clients_max) .. ','
88 details = details .. ',?,/,?,'
92 details = details .. "1,"
94 details = details .. "0,"
98 details = details .. "1,"
100 details = details .. "0,"
104 details = details .. "1,"
106 details = details .. "0,"
109 return details .. text
112 --------------------------------------------------------------------------------
113 os.tempfolder = function()
114 if core.setting_get("TMPFolder") then
115 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
118 local filetocheck = os.tmpname()
119 os.remove(filetocheck)
121 local randname = "MTTempModFolder_" .. math.random(0,10000)
122 if DIR_DELIM == "\\" then
123 local tempfolder = os.getenv("TEMP")
124 return tempfolder .. filetocheck
126 local backstring = filetocheck:reverse()
127 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
132 --------------------------------------------------------------------------------
133 function menu_render_worldlist()
136 local current_worldlist = menudata.worldlist:get_list()
138 for i,v in ipairs(current_worldlist) do
140 retval = retval ..","
143 retval = retval .. core.formspec_escape(v.name) ..
144 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
150 --------------------------------------------------------------------------------
151 function menu_handle_key_up_down(fields,textlist,settingname)
153 if fields["key_up"] then
154 local oldidx = core.get_textlist_index(textlist)
156 if oldidx ~= nil and oldidx > 1 then
157 local newidx = oldidx -1
158 core.setting_set(settingname,
159 menudata.worldlist:get_raw_index(newidx))
164 if fields["key_down"] then
165 local oldidx = core.get_textlist_index(textlist)
167 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
168 local newidx = oldidx + 1
169 core.setting_set(settingname,
170 menudata.worldlist:get_raw_index(newidx))
179 --------------------------------------------------------------------------------
180 function asyncOnlineFavourites()
182 menudata.favorites = {}
185 return core.get_favorites("online")
189 menudata.favorites = result
190 core.event_handler("Refresh")
195 --------------------------------------------------------------------------------
196 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
197 local textlines = core.splittext(text,textlen)
199 local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
200 .. width .. "," .. height .. ";"
203 for i=1, #textlines, 1 do
204 textlines[i] = textlines[i]:gsub("\r","")
205 retval = retval .. core.formspec_escape(textlines[i]) .. ","
208 retval = retval .. ";0;"
211 retval = retval .. "true"
214 retval = retval .. "]"