]> git.lizzy.rs Git - minetest.git/blob - builtin/mainmenu/textures.lua
Rename 'subgame' to 'game'
[minetest.git] / builtin / mainmenu / textures.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 mm_texture = {}
20
21 --------------------------------------------------------------------------------
22 function mm_texture.init()
23         mm_texture.defaulttexturedir = core.get_texturepath() .. DIR_DELIM .. "base" ..
24                                                 DIR_DELIM .. "pack" .. DIR_DELIM
25         mm_texture.basetexturedir = mm_texture.defaulttexturedir
26         
27         mm_texture.texturepack = core.settings:get("texture_path")
28         
29         mm_texture.gameid = nil
30 end
31
32 --------------------------------------------------------------------------------
33 function mm_texture.update(tab,gamedetails)
34         if tab ~= "singleplayer" then
35                 mm_texture.reset()
36                 return
37         end
38
39         if gamedetails == nil then
40                 return
41         end
42         
43         mm_texture.update_game(gamedetails)
44 end
45
46 --------------------------------------------------------------------------------
47 function mm_texture.reset()
48         mm_texture.gameid = nil
49         local have_bg      = false
50         local have_overlay = mm_texture.set_generic("overlay")
51         
52         if not have_overlay then
53                 have_bg = mm_texture.set_generic("background")
54         end
55         
56         mm_texture.clear("header")
57         mm_texture.clear("footer")
58         core.set_clouds(false)
59         
60         mm_texture.set_generic("footer")
61         mm_texture.set_generic("header")
62         
63         if not have_bg then
64                 if core.settings:get_bool("menu_clouds") then
65                         core.set_clouds(true)
66                 else
67                         mm_texture.set_dirt_bg()
68                 end
69         end
70 end
71
72 --------------------------------------------------------------------------------
73 function mm_texture.update_game(gamedetails)
74         if mm_texture.gameid == gamedetails.id then
75                 return
76         end
77         
78         local have_bg      = false
79         local have_overlay = mm_texture.set_game("overlay",gamedetails)
80         
81         if not have_overlay then
82                 have_bg = mm_texture.set_game("background",gamedetails)
83         end
84         
85         mm_texture.clear("header")
86         mm_texture.clear("footer")
87         core.set_clouds(false)
88         
89         if not have_bg then
90                 
91                 if core.settings:get_bool("menu_clouds") then
92                         core.set_clouds(true)
93                 else
94                         mm_texture.set_dirt_bg()
95                 end
96         end
97         
98         mm_texture.set_game("footer",gamedetails)
99         mm_texture.set_game("header",gamedetails)
100         
101         mm_texture.gameid = gamedetails.id
102 end
103
104 --------------------------------------------------------------------------------
105 function mm_texture.clear(identifier)
106         core.set_background(identifier,"")
107 end
108
109 --------------------------------------------------------------------------------
110 function mm_texture.set_generic(identifier)
111         --try texture pack first
112         if mm_texture.texturepack ~= nil then
113                 local path = mm_texture.texturepack .. DIR_DELIM .."menu_" ..
114                                                                                 identifier .. ".png"
115                 if core.set_background(identifier,path) then
116                         return true
117                 end
118         end
119         
120         if mm_texture.defaulttexturedir ~= nil then
121                 local path = mm_texture.defaulttexturedir .. DIR_DELIM .."menu_" ..
122                                                                                 identifier .. ".png"
123                 if core.set_background(identifier,path) then
124                         return true
125                 end
126         end
127         
128         return false
129 end
130
131 --------------------------------------------------------------------------------
132 function mm_texture.set_game(identifier, gamedetails)
133         
134         if gamedetails == nil then
135                 return false
136         end
137
138         if mm_texture.texturepack ~= nil then
139                 local path = mm_texture.texturepack .. DIR_DELIM ..
140                         gamedetails.id .. "_menu_" .. identifier .. ".png"
141                 if core.set_background(identifier, path) then
142                         return true
143                 end
144         end
145         
146         -- Find out how many randomized textures the game provides
147         local n = 0
148         local filename
149         local menu_files = core.get_dir_list(gamedetails.path .. DIR_DELIM .. "menu", false)
150         for i = 1, #menu_files do
151                 filename = identifier .. "." .. i .. ".png"
152                 if table.indexof(menu_files, filename) == -1 then
153                         n = i - 1
154                         break
155                 end
156         end
157         -- Select random texture, 0 means standard texture
158         n = math.random(0, n)
159         if n == 0 then
160                 filename = identifier .. ".png"
161         else
162                 filename = identifier .. "." .. n .. ".png"
163         end
164
165         local path = gamedetails.path .. DIR_DELIM .. "menu" ..
166                 DIR_DELIM .. filename
167         if core.set_background(identifier, path) then
168                 return true
169         end
170         
171         return false
172 end
173
174 function mm_texture.set_dirt_bg()
175         if mm_texture.texturepack ~= nil then
176                 local path = mm_texture.texturepack .. DIR_DELIM .."default_dirt.png"
177                 if core.set_background("background", path, true, 128) then
178                         return true
179                 end
180         end
181         
182         -- Use universal fallback texture in textures/base/pack
183         local minimalpath = defaulttexturedir .. "menu_bg.png"
184         core.set_background("background", minimalpath, true, 128)
185 end