Module:Sandbox/Layered blueprint

Jump to navigation Jump to search

This module implements {{Sandbox/Layered_blueprint}}.

Dependencies[edit source]

[view] [edit] [history] [refresh]The above documentation is transcluded from Module:Sandbox/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:Sandbox/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
		local tab_div = mw.html.create( 'div' )
		if v == defaultLayer or not defaultLayer and i == 1 then
			tab_div
				:attr( 'data-checked', tostring(v == defaultLayer))
		end
		
		tab_div
			:attr( 'id', name .. '-layer' .. i )
			:attr( 'class', 'layered-blueprint-tab' )
			:attr( 'data-name', name )
			:wikitext( v )
		
		body:wikitext( tostring( tab_div ) )
		body:tag( 'div' ):addClass( 'layered-blueprint-layer' ):newline():wikitext( layers[i] )
	end
	
	return body
end
return p