textures.lua 4.9 KB

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