Module:Sprite: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 12: | Line 12: | ||
local defaultPos = args.defaultpos or 1 | local defaultPos = args.defaultpos or 1 | ||
local pos = math.abs( args.pos or defaultPos ) - 1 | local pos = math.abs( args.pos or defaultPos ) - 1 | ||
local link = args.link | local link = args.link | ||
local align = args.align or 'text-top' | local align = args.align or 'text-top' | ||
local tiles = sheetWidth / size | local tiles = sheetWidth / size | ||
Line 19: | Line 19: | ||
local top = math.floor( pos / tiles ) * size | local top = math.floor( pos / tiles ) * size | ||
local text = args.text or '' | local text = args.text or '' | ||
local css = args.css | local css = args.css | ||
local styles = {} | local styles = {} | ||
local imgClasses = { | local imgClasses = { | ||
Line 45: | Line 45: | ||
table.insert( styles, 'vertical-align:' .. align ) | table.insert( styles, 'vertical-align:' .. align ) | ||
end | end | ||
if css | if css then | ||
table.insert( styles, css ) | table.insert( styles, css ) | ||
end | end | ||
Line 58: | Line 58: | ||
if text ~= '' then | if text ~= '' then | ||
text = '<span class="nowrap" style="padding-left:0. | text = '<span class="nowrap" style="padding-left:0.33em">' .. text .. '</span>' | ||
end | end | ||
if link | if link then | ||
if link:find( '//' ) then | if link:find( '//' ) then | ||
-- External link | -- External link | ||
Line 85: | Line 85: | ||
else | else | ||
local ids = mw.loadData( 'Module:Sprite/' .. args.name ) | local ids = mw.loadData( 'Module:Sprite/' .. args.name ) | ||
local | local id = mw.text.trim( args[1] or '' ) | ||
local pos = ids[ | local pos = ids[id] or ids[id:lower():gsub( '%s', '-' )] | ||
if not pos then | if not pos then | ||
category = '[[Category:Pages with missing sprites]]' | category = '[[Category:Pages with missing sprites]]' | ||
Line 114: | Line 114: | ||
function p.doc( f ) | function p.doc( f ) | ||
local args = f.args | local args = 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( '}$', '' ) | ||
local html = {} | local html = {} | ||
local | local ids = {} | ||
local | local posKeys = {} | ||
local section = '' | local section = '' | ||
for line in mw.text.gsplit( idTable, ',' ) do | for line in mw.text.gsplit( idTable, ',' ) do | ||
line = mw.text.trim( line ) | line = mw.text.trim( line ) | ||
id = line:match( '^%[[\'"](.+)[\'"]%]' ) or line:match( '^%w+' ) or '' | |||
pos = line:match( '=%s*(%d+)%s*,?$' ) or '' | |||
section = line:match( '^%-%-%s*(.+)%s*%-%-$' ) or section | section = line:match( '^%-%-%s*(.+)%s*%-%-$' ) or section | ||
if | if id ~= '' and pos ~= '' then | ||
if | if ids[pos] then | ||
if type( | if type( ids[pos].id ) == 'table' then | ||
table.insert( | table.insert( ids[pos].id, id ) | ||
else | else | ||
ids[pos].id = { ids[pos].id, id } | |||
end | end | ||
else | else | ||
ids[pos] = { id = id, section = section } | |||
table.insert( | table.insert( posKeys, pos ) | ||
end | end | ||
end | end | ||
Line 146: | Line 145: | ||
local listFoot = '</ul>' | local listFoot = '</ul>' | ||
local lastSection = '' | local lastSection = '' | ||
for i, | for i, pos in ipairs( posKeys ) do | ||
local | local id = ids[pos].id | ||
local newSection = mw.text.trim( | local newSection = mw.text.trim( ids[pos].section ) | ||
if newSection ~= lastSection or i == 1 then | if newSection ~= lastSection or i == 1 then | ||
Line 161: | Line 160: | ||
table.insert( list, listHead ) | table.insert( list, listHead ) | ||
end | end | ||
table.insert( list, '<li><table><tr><td data- | table.insert( list, '<li><table><tr><td data-pos="' .. pos .. '">' ) | ||
if type( | if type( id ) == 'table' then | ||
for i, | for i, id2 in ipairs( id ) do | ||
if i == 1 then | if i == 1 then | ||
args[1] = | args[1] = id2 | ||
table.insert( list, p.sprite( args ) .. '</td><td><div class="sprite- | table.insert( list, p.sprite( args ) .. '</td><td><div class="sprite-id"><code>' .. id2 .. '</code></div>' ) | ||
else | else | ||
table.insert( list, '<div class="sprite- | table.insert( list, '<div class="sprite-id"><code>' .. id2 .. '</code></div>' ) | ||
end | end | ||
end | end | ||
else | else | ||
args[1] = | args[1] = id | ||
table.insert( list, p.sprite( args ) .. '</td><td><div class="sprite- | table.insert( list, p.sprite( args ) .. '</td><td><div class="sprite-id"><code>' .. id .. '</code></div>' ) | ||
end | end | ||
table.insert( list, '</td></tr></table></li>' ) | table.insert( list, '</td></tr></table></li>' ) | ||
if i == # | if i == #posKeys then | ||
table.insert( list, listFoot ) | table.insert( list, listFoot ) | ||
end | end |
Revision as of 11:31, 23 January 2014
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
local name = args.name
local image = args.image
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
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 css = args.css
local styles = {}
local imgClasses = {
Block = 1,
Entity = 1,
Item = 1,
Schematic = 1
}
if imgClasses[name] then
class = name:lower() .. '-sprite ' .. class
else
table.insert( styles, 'background-image:{{FileUrl|' .. ( image or name .. 'Sprite.png' ) .. '}}' )
end
if left > 0 or top > 0 then
table.insert( styles, 'background-position:-' .. left .. 'px -' .. top .. 'px' )
end
if scale ~= 1 then
table.insert( styles, 'background-size:' .. sheetWidth .. 'px auto' )
end
if size ~= 16 then
table.insert( styles, 'height:' .. size .. 'px;width:' .. size .. 'px' )
end
if align ~= 'text-top' then
table.insert( styles, 'vertical-align:' .. align )
end
if css then
table.insert( styles, css )
end
local sprite = table.concat( {
'<span',
'class="sprite sprite2 ' .. class .. '"',
'style="' .. table.concat( styles, ';' ) .. '"',
'><br></span>'
}, ' ' )
sprite = sprite:gsub( '%s([">])', '%1' )
if text ~= '' then
text = '<span class="nowrap" style="padding-left:0.33em">' .. 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 ids = mw.loadData( 'Module:Sprite/' .. args.name )
local id = mw.text.trim( args[1] or '' )
local pos = ids[id] or ids[id:lower():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] or '' ):match( '^(.-)%-' ) or args[1]
local text = args[2] or link
args[1] = args.id or args[1]
args.link = link
args.text = text
return p.sprite( args )
end
function p.doc( f )
local args = f.args
local idTable = mw.title.new( 'Module:Sprite/' .. args.name ):getContent()
idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
local html = {}
local ids = {}
local posKeys = {}
local section = ''
for line in mw.text.gsplit( idTable, ',' ) do
line = mw.text.trim( line )
id = line:match( '^%[[\'"](.+)[\'"]%]' ) or line:match( '^%w+' ) or ''
pos = line:match( '=%s*(%d+)%s*,?$' ) or ''
section = line:match( '^%-%-%s*(.+)%s*%-%-$' ) or section
if id ~= '' and pos ~= '' then
if ids[pos] then
if type( ids[pos].id ) == 'table' then
table.insert( ids[pos].id, id )
else
ids[pos].id = { ids[pos].id, id }
end
else
ids[pos] = { id = id, section = section }
table.insert( posKeys, pos )
end
end
end
local list = {}
local listHead = '<ul class="spritedoc-multicolumn">'
local listFoot = '</ul>'
local lastSection = ''
for i, pos in ipairs( posKeys ) do
local id = ids[pos].id
local newSection = mw.text.trim( ids[pos].section )
if newSection ~= lastSection or i == 1 then
if newSection ~= lastSection then
if lastSection ~= '' then
table.insert( list, listFoot )
end
table.insert( list, '\n===' .. newSection .. '===\n' )
lastSection = newSection
end
table.insert( list, listHead )
end
table.insert( list, '<li><table><tr><td data-pos="' .. pos .. '">' )
if type( id ) == 'table' then
for i, id2 in ipairs( id ) do
if i == 1 then
args[1] = id2
table.insert( list, p.sprite( args ) .. '</td><td><div class="sprite-id"><code>' .. id2 .. '</code></div>' )
else
table.insert( list, '<div class="sprite-id"><code>' .. id2 .. '</code></div>' )
end
end
else
args[1] = id
table.insert( list, p.sprite( args ) .. '</td><td><div class="sprite-id"><code>' .. id .. '</code></div>' )
end
table.insert( list, '</td></tr></table></li>' )
if i == #posKeys then
table.insert( list, listFoot )
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>'
end
return p