13 Articles
Project Nakagami

Module:ItemIcon: Difference between revisions

No edit summary
No edit summary
Line 2: Line 2:


function p.AddSpriteInternal(icon, size, link, additionalCssText)
function p.AddSpriteInternal(icon, size, link, additionalCssText)
    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_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
Line 51: Line 50:
-- 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 = 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

Revision as of 07:00, 9 August 2024

Documentation for this module may be created at Module:ItemIcon/doc

local p = {} --package

function p.AddSpriteInternal(icon, size, link, 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 DATA_MODULE = 'ItemIconData' -- the Module page containing the data table

    local DataTable = mw.loadData('Module:' .. DATA_MODULE)
    if not DataTable then DataTable = {} end

    local LocationFromTable = DataTable.Locations[icon]
    if not LocationFromTable then LocationFromTable = { X = 0, Y = 0 } end

    local SpriteX = LocationFromTable.X
    local SpriteY = LocationFromTable.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)

    if link ~= '' then
        if link:find('//') then -- External Link
            return '[' .. link .. ' ' .. tostring(SpriteHTML) .. ']'
        else -- Internal Link
            return '[[' .. link .. '|' .. tostring(SpriteHTML) .. ']]'
        end
    else
        return tostring(SpriteHTML)
    end
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

    return p.AddSpriteInternal(IconName, IconSize, LinkDest, '')
end

return p