Use get_texturepath() instead of get_gamepath()/../textures
[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_texturepath() .. DIR_DELIM .. "base" .. 
24                                                 DIR_DELIM .. "pack" .. DIR_DELIM
25         mm_texture.basetexturedir = mm_texture.defaulttexturedir
26         
27         mm_texture.texturepack = engine.setting_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         engine.set_clouds(false)
59         
60         mm_texture.set_generic("footer")
61         mm_texture.set_generic("header")
62         
63         if not have_bg and
64                 engine.setting_getbool("enable_clouds") then
65                         engine.set_clouds(true)
66         end
67 end
68
69 --------------------------------------------------------------------------------
70 function mm_texture.update_game(gamedetails)
71         if mm_texture.gameid == gamedetails.id then
72                 return
73         end
74         
75         local have_bg      = false 
76         local have_overlay = mm_texture.set_game("overlay",gamedetails)
77         
78         if not have_overlay then
79                 have_bg = mm_texture.set_game("background",gamedetails)
80         end
81         
82         mm_texture.clear("header")
83         mm_texture.clear("footer")
84         engine.set_clouds(false)
85         
86         if not have_bg and
87                 engine.setting_getbool("enable_clouds") then
88                         engine.set_clouds(true)
89         end
90         
91         mm_texture.set_game("footer",gamedetails)
92         mm_texture.set_game("header",gamedetails)
93         
94         mm_texture.gameid = gamedetails.id
95 end
96
97 --------------------------------------------------------------------------------
98 function mm_texture.clear(identifier)
99         engine.set_background(identifier,"")
100 end
101
102 --------------------------------------------------------------------------------
103 function mm_texture.set_generic(identifier)
104         --try texture pack first
105         if mm_texture.texturepack ~= nil then
106                 local path = mm_texture.texturepack .. DIR_DELIM .."menu_" .. 
107                                                                                 identifier .. ".png"
108                 if engine.set_background(identifier,path) then
109                         return true
110                 end
111         end
112         
113         if mm_texture.defaulttexturedir ~= nil then
114                 local path = mm_texture.defaulttexturedir .. DIR_DELIM .."menu_" .. 
115                                                                                 identifier .. ".png"
116                 if engine.set_background(identifier,path) then
117                         return true
118                 end
119         end
120         
121         return false
122 end
123
124 --------------------------------------------------------------------------------
125 function mm_texture.set_game(identifier,gamedetails)
126         
127         if gamedetails == nil then
128                 return false
129         end
130
131         if mm_texture.texturepack ~= nil then
132                 local path = mm_texture.basetexturedir .. 
133                                                 gamedetails.id .. "_menu_" .. identifier .. ".png"
134                                                 
135                 if engine.set_background(identifier,path) then
136                         return true
137                 end
138         end
139         
140         local path = gamedetails.path .. DIR_DELIM .."menu" .. 
141                                                                          DIR_DELIM .. identifier .. ".png"
142         if engine.set_background(identifier,path) then
143                 return true
144         end
145         
146         return false
147 end