Module:Sprite

Revision as of 12:51, 28 May 2022 by Auburnsummer (talk | contribs)

Documentation for this module may be created at Module:Sprite/doc

local p = {}

p.main = function( frame )
	local ratio = tonumber(frame.args['ratio'])
	local sheetWidth = tonumber(frame.args['sheetWidth'])
	local sheetHeight = tonumber(frame.args['sheetHeight'])
	local noOfRows = tonumber(frame.args['noOfRows'])
	local noOfColumns = tonumber(frame.args['noOfColumns'])
	local offsetX = tonumber(frame.args['offsetX']) * -1
	local offsetY = tonumber(frame.args['offsetY']) * -1
	local row = tonumber(frame.args['row']) - 1
	local col = tonumber(frame.args['col']) - 1
	local image = frame.args['image']
	local cellWidth = sheetWidth / noOfColumns
	local cellHeight = sheetHeight / noOfRows
	local outline = tonumber(frame.args['outline'])
	local outlineCss
	if outline == 1 then
		outlineCss = "drop-shadow(0.5px 0.5px 0 black) drop-shadow(-0.5px -0.5px 0 black) drop-shadow(-0.5px 0.5px 0 black) drop-shadow(0.5px -0.5px 0 black)"
	else
		outlineCss = ""
	end
	local span = mw.html.create('span')
		:css({
			position = 'relative',
			height = tostring(cellHeight * ratio) .. 'px',
			width = tostring(cellWidth * ratio) .. 'px',
			overflow = 'hidden',
			display = 'inline-block',
			['vertical-align'] = 'middle'
		})
		:tag('span')
		:css({
			position = 'absolute',
			height = tostring(sheetHeight * ratio) .. 'px',
			width = tostring(sheetWidth * ratio) .. 'px',
			top = tostring((offsetY - cellHeight * row) * ratio) .. 'px',
			left = tostring((offsetX - cellWidth * col) * ratio) .. 'px',
			filter = outlineCss
		})
		:wikitext(string.format('[[%s|%dpx|class=pixelart]]', image, sheetWidth * ratio))
		:done()
	return tostring(span)
end

return p