Module:Sprite

From Leaguepedia | League of Legends Esports Wiki
Revision as of 04:21, 15 February 2018 by RheingoldRiver (talk | contribs) (Created page with "local p = {} function p.base( f ) local args = f if f == mw.getCurrentFrame() then args = require( 'Module:ProcessArgs' ).merge( true ) end -- Default settings local...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

To edit the documentation or categories for this module, click here.

Available Args[edit]

  • |1= whatever you want to display
  • |nolink=
  • |notext=
  • |noimage=
  • |nocat= to suppress error categories
  • |size= DONT PUT PX
  • |backwards= swaps text & image from normal (so image is on the right)
  • |display= - this is different from MCW version of this module, since on esports wikis we do |2= is a style
  • |nosize= - don't put the size inline, the parent class will have it (useful to decrease template inclusion size)

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 = {
		sheetsize = 256,
		size = 16,
		thumbnails = {},
		pos = 1,
		link = '',
		align = 'text-top',
		class = '',
		text = '',
		textstyle = '',
		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 sheetWidth = args.sheetsize or default.sheetsize
	local size = tonumber( args.size or default.size )
	local origSize = args.origsize or default.origsize or default.size or size
	local width = tonumber( args.width or default.width or size )
	local origWidth = args.origwidth or default.origwidth or default.width or origSize or width
	local height = tonumber( args.height or default.height or size )
	local origHeight = args.origheight or default.origheight or default.height or origSize or height
	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 textStyle = args.textstyle or default.textstyle
	local title = args.title or default.title
	local css = args.css or default.css
	
	if ( args.width or default.width ) and ( args.height or default.height ) then
		if width ~= origWidth then
			height = origHeight / origWidth * width
		elseif height ~= origHeight then
			width = origWidth / origHeight * height
		end
	end
	
	local tiles = sheetWidth / ( default.width or default.size )
	local left = math.floor( pos % tiles * width + 0.5 )
	local top = math.floor( math.floor( pos / tiles ) * height + 0.5 )
	width = math.floor( width + 0.5 )
	height = math.floor( height + 0.5 )
	
	local classes = { 'sprite' }
	local styles = {}
	if args.stylesheet or default.stylesheet then
		table.insert( classes, mw.ustring.lower( name:gsub( ' ', '-' ) ) .. '-sprite' )
	else
		table.insert( styles, 'background-image:{{FileUrl|' .. ( args.image or default.image or name .. 'Sprite.png' ) .. '|' .. ( args.sheetsize or default.sheetsize ) .. '}}' )
	end
	if left > 0 or top > 0 then
		table.insert( styles, 'background-position:-' .. left .. 'px -' .. top .. 'px' )
	end
	if default.thumbnails[width] then
		table.insert( classes, 'sprite-size-' .. width )
	elseif width ~= origWidth or height ~= origHeight then
		local closestSize
		for thumbSize in pairs( default.thumbnails ) do
			if thumbSize - width > 0 and ( not closestSize or thumbSize < closestSize ) then
				closestSize = thumbSize
			end
		end
		
		if closestSize then
			table.insert( classes, 'sprite-size-' .. closestSize )
		end
		table.insert( styles, 'background-size:' .. width * tiles .. 'px auto' )
		table.insert( styles, 'width:' .. width .. 'px' )
		table.insert( styles, 'height:' .. height .. 'px' )
	end
	if align ~= defaultStyle.align then
		table.insert( styles, 'vertical-align:' .. align )
	end
	if css then
		table.insert( styles, css )
	end
	
	if class ~= '' then
		table.insert( classes, class )
	end
	if title ~= '' then
		title = ' title="' .. title .. '"'
	end
	
	local sprite = table.concat( {
		'<span',
			'class="' .. table.concat( classes, ' ' ) .. '"',
			'style="' .. table.concat( styles, ';' ) .. '"',
			title,
		'><br></span>'
	}, ' ' )
	sprite = sprite:gsub( '%s+([">])', '%1' )
	
	if text ~= '' then
		if textStyle ~= '' then
			textStyle = ' style="' .. textStyle .. '"'
		end
		text = '<span class="sprite-text"' .. textStyle .. 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
	local newArgs
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
		newArgs = args
	else
		newArgs = mw.clone( args )
	end
	
	local category = ''
	if tonumber( args[1] ) then
		newArgs.pos = args[1]
	else
		local default = {}
		if args.settings then
			default = mw.loadData( 'Module:' .. args.settings )
		end
		
		local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids ) )
		local id = mw.text.trim( args[1] or '' )
		local pos = ids[id] or ids[mw.ustring.lower( id )]
		if not pos then
			category = '[[Category:Pages with missing sprites]]'
		end
		newArgs.pos = pos
	end
	
	if args.nolink then
		newArgs.link = nil
	else
		newArgs.link = args.link or args[1]
	end
	if args.notext then
		newArgs.text = nil
	else
		newArgs.text = args[2] or args[1]
	end
	
	return p.base( newArgs ) .. category
end
return p