Module:InventorySlot: Difference between revisions

Jump to navigation Jump to search
no edit summary
(Should allow arbitrary html be inserted into the slots. Mainly used for specialized items)
No edit summary
(5 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, large)
function UISlot:new(item, large)
Line 13: Line 18:
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)
if string.sub(item,1,1) == '<' then
if string.sub(item or "",1,1) == '<' then
-- If the item is already html just save that
-- If the item is already html just save that
self.__html = item
self.__html = item
Line 20: Line 27:
end
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 item = nil end
self.__sprite = item and Sprite:Item(item) --nil if item = nil else get sprite
if item then
return self
-- 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
end


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


-- Generate the HTML for a grid of items
function UISlot.Call(items, width, height, size)
function UISlot.Call(items, width, height, size)
local w = width or 1
local w = width or 1
Line 43: Line 82:
end
end


-- Convert a 2d list of itemnames to an HTML grid
function UISlot.Grid(items, large)
function UISlot.Grid(items, large)
local root = mw.html.create( "table" ):addClass("invgrid")
local root = mw.html.create( "table" ):addClass("invgrid")
Line 60: Line 100:
else
else
slot = mw.html.create("div")
slot = mw.html.create("div")
end
slot:addClass( "invslot" )
if self.__large then
slot:addClass( "invslot-large")
end
end


if self.__html then
if self.__html then
return slot:wikitext(self.__html)
slot:wikitext(self.__html)
end
end
slot:addClass( "invslot" )


if self.__sprite then
if self.__sprite then
local spriteDiv = self.__sprite:GenerateHTML(slot)
self.__sprite:GenerateHTML(slot)
end
if self.__large then
slot:addClass( "invslot-large")
end
end
return slot
return slot
Bureaucrats, emailconfirmed, Interface administrators, staff, Administrators, translation-admin, translator, Widget editors
154

edits

Navigation menu