Allow multiple 'wherein' nodes in oredef
[oweals/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.set_game("footer",gamedetails)
93         mm_texture.set_game("header",gamedetails)
94         
95         mm_texture.gameid = gamedetails.id
96 end
97
98 --------------------------------------------------------------------------------
99 function mm_texture.clear(identifier)
100         engine.set_background(identifier,"")
101 end
102
103 --------------------------------------------------------------------------------
104 function mm_texture.set_generic(identifier)
105         --try texture pack first
106         if mm_texture.texturepack ~= nil then
107                 local path = mm_texture.texturepack .. DIR_DELIM .."menu_" .. 
108                                                                                 identifier .. ".png"
109                 if engine.set_background(identifier,path) then
110                         return true
111                 end
112         end
113         
114         if mm_texture.defaulttexturedir ~= nil then
115                 local path = mm_texture.defaulttexturedir .. DIR_DELIM .."menu_" .. 
116                                                                                 identifier .. ".png"
117                 if engine.set_background(identifier,path) then
118                         return true
119                 end
120         end
121         
122         return false
123 end
124
125 --------------------------------------------------------------------------------
126 function mm_texture.set_game(identifier,gamedetails)
127         
128         if gamedetails == nil then
129                 return false
130         end
131
132         if mm_texture.texturepack ~= nil then
133                 local path = mm_texture.basetexturedir .. 
134                                                 gamedetails.id .. "_menu_" .. identifier .. ".png"
135                                                 
136                 if engine.set_background(identifier,path) then
137                         return true
138                 end
139         end
140         
141         local path = gamedetails.path .. DIR_DELIM .."menu" .. 
142                                                                          DIR_DELIM .. identifier .. ".png"
143         if engine.set_background(identifier,path) then
144                 return true
145         end
146         
147         return false
148 end