Jump to content

Module:Sprite

From SankoQuest Wiki

This is the documentation page for Module:Sprite.

Renders an item icon from the Sanko item sprite atlas as a CSS background-positioned <span>. Atlas coordinates come from the data module Module:Sprite/Item (loaded via mw.loadData).

Functions

item

The only public function. Called as {{#invoke:Sprite|item|name=...|size=...|link=...}}.

Parameter Meaning Default
name (or first positional) Item name; must match a key in Module:Sprite/Item's items table exactly (trimmed, case-sensitive).
size Rendered icon size in pixels; the atlas is scaled by size / cell (cell is 32). 32
link Page to wikilink the icon to. Empty = no link. none

Output is a span.sanko-sprite with role="img", aria-label, and data-minetip-title set to the item name, plus computed background-position/background-size. When link is set the span is wrapped in span.sanko-sprite-link containing a wikilink.

Used by

Prefer calling these templates from articles rather than #invoke directly, e.g. {{ItemSprite|Air Shard|size=32}}.

Notes and sharp edges

  • The lookup is exact-match on the trimmed name. An unknown name does not error; it renders a span.sanko-sprite-missing placeholder showing ? (minetip "Unknown item" when the name is blank). If an icon shows ?, check spelling/capitalization against Module:Sprite/Item.
  • The module only reads frame.args, not the parent frame's arguments — templates must pass name/size/link through explicitly, as the two templates above do.
  • The module sets background position/size only; the atlas image itself is attached by the .sanko-sprite site CSS, not by this module. The url/image fields in the data module are not read here.
  • Offsets are math.floor-ed after scaling, so sizes that are not multiples of the 32 px cell can be off by up to a pixel.

local p = {}
local itemData = mw.loadData('Module:Sprite/Item')

local function trim(value)
    if value == nil then
        return ''
    end
    return mw.text.trim(tostring(value))
end

local function spriteFor(name, size, link)
    local key = trim(name)
    local entry = itemData.items[key]
    local cell = tonumber(itemData.cell) or 32
    local width = tonumber(itemData.width) or cell
    local height = tonumber(itemData.height) or cell
    local renderedSize = tonumber(size) or cell
    local scale = renderedSize / cell

    if not entry then
        return tostring(mw.html.create('span')
            :addClass('sanko-sprite')
            :addClass('sanko-sprite-missing')
            :attr('data-minetip-title', key ~= '' and key or 'Unknown item')
            :css('width', renderedSize .. 'px')
            :css('height', renderedSize .. 'px')
            :wikitext('?'))
    end

    local x = math.floor((tonumber(entry.x) or 0) * scale)
    local y = math.floor((tonumber(entry.y) or 0) * scale)
    local bgWidth = math.floor(width * scale)
    local bgHeight = math.floor(height * scale)

    local node = mw.html.create('span')
        :addClass('sanko-sprite')
        :attr('role', 'img')
        :attr('aria-label', key)
        :attr('data-minetip-title', key)
        :css('background-position', '-' .. x .. 'px -' .. y .. 'px')
        :css('background-size', bgWidth .. 'px ' .. bgHeight .. 'px')
        :css('width', renderedSize .. 'px')
        :css('height', renderedSize .. 'px')

    if link and link ~= '' then
        return tostring(mw.html.create('span')
            :addClass('sanko-sprite-link')
            :wikitext('[[' .. link .. '|' .. tostring(node) .. ']]'))
    end

    return tostring(node)
end

function p.item(frame)
    local args = frame.args
    return spriteFor(args.name or args[1], args.size, args.link)
end

return p