game_theme.lua 5.6 KB

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