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