sfinv_page.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. local S
  2. if minetest.get_modpath("intllib") then
  3. S = intllib.Getter()
  4. else
  5. S = function(s) return s end
  6. end
  7. -- generate the current formspec
  8. local function get_formspec(player, context)
  9. local name = player:get_player_name()
  10. local skin = skins.get_player_skin(player)
  11. local texture = skin:get_texture()
  12. local m_name = skin:get_meta_string("name")
  13. local m_author = skin:get_meta_string("author")
  14. local m_license = skin:get_meta_string("license")
  15. -- overview page
  16. local formspec = "image[0,.75;1,2;"..skin:get_preview().."]"
  17. if texture then
  18. formspec = formspec.."label[6,.5;"..S("Raw texture")..":]"
  19. .."image[6,1;2,1;"..skin:get_texture().."]"
  20. end
  21. if m_name ~= "" then
  22. formspec = formspec.."label[2,.5;"..S("Name")..": "..minetest.formspec_escape(m_name).."]"
  23. end
  24. if m_author ~= "" then
  25. formspec = formspec.."label[2,1;"..S("Author")..": "..minetest.formspec_escape(m_author).."]"
  26. end
  27. if m_license ~= "" then
  28. formspec = formspec.."label[2,1.5;"..S("License")..": "..minetest.formspec_escape(m_license).."]"
  29. end
  30. local page = 1
  31. if context.skins_page then
  32. page = context.skins_page
  33. else
  34. page = skin:get_meta("inv_page") or 1
  35. end
  36. for i = (page-1)*16+1, page*16 do
  37. local skin = context.skins_list[i]
  38. if not skin then
  39. break
  40. end
  41. local index_p = skin:get_meta("inv_page_index")
  42. local x = (index_p-1) % 8
  43. local y
  44. if index_p > 8 then
  45. y = 5.5
  46. else
  47. y = 3.2
  48. end
  49. formspec = formspec.."image_button["..x..","..y..";1,2;"..
  50. skin:get_preview()..";skins_set$"..i..";]"..
  51. "tooltip[skins_set$"..i..";"..minetest.formspec_escape(skin:get_meta_string("name")).."]"
  52. end
  53. if context.total_pages > 1 then
  54. local page_prev = page - 1
  55. local page_next = page + 1
  56. if page_prev < 1 then
  57. page_prev = context.total_pages
  58. end
  59. if page_next > context.total_pages then
  60. page_next = 1
  61. end
  62. local page_list = ""
  63. context.dropdown_values = {}
  64. for pg=1, context.total_pages do
  65. local pagename = S("Page").." "..pg.."/"..context.total_pages
  66. context.dropdown_values[pagename] = pg
  67. if pg > 1 then page_list = page_list.."," end
  68. page_list = page_list..pagename
  69. end
  70. formspec = formspec
  71. .."button[0,8.3;1,.5;skins_page$"..page_prev..";<<]"
  72. .."dropdown[1,8.16;6.5,.5;skins_selpg;"..page_list..";"..page.."]"
  73. .."button[7,8.3;1,.5;skins_page$"..page_next..";>>]"
  74. end
  75. return formspec
  76. end
  77. sfinv.register_page("skins:overview", {
  78. title = "Skins",
  79. get = function(self, player, context)
  80. -- collect skins data
  81. context.skins_list = skins.get_skinlist("player:"..player:get_player_name(), true)
  82. context.total_pages = 1
  83. for i, skin in ipairs(context.skins_list ) do
  84. local page = math.floor((i-1) / 16)+1
  85. skin:set_meta("inv_page", page)
  86. skin:set_meta("inv_page_index", (i-1)%16+1)
  87. context.total_pages = page
  88. end
  89. -- generate first formspec
  90. return sfinv.make_formspec(player, context, get_formspec(player, context))
  91. end,
  92. on_player_receive_fields = function(self, player, context, fields)
  93. for field, _ in pairs(fields) do
  94. local current = string.split(field, "$", 2)
  95. if current[1] == "skins_set" then
  96. skins.set_player_skin(player, context.skins_list[tonumber(current[2])])
  97. sfinv.set_player_inventory_formspec(player)
  98. return
  99. elseif current[1] == "skins_page" then
  100. context.skins_page = tonumber(current[2])
  101. sfinv.set_player_inventory_formspec(player)
  102. return
  103. end
  104. end
  105. if fields.skins_selpg then
  106. context.skins_page = tonumber(context.dropdown_values[fields.skins_selpg])
  107. sfinv.set_player_inventory_formspec(player)
  108. return
  109. end
  110. end
  111. })