unified_inventory_page.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. local dropdown_values = {}
  8. local skins_reftab = {}
  9. local skins_list = skins.get_skinlist()
  10. unified_inventory.register_page("skins", {
  11. get_formspec = function(player)
  12. local name = player:get_player_name()
  13. local skin = skins.get_player_skin(player)
  14. local texture = skin:get_texture()
  15. local m_name = skin:get_meta_string("name")
  16. local m_author = skin:get_meta_string("author")
  17. local m_license = skin:get_meta_string("license")
  18. local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]".."image[0,.75;1,2;"..skin:get_preview().."]"
  19. if texture then
  20. formspec=formspec.."label[6,.5;"..S("Raw texture")..":]"
  21. .."image[6,1;2,1;"..texture.."]"
  22. end
  23. if m_name ~= "" then
  24. formspec = formspec.."label[2,.5;"..S("Name")..": "..minetest.formspec_escape(m_name).."]"
  25. end
  26. if m_author ~= "" then
  27. formspec = formspec.."label[2,1;"..S("Author")..": "..minetest.formspec_escape(m_author).."]"
  28. end
  29. if m_license ~= "" then
  30. formspec = formspec.."label[2,1.5;"..S("License")..": "..minetest.formspec_escape(m_license).."]"
  31. end
  32. local page = skin:get_meta("inv_page") or 1
  33. formspec = formspec .. "button[.75,3;6.5,.5;skins_page$"..page..";"..S("Change").."]"
  34. return {formspec=formspec}
  35. end,
  36. })
  37. unified_inventory.register_button("skins", {
  38. type = "image",
  39. image = "skins_button.png",
  40. })
  41. -- Create all of the skin-picker pages.
  42. local total_pages = 1
  43. for i, skin in ipairs(skins_list) do
  44. local page = math.floor((i-1) / 16)+1
  45. skin:set_meta("inv_page", page)
  46. skin:set_meta("inv_page_index", (i-1)%16+1)
  47. total_pages = page
  48. end
  49. for page=1, total_pages do
  50. local formspec = "background[0.06,0.99;7.92,7.52;ui_misc_form.png]"
  51. for i = (page-1)*16+1, page*16 do
  52. local skin = skins_list[i]
  53. if not skin then
  54. break
  55. end
  56. local index_p = skin:get_meta("inv_page_index")
  57. local x = (index_p-1) % 8
  58. local y
  59. if index_p > 8 then
  60. y = 1.8
  61. else
  62. y = -0.1
  63. end
  64. formspec = (formspec.."image_button["..x..","..y..";1,2;"..
  65. skin:get_preview()..";skins_set$"..i..";]"..
  66. "tooltip[skins_set$"..i..";"..minetest.formspec_escape(skin:get_meta_string("name")).."]")
  67. end
  68. if total_pages > 1 then
  69. local page_prev = page - 1
  70. local page_next = page + 1
  71. if page_prev < 1 then
  72. page_prev = total_pages
  73. end
  74. if page_next > total_pages then
  75. page_next = 1
  76. end
  77. local page_list = ""
  78. dropdown_values = {}
  79. for pg=1, total_pages do
  80. local pagename = S("Page").." "..pg.."/"..total_pages
  81. dropdown_values[pagename] = pg
  82. if pg > 1 then page_list = page_list.."," end
  83. page_list = page_list..pagename
  84. end
  85. formspec = (formspec
  86. .."button[0,3.8;1,.5;skins_page$"..page_prev..";<<]"
  87. .."dropdown[1,3.68;6.5,.5;skins_selpg;"..page_list..";"..page.."]"
  88. .."button[7,3.8;1,.5;skins_page$"..page_next..";>>]")
  89. end
  90. unified_inventory.register_page("skins_page$"..(page), {
  91. get_formspec = function(player)
  92. return {formspec=formspec}
  93. end
  94. })
  95. end
  96. -- click button handlers
  97. minetest.register_on_player_receive_fields(function(player, formname, fields)
  98. if fields.skins then
  99. unified_inventory.set_inventory_formspec(player, "craft")
  100. return
  101. end
  102. for field, _ in pairs(fields) do
  103. local current = string.split(field, "$", 2)
  104. if current[1] == "skins_set" then
  105. skins.set_player_skin(player, skins_list[tonumber(current[2])])
  106. unified_inventory.set_inventory_formspec(player, "skins")
  107. return
  108. elseif current[1] == "skins_page" then
  109. unified_inventory.set_inventory_formspec(player, "skins_page$"..current[2])
  110. return
  111. end
  112. end
  113. if fields.skins_selpg then
  114. local page = dropdown_values[fields.skins_selpg]
  115. unified_inventory.set_inventory_formspec(player, "skins_page$"..(page))
  116. return
  117. end
  118. end)