Module:Layered blueprint

From Minecraft Wiki
Jump to: navigation, search
[view | edit | history | purge]DocumentationJump to code ↴

This module implements {{layered blueprint}}.

Dependencies[edit]

[view | edit | history | purge]The above documentation is transcluded from Module:Layered blueprint/doc.
local p = {}
local getLayerSize = function( data )
	local width = 0
	local height = #data
	for _, v in ipairs( data ) do
		width = math.max( #v, width )
	end
	
	return width, height
end
p.main = function( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	else
		f = mw.getCurrentFrame()
	end
	
	local text = require( [[Module:Text]] )
	local blockGrid = require( [[Module:Sprite grid]] ).grid
	local keys = {}
	for k, v in pairs( args ) do
		if type( k ) == 'string' and #k == 1 then
			keys[k] = v
		end
	end
	
	local layerNames = {}
	local layers = {}
	local width = 0
	local height = 0
	local scale = args.scale or 1
	for _, v in ipairs( args ) do
		if v:match( '%-%-%-%-' ) then
			table.insert( layerNames, text.trim( string.sub( v, 5 ) ) )
		else
			local data = text.split( text.trim( v, '\n' ), '\n' )
			local layerWidth, layerHeight = getLayerSize( data )
			width = math.max( layerWidth, width )
			height = math.max( layerHeight, height )
			data.keys = keys
            data.scale = scale
            data.sheet = args.sheet
			table.insert( layers, blockGrid( data ) )
		end
	end
	
	local name = args.name:gsub( ' ', '_' )
	local defaultLayer = args.default
	local body = mw.html.create( 'div' ):addClass( 'layered-blueprint' ):css( {
		width = width * 16 * scale .. 'px',
		['min-height'] = height * 16 * scale .. 'px'
	} )
	for i, v in ipairs( layerNames ) do
		body:wikitext(
			f:callParserFunction( '#widget:radio button', {
				radioClass = 'layered-blueprint-radio',
				labelClass = 'layered-blueprint-tab',
				name = name,
				id = name .. '-layer' .. i,
				label = v,
				checked = v == defaultLayer or not defaultLayer and i == 1
			} )
		)
		body:tag( 'div' ):addClass( 'layered-blueprint-layer' ):newline():wikitext( layers[i] )
	end
	
	return body
end
return p