Module:Sprite

local p = {}

function p.base(f) local args = f	if f == mw.getCurrentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) else f = mw.getCurrentFrame end local default = { scale = 1, sheetsize = 256, size = 16, pos = 1 }	local data = args.data and mw.loadData( 'Module:' .. args.data ) or {} local settings = data.settings if settings then for k, v in pairs( settings ) do			default[k] = v		end end local setting = function( arg ) return args[arg] or default[arg] end local height = setting('height') or setting('size') local width = setting('width') or setting('size') local sheetsize = setting('sheetsize') local tiles = sheetsize/width local pos = setting('pos') local scale = setting('scale') local autoscale = setting('autoscale') local rootstyles = {"positon: relative"} local spritestyles = {"position: absolute"} if pos then local x = pos % tiles * width * scale local y = math.floor( pos / tiles ) * height * scale spritestyles[#spritestyles+1] = "left: -" .. x .. "px" spritestyles[#spritestyles+1] = "top: -" .. y .. "px" end rootstyles[#rootstyles+1] = "height: " .. height .. "px" rootstyles[#rootstyles+1] = "width: " .. width .. "px" local root = mw.html.create('div') local sprite = mw.html.create('div') sprite:wikitext(""); sprite:cssText(table.concat(spritestyles, ';')) root:cssText(table.concat(rootstyles, ';')) root:node(sprite) return root end

function p.sprite(f) local args = f	if f == mw.getCurrentFrame then args = require( 'Module:ProcessArgs' ).merge( true ) else f = mw.getCurrentFrame end local sprite = p.base(f) return sprite end

function p.test(frame) local root = mw.html.create('div') local sprite = mw.html.create( 'div' ) sprite:wikitext("") sprite:cssText(table.concat({"left:-" .. frame.args.size*frame.args.x .. "px", "top:-" .. frame.args.size*frame.args.y .. "px", "position: absolute"}, ';')) root:cssText(table.concat({"width: " .. frame.args.size .. "px", "height: " .. frame.args.size .. "px", "overflow: hidden", "position: relative"}, ';')) root:node(sprite) return tostring(root) end

return p