tab_texturepacks.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. retval = {"None"}
  20. for _,i in ipairs(list) do
  21. if i~="base" then
  22. table.insert(retval, i)
  23. end
  24. end
  25. return retval
  26. end
  27. --------------------------------------------------------------------------------
  28. local function render_texture_pack_list(list)
  29. local retval = ""
  30. for i, v in ipairs(list) do
  31. if retval ~= "" then
  32. retval = retval ..","
  33. end
  34. retval = retval .. core.formspec_escape(v)
  35. end
  36. return retval
  37. end
  38. --------------------------------------------------------------------------------
  39. local function get_formspec(tabview, name, tabdata)
  40. local retval = "label[4,-0.25;".. fgettext("Select texture pack:") .. "]"..
  41. "vertlabel[0,-0.25;".. fgettext("TEXTURE PACKS") .. "]" ..
  42. "textlist[4,0.25;7.5,5.0;TPs;"
  43. local current_texture_path = core.setting_get("texture_path")
  44. local list = filter_texture_pack_list(core.get_dirlist(core.get_texturepath(), true))
  45. local index = tonumber(core.setting_get("mainmenu_last_selected_TP"))
  46. if index == nil then index = 1 end
  47. if current_texture_path == "" then
  48. retval = retval ..
  49. render_texture_pack_list(list) ..
  50. ";" .. index .. "]"
  51. return retval
  52. end
  53. local infofile = current_texture_path ..DIR_DELIM.."info.txt"
  54. local infotext = ""
  55. local f = io.open(infofile, "r")
  56. if f==nil then
  57. infotext = fgettext("No information available")
  58. else
  59. infotext = f:read("*all")
  60. f:close()
  61. end
  62. local screenfile = current_texture_path..DIR_DELIM.."screenshot.png"
  63. local no_screenshot = nil
  64. if not file_exists(screenfile) then
  65. screenfile = nil
  66. no_screenshot = defaulttexturedir .. "no_screenshot.png"
  67. end
  68. return retval ..
  69. render_texture_pack_list(list) ..
  70. ";" .. index .. "]" ..
  71. "image[0.65,0.25;4.0,3.7;"..core.formspec_escape(screenfile or no_screenshot).."]"..
  72. "textarea[1.0,3.25;3.7,1.5;;"..core.formspec_escape(infotext or "")..";]"
  73. end
  74. --------------------------------------------------------------------------------
  75. local function main_button_handler(tabview, fields, name, tabdata)
  76. if fields["TPs"] ~= nil then
  77. local event = core.explode_textlist_event(fields["TPs"])
  78. if event.type == "CHG" or event.type == "DCL" then
  79. local index = core.get_textlist_index("TPs")
  80. core.setting_set("mainmenu_last_selected_TP",
  81. index)
  82. local list = filter_texture_pack_list(core.get_dirlist(core.get_texturepath(), true))
  83. local current_index = core.get_textlist_index("TPs")
  84. if current_index ~= nil and #list >= current_index then
  85. local new_path = core.get_texturepath()..DIR_DELIM..list[current_index]
  86. if list[current_index] == "None" then new_path = "" end
  87. core.setting_set("texture_path", new_path)
  88. end
  89. end
  90. return true
  91. end
  92. return false
  93. end
  94. --------------------------------------------------------------------------------
  95. tab_texturepacks = {
  96. name = "texturepacks",
  97. caption = fgettext("Texturepacks"),
  98. cbf_formspec = get_formspec,
  99. cbf_button_handler = main_button_handler,
  100. on_change = nil
  101. }