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 function render_favorite(spec,render_details)
30 if spec.name ~= nil then
31 text = text .. core.formspec_escape(spec.name:trim())
33 -- if spec.description ~= nil and
34 -- core.formspec_escape(spec.description):trim() ~= "" then
35 -- text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
38 if spec.address ~= nil then
39 text = text .. spec.address:trim()
41 if spec.port ~= nil then
42 text = text .. ":" .. spec.port
47 if not render_details then
52 if spec.password == true then
53 details = details .. "*"
55 details = details .. "_"
59 details = details .. "C"
61 details = details .. "_"
65 details = details .. "D"
67 details = details .. "_"
71 details = details .. "P"
73 details = details .. "_"
75 details = details .. " "
77 local playercount = ""
79 if spec.clients ~= nil and
80 spec.clients_max ~= nil then
81 playercount = string.format("%03d",spec.clients) .. "/" ..
82 string.format("%03d",spec.clients_max) .. " "
85 return playercount .. core.formspec_escape(details) .. text
88 --------------------------------------------------------------------------------
89 os.tempfolder = function()
90 if core.setting_get("TMPFolder") then
91 return core.setting_get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000)
94 local filetocheck = os.tmpname()
95 os.remove(filetocheck)
97 local randname = "MTTempModFolder_" .. math.random(0,10000)
98 if DIR_DELIM == "\\" then
99 local tempfolder = os.getenv("TEMP")
100 return tempfolder .. filetocheck
102 local backstring = filetocheck:reverse()
103 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
108 --------------------------------------------------------------------------------
109 function menu_render_worldlist()
112 local current_worldlist = menudata.worldlist:get_list()
114 for i,v in ipairs(current_worldlist) do
116 retval = retval ..","
119 retval = retval .. core.formspec_escape(v.name) ..
120 " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
126 --------------------------------------------------------------------------------
127 function menu_handle_key_up_down(fields,textlist,settingname)
129 if fields["key_up"] then
130 local oldidx = core.get_textlist_index(textlist)
132 if oldidx ~= nil and oldidx > 1 then
133 local newidx = oldidx -1
134 core.setting_set(settingname,
135 menudata.worldlist:get_raw_index(newidx))
140 if fields["key_down"] then
141 local oldidx = core.get_textlist_index(textlist)
143 if oldidx ~= nil and oldidx < menudata.worldlist:size() then
144 local newidx = oldidx + 1
145 core.setting_set(settingname,
146 menudata.worldlist:get_raw_index(newidx))
155 --------------------------------------------------------------------------------
156 function asyncOnlineFavourites()
158 menudata.favorites = {}
161 return core.get_favorites("online")
165 menudata.favorites = result
166 core.event_handler("Refresh")
171 --------------------------------------------------------------------------------
172 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
173 local textlines = core.splittext(text,textlen)
175 local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
176 .. width .. "," .. height .. ";"
179 for i=1, #textlines, 1 do
180 textlines[i] = textlines[i]:gsub("\r","")
181 retval = retval .. core.formspec_escape(textlines[i]) .. ","
184 retval = retval .. ";0;"
187 retval = retval .. "true"
190 retval = retval .. "]"