Module:Sprite : Différence entre versions

De Minecraft Wiki
Aller à : navigation, rechercher
(Mise à jour)
m (Révocation des modifications de ObelusPA2 (discussion) vers la dernière version de Yanis48)
Ligne 7 : Ligne 7 :
 
f = mw.getCurrentFrame()
 
f = mw.getCurrentFrame()
 
end
 
end
 
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {}
 
local settings = data.settings
 
 
 
 
-- Default settings
 
-- Default settings
Ligne 21 : Ligne 18 :
 
 
 
local defaultStyle = default
 
local defaultStyle = default
if settings then
+
if args.settings then
 
local settings = mw.loadData( 'Module:' .. args.settings )
 
if not settings.stylesheet then
 
if not settings.stylesheet then
 
-- Make a separate clone of the current default settings
 
-- Make a separate clone of the current default settings
Ligne 43 : Ligne 41 :
 
local styles = {}
 
local styles = {}
 
 
if setting( 'url' ) then
 
styles[#styles + 1] = 'background-image:' .. setting( 'url' )
 
end
 
 
if setting( 'stylesheet' ) then
 
if setting( 'stylesheet' ) then
 
sprite:addClass(
 
sprite:addClass(
Ligne 51 : Ligne 46 :
 
mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
 
mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
 
)
 
)
  +
else
elseif not setting( 'url' ) then
 
styles[#styles + 1] = 'background-image:' .. p.getUrl(
+
table.insert( styles, 'background-image:{{FileUrl|' .. (
 
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png'
 
setting( 'image' ) or setting( 'name' ) .. 'Sprite.png'
)
+
) .. '}}' )
 
end
 
end
 
local class = setting( 'class' )
 
local class = setting( 'class' )
Ligne 62 : Ligne 57 :
 
 
 
local size = setting( 'size' )
 
local size = setting( 'size' )
  +
local pos = math.abs( setting( 'pos' ) ) - 1
 
local sheetWidth = setting( 'sheetsize' )
 
local sheetWidth = setting( 'sheetsize' )
 
local tiles = sheetWidth / size
 
local tiles = sheetWidth / size
local pos = setting( 'pos' ) - 1
+
local left = pos % tiles * size
 
local top = math.floor( pos / tiles ) * size
 
local scale = setting( 'scale' )
 
local scale = setting( 'scale' )
 
local autoScale = setting( 'autoscale' )
 
local autoScale = setting( 'autoscale' )
 
 
if pos then
+
if left > 0 or top > 0 then
local left = pos % tiles * size * scale
+
table.insert( styles, 'background-position:-' .. left * scale .. 'px -' .. top * scale .. 'px' )
local top = math.floor( pos / tiles ) * size * scale
 
styles[#styles + 1] = 'background-position:-' .. left .. 'px -' .. top .. 'px'
 
 
end
 
end
 
 
if not autoScale and scale ~= defaultStyle.scale then
 
if not autoScale and scale ~= defaultStyle.scale then
styles[#styles + 1] = 'background-size:' .. sheetWidth * scale .. 'px auto'
+
table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
 
end
 
end
 
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
 
if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
styles[#styles + 1] = 'height:' .. size * scale .. 'px'
+
table.insert( styles, 'height:' .. size * scale .. 'px' )
styles[#styles + 1] = 'width:' .. size * scale .. 'px'
+
table.insert( styles, 'width:' .. size * scale .. 'px' )
 
end
 
end
 
 
local align = setting( 'align' )
 
local align = setting( 'align' )
 
if align ~= defaultStyle.align then
 
if align ~= defaultStyle.align then
styles[#styles + 1] = 'vertical-align:' .. align
+
table.insert( styles, 'vertical-align:' .. align )
 
end
 
end
styles[#styles + 1] = setting( 'css' )
+
table.insert( styles, setting( 'css' ) )
 
 
sprite:cssText( table.concat( styles, ';' ) )
 
sprite:cssText( table.concat( styles, ';' ) )
 
 
Ligne 134 : Ligne 126 :
 
end
 
end
 
 
local data = args.data and mw.loadData( 'Module:' .. args.data ) or {}
 
 
local categories = {}
 
local categories = {}
 
local idData = args.iddata
 
local idData = args.iddata
 
if not idData then
 
if not idData then
local name = args.name or data.settings.name
+
local default = {}
 
if args.settings then
 
default = mw.loadData( 'Module:' .. args.settings )
  +
end
 
  +
local name = args.name or default.name
  +
local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or name .. '/IDs' ) )
  +
ids = ids.ids or ids
 
local id = mw.text.trim( tostring( args[1] or '' ) )
 
local id = mw.text.trim( tostring( args[1] or '' ) )
idData = data.ids[id] or data.ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
+
idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
 
end
 
end
 
 
 
local title = mw.title.getCurrentTitle()
 
local title = mw.title.getCurrentTitle()
 
-- Remove categories on language pages, talk pages, and in User/UserWiki/UserProfile namespaces
 
-- Remove categories on language pages, talk pages, and in User/UserWiki/UserProfile namespaces
local disallowCats = args.nocat or title.isTalkPage or title.nsText:find( '^Utilisateur' )
+
local disallowCats = title.isTalkPage or title.nsText:find( '^Utilisateur' )
 
if idData then
 
if idData then
if idData.deprecated and not disallowCats then
+
if type( idData ) == 'table' then
  +
if idData.deprecated and not disallowCats then
categories[#categories + 1] = '[[Catégorie:Pages utilisant des noms de sprites obsolètes]]'
+
table.insert( categories, '[[Catégorie:Pages utilisant des noms de sprites obsolètes]]' )
  +
end
  +
 
args.pos = idData.pos
  +
else
  +
args.pos = idData
 
end
 
end
 
args.pos = idData.pos
 
 
elseif not disallowCats then
 
elseif not disallowCats then
categories[#categories + 1] = '[[Catégorie:Pages avec des sprites manquants]]'
+
table.insert( categories, '[[Catégorie:Pages avec des sprites manquants]]' )
 
end
 
end
 
 
return p.base( args ), table.concat( categories )
+
return p.base( args ), table.concat( categories, '' )
 
end
 
end
   
Ligne 169 : Ligne 171 :
 
link = args[1]:match( '^(.-)%+' ) or args[1]
 
link = args[1]:match( '^(.-)%+' ) or args[1]
 
end
 
end
local text
+
local text = args.text or args[2] or link
if not args.notext then
 
text = args.text or args[2] or link
 
end
 
 
 
 
args[1] = args.id or args[1]
 
args[1] = args.id or args[1]
Ligne 179 : Ligne 178 :
 
 
 
return p.sprite( args )
 
return p.sprite( args )
end
 
 
function p.getUrl( spritesheet, query )
 
return mw.getCurrentFrame():expandTemplate{ title = 'FileUrl', args = { spritesheet, query = query } }
 
 
end
 
end
   
Ligne 192 : Ligne 187 :
 
f = mw.getCurrentFrame()
 
f = mw.getCurrentFrame()
 
end
 
end
local dataPage = mw.text.trim( args[1] )
+
local settingsPage = mw.text.trim( args[1] )
local data = mw.loadData( 'Module:' .. dataPage )
+
local settings = mw.loadData( 'Module:' .. settingsPage )
 
local idsPage = 'Module:' .. ( settings.ids or settings.name .. '/IDs' )
 
 
 
local getProtection = function( title, action, extra )
 
local getProtection = function( title, action, extra )
 
local protections = { 'edit' }
 
local protections = { 'edit' }
 
if extra then
 
if extra then
protections[#protections + 1] = extra
+
table.insert( protections, extra )
 
end
 
end
 
 
Ligne 208 : Ligne 204 :
 
end
 
end
 
 
protections[#protections + 1] = protection
+
table.insert( protections, protection )
 
end
 
end
 
 
local direct = title.protectionLevels[action] or {}
+
local direct = title.protectionLevels[action]
 
for _, protection in ipairs( direct ) do
 
for _, protection in ipairs( direct ) do
 
addProtection( protection )
 
addProtection( protection )
Ligne 217 : Ligne 213 :
 
local cascading = title.cascadingProtection.restrictions[action] or {}
 
local cascading = title.cascadingProtection.restrictions[action] or {}
 
if #cascading > 0 then
 
if #cascading > 0 then
protections[#protections + 1] = 'protect'
+
table.insert( protections, 'protect' )
 
end
 
end
 
for _, protection in ipairs( cascading ) do
 
for _, protection in ipairs( cascading ) do
Ligne 226 : Ligne 222 :
 
end
 
end
 
 
 
local body
local dataTitle = mw.title.new( 'Module:' .. dataPage )
 
  +
if args.refresh then
local spritesheet = data.settings.image or data.settings.name .. 'Sprite.png'
 
local spriteTitle = mw.title.new( 'File:' .. spritesheet )
+
body = mw.html.create( '' )
  +
else
local dataProtection = getProtection( dataTitle, 'edit' )
 
  +
local idsTitle = mw.title.new( idsPage )
local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' )
 
  +
local spritesheet = settings.image or settings.name .. 'Sprite.png'
local body = mw.html.create( 'div' ):attr( {
 
 
local spriteTitle = mw.title.new( 'File:' .. spritesheet )
id = 'spritedoc',
 
 
local idsProtection = getProtection( idsTitle, 'edit' )
['data-dataprotection'] = dataProtection,
 
 
local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' )
['data-datatimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', 'Module:' .. dataPage ),
 
 
body = mw.html.create( 'div' ):attr( {
['data-datapage'] = 'Module:' .. dataPage,
 
 
id = 'spritedoc',
['data-spritesheet'] = spritesheet,
 
['data-spriteprotection'] = spriteProtection,
+
['data-idspage'] = idsTitle.id,
  +
['data-idsprotection'] = idsProtection,
['data-urlfunc'] = "require( [[Module:Sprite]] ).getUrl( '" .. spritesheet .. "', '$1' )",
 
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite|doc|' .. dataPage .. '|refresh=1}}' ),
+
['data-idstimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', idsPage ),
['data-settings'] = mw.text.jsonEncode( data.settings ),
+
['data-spritesheet'] = spritesheet,
  +
['data-spriteprotection'] = spriteProtection,
} )
 
  +
['data-pos'] = settings.pos or 1,
  +
['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite|doc|' .. settingsPage .. '|refresh=1}}' )
 
} )
 
end
 
  +
local data = mw.loadData( idsPage )
 
 
 
local sections = {}
 
local sections = {}
for _, sectionData in ipairs( data.sections or { name = 'Uncategorized' } ) do
+
for _, sectionData in ipairs( data.sections or { 'Uncategorized' } ) do
 
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id )
 
local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id )
  +
-- https://phabricator.wikimedia.org/T73594
sectionTag:tag( 'h3' ):wikitext( sectionData.name )
+
sectionTag:wikitext( '<h3>', sectionData[1], '</h3>' )
 
sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
 
sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
 
end
 
end
 
 
 
local keyedData = {}
 
local keyedData = {}
local i = 1
 
 
for name, idData in pairs( data.ids ) do
 
for name, idData in pairs( data.ids ) do
keyedData[i] = {
+
table.insert( keyedData, {
 
sortKey = mw.ustring.lower( name ),
 
sortKey = mw.ustring.lower( name ),
 
name = name,
 
name = name,
 
data = idData
 
data = idData
}
+
} )
i = i + 1
 
 
end
 
end
 
table.sort( keyedData, function( a, b )
 
table.sort( keyedData, function( a, b )
Ligne 272 : Ligne 273 :
 
local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
 
local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
 
box:tag( 'div' ):addClass( 'spritedoc-image' )
 
box:tag( 'div' ):addClass( 'spritedoc-image' )
:wikitext( p.base{ pos = pos, data = dataPage } )
+
:wikitext( p.base{ pos = pos, settings = settingsPage } )
 
 
 
names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
 
names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
Ligne 289 : Ligne 290 :
 
return '', tostring( body )
 
return '', tostring( body )
 
end
 
end
return f:callParserFunction( '#widget:SpriteDoc.css' ), tostring( body )
+
return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ), tostring( body )
 
end
 
end
 
return p
 
return p

Version du 14 avril 2018 à 23:19

[créer | historique | purger]Documentation
Ce module n'a pas de documentation. Si vous savez comment l'utiliser, merci de la créer.
local p = {}
function p.base( f )
	local args = f
	if f == mw.getCurrentFrame() then 
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end
	
	-- Default settings
	local default = {
		scale = 1,
		sheetsize = 256,
		size = 16,
		pos = 1,
		align = 'text-top'
	}
	
	local defaultStyle = default
	if args.settings then
		local settings = mw.loadData( 'Module:' .. args.settings )
		if not settings.stylesheet then
			-- Make a separate clone of the current default settings
			defaultStyle = mw.clone( default )
		end
		for k, v in pairs( settings ) do
			default[k] = v
		end
	end
	
	local setting = function( arg )
		return args[arg] or default[arg]
	end
	
	local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
	sprite:tag( 'br' )
	
	-- mw.html's css method performs very slow escaping, which doubles the time it takes
	-- to run, so we'll construct the styles manually, and put them in the cssText
	-- method, which only does html escaping (which isn't slow)
	local styles = {}
	
	if setting( 'stylesheet' ) then
		sprite:addClass(
			setting( 'classname' ) or
			mw.ustring.lower( setting( 'name' ):gsub( ' ', '-' ) ) .. '-sprite'
		)
	else
		table.insert( styles, 'background-image:{{FileUrl|' .. (
			setting( 'image' ) or setting( 'name' ) .. 'Sprite.png'
		) .. '}}' )
	end
	local class = setting( 'class' )
	if class then
		sprite:addClass( class )
	end
	
	local size = setting( 'size' )
	local pos = math.abs( setting( 'pos' ) ) - 1
	local sheetWidth = setting( 'sheetsize' )
	local tiles = sheetWidth / size
	local left = pos % tiles * size
	local top = math.floor( pos / tiles ) * size
	local scale = setting( 'scale' )
	local autoScale = setting( 'autoscale' )
	
	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.scale then
		table.insert( styles, 'background-size:' .. sheetWidth * scale .. 'px auto' )
	end
	if size ~= defaultStyle.size or ( not autoScale and scale ~= defaultStyle.scale ) then
		table.insert( styles, 'height:' .. size * scale .. 'px' )
		table.insert( styles, 'width:' .. size * scale .. 'px' )
	end
	local align = setting( 'align' )
	if align ~= defaultStyle.align then
		table.insert( styles, 'vertical-align:' .. align )
	end
	table.insert( styles, setting( 'css' ) )
	sprite:cssText( table.concat( styles, ';' ) )
	
	local text = setting( 'text' )
	local root
	local spriteText
	if text then
		root = mw.html.create( 'span' ):addClass( 'nowrap' )
		spriteText = mw.html.create( 'span' ):addClass( 'sprite-text' ):wikitext( text )
	end
	
	local title = setting( 'title' )
	if title then
		( root or sprite ):attr( 'title', title )
	end
	
	if not root then
		root = mw.html.create( '' )
	end
	root:node( sprite )
	if spriteText then
		root:node( spriteText )
	end
	
	local link = setting( 'link' ) or ''
	if link ~= '' and mw.ustring.lower( link ) ~= 'none' then
		-- External link
		if link:find( '//' ) then
			return '[' .. link .. ' ' .. tostring( root ) .. ']'
		end
		
		-- Internal link
		local linkPrefix = setting( 'linkprefix' ) or ''
		return '[[' .. linkPrefix .. link .. '|' .. tostring( root ) .. ']]'
	end
	
	return tostring( root )
end

function p.sprite( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module:ProcessArgs' ).merge( true )
	else
		f = mw.getCurrentFrame()
	end
	
	local categories = {}
	local idData = args.iddata
	if not idData then
		local default = {}
		if args.settings then
			default = mw.loadData( 'Module:' .. args.settings )
		end
		
		local name = args.name or default.name
		local ids = mw.loadData( 'Module:' .. ( args.ids or default.ids or name .. '/IDs' ) )
		ids = ids.ids or ids
		local id = mw.text.trim( tostring( args[1] or '' ) )
		idData = ids[id] or ids[mw.ustring.lower( id ):gsub( '[%s%+]', '-' )]
	end
	
	local title = mw.title.getCurrentTitle()
	-- Remove categories on language pages, talk pages, and in User/UserWiki/UserProfile namespaces
	local disallowCats = title.isTalkPage or title.nsText:find( '^Utilisateur' )
	if idData then
		if type( idData ) == 'table' then
			if idData.deprecated and not disallowCats then
				table.insert( categories, '[[Catégorie:Pages utilisant des noms de sprites obsolètes]]' )
			end
			
			args.pos = idData.pos
		else
			args.pos = idData
		end
	elseif not disallowCats then
		table.insert( categories, '[[Catégorie:Pages avec des sprites manquants]]' )
	end
	
	return p.base( args ), table.concat( categories, '' )
end

function p.link( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = require( 'Module: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.text or args[2] or link
	
	args[1] = args.id or args[1]
	args.link = args.link or link
	args.text = text
	
	return p.sprite( args )
end

function p.doc( f )
	local args = f
	if f == mw.getCurrentFrame() then
		args = f.args
	else
		f = mw.getCurrentFrame()
	end
	local settingsPage = mw.text.trim( args[1] )
	local settings = mw.loadData( 'Module:' .. settingsPage )
	local idsPage = 'Module:' .. ( settings.ids or settings.name .. '/IDs' )
	
	local getProtection = function( title, action, extra )
		local protections = { 'edit' }
		if extra then
			table.insert( protections, extra )
		end
		
		local addProtection = function( protection )
			if protection == 'autoconfirmed' then
				protection = 'editsemiprotected'
			elseif protection == 'sysop' then
				protection = 'editprotected'
			end
			
			table.insert( protections, protection )
		end
		
		local direct = title.protectionLevels[action]
		for _, protection in ipairs( direct ) do
			addProtection( protection )
		end
		local cascading = title.cascadingProtection.restrictions[action] or {}
		if #cascading > 0 then
			table.insert( protections, 'protect' )
		end
		for _, protection in ipairs( cascading ) do
			addProtection( protection )
		end
		
		return table.concat( protections, ',' )
	end
	
	local body
	if args.refresh then
		body = mw.html.create( '' )
	else
		local idsTitle = mw.title.new( idsPage )
		local spritesheet = settings.image or settings.name .. 'Sprite.png'
		local spriteTitle = mw.title.new( 'File:' .. spritesheet )
		local idsProtection = getProtection( idsTitle, 'edit' )
		local spriteProtection = getProtection( spriteTitle, 'upload', 'upload,reupload' )
		body = mw.html.create( 'div' ):attr( {
			id = 'spritedoc',
			['data-idspage'] = idsTitle.id,
			['data-idsprotection'] = idsProtection,
			['data-idstimestamp'] = f:callParserFunction( 'REVISIONTIMESTAMP', idsPage ),
			['data-spritesheet'] = spritesheet,
			['data-spriteprotection'] = spriteProtection,
			['data-pos'] = settings.pos or 1,
			['data-refreshtext'] = mw.text.nowiki( '{{#invoke:sprite|doc|' .. settingsPage .. '|refresh=1}}' )
		} )
	end
	
	local data = mw.loadData( idsPage )
	
	local sections = {}
	for _, sectionData in ipairs( data.sections or { 'Uncategorized' } ) do
		local sectionTag = body:tag( 'div' ):addClass( 'spritedoc-section' ):attr( 'data-section-id', sectionData.id )
		-- https://phabricator.wikimedia.org/T73594
		sectionTag:wikitext( '<h3>', sectionData[1], '</h3>' )
		sections[sectionData.id] = { boxes = sectionTag:tag( 'ul' ):addClass( 'spritedoc-boxes' ) }
	end
	
	local keyedData = {}
	for name, idData in pairs( data.ids ) do
		table.insert( keyedData, {
			sortKey = mw.ustring.lower( name ),
			name = name,
			data = idData
		} )
	end
	table.sort( keyedData, function( a, b )
		return a.sortKey < b.sortKey
	end )
	
	for _, data in ipairs( keyedData ) do
		local idData = data.data
		local pos = idData.pos
		local section = sections[idData.section]
		local names = section[pos]
		if not names then
			local box = section.boxes:tag( 'li' ):addClass( 'spritedoc-box' ):attr( 'data-pos', pos )
			box:tag( 'div' ):addClass( 'spritedoc-image' )
				:wikitext( p.base{ pos = pos, settings = settingsPage } )
			
			names = box:tag( 'ul' ):addClass( 'spritedoc-names' )
			section[pos] = names
		end
		local nameElem = mw.html.create( 'li' ):addClass( 'spritedoc-name' )
		local codeElem = nameElem:tag( 'code' ):wikitext( data.name )
		
		if idData.deprecated then
			codeElem:addClass( 'spritedoc-deprecated' )
		end
		names:wikitext( tostring( nameElem ) )
	end
	
	if args.refresh then
		return '', tostring( body )
	end
	return f:callParserFunction( '#widget:Stylesheet', { page = 'SpriteDoc' } ), tostring( body )
end
return p