Module:Sprite

From Feed The Beast Wiki
Jump to: navigation, search
Lua Logo.svg This is the Lua module for Template:Sprite. Below is the documentation for that template.

This template is used to cut out a sprite from a sprite sheet.

Syntax[edit]

{{Sprite|<params>}}

Parameters[edit]

  • image: The sprite sheet image filename.
  • link: The link for the image. (optional)
  • title: The title for the image. (optional)
  • size: The sprite size. (default: 16)
  • sheetsize: The size of the sprite sheet. (default: 256)
  • pos: The position of the selected sprite, counted from left to right and top to bottom, starting at 1. (default: 1)
  • align: The alignment of the image. (default: middle)

Examples[edit]

{{Sprite
|image=GridNumbersCSS.png
|size=16
|sheetsize=64
|pos=5
}}

gives...

GridNumbersCSS.png


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