magic-compass/formspec.lua

76 lines
3.0 KiB
Lua

-- REMINDER: oltre a `itemID` c'era anche un `idx` per far sì che si potevano avere altre righe visibili dopo alcune nascoste (idx = SLOTS_PER_ROW * (i- hidden_rows) + j).
-- Tuttavia, l'errore sembra dovuto a Minetest che non ne vuole sapere di renderizzare le linee seguenti a quelle nascoste, che gli ID del formspec siano continui o meno;
-- questo spiega anche il `last_non_empty_row = i` nella riga invisibile al giocatore, che doveva permetter tale cosa ma che è attualmente inutile
function magic_compass.get_formspec(p_name)
local SLOTS_PER_ROW = 5
local rows = math.floor(table.maxn(magic_compass.items) / SLOTS_PER_ROW - 0.1) + 1
local hidden_rows = 0
local last_non_empty_row = 0
local formspec = {}
-- aggiungo i vari slot (matrice i*j)
for i = 1, rows do
local hidden_items = 0
local is_row_empty = true
for j = 1, SLOTS_PER_ROW do
local x = 0.5 + (j-1)
local y = 0.33 + (i-1)
local itemID = SLOTS_PER_ROW * (i-1) + j
local item = magic_compass.items[itemID]
if item then
if item.privs then
if not item.hide or (item.hide and minetest.check_player_privs(p_name, minetest.string_to_privs(item.privs, ", "))) then
table.insert(formspec, itemID, "item_image_button[" .. x .. "," .. y .. ";1,1;magic_compass:" .. itemID .. ";" .. itemID .. ";]")
if is_row_empty then is_row_empty = false end
else
table.insert(formspec, itemID, "image_button[" .. x .. "," .. y .. ";1,1;;EMPTY;]")
hidden_items = hidden_items + 1
end
else
table.insert(formspec, itemID, "item_image_button[" .. x .. "," .. y .. ";1,1;magic_compass:" .. itemID .. ";" .. itemID .. ";]")
if is_row_empty then is_row_empty = false end
end
else
table.insert(formspec, itemID, "image_button[" .. x .. "," .. y .. ";1,1;;EMPTY;]")
end
end
if not is_row_empty then
last_non_empty_row = i
end
-- se una riga contiene solo oggetti non visibili al giocatore, non la mostro
if hidden_items > 0 and is_row_empty then
local rows_to_hide = i - last_non_empty_row
for k = (SLOTS_PER_ROW * (i - rows_to_hide)) + 1, SLOTS_PER_ROW * i do
formspec[k] = nil
end
hidden_rows = hidden_rows + rows_to_hide
last_non_empty_row = i
end
end
local shown_rows = rows - hidden_rows
table.insert(formspec, 1, "size[6," .. 1.5 + (shown_rows-1) .. "]")
--"style_type[image_button;border=false,alpha=false,bgimg=" .. magic_compass.menu_gui_button_bg .. "]",
--"style_type[item_image_button;border=false,alpha=false,bgimg=" .. magic_compass.menu_gui_button_bg .. "]",
table.insert(formspec, 2, "hypertext[0.25,-0.2;6,1;title;<style font=mono><center>" .. magic_compass.menu_title .. "</center></style>]")
if magic_compass["menu_gui_bg_"..shown_rows.."rows"] then
table.insert(formspec, 3, "background[0,0;6," .. 1.5 + (shown_rows -1) .. ";" .. magic_compass["menu_gui_bg_"..shown_rows.."rows"] .. ";true]")
end
return table.concat(formspec,"")
end