Difference between revisions of "Module:InventorySlot"

From Gamemode 4
Jump to navigation Jump to search
(Off by one)
(Grids)
Line 5: Line 5:
 
local Sprite = require([[Module:Sprite]]):new("inventory")
 
local Sprite = require([[Module:Sprite]]):new("inventory")
  
function UISlot:new(item)
+
function UISlot:new(item, large)
 
o = {}
 
o = {}
 
setmetatable(o, {__index = self})
 
setmetatable(o, {__index = self})
 
o:SetItem(item)
 
o:SetItem(item)
 +
o.__large = large
 
return o
 
return o
 
end
 
end
  
 
function UISlot:SetItem(item)
 
function UISlot:SetItem(item)
 +
if item == "" then item = nil end
 
self.__sprite = item and Sprite:Item(item) --nil if item = nil else get sprite
 
self.__sprite = item and Sprite:Item(item) --nil if item = nil else get sprite
 
return self
 
return self
 
end
 
end
  
function UISlot.Item(item)
+
function UISlot.Item(item, large, html)
return UISlot:new(item):GenerateHTML()
+
return UISlot:new(item, large):GenerateHTML(html)
 
end
 
end
  
 
function UISlot.Call(frame)
 
function UISlot.Call(frame)
return UISlot.Item(frame.args[1] or frame.args.item)
+
local w = frame.args.width or 1
 +
local h = frame.args.height or 1
 +
local grid = {}
 +
for i = 1,h do
 +
grid[i] = {}
 +
for j = 1,w do
 +
grid[i][j] = frame.args[j+(i-1)*w]
 +
end
 +
end
 +
return UISlot.Grid(grid)
 
end
 
end
  
function UISlot:GenerateHTML ()
+
function UISlot.Grid(items)
local slot = mw.html.create( 'div' ):addClass( 'invslot-item' )
+
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], false, 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
 
if self.__sprite then
 
self.__sprite:GenerateHTML(slot)
 
self.__sprite:GenerateHTML(slot)
 +
end
 +
if self.__large then
 +
slot:addClass( "invslot-large")
 
end
 
end
 
return slot
 
return slot

Revision as of 16:27, 8 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(frame)
	local w = frame.args.width or 1
	local h = frame.args.height or 1
	local grid = {}
	for i = 1,h do
		grid[i] = {}
		for j = 1,w do
			grid[i][j] = frame.args[j+(i-1)*w]
		end
	end
	return UISlot.Grid(grid)
end

function UISlot.Grid(items)
	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], false, 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