Module:Sprite: Difference between revisions
Add multiplier support (x2, x3, etc.) for sprite scaling (via update-page on MediaWiki MCP Server) |
Limit scale to integers 1-10 (via update-page on MediaWiki MCP Server) |
||
| Line 28: | Line 28: | ||
-- Check if sizeArg is a multiplier (starts with 'x') | -- Check if sizeArg is a multiplier (starts with 'x') | ||
local multiplier = sizeArg:match('^x(%d+ | local multiplier = sizeArg:match('^x(%d+)$') | ||
if multiplier then | if multiplier then | ||
multiplier = tonumber(multiplier) | multiplier = tonumber(multiplier) | ||
elseif scale then | elseif scale then | ||
multiplier = tonumber(scale) | multiplier = math.floor(tonumber(scale) or 1) | ||
end | end | ||
-- Clamp multiplier to reasonable integer range | |||
if multiplier then | if multiplier then | ||
multiplier = math.max(1, math.min(multiplier, 10)) | |||
-- Get original file dimensions | -- Get original file dimensions | ||
local file = mw.title.new('File:' .. filename) | local file = mw.title.new('File:' .. filename) | ||
if file and file.file and file.file.width then | if file and file.file and file.file.width then | ||
size = | size = file.file.width * multiplier | ||
else | else | ||
-- Fallback: assume 16px base size | -- Fallback: assume 16px base size | ||
size = | size = 16 * multiplier | ||
end | end | ||
else | else | ||
Latest revision as of 00:52, 28 January 2026
Documentation for this module may be created at Module:Sprite/doc
local p = {}
function p.getHighestStack(frame)
local name = frame.args[1]
if not name then return '' end
for i = 20, 1, -1 do
local file = mw.title.new('File:' .. name .. i .. '.png')
if file and file.exists then
return tostring(i)
end
end
return ''
end
function p.render(frame)
local name = frame.args[1]
local sizeArg = frame.args[2] or frame.args.size or '32'
local scale = frame.args.scale
local class = frame.args.class or ''
if not name then return '' end
local suffix = p.getHighestStack(frame)
local filename = name .. suffix .. '.png'
-- Determine final size
local size
-- Check if sizeArg is a multiplier (starts with 'x')
local multiplier = sizeArg:match('^x(%d+)$')
if multiplier then
multiplier = tonumber(multiplier)
elseif scale then
multiplier = math.floor(tonumber(scale) or 1)
end
-- Clamp multiplier to reasonable integer range
if multiplier then
multiplier = math.max(1, math.min(multiplier, 10))
-- Get original file dimensions
local file = mw.title.new('File:' .. filename)
if file and file.file and file.file.width then
size = file.file.width * multiplier
else
-- Fallback: assume 16px base size
size = 16 * multiplier
end
else
size = sizeArg
end
local classAttr = ''
if class ~= '' then
classAttr = '|class=' .. class
end
return string.format(
'<span class="pixel-sprite">[[File:%s|%spx|link=|alt=%s%s]]</span>',
filename, size, name, classAttr
)
end
return p