13 Articles
Project Nakagami

Module:ItemIcon: Difference between revisions

(Testing definition of both spritesheet height and width in attempt to fix iOS rendering)
m (Fixing typo)
Line 43: Line 43:
     StyleItems[#StyleItems + 1] = 'height:' .. IconSize .. 'px'
     StyleItems[#StyleItems + 1] = 'height:' .. IconSize .. 'px'
     StyleItems[#StyleItems + 1] = 'width:' .. IconSize .. 'px'
     StyleItems[#StyleItems + 1] = 'width:' .. IconSize .. 'px'
     StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSHeetSizeY .. 'px'
     StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSheetSizeY .. 'px'
     StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px'
     StyleItems[#StyleItems + 1] = 'background-position:' .. ImageXOffset .. 'px ' .. ImageYOffset .. 'px'



Revision as of 04:31, 24 August 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_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 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 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:' .. IconSize .. 'px'
    StyleItems[#StyleItems + 1] = 'width:' .. IconSize .. 'px'
    StyleItems[#StyleItems + 1] = 'background-size:' .. ScaledSheetSizeX .. 'px ' .. ScaledSheetSizeY .. 'px'
    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