]> git.lizzy.rs Git - minetest.git/blob - builtin/mm_textures.lua
Fix background/overlay/footer/header handling
[minetest.git] / builtin / mm_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 = engine.get_gamepath() .. DIR_DELIM .. ".." ..
24                                                 DIR_DELIM .. "textures" .. DIR_DELIM .. "base" .. 
25                                                 DIR_DELIM .. "pack" .. DIR_DELIM
26         mm_texture.basetexturedir = mm_texture.defaulttexturedir
27         
28         mm_texture.texturepack = engine.setting_get("texture_path")
29         
30         mm_texture.gameid = nil
31 end
32
33 --------------------------------------------------------------------------------
34 function mm_texture.update(tab,gamedetails)
35         if tab ~= "singleplayer" then
36                 mm_texture.reset()
37                 return
38         end
39
40         if gamedetails == nil then
41                 return
42         end
43         
44         mm_texture.update_game(gamedetails)
45 end
46
47 --------------------------------------------------------------------------------
48 function mm_texture.reset()
49         mm_texture.gameid = nil
50         local have_bg      = false
51         local have_overlay = mm_texture.set_generic("overlay")
52         
53         if not have_overlay then
54                 have_bg = mm_texture.set_generic("background")
55         end
56         
57         mm_texture.clear("header")
58         mm_texture.clear("footer")
59         engine.set_clouds(false)
60         
61         mm_texture.set_generic("footer")
62         mm_texture.set_generic("header")
63         
64         if not have_bg and
65                 engine.setting_getbool("enable_clouds") then
66                         engine.set_clouds(true)
67         end
68 end
69
70 --------------------------------------------------------------------------------
71 function mm_texture.update_game(gamedetails)
72         if mm_texture.gameid == gamedetails.id then
73                 return
74         end
75         
76         local have_bg      = false 
77         local have_overlay = mm_texture.set_game("overlay",gamedetails)
78         
79         if not have_overlay then
80                 have_bg = mm_texture.set_game("background",gamedetails)
81         end
82         
83         mm_texture.clear("header")
84         mm_texture.clear("footer")
85         engine.set_clouds(false)
86         
87         if not have_bg and
88                 engine.setting_getbool("enable_clouds") then
89                         engine.set_clouds(true)
90         end
91         
92         mm_texture.gameid = gamedetails.id
93 end
94
95 --------------------------------------------------------------------------------
96 function mm_texture.clear(identifier)
97         engine.set_background(identifier,"")
98 end
99
100 --------------------------------------------------------------------------------
101 function mm_texture.set_generic(identifier)
102         --try texture pack first
103         if mm_texture.texturepack ~= nil then
104                 local path = mm_texture.texturepack .. DIR_DELIM .."menu_" .. 
105                                                                                 identifier .. ".png"
106                 if engine.set_background(identifier,path) then
107                         return true
108                 end
109         end
110         
111         if mm_texture.defaulttexturedir ~= nil then
112                 local path = mm_texture.defaulttexturedir .. DIR_DELIM .."menu_" .. 
113                                                                                 identifier .. ".png"
114                 if engine.set_background(identifier,path) then
115                         return true
116                 end
117         end
118         
119         return false
120 end
121
122 --------------------------------------------------------------------------------
123 function mm_texture.set_game(identifier,gamedetails)
124         
125         if gamedetails == nil then
126                 return false
127         end
128
129         if mm_texture.texturepack ~= nil then
130                 local path = mm_texture.basetexturedir .. 
131                                                 gamedetails.id .. "_menu_" .. identifier .. ".png"
132                                                 
133                 if engine.set_background(identifier,path) then
134                         return true
135                 end
136         end
137         
138         local path = gamedetails.path .. DIR_DELIM .."menu" .. 
139                                                                          DIR_DELIM .. identifier .. ".png"
140         if engine.set_background(identifier,path) then
141                 return true
142         end
143         
144         return false
145 end