Module:InventorySlot: Difference between revisions

From Official Gamemode 4 Wiki
Jump to navigation Jump to search
(Off by one)
No edit summary
(11 intermediate revisions by the same user not shown)
Line 3: Line 3:
local UISlot = {}
local UISlot = {}


local Sprite = require([[Module:Sprite]]):new("inventory")
local sprite = require([[Module:Sprite]])
local Sprites = {
minecraft = sprite:new("inventory"),
gm4 = sprite:new("gm4"),
effect = sprite:new("effect")
}


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


-- Sets the item of this UISlot. If 'raw html' (=first char == '<') then
-- this will return said html wrapped instead of doing a item lookup
function UISlot:SetItem(item)
function UISlot:SetItem(item)
self.__sprite = item and Sprite:Item(item) --nil if item = nil else get sprite
if string.sub(item or "",1,1) == '<' then
-- If the item is already html just save that
self.__html = item
return self
end
 
self.__sprite = UISlot.Decode(item)
 
 
 
return self
return self
end
end


function UISlot.Item(item)
function UISlot.Decode(item)
return UISlot:new(item):GenerateHTML()
if not(item) then return nil end
item = mw.text.trim(item)
if item == "" then item = nil end
if item then
-- split at ':'
local t={}
for str in string.gmatch(item, "([^:]+)") do
table.insert(t, str)
end
if #t == 1 then
return Sprites.minecraft:Item(item)
elseif #t == 2 then
local spr = Sprites[mw.text.trim(t[1])]
if spr then
return spr:Item(mw.text.trim(t[2]))
else
error("Unknown namespace: " .. mw.text.trim(t[1]))
end
else
error("Got unexpected amount of ':', namely:" .. (#t - 1))
end
 
else
return nil
end
end
 
-- Append the HTML for an item
function UISlot.Item(item, large, html)
return UISlot:new(item, large):GenerateHTML(html)
end
 
-- Generate the HTML for a grid of items
function UISlot.Call(items, width, height, size)
local w = width or 1
local h = height or 1
local grid = {}
local large = size
for i = 1,h do
grid[i] = {}
for j = 1,w do
grid[i][j] = mw.text.trim(items[j+(i-1)*w] or "")
end
end
return UISlot.Grid(grid, large)
end
end


function UISlot.Call(frame)
-- Convert a 2d list of itemnames to an HTML grid
return UISlot.Item(frame.args[1] or frame.args.item)
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
end


function UISlot:GenerateHTML ()
function UISlot:GenerateHTML (html)
local slot = mw.html.create( 'div' ):addClass( 'invslot-item' )
local slot
if html then
slot = html:tag("td")
else
slot = mw.html.create("div")
end
slot:addClass( "invslot" )
 
if self.__large then
slot:addClass( "invslot-large")
end
 
if self.__html then
slot:wikitext(self.__html)
end
 
if self.__sprite then
if self.__sprite then
self.__sprite:GenerateHTML(slot)
self.__sprite:GenerateHTML(slot)

Revision as of 20:57, 30 June 2020

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

local UISlot = {}

local sprite = require([[Module:Sprite]])
local Sprites = {
	minecraft = sprite:new("inventory"),
	gm4 = sprite:new("gm4"),
	effect = sprite:new("effect")
}

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

-- Sets the item of this UISlot. If 'raw html' (=first char == '<') then
--			this will return said html wrapped instead of doing a item lookup
function UISlot:SetItem(item)
	if string.sub(item or "",1,1) == '<' then
		-- If the item is already html just save that
		self.__html = item
		return self
	end

	self.__sprite = UISlot.Decode(item)



	return self
end

function UISlot.Decode(item)
	if not(item) then return nil end
	item = mw.text.trim(item)
	if item == "" then item = nil end
	if item then
		-- split at ':'
		local t={}
		for str in string.gmatch(item, "([^:]+)") do
			table.insert(t, str)
		end
		if #t == 1 then
			return Sprites.minecraft:Item(item)
		elseif #t == 2 then
			local spr = Sprites[mw.text.trim(t[1])]
			if spr then
				return spr:Item(mw.text.trim(t[2]))
			else
				error("Unknown namespace: " .. mw.text.trim(t[1]))
			end
		else
			error("Got unexpected amount of ':', namely:" .. (#t - 1))
		end

	else
		return nil
	end
end

-- Append the HTML for an item
function UISlot.Item(item, large, html)
	return UISlot:new(item, large):GenerateHTML(html)
end

-- Generate the HTML for a grid of items
function UISlot.Call(items, width, height, size)
	local w = width or 1
	local h = height or 1
	local grid = {}
	local large = size
	for i = 1,h do
		grid[i] = {}
		for j = 1,w do
			grid[i][j] = mw.text.trim(items[j+(i-1)*w] or "")
		end
	end
	return UISlot.Grid(grid, large)
end

-- Convert a 2d list of itemnames to an HTML grid
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.__large then
		slot:addClass( "invslot-large")
	end

	if self.__html then
		slot:wikitext(self.__html)
	end

	if self.__sprite then
		self.__sprite:GenerateHTML(slot)
	end
	return slot
end

return UISlot