Template:ItemIcon: Difference between revisions
From Project Nakagami
No edit summary |
No edit summary Tag: Reverted |
||
| Line 1: | Line 1: | ||
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 DefaulLink | |||
if UsedLink ~= '' 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() | |||
if (not Link) or (Link == '') then Link = DefaultLink end | |||
local Formatted = IconHTML .. (Format.Newline and '<br />' or '') .. (Format.Name and 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 | |||
Revision as of 05:21, 16 August 2024
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 DefaulLink
if UsedLink ~= then
if UsedLink:find('//') then -- External Link
return '[' .. UsedLink .. ' ' .. IconHTML .. ']'
else -- Internal Link
return '' .. 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()
if (not Link) or (Link == ) then Link = DefaultLink end
local Formatted = IconHTML .. (Format.Newline and '
' or ) .. (Format.Name and ItemName or )
if Format.Link and Link then
if Link:find('//') then -- External Link
return '[' .. Link .. ' ' .. Formatted .. ']'
else -- Internal Link
return '' .. Formatted .. ''
end
else
return Formatted
end
end
return p