Module:Sprite

local p = {}

function p.base(setting) 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 name = setting('name') local tint = setting('tint') local styles = {"display: inline-block", "background-blend-mode: multiply", "position:absolute", "height:100%", "width:100%"} local bg_size = sheetsize * scale .. 'px auto' styles[#styles + 1] = 'background-size:' .. bg_size if pos then local x = pos % tiles * width * scale local y = math.floor( pos / tiles ) * height * scale local bg_pos = '-' .. x .. 'px -' .. y .. 'px' styles[#styles + 1] = 'background-position:' .. bg_pos if tint then styles[#styles + 1] = 'background-color:'.. tint styles[#styles + 1] = 'mask-size:' .. bg_size styles[#styles + 1] = '-webkit-mask-size:' .. bg_size styles[#styles + 1] = 'mask-position:' .. bg_pos styles[#styles + 1] = '-webkit-mask-position:' .. bg_pos end end local sprite = mw.html.create('span') sprite:cssText(table.concat(styles, ';')):addClass('sprite'):addClass(name .. '-sprite') return sprite 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 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 idData = args.iddata if not idData then local name = args.name or data.settings.name local id = mw.text.trim( tostring( args[1] or '' ) ) idData = data.ids[id] or data.ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )] end if idData then args.pos = idData.pos if not args.tint then args.tint = idData.tint end end local wrapper = mw.html.create('span') local sprite = p.base(setting) wrapper:node(sprite) if idData and idData.overlay then args.pos = idData.overlay.pos args.tint = idData.overlay.tint local overlay = p.base(setting) wrapper:node(overlay) end local height = setting('height') or setting('size') local width = setting('width') or setting('size') local scale = setting('scale') wrapper:cssText('display: inline-block;position:relative;height:' .. height * scale .. 'px;width:' .. width * scale .. 'px') return wrapper end

return p