Module:ComponentMethods

From Resonite Wiki
Revision as of 07:02, 15 July 2024 by Bluigi (talk | contribs) (Trim description)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:ComponentMethods/doc

-- test in console:
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})
-- =p.main(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={"Resize(size, filtering)\n", "size:int,int2:Scaling factor.\n;filtering:Filtering:Resampling method.\n", "Replaces the 2D texture with a scaled copy, resampled using the given filtering method. Scales separately in the X and Y axes when an int2 is passed."}}:newChild{title="Module:Test",args={}})
-- =p.footnote(mw.getCurrentFrame():newChild{title="Template:Table ComponentMethods",args={" SetUpRenderer()  *", " ", " Attaches components for a default [[PBS_Metallic]] material and [[Component:MeshRenderer|MeshRenderer]] referencing this mesh."}}:newChild{title="Module:Test",args={}})

local p = {}

function p.main(frame)
	local templateFrame = frame:getParent()

	local body = ""
	local i = 0

	while true do
		local argI = i * 3 + 1
		local method = templateFrame.args[argI]
		local params = templateFrame.args[argI + 1]
		local description = templateFrame.args[argI + 2]

		if not description then
			break
		end

		local _, nameEnd = method:find(")")
		if not nameEnd then error('Missing closing parenthesis in method name "' .. method .. '"') end
		local name = mw.text.trim(method:sub(1, nameEnd)):gsub(" +", " ")
		local rest = mw.text.trim(method:sub(nameEnd + 1))

		params = mw.text.trim(params)
		description = mw.text.trim(description)
		
		local noParams = #params == 0

		body = body .. '|- style="vertical-align:top;"\n'
		body = body .. "|<code>" .. name .. "</code> " .. rest .. "\n"
		if noParams then
			body = body .. "|''none''\n"
		else
			body = body .. "|"
			local first = false
			repeat
				local _, paramEnd = params:find(";")
				paramEnd = paramEnd or #params + 1
				local param = mw.text.trim(params:sub(0, paramEnd - 1))
				local name, types, desc = param:match("([^:]*):([^:]*):(.*)")
				if not desc then error('Missing parts in param spec "' .. param .. '"') end

				body = body .. "\n<code>" .. name .. "</code>&nbsp;:&nbsp;"

				local first = true
				for t in types:gmatch("([^,]+)") do
					if not first then body = body .. "&nbsp;or&nbsp;" end
					-- use custom link if specified
					if t:match("^%[%[.+]]$") then
						body = body .. t
					else
						body = body .. "[[Type:" .. t .. "|" .. t .. "]]"
					end
					first = false
				end
				body = body .. "<br>" .. desc .. "\n"

				params = mw.text.trim(params:sub(paramEnd + 1))
			until #params == 0
		end
		body = body .. "|" .. description .. "\n"
		i = i + 1
	end

	return body
end

function p.footnote(frame)
	local templateFrame = frame:getParent()

	local body = ""
	local anyHidden = false
	local i = 0

	while true do
		local method = templateFrame.args[3 * i + 1]
		
		if not method then
			break
		end

		local _, j = method:find(")")
		if method:sub(j + 1):find("*") then
			anyHidden = true
			break
		end

		i = i + 1
	end
	
	if anyHidden then
		body = body .. "''* Methods marked with an asterisk are invisible in the inspector by default.''"
	end

	return body
end

return p