Module:Sprite

From Modded Wiki
Revision as of 04:04, 18 March 2014 by minecraft>Majr (See if unused code changes memory usage)
Jump to navigation Jump to search

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

local p = {}
function p.base( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge( true )
	end
	
	-- Default settings
	local default = {
		scale = 1,
		sheetsize = 256,
		size = 16,
		pos = 1,
		link = '',
		align = 'text-top',
		class = '',
		text = '',
		title = ''
	}
	
	local defaultStyle = mw.clone( default )
	if args.settings then
		local settings = mw.loadData( 'Module:' .. args.settings )
		for k, v in pairs( settings ) do
			default[k] = v
			if settings.stylesheet then
				defaultStyle[k] = v
			end
		end
	end
	
	local name = args.name or default.name
	local scale = args.scale or default.scale
	local autoScale = args.autoscale or default.autoscale
	local sheetWidth = args.sheetsize or default.sheetsize
	local size = args.size or default.size
	local pos = math.abs( args.pos or default.pos ) - 1
	local link = args.link or default.link
	local align = args.align or default.align
	local class = args.class or default.class
	local text = args.text or default.text
	local title = args.title or default.title
	local css = args.css or default.css
	local className = args.classname or default.classname

	local tiles = sheetWidth / size
	local left = pos % tiles * size
	local top = math.floor( pos / tiles ) * size
	
	local styles = {}
	if args.stylesheet or default.stylesheet then
		class = ( className or mw.ustring.lower( name ) .. '-sprite ' ) .. class
	else
		table.insert( styles, 'background-image:{{FileUrl|' .. ( args.image or default.image or name .. 'Sprite.png' ) .. '}}' )
	end
	if left > 0 or top > 0 then
		table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
	end
	if not autoScale and scale ~= defaultStyle.scale then
		table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
	end
	if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
		table.insert( styles, 'height:' .. size * scale .. 'px;width:' .. size * scale .. 'px' )
	end
	if align ~= defaultStyle.align then
		table.insert( styles, 'vertical-align:' .. align )
	end
	if css then
		table.insert( styles, css )
	end
	if title ~= '' then
		title = ' title="' .. title .. '"'
	end
	
	local sprite = table.concat( {
		'<span',
			'class="sprite ' .. class .. '"',
			'style="' .. table.concat( styles, ';' ) .. '"',
			title,
		'><br></span>'
	}, ' ' )
	sprite = sprite:gsub( '%s+([">])', '%1' )
	
	if text ~= '' then
		text = '<span class="sprite-text nowrap"' .. title .. '>' .. text .. '</span>'
	end
	
	if link ~= '' then
		if link:find( '//' ) then
			-- External link
			return '[' .. link .. ' ' .. sprite .. text .. ']'
		else
			-- Internal link
			return '[[' .. link .. '|' .. sprite .. text .. ']]'
		end
	else
		return sprite .. text
	end
end

function p.sprite( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end
	
	local category = ''
	if tonumber( args[1] ) then
		args.pos = args[1]
	else
		local default = {}
		if args.settings then
			default = mw.loadData( 'Module:' .. args.settings )
		end
		
		local name = args.name or default.name
		local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or 'Sprite/' .. name ) )
		local id = mw.text.trim( args[1] or '' )
		local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
		if not pos then
			category = '[[Category:Pages with missing sprites]]'
		end
		args.pos = pos
	end
	
	return p.base( args ) .. category
end

function p.link( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	end
	
	local link = args[1]
	if args[1] and not args.id then
		link = args[1]:match( '^(.-)%+' ) or args[1]
	end
	local text = args.text or args[2] or link
	
	args[1] = args.id or args[1]
	args.link = link
	args.text = text
	
	return p.sprite( args )
end
return p