forked from prestidigitator/minetest-mod-wardrobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formspec.lua
104 lines (81 loc) · 2.92 KB
/
formspec.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
local S = core.get_translator("wardrobe")
wardrobe.formspec_name = "wardrobe_wardrobeSkinForm"
function wardrobe.show_formspec(player, page)
local pname = player:get_player_name()
if not pname or pname == "" then return end
local page_count = math.ceil(wardrobe.skin_count / wardrobe.skins_per_page)
local page_prev = page-1
local page_next = page+1
if page_prev < 1 then
page_prev = page_count
elseif page_next > page_count then
page_next = 1
end
local n = wardrobe.skin_count
if n <= 0 then return end
local nPages = math.ceil(n / wardrobe.skins_per_page)
if not page or page > nPages then page = 1 end
local s = 1 + wardrobe.skins_per_page*(page-1) -- first skin index for page
local e = math.min(s+wardrobe.skins_per_page-1, n) -- last skin index for page
local skins = {}
for i = s, e do
local skin = wardrobe.skins[i]
local skinName = core.formspec_escape(wardrobe.skinNames[skin])
table.insert(skins, {skin, skinName})
end
local formspec
if not wardrobe.previews then
formspec = "size[5,10]"
.. "label[0,0;" .. S("Change Into:") .. "]"
.. "label[1.8,0.5;" .. S("Page @1", tostring(page) .. " / " .. tostring(page_count)) .. "]"
for idx, s in ipairs(skins) do
formspec = formspec
.. "button_exit[0," .. idx ..";5,1;s:" .. s[1] .. ";" .. s[2] .. "]"
end
formspec = formspec
.. "button[1.5,9;1,1;n:p" .. tostring(page_prev) .. ";" .. S("<<") .. "]"
.. "button[2.5,9;1,1;n:p" .. tostring(page_next) .. ";" .. S(">>") .. "]"
else
formspec = "size[12,10]"
.. "label[0,0;" .. S("Change Into:") .. "]"
.. "label[5.3,0.5;" .. S("Page @1", tostring(page) .. " / " .. tostring(page_count)) .. "]"
local border_l = 0
local addon = 1
for idx, s in ipairs(skins) do
local preview = s[1]:split(".png")[1] .. "-preview.png"
if idx % 5 == 0 then
addon = 1
border_l = border_l + 6
end
formspec = formspec
.. "button_exit[" .. border_l .. "," .. addon+.5 ..";5,1;s:" .. s[1] .. ";" .. s[2] .. "]"
if wardrobe.cached_previews[s[1]] then
formspec = formspec
.. "image[" .. border_l+5 .. "," .. addon .. ";1,2;" .. preview .."]"
end
addon = addon + 2
end
formspec = formspec
.. "button[5,9;1,1;n:p" .. tostring(page_prev) .. ";" .. S("<<") .. "]"
.. "button[6,9;1,1;n:p" .. tostring(page_next) .. ";" .. S(">>") .. "]"
end
core.show_formspec(pname, wardrobe.formspec_name, formspec)
end
core.register_on_player_receive_fields(function(player, formName, fields)
if formName ~= wardrobe.formspec_name then return end
local pname = player:get_player_name()
if not pname or pname == "" then return end
for fieldName in pairs(fields) do
if #fieldName > 2 then
local action = string.sub(fieldName, 1, 1)
local value = string.sub(fieldName, 3)
if action == "n" then
wardrobe.show_formspec(player, tonumber(string.sub(value, 2)))
return
elseif action == "s" then
wardrobe.changePlayerSkin(pname, value)
return
end
end
end
end)