Module:ItemIcon
From Project Nakagami
Documentation for this module may be created at Module:ItemIcon/doc
local p = {} --package
-- Allowed Arguments:
-- icon: The name of the icon to show, defined in the image index.
-- size: The size, in pixels, to use for the icon on the page.
-- link: The page to link to if the icon is clicked
function p.AddSprite(frame)
local DEFAULT_ICON_SIZE = 16 -- how big icons should be on the page if not specifically sized
local SHEET_ICON_SIZE = 16 -- how large icons are in the spritesheet
local SHEET_SIZE = 256 -- how wide the spritesheet image is
local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element
-- use mw.loadData(MODULE) to grab the big table
local IconName = frame:getArgument('icon'):expand()
if not IconName then IconName = 'NONE' end
local IconSize = tonumber(frame:getArgument('size'):expand())
if not IconSize then IconSize = DEFAULT_ICON_SIZE end
mw.logObject(IconSize)
local LinkDest = frame:getArgument('link'):expand()
if not LinkDest then LinkDest = '' end
local SpriteX = 1 -- TODO GET
local SpriteY = 0 -- TODO GET
local IconScale = IconSize / SHEET_ICON_SIZE
local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
local ScaledSheetSize = SHEET_SIZE * IconScale
local SpriteHTML = mw.html.create('span');
SpriteHTML:addClass(SPAN_CLASS)
local StyleItems = {}
StyleItems[#StyleItems + 1] = 'height:' .. IconSize .. 'px'
StyleItems[#StyleItems + 1] = 'width:' .. IconSize .. 'px'
StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSize .. 'px auto'
StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px'
SpriteHTML:cssText(table.concat(StyleItems, ';'))
if LinkDest ~= '' then
if LinkDest:find('//') then -- External Link
return '[' .. LinkDest .. ' ' .. tostring(SpriteHTML) .. ']'
else -- Internal Link
return '[[' .. LinkDest .. '|' .. tostring(SpriteHTML) .. ']]'
end
else
return tostring(SpriteHTML)
end
end
return p