13 Articles
Project Nakagami

Module:ItemIcon: Difference between revisions

m (Fixing typo)
No edit summary
Line 1: Line 1:
local p = {} --package
local p = {} --package


-- Allowed Arguments:
function p.AddSpriteInternal(icon, size, link, additionalCssText)
-- 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 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
Line 16: Line 12:
     if not DataTable then DataTable = {} end
     if not DataTable then DataTable = {} end


    local IconName = frame:getArgument('icon'):expand()
     local LocationFromTable = DataTable.Locations[icon]
    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
     if not LocationFromTable then LocationFromTable = { X = 0, Y = 0 } end


Line 31: Line 18:
     local SpriteY = LocationFromTable.Y
     local SpriteY = LocationFromTable.Y


     local IconScale = IconSize / SHEET_ICON_SIZE
     local IconScale = size / SHEET_ICON_SIZE
     local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
     local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
     local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
     local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
Line 41: Line 28:
      
      
     local StyleItems = {}
     local StyleItems = {}
     StyleItems[#StyleItems + 1] = 'height:' .. IconSize .. 'px'
     StyleItems[#StyleItems + 1] = 'height:' .. size .. 'px'
     StyleItems[#StyleItems + 1] = 'width:' .. IconSize .. 'px'
     StyleItems[#StyleItems + 1] = 'width:' .. size .. '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'


     SpriteHTML:cssText(table.concat(StyleItems, ';'))
     SpriteHTML:cssText(table.concat(StyleItems, ';') .. ';' .. additionalCssText)


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


return p
return p

Revision as of 06:58, 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 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 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 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