Module:Sprite: Difference between revisions

Jump to navigation Jump to search
ftb>RZR0
m (Bugfix)
(No difference)

Revision as of 17:03, 3 August 2015

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

local p = {}

local g = require("Module:Common")

function p.calcSpriteSheetPos(index, spriteSize, sheetSize)
	local iconsOnRow = sheetSize / spriteSize
	return {
		top = -math.floor(index / iconsOnRow) * spriteSize,
		left = -math.mod(index, iconsOnRow) * spriteSize
	}
end

function p.makeSprite(image, index, spriteSize, sheetSize, link, title, align)
	local output = "<span style=\"position: relative; height: "
	output = output .. spriteSize .. "px; width: "
	output = output .. spriteSize .. "px; overflow: hidden; display: inline-block; vertical-align: "
	output = output .. (align or "middle") .. ";\"><span style=\"position: absolute; height: "
	output = output .. sheetSize .. "px; width: " .. sheetSize .. "px;"
	local spritePos = p.calcSpriteSheetPos(index, spriteSize, sheetSize)
	if spritePos.top ~= 0 then
		output = output .. "top: " .. spritePos.top .. "px;"
	end
	output = output .. "left: " .. spritePos.left .. "px;\">[[File:" .. image
	if g.isGiven(link) then
		output = output .. "|link=" .. link
	end
	if g.isGiven(title) then
		output = output .. "|" .. title
	end
	output = output .. "]]</span></span>"
	return output
end

function p.main(frame)
	local frame, args = g.getFrameAndArgs(frame)
	
	if not (g.isGiven(args.pos) and tonumber(args.pos)) then
		args.pos = 1
	end
	
	if not (g.isGiven(args.size) and tonumber(args.size)) then
		args.size = 16
	end
	
	if not (g.isGiven(args.sheetsize) and tonumber(args.sheetsize)) then
		args.sheetsize = 256
	end
	
	return p.makeSprite(args.image, tonumber(args.pos) - 1, args.size,
		args.sheetsize, args.link, args.title, args.align)
end

return p