game_theme.lua 4.8 KB

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