]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/common.lua
Right mouse button behaviour for craft/inventory If right mousebutton clicked once...
[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 function render_favorite(spec,render_details)
28         local text = ""
29
30         if spec.name ~= nil then
31                 text = text .. core.formspec_escape(spec.name:trim())
32
33 --              if spec.description ~= nil and
34 --                      core.formspec_escape(spec.description):trim() ~= "" then
35 --                      text = text .. " (" .. core.formspec_escape(spec.description) .. ")"
36 --              end
37         else
38                 if spec.address ~= nil then
39                         text = text .. spec.address:trim()
40
41                         if spec.port ~= nil then
42                                 text = text .. ":" .. spec.port
43                         end
44                 end
45         end
46
47         if not render_details then
48                 return text
49         end
50
51         local details = ""
52         if spec.password == true then
53                 details = details .. "*"
54         else
55                 details = details .. "_"
56         end
57
58         if spec.creative then
59                 details = details .. "C"
60         else
61                 details = details .. "_"
62         end
63
64         if spec.damage then
65                 details = details .. "D"
66         else
67                 details = details .. "_"
68         end
69
70         if spec.pvp then
71                 details = details .. "P"
72         else
73                 details = details .. "_"
74         end
75         details = details .. " "
76
77         local playercount = ""
78
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) .. " "
83         end
84
85         return playercount .. core.formspec_escape(details) ..  text
86 end
87
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)
92         end
93
94         local filetocheck = os.tmpname()
95         os.remove(filetocheck)
96
97         local randname = "MTTempModFolder_" .. math.random(0,10000)
98         if DIR_DELIM == "\\" then
99                 local tempfolder = os.getenv("TEMP")
100                 return tempfolder .. filetocheck
101         else
102                 local backstring = filetocheck:reverse()
103                 return filetocheck:sub(0,filetocheck:len()-backstring:find(DIR_DELIM)+1) ..randname
104         end
105
106 end
107
108 --------------------------------------------------------------------------------
109 function menu_render_worldlist()
110         local retval = ""
111
112         local current_worldlist = menudata.worldlist:get_list()
113
114         for i,v in ipairs(current_worldlist) do
115                 if retval ~= "" then
116                         retval = retval ..","
117                 end
118
119                 retval = retval .. core.formspec_escape(v.name) ..
120                                         " \\[" .. core.formspec_escape(v.gameid) .. "\\]"
121         end
122
123         return retval
124 end
125
126 --------------------------------------------------------------------------------
127 function menu_handle_key_up_down(fields,textlist,settingname)
128
129         if fields["key_up"] then
130                 local oldidx = core.get_textlist_index(textlist)
131
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))
136                 end
137                 return true
138         end
139
140         if fields["key_down"] then
141                 local oldidx = core.get_textlist_index(textlist)
142
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))
147                 end
148                 
149                 return true
150         end
151         
152         return false
153 end
154
155 --------------------------------------------------------------------------------
156 function asyncOnlineFavourites()
157
158         menudata.favorites = {}
159         core.handle_async(
160                 function(param)
161                         return core.get_favorites("online")
162                 end,
163                 nil,
164                 function(result)
165                         menudata.favorites = result
166                         core.event_handler("Refresh")
167                 end
168                 )
169 end
170
171 --------------------------------------------------------------------------------
172 function text2textlist(xpos,ypos,width,height,tl_name,textlen,text,transparency)
173         local textlines = core.splittext(text,textlen)
174         
175         local retval = "textlist[" .. xpos .. "," .. ypos .. ";"
176                                                                 .. width .. "," .. height .. ";"
177                                                                 .. tl_name .. ";"
178         
179         for i=1, #textlines, 1 do
180                 textlines[i] = textlines[i]:gsub("\r","")
181                 retval = retval .. core.formspec_escape(textlines[i]) .. ","
182         end
183         
184         retval = retval .. ";0;"
185         
186         if transparency then
187                 retval = retval .. "true"
188         end
189         
190         retval = retval .. "]"
191
192         return retval
193 end