Module:Sprite: Difference between revisions

No edit summary
Add settings file to prevent settings having to be duplicated; smarter default styling to reduce inline styling when possible.
Line 5: Line 5:
args = require( 'Module:ProcessArgs' ).merge( true )
args = require( 'Module:ProcessArgs' ).merge( true )
end
end
local name = args.name
local image = args.image
-- List of sprite names which have their default styling set in a stylesheet
local scale = args.scale or 1
local sheetWidth = ( args.sheetsize or 256 ) * scale
local size = ( args.size or 16 ) * scale
local defaultPos = args.defaultpos or 1
local pos = math.abs( args.pos or defaultPos ) - 1
local link = args.link or ''
local align = args.align or 'text-top'
local tiles = sheetWidth / size
local class = args.class or ''
local left = pos % tiles * size
local top = math.floor( pos / tiles ) * size
local text = args.text or ''
local title = args.title or ''
local css = args.css
local styles = {}
local imgClasses = {
local imgClasses = {
Block = 1,
Block = 1,
Line 29: Line 14:
}
}
-- Default settings
local default = {
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
link = '',
align = 'text-top',
class = '',
text = '',
title = ''
}
local defaultStyle = default
if args.settings then
for k, v in pairs( mw.loadData( 'Module:' .. args.settings ) ) do
default[k] = v
if imgClasses[name] then
defaultStyle[k] = v
end
end
end
local name = args.name or default.name
local image = args.image or default.image or name .. 'Sprite.png'
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 tiles = sheetWidth / size
local left = pos % tiles * size
local top = math.floor( pos / tiles ) * size
local styles = {}
if imgClasses[name] then
if imgClasses[name] then
class = mw.ustring.lower( name ) .. '-sprite ' .. class
class = mw.ustring.lower( name ) .. '-sprite ' .. class
else
else
table.insert( styles, 'background-image:{{FileUrl|' .. ( image or name .. 'Sprite.png' ) .. '}}' )
table.insert( styles, 'background-image:{{FileUrl|' .. image .. '}}' )
end
end
if left > 0 or top > 0 then
if left > 0 or top > 0 then
table.insert( styles, 'background-position:-' .. left .. 'px -' .. top .. 'px' )
table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
end
end
if scale ~= 1 then
if not autoScale and scale ~= defaultStyle.scale then
table.insert( styles, 'background-size:' .. sheetWidth .. 'px auto' )
table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
end
end
if size ~= 16 then
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
table.insert( styles, 'height:' .. size .. 'px;width:' .. size .. 'px' )
table.insert( styles, 'height:' .. size * scale .. 'px;width:' .. size * scale .. 'px' )
end
end
if align ~= 'text-top' then
if align ~= defaultStyle.align then
table.insert( styles, 'vertical-align:' .. align )
table.insert( styles, 'vertical-align:' .. align )
end
if title ~= '' then
title = 'title="' .. title .. '"'
end
end
if css then
if css then
table.insert( styles, css )
table.insert( styles, css )
end
if title ~= '' then
title = ' title="' .. title .. '"'
end
end
Line 63: Line 90:
if text ~= '' then
if text ~= '' then
text = '<span class="sprite-text nowrap" ' .. title .. '>' .. text .. '</span>'
text = '<span class="sprite-text nowrap"' .. title .. '>' .. text .. '</span>'
end
end
Line 89: Line 116:
args.pos = args[1]
args.pos = args[1]
else
else
local ids = mw.loadData( 'Module:Sprite/' .. args.name )
local settings = args.settings
if settings then
settings = mw.loadData( 'Module:' .. settings )
end
local name = args.name or settings.name
local ids = mw.loadData( 'Module:' .. ( args.ids or settings.ids or 'Sprite/' .. name ) )
local id = mw.text.trim( args[1] or '' )
local id = mw.text.trim( args[1] or '' )
local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
Line 121: Line 154:


function p.doc( f )
function p.doc( f )
local args = f.args
local args = require( 'Module:ProcessArgs' ).norm( f.args )
local idTable = mw.title.new( 'Module:Sprite/' .. args.name ):getContent()
local idTable = mw.title.new( 'Module:Sprite/' .. args.name ):getContent()
idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
Line 189: Line 222:
end
end
return f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-details=\'{"name":"' .. args.name .. '","size":' .. ( args.size or 16 ) .. '}\'>' .. table.concat( list ) .. '</div>'
local out = table.concat( list )
if not args.refresh then
out = f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-details=\'{"name":"' .. args.name .. '","size":' .. ( args.size or 16 ) .. '}\'>' .. out .. '</div>'
end
return out
end
end
return p
return p