tab_texturepacks.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. --Minetest
  2. --Copyright (C) 2014 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. local function filter_texture_pack_list(list)
  19. local retval = {}
  20. for _, item in ipairs(list) do
  21. if item ~= "base" then
  22. retval[#retval + 1] = item
  23. end
  24. end
  25. table.sort(retval)
  26. table.insert(retval, 1, fgettext("None"))
  27. return retval
  28. end
  29. --------------------------------------------------------------------------------
  30. local function render_texture_pack_list(list)
  31. local retval = ""
  32. for i, v in ipairs(list) do
  33. if v:sub(1, 1) ~= "." then
  34. if retval ~= "" then
  35. retval = retval .. ","
  36. end
  37. retval = retval .. core.formspec_escape(v)
  38. end
  39. end
  40. return retval
  41. end
  42. --------------------------------------------------------------------------------
  43. local function get_formspec(tabview, name, tabdata)
  44. local retval = "label[4,-0.25;" .. fgettext("Select texture pack:") .. "]" ..
  45. "textlist[4,0.25;7.5,5.0;TPs;"
  46. local current_texture_path = core.settings:get("texture_path")
  47. local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
  48. local index = tonumber(core.settings:get("mainmenu_last_selected_TP"))
  49. if not index then index = 1 end
  50. if current_texture_path == "" then
  51. retval = retval ..
  52. render_texture_pack_list(list) ..
  53. ";" .. index .. "]" ..
  54. "textarea[0.6,2.85;3.7,1.5;;" ..
  55. fgettext("Default textures will be used.") ..
  56. ";]"
  57. return retval
  58. end
  59. local infofile = current_texture_path .. DIR_DELIM .. "description.txt"
  60. -- This adds backwards compatibility for old texture pack description files named
  61. -- "info.txt", and should be removed once all such texture packs have been updated
  62. if not file_exists(infofile) then
  63. infofile = current_texture_path .. DIR_DELIM .. "info.txt"
  64. if file_exists(infofile) then
  65. core.log("deprecated", "info.txt is deprecated. description.txt should be used instead.")
  66. end
  67. end
  68. local infotext = ""
  69. local f = io.open(infofile, "r")
  70. if not f then
  71. infotext = fgettext("No information available")
  72. else
  73. infotext = f:read("*all")
  74. f:close()
  75. end
  76. local screenfile = current_texture_path .. DIR_DELIM .. "screenshot.png"
  77. local no_screenshot
  78. if not file_exists(screenfile) then
  79. screenfile = nil
  80. no_screenshot = defaulttexturedir .. "no_screenshot.png"
  81. end
  82. return retval ..
  83. render_texture_pack_list(list) ..
  84. ";" .. index .. "]" ..
  85. "image[0.25,0.25;4.05,2.7;" .. core.formspec_escape(screenfile or no_screenshot) .. "]" ..
  86. "textarea[0.6,2.85;3.7,1.5;;" .. core.formspec_escape(infotext or "") .. ";]"
  87. end
  88. --------------------------------------------------------------------------------
  89. local function main_button_handler(tabview, fields, name, tabdata)
  90. if fields["TPs"] then
  91. local event = core.explode_textlist_event(fields["TPs"])
  92. if event.type == "CHG" or event.type == "DCL" then
  93. local index = core.get_textlist_index("TPs")
  94. core.settings:set("mainmenu_last_selected_TP", index)
  95. local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
  96. local current_index = core.get_textlist_index("TPs")
  97. if current_index and #list >= current_index then
  98. local new_path = core.get_texturepath() .. DIR_DELIM .. list[current_index]
  99. if list[current_index] == fgettext("None") then
  100. new_path = ""
  101. end
  102. core.settings:set("texture_path", new_path)
  103. end
  104. end
  105. return true
  106. end
  107. return false
  108. end
  109. --------------------------------------------------------------------------------
  110. return {
  111. name = "texturepacks",
  112. caption = fgettext("Texturepacks"),
  113. cbf_formspec = get_formspec,
  114. cbf_button_handler = main_button_handler,
  115. on_change = nil
  116. }