game_theme.lua 5.4 KB

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