2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/公的驱逐舰/Aksabun

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [创建] [刷新]
-- THIS IS NOT THE SAME AS THE DEPLOYED M:Aksabun!!!!! It is a WIP too.
-- This is an attempt to implement the new sabun system for Arknights, which
-- requires reconstitution of the main image and the face delta. 
-- Crappy code originally by One-Six(U:公的驱逐舰), released under CC BY 4.0.
-- Gotta Praise the Crocc.

local getArgs = require('Module:Arguments').getArgs
--local getData = require('Module:Aksabun/data').getData
local getData = function() return nil end -- dummy getData for testing

local p = {}
local wrapperArray = { 'Template:Aksabun', 'Template:沙盒' }

local function isEmpty( s )
	return (s == nil or s == '')
end

local function getArgFromAlias ( args, argsAliasArray )
	for i = 1, #argsAliasArray do
		if ( args[argsAliasArray[i]] ~= nil ) then
			return args[argsAliasArray[i]]
		end
	end
	return nil
end

local function arrayFromCSV ( s, sep )
	if s == nil then return {} end
	local t = {}
	for w in (s:gsub("%"..(sep or ',').."?%s*$",(sep or ','))):gmatch("([^%"..(sep or ',').."]*)%"..(sep or ',').."%s*") do 
		t[#t+1] = w
	end
	return t
end

local function compactArray ( a )
	local j=0
	for i=1,#a do
		if a[i]~=nil then
			j=j+1
			a[j]=a[i]
        end
	end
	for i=j+1,#a do
		a[i]=nil
	end
	return a
end

local function tableInvert( t )
	local s= {}
	for k,v in ipairs(t) do
		s[v]=k
	end
	return s
end

-- manual data gathering for testing
function getDataFromArgs ( args, sabunType, sabunCode )
	local data = {
			isDelta = (string.lower(args["is-delta"] or '') == 'true'),
			lastNum = tonumber(args["last-num"]) or 1,
			separator = args.separator or '_',
			charpopX = tonumber(args["charpop-x"]) or 0,
			charpopY = tonumber(args["charpop-y"]) or 0,
			deltaX = tonumber(args["delta-x"]) or 0,
			deltaY = tonumber(args["delta-y"]) or 0,
			deltaW = tonumber(args["delta-w"]) or .1,
			deltaH = tonumber(args["delta-h"]) or nil
		}
	data.basename = ((not data.isDelta) and "Ak_" or '') .. sabunType .. "_" .. sabunCode
	return data
end

function genDeltaSabun ( frame, data, sabun, size )
	return '<div style="position:relative;width:'..size..'px;height:'..size..'px;">'
		.. '<div style="display:none;">[[File:'..data.basename..'.png]]</div><div style="display:none;">[[File:'..data.basename..'.png]]</div>'
		.. '<img style="position:absolute;top:0;left:0;width:'..size..'px;height:'..size..'px;z-index:0;" '
		.. 'src="'..frame:callParserFunction( 'filepath', data.basename..'.png' )..'" / >'
		.. '<img style="position:absolute;top:'..data.deltaY*size..'px;left:'..data.deltaX*size..'px;width:'..data.deltaW*size..'px;height:'..(data.deltaH or data.deltaW)*size..'px;z-index:1;" '
		.. 'src="'..frame:callParserFunction( 'filepath', data.basename..'@'..sabun..'.png' )..'" / >'
		.. '</div>'
end

-- EXPOSED METHODS

-- single formatted sabun division for pseudo-img uses, like in T:Akcharpop
function p.single ( frame )
	local args = getArgs ( frame, { wrappers = wrapperArray	} )
	return p._single ( frame, args )
end
function p._single ( frame, args )
	local sabunType = string.lower ( getArgFromAlias ( args, { "sabun-type", "差分类型", "1", 1 } ) )
		-- 'npc' shorthand is no longer allowed since there are potentially 'npc' sabun types
	local sabunCode = getArgFromAlias ( args, { "sabun-code", "差分代号", "2", 2 } )
	local sabun = getArgFromAlias ( args, { "sabun", "差分", "3", 3 } )
	local data = args.data or getData ( sabunType, sabunCode ) or getDataFromArgs ( args, sabunType, sabunCode )
	local size = tonumber ( args.size ) or 400
	if data.isDelta then
		return genDeltaSabun ( frame, data, sabun, size )
	else 
		return '[[File:'..data.basename..data.separator..sabun..'.png|'..size..'px]]'
	end
end

-- formatted sabun table, for use by other modules (mainly M:Akopbasics)
-- expect datatable input: size
function p._sabunTable ( frame, args )
	local t = {}
	local size = tonumber ( args.size ) or 400
	local sabunType = string.lower ( getArgFromAlias ( args, { "sabun-type", "差分类型", "1", 1 } ) )
		-- 'npc' shorthand is no longer allowed since there are potentially 'npc' sabun types
	local sabunCode = getArgFromAlias ( args, { "sabun-code", "差分代号", "2", 2 } )
	--local baseFile = frame:callParserFunction( 'filepath', args.basename..'@'..args.sabun..'png', size )
	local data = getData ( sabunType, sabunCode ) or getDataFromArgs ( args, sabunType, sabunCode )
	local i = 1
	for k, v in ipairs(data.sabunList) do
		local nextFile = frame:callParserFunction( 'filepath', args.basename..'@'..i..'png' )
		t[#t+1] = p._single ( frame, { 
			["sabun-type"] = sabunType,
			["sabun-code"] = sabunCode,
			sabun = v,
			size = size,
			data = data
		} )
	end
	return t
end

-- formatted sabun tabs, for direct presentation.
function p.sabunTabs ( frame )
end
function p._sabunTabs ( frame, args )
	local sabunTable = p._sabunTable ( frame, args )
	local size = tonumber ( getArgFromAlias ( args, { "size" } ) ) or 400
	local outputString = '<div class="Tabs black" data-default-tab="1" data-auto-width="yes" style="max-width:calc('..size..'px + 2em);">'
	for i = 1, #sabunTable do
		outputString = outputString .. '<div class="Tab"><div class="TabLabelText">差分'..i..'</div><div class="TabContentText">'..sabunTable[i]..'</div></div>'
	end
	return outputString .. '</div>'
end

return p