Модуль:Участник:Asassin 1/Спрайт — различия между версиями

Материал из Playzone Minecraft Wiki
Перейти к: навигация, поиск
(Новая страница: «local p = {} function p.base( f ) local args = f if f == mw.getCurrentFrame() then args = require( 'Модуль:ProcessArgs' ).merge( true ) end -- На…»)
 
м (1 версия импортирована)
 
(нет различий)

Текущая версия на 01:27, 18 января 2017

Для документации этого модуля может быть создана страница Модуль:Участник:Asassin 1/Спрайт/doc

local p = {}
function p.base( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Модуль:ProcessArgs' ).merge( true )
	end
	
	-- Настройки по умолчанию
	local default = {
		["масштаб"] = 1,
		["формат"] = 256,
		["разм"] = 16,
		["поз"] = 1,
		["ссылка"] = '',
		["выравн"] = 'text-top',
		["класс"] = '',
		["текст"] = '',
		["назв"] = ''
	}
	
	local defaultStyle = mw.clone( default )
	if args["настройки"] then
		local settings = mw.loadData( 'Модуль:' .. args["настройки"] .. 'Спрайт' )
		for k, v in pairs( settings ) do
			default[k] = v
			if settings["таблстилей"] then
				defaultStyle[k] = v
			end
		end
	end
	
	local name = args["имя"] or default["имя"]
	local scale = args["масштаб"] or default["масштаб"]
	local autoScale = args["автомасштаб"] or default["автомасштаб"]
	local sheetWidth = args["формат"] or default["формат"]
	local size = args["разм"] or default["разм"]
	local pos = math.abs( args["поз"] or default["поз"] ) - 1
	local link = args["ссылка"] or default["ссылка"]
	local align = args["выравн"] or default["выравн"]
	local class = args["класс"] or default["класс"]
	local text = args["текст"] or default["текст"]
	local title = args["назв"] or default["назв"]
	local css = args["css"] or default["css"]
	local className = args["имякласса"] or default["имякласса"]

	local tiles = sheetWidth / size
	local left = pos % tiles * size
	local top = math.floor( pos / tiles ) * size
	
	local styles = {}
	if args["таблстилей"] or default["таблстилей"] then
		class = ( className or mw.ustring.lower( name:gsub( ' ', '-' ) ) .. '-sprite ' ) .. class
	else
		table.insert( styles, 'background-image:{{FileUrl|' .. ( args["изобр"] or default["изобр"] or name .. 'CSS.png' ) .. '}}' )
	end
	if left > 0 or top > 0 then
		table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
	end
	if not autoScale and scale ~= defaultStyle["масштаб"] then
		table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
	end
	if size ~= defaultStyle["разм"] or ( not autoScale and scale ~= defaultStyle["масштаб"] ) then
		table.insert( styles, 'height:' .. size * scale .. 'px;width:' .. size * scale .. 'px' )
	end
	if align ~= defaultStyle["выравн"] then
		table.insert( styles, 'vertical-align:' .. align )
	end
	if css then
		table.insert( styles, css )
	end
	if title ~= '' then
		title = ' title="' .. title .. '"'
	end
	
	local sprite = table.concat( {
		'<span',
			'class="sprite ' .. class .. '"',
			'style="' .. table.concat( styles, ';' ) .. '"',
			title,
		'><br></span>'
	}, ' ' )
	sprite = sprite:gsub( '%s+([">])', '%1' )
	
	if text ~= '' then
		text = '<span class="sprite-text nowrap"' .. title .. '>' .. text .. '</span>'
	end
	
	if link ~= '' then
		if link:find( '//' ) then
			-- Внешняя ссылка
			return '[' .. link .. ' ' .. sprite .. text .. ']'
		else
			-- Внутренняя ссылка
			local linkPrefix = args["предссылки"] or default["предссылки"] or ''
			return '[[' .. linkPrefix .. link .. '|' .. sprite .. text .. ']]'
		end
	else
		return sprite .. text
	end
end

function p.sprite( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Модуль:ProcessArgs' ).merge( true )
	end
	
	local category = ''
	if tonumber( args[1] ) then
		args["поз"] = args[1]
	else
		local default = {}
		if args["настройки"] then
			default = mw.loadData( 'Модуль:' .. args["настройки"] .. 'Спрайт'  )
		end
		
		local name = args["имя"] or default["имя"]
		local ids = mw.loadData( 'Модуль:' .. ( args["IDы"] or default["IDы"] or 'Спрайт/' .. name ) )
		local id = mw.text.trim( args[1] or '' )
		local pos = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
		if not pos and not mw.title.getCurrentTitle().isSubpage then
			category = '[[Категория:Страницы с неработающими спрайтами]]'
		end
		args["поз"] = pos
	end
	
	return p.base( args ) .. category
end

function p.link( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Модуль:ProcessArgs' ).merge( true )
	end
	
	local link = args[1]
	if args[1] and not args.id then
		link = args[1]:match( '^(.-)%+' ) or args[1]
	end
	local text = args["текст"] or args[2] or link
	
	args[1] = args["ID"] or args[1]
	args["ссылка"] = link
	args["текст"] = text
	
	return p.sprite( args )
end

function p.doc( f )
	local settings = mw.loadData( 'Модуль:' .. f.args[1] .. 'Спрайт' )
	local idTable = mw.title.new( 'Модуль:' .. ( settings.ids or 'Спрайт/' .. settings.name ) ):getContent()
	idTable = idTable:gsub( '(\n%s*%-%-%s*.-%s*%-%-%s*\n)', '%1,' ):gsub( '^return {', '' ):gsub( '}$', '' )
	
	local html = {}
	local ids = {}
	local posKeys = {}
	local section = ''
	for line in mw.text.gsplit( idTable, ',' ) do
		line = mw.text.trim( line )
		id = line:match( '^%[[\'"](.+)[\'"]%]' ) or line:match( '^%w+' ) or ''
		pos = line:match( '=%s*(%d+)%s*,?$' ) or ''
		section = line:match( '^%-%-%s*(.+)%s*%-%-$' ) or section
		
		if id ~= '' and pos ~= '' then
			if ids[pos] then
				if type( ids[pos].id ) == 'table' then
					table.insert( ids[pos].id, id )
				else
					ids[pos].id = { ids[pos].id, id }
				end
			else
				ids[pos] = { id = id, section = section }
				table.insert( posKeys, pos )
			end
		end
	end
		
	local list = {}
	local listHead = '<ul class="spritedoc-multicolumn">'
	local listFoot = '</ul>'
	local lastSection = ''
	for i, pos in ipairs( posKeys ) do
		local id = ids[pos].id
		local newSection = mw.text.trim( ids[pos].section )
		
		if newSection ~= lastSection or i == 1 then
			if newSection ~= lastSection then
				if lastSection ~= '' then
					table.insert( list, listFoot )
				end
				
				table.insert( list, '\n=== ' .. newSection .. ' ===\n' )
				lastSection = newSection
			end
			table.insert( list, listHead )
		end
		table.insert( list, '<li><table><tr><td data-pos="' .. pos .. '">' )
		if type( id ) == 'table' then
			for i, id2 in ipairs( id ) do
				if i == 1 then
					table.insert( list, p.sprite{ id2, settings = f.args[1] } .. '</td><td><div class="sprite-id"><code>' .. id2 .. '</code></div>' )
				else
					table.insert( list, '<div class="sprite-id"><code>' .. id2 .. '</code></div>' )
				end
			end
		else
			table.insert( list, p.sprite{ id, settings = f.args[1] } .. '</td><td><div class="sprite-id"><code>' .. id .. '</code></div>' )
		end
		table.insert( list, '</td></tr></table></li>' )
		
		if i == #posKeys then
			table.insert( list, listFoot )
		end
	end
	
	local out = table.concat( list )
	if not f.args.refresh then
		out = f:preprocess( '{{#widget:stylesheet|page=Sprite doc}}' ) .. '<div id="sprite-doc" data-settings="' .. f.args[1] .. '">' .. out .. '</div>'
	end
	
	return out
end
return p