Module:ItemIcon: Difference between revisions
From Project Nakagami
(Testing definition of both spritesheet height and width in attempt to fix iOS rendering) |
No edit summary |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} --package | local p = {} --package | ||
-- | -- returns HTML, name, defaultLink | ||
function p.AddSpriteInternal(icon, size, additionalCssText) | |||
function p. | |||
local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet | local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet | ||
local SHEET_SIZE_X = 2048 -- how wide the spritesheet image is | local SHEET_SIZE_X = 2048 -- how wide the spritesheet image is | ||
local SHEET_SIZE_Y = 23040 -- how tall the spritesheet image is | local SHEET_SIZE_Y = 23040 -- how tall 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 DataTable = mw.loadData('Module:' | local DataTable = mw.loadData('Module:ItemIconData') | ||
if not DataTable then DataTable = {} end | if not DataTable then DataTable = {} end | ||
local DataFromTable = DataTable.Locations[icon] | |||
if not DataFromTable then DataFromTable = { X = 0, Y = 0, N = 'NONE', L = nil } end | |||
local SpriteX = DataFromTable.X | |||
local SpriteY = DataFromTable.Y | |||
local IconScale = size / SHEET_ICON_SIZE | |||
local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1 | |||
local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1 | |||
local ScaledSheetSizeX = SHEET_SIZE_X * IconScale | |||
local ScaledSheetSizeY = SHEET_SIZE_Y * IconScale | |||
local SpriteHTML = mw.html.create('span'); | |||
SpriteHTML:addClass(SPAN_CLASS) | |||
local StyleItems = {} | |||
StyleItems[#StyleItems + 1] = 'height:' .. size .. 'px' | |||
StyleItems[#StyleItems + 1] = 'width:' .. size .. 'px' | |||
StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSheetSizeY .. 'px' | |||
StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px' | |||
SpriteHTML:cssText(table.concat(StyleItems, ';') .. ';' .. additionalCssText) | |||
return tostring(SpriteHTML), (DataFromTable.N or 'NONE'), DataFromTable.L | |||
end | |||
-- 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 IconName = frame:getArgument('icon'):expand() | local IconName = frame:getArgument('icon'):expand() | ||
if not IconName then IconName = 'none' end | if not IconName then IconName = 'none' end | ||
| Line 25: | Line 53: | ||
if not LinkDest then LinkDest = '' end | if not LinkDest then LinkDest = '' end | ||
local | local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(IconName, IconSize, '') | ||
local | local UsedLink = link or DefaultLink | ||
if UsedLink and UsedLink ~= '' and UsedLink ~= 'none' then | |||
local | if UsedLink:find('//') then -- External Link | ||
return '[' .. UsedLink .. ' ' .. IconHTML .. ']' | |||
else -- Internal Link | |||
return '[[' .. UsedLink .. '|' .. IconHTML .. ']]' | |||
end | |||
else | |||
return IconHTML | |||
end | |||
end | |||
function p.AddSpritePredef(frame) | |||
local FORMATS = { | |||
['inline'] = { Size=32, Link=true, Newline=false, Name=true }, | |||
['itemtable'] = { Size=64, Link=true, Newline=true, Name=true }, | |||
['icon'] = { Size=32, Link=true, Newline=false, Name=false }, | |||
['nolink'] = { Size=32, Link=false, Newline=false, Name=true } | |||
} | |||
TemplateCall = frame:getParent(); | |||
local | local ItemRef = TemplateCall:getArgument('item'):expand() | ||
if not ItemRef then ItemRef = TemplateCall.args[1] end | |||
if not ItemRef then ItemRef = 'none' end | |||
local FormatName = TemplateCall:getArgument('format'):expand() | |||
if not FormatName then FormatName = TemplateCall.args[2] end | |||
if (not FormatName) or (not FORMATS[FormatName]) then FormatName = 'inline' end | |||
local Format = FORMATS[FormatName] | |||
local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(ItemRef, Format.Size, '') | |||
local Link = TemplateCall:getArgument('link'):expand() or DefaultLink or ItemName | |||
local Formatted = IconHTML .. (Format.Newline and '<br />' or '') .. (Format.Name and ((Format.Newline and '' or ' ') .. ItemName) or '') | |||
if | if Format.Link and Link then | ||
if | if Link:find('//') then -- External Link | ||
return '[' .. | return '[' .. Link .. ' ' .. Formatted .. ']' | ||
else -- Internal Link | else -- Internal Link | ||
return '[[' .. | return '[[' .. Link .. '|' .. Formatted .. ']]' | ||
end | end | ||
else | else | ||
return | return Formatted | ||
end | end | ||
end | end | ||
return p | return p | ||
Latest revision as of 05:30, 16 August 2024
Documentation for this module may be created at Module:ItemIcon/doc
local p = {} --package
-- returns HTML, name, defaultLink
function p.AddSpriteInternal(icon, size, additionalCssText)
local SHEET_ICON_SIZE = 64 -- how large icons are in the spritesheet
local SHEET_SIZE_X = 2048 -- how wide the spritesheet image is
local SHEET_SIZE_Y = 23040 -- how tall the spritesheet image is
local SPAN_CLASS = 'item-icon' -- the class attribute on the image span element
local DataTable = mw.loadData('Module:ItemIconData')
if not DataTable then DataTable = {} end
local DataFromTable = DataTable.Locations[icon]
if not DataFromTable then DataFromTable = { X = 0, Y = 0, N = 'NONE', L = nil } end
local SpriteX = DataFromTable.X
local SpriteY = DataFromTable.Y
local IconScale = size / SHEET_ICON_SIZE
local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
local ScaledSheetSizeX = SHEET_SIZE_X * IconScale
local ScaledSheetSizeY = SHEET_SIZE_Y * IconScale
local SpriteHTML = mw.html.create('span');
SpriteHTML:addClass(SPAN_CLASS)
local StyleItems = {}
StyleItems[#StyleItems + 1] = 'height:' .. size .. 'px'
StyleItems[#StyleItems + 1] = 'width:' .. size .. 'px'
StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSheetSizeY .. 'px'
StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px'
SpriteHTML:cssText(table.concat(StyleItems, ';') .. ';' .. additionalCssText)
return tostring(SpriteHTML), (DataFromTable.N or 'NONE'), DataFromTable.L
end
-- 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 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 IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(IconName, IconSize, '')
local UsedLink = link or DefaultLink
if UsedLink and UsedLink ~= '' and UsedLink ~= 'none' then
if UsedLink:find('//') then -- External Link
return '[' .. UsedLink .. ' ' .. IconHTML .. ']'
else -- Internal Link
return '[[' .. UsedLink .. '|' .. IconHTML .. ']]'
end
else
return IconHTML
end
end
function p.AddSpritePredef(frame)
local FORMATS = {
['inline'] = { Size=32, Link=true, Newline=false, Name=true },
['itemtable'] = { Size=64, Link=true, Newline=true, Name=true },
['icon'] = { Size=32, Link=true, Newline=false, Name=false },
['nolink'] = { Size=32, Link=false, Newline=false, Name=true }
}
TemplateCall = frame:getParent();
local ItemRef = TemplateCall:getArgument('item'):expand()
if not ItemRef then ItemRef = TemplateCall.args[1] end
if not ItemRef then ItemRef = 'none' end
local FormatName = TemplateCall:getArgument('format'):expand()
if not FormatName then FormatName = TemplateCall.args[2] end
if (not FormatName) or (not FORMATS[FormatName]) then FormatName = 'inline' end
local Format = FORMATS[FormatName]
local IconHTML, ItemName, DefaultLink = p.AddSpriteInternal(ItemRef, Format.Size, '')
local Link = TemplateCall:getArgument('link'):expand() or DefaultLink or ItemName
local Formatted = IconHTML .. (Format.Newline and '<br />' or '') .. (Format.Name and ((Format.Newline and '' or ' ') .. ItemName) or '')
if Format.Link and Link then
if Link:find('//') then -- External Link
return '[' .. Link .. ' ' .. Formatted .. ']'
else -- Internal Link
return '[[' .. Link .. '|' .. Formatted .. ']]'
end
else
return Formatted
end
end
return p