Difference between revisions of "Module:InventorySlot"

From Gamemode 4
Jump to navigation Jump to search
(Grids)
Line 23: Line 23:
 
end
 
end
  
function UISlot.Call(frame)
+
function UISlot.Call(items, width, height, size)
local w = frame.args.width or 1
+
local w = width or 1
local h = frame.args.height or 1
+
local h = height or 1
 
local grid = {}
 
local grid = {}
 +
local large = size
 +
if size == nil then
 +
size = w == 1 and h == 1
 +
end
 
for i = 1,h do
 
for i = 1,h do
 
grid[i] = {}
 
grid[i] = {}
 
for j = 1,w do
 
for j = 1,w do
grid[i][j] = frame.args[j+(i-1)*w]
+
grid[i][j] = items[j+(i-1)*w]
 
end
 
end
 
end
 
end
return UISlot.Grid(grid)
+
return UISlot.Grid(grid, large)
 
end
 
end
  
function UISlot.Grid(items)
+
function UISlot.Grid(items, large)
 
local root = mw.html.create( "table" ):addClass("invgrid")
 
local root = mw.html.create( "table" ):addClass("invgrid")
 
for i = 1, #items do
 
for i = 1, #items do
 
local row = root:tag("tr"):addClass("invrow")
 
local row = root:tag("tr"):addClass("invrow")
 
for j = 1, #items[i] do
 
for j = 1, #items[i] do
UISlot.Item(items[i][j], false, row)
+
UISlot.Item(items[i][j], large, row)
 
end
 
end
 
end
 
end

Revision as of 15:04, 10 November 2018

-- Based on work by the MC wiki: https://minecraft.gamepedia.com/

local UISlot = {}

local Sprite = require([[Module:Sprite]]):new("inventory")

function UISlot:new(item, large)
	o = {}
	setmetatable(o, {__index = self})
	o:SetItem(item)
	o.__large = large
	return o
end

function UISlot:SetItem(item)
	if item == "" then item = nil end
	self.__sprite = item and Sprite:Item(item) --nil if item = nil else get sprite
	return self
end

function UISlot.Item(item, large, html)
	return UISlot:new(item, large):GenerateHTML(html)
end

function UISlot.Call(items, width, height, size)
	local w = width or 1
	local h = height or 1
	local grid = {}
	local large = size
	if size == nil then
		size = w == 1 and h == 1
	end
	for i = 1,h do
		grid[i] = {}
		for j = 1,w do
			grid[i][j] = items[j+(i-1)*w]
		end
	end
	return UISlot.Grid(grid, large)
end

function UISlot.Grid(items, large)
	local root = mw.html.create( "table" ):addClass("invgrid")
	for i = 1, #items do
		local row = root:tag("tr"):addClass("invrow")
		for j = 1, #items[i] do
			UISlot.Item(items[i][j], large, row)
		end
	end
	return root
end

function UISlot:GenerateHTML (html)
	local slot
	if html then
		slot = html:tag("td")
	else
		slot = mw.html.create("div")
	end

	slot:addClass( "invslot" )

	if self.__sprite then
		self.__sprite:GenerateHTML(slot)
	end
	if self.__large then
		slot:addClass( "invslot-large")
	end
	return slot
end

return UISlot