Module:Sprite: Difference between revisions
imported>Ttv pedro270707 mNo edit summary |
imported>Ttv pedro270707 m (Scuffed module update) |
||
| Line 17: | Line 17: | ||
f = mw.getCurrentFrame() | f = mw.getCurrentFrame() | ||
end | end | ||
local | |||
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {} | |||
local settings = data.settings | |||
local ids = data.ids | |||
local default = { | local default = { | ||
| Line 26: | Line 29: | ||
align = 'text-top' | align = 'text-top' | ||
} | } | ||
local | |||
local | local defaultStyle = default | ||
local size = | if settings then | ||
local pos = args. | if not settings.stylesheet then | ||
local align = | -- Make a separate clone of the current default settings | ||
defaultStyle = mw.clone( default ) | |||
end | |||
for k, v in pairs( settings ) do | |||
default[k] = v | |||
end | |||
end | |||
local setting = function( arg ) | |||
return args[arg] or default[arg] | |||
end | |||
local scale = setting( 'scale' ) | |||
local sheetsize = setting( 'sheetsize' ) | |||
local size = setting( 'size' ) | |||
local pos = args.id and ids[args.id] or setting( 'pos' ) | |||
local align = setting( 'align' ) | |||
local spritePosition = p.getSpritePosition( pos, sheetsize, size ) | local spritePosition = p.getSpritePosition( pos, sheetsize, size ) | ||
local image = setting( 'image' ) | |||
local fileUrl = f:expandTemplate{title = 'FileUrl', args = {[1] = | local fileUrl = f:expandTemplate{title = 'FileUrl', args = {[1] = image} } | ||
local styles = { | local styles = { | ||
| Line 41: | Line 61: | ||
"display: inline-block", | "display: inline-block", | ||
"width: " .. size * scale .. "px", | "width: " .. size * scale .. "px", | ||
"height: " .. size * scale .. "px | "height: " .. size * scale .. "px" | ||
} | } | ||
| Line 50: | Line 69: | ||
sprite:attr( 'data-bgimage', fileUrl ) | sprite:attr( 'data-bgimage', fileUrl ) | ||
return tostring( sprite ) | |||
return tostring( | |||
end | end | ||
return p | return p | ||
Revision as of 01:38, 10 March 2023
Documentation for this module may be created at Module:Sprite/doc
local p = {}
function p.getSpritePosition( pos, sheetsize, size )
local spritesPerRow = sheetsize / size
local row = math.floor(pos / spritesPerRow)
local col = pos % spritesPerRow
local x = col * size
local y = row * size
return { ['x'] = x, ['y'] = y }
end
function p.get(f)
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {}
local settings = data.settings
local ids = data.ids
local default = {
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
align = 'text-top'
}
local defaultStyle = default
if settings then
if not settings.stylesheet then
-- Make a separate clone of the current default settings
defaultStyle = mw.clone( default )
end
for k, v in pairs( settings ) do
default[k] = v
end
end
local setting = function( arg )
return args[arg] or default[arg]
end
local scale = setting( 'scale' )
local sheetsize = setting( 'sheetsize' )
local size = setting( 'size' )
local pos = args.id and ids[args.id] or setting( 'pos' )
local align = setting( 'align' )
local spritePosition = p.getSpritePosition( pos, sheetsize, size )
local image = setting( 'image' )
local fileUrl = f:expandTemplate{title = 'FileUrl', args = {[1] = image} }
local styles = {
"vertical-align: " .. align,
"background-position: " .. -spritePosition.x * scale .. "px " .. -spritePosition.y * scale .. "px",
"background-size: " .. sheetsize * scale .. "px auto",
"display: inline-block",
"width: " .. size * scale .. "px",
"height: " .. size * scale .. "px"
}
local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
sprite:addClass( 'pixelated' )
sprite:cssText( table.concat( styles, ";" ) )
sprite:attr( 'data-bgimage', fileUrl )
return tostring( sprite )
end
return p