tab_texturepacks.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.setting_get("texture_path")
  47. local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
  48. local index = tonumber(core.setting_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. return retval
  55. end
  56. local infofile = current_texture_path .. DIR_DELIM .. "description.txt"
  57. -- This adds backwards compatibility for old texture pack description files named
  58. -- "info.txt", and should be removed once all such texture packs have been updated
  59. if not file_exists(infofile) then
  60. infofile = current_texture_path .. DIR_DELIM .. "info.txt"
  61. if file_exists(infofile) then
  62. core.log("deprecated", "info.txt is deprecated. description.txt should be used instead.")
  63. end
  64. end
  65. local infotext = ""
  66. local f = io.open(infofile, "r")
  67. if not f then
  68. infotext = fgettext("No information available")
  69. else
  70. infotext = f:read("*all")
  71. f:close()
  72. end
  73. local screenfile = current_texture_path .. DIR_DELIM .. "screenshot.png"
  74. local no_screenshot
  75. if not file_exists(screenfile) then
  76. screenfile = nil
  77. no_screenshot = defaulttexturedir .. "no_screenshot.png"
  78. end
  79. return retval ..
  80. render_texture_pack_list(list) ..
  81. ";" .. index .. "]" ..
  82. "image[0.25,0.25;4.05,2.7;" .. core.formspec_escape(screenfile or no_screenshot) .. "]" ..
  83. "textarea[0.6,2.85;3.7,1.5;;" .. core.formspec_escape(infotext or "") .. ";]"
  84. end
  85. --------------------------------------------------------------------------------
  86. local function main_button_handler(tabview, fields, name, tabdata)
  87. if fields["TPs"] then
  88. local event = core.explode_textlist_event(fields["TPs"])
  89. if event.type == "CHG" or event.type == "DCL" then
  90. local index = core.get_textlist_index("TPs")
  91. core.setting_set("mainmenu_last_selected_TP", index)
  92. local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
  93. local current_index = core.get_textlist_index("TPs")
  94. if current_index and #list >= current_index then
  95. local new_path = core.get_texturepath() .. DIR_DELIM .. list[current_index]
  96. if list[current_index] == fgettext("None") then
  97. new_path = ""
  98. end
  99. core.setting_set("texture_path", new_path)
  100. end
  101. end
  102. return true
  103. end
  104. return false
  105. end
  106. --------------------------------------------------------------------------------
  107. return {
  108. name = "texturepacks",
  109. caption = fgettext("Texturepacks"),
  110. cbf_formspec = get_formspec,
  111. cbf_button_handler = main_button_handler,
  112. on_change = nil
  113. }