Module:ItemIcon: Difference between revisions
From Project Nakagami
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
-- link: The page to link to if the icon is clicked | -- link: The page to link to if the icon is clicked | ||
function p.AddSprite(frame) | function p.AddSprite(frame) | ||
local DEFAULT_ICON_SIZE = | local DEFAULT_ICON_SIZE = 32 -- how big icons should be on the page if not specifically sized | ||
local SHEET_ICON_SIZE = | local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet | ||
local SHEET_SIZE = | local SHEET_SIZE = 2048 -- how wide the spritesheet image is | ||
local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element | local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element | ||
local DATA_MODULE = 'ItemIconData' -- the Module page containing the data table | local DATA_MODULE = 'ItemIconData' -- the Module page containing the data table | ||
Revision as of 07:24, 31 January 2022
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 = 32 -- how big icons should be on the page if not specifically sized
local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet
local SHEET_SIZE = 2048 -- how wide the spritesheet image is
local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element
local DATA_MODULE = 'ItemIconData' -- the Module page containing the data table
local DataTable = mw.loadData('Module:' .. DATA_MODULE)
if not DataTable then DataTable = {} end
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
local LinkDest = frame:getArgument('link'):expand()
if not LinkDest then LinkDest = '' end
local LocationFromTable = DataTable.Locations[IconName]
if not LocationFromTable then LocationFromTable = { X = 0, Y = 0 } end
local SpriteX = LocationFromTable.X
local SpriteY = LocationFromTable.Y
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