Module:Paintsimmon/Infobox

From RimWorld Wiki
Jump to navigation Jump to search

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

local p = {} --p stands for package

local lineSep = '\n\n' -- Two newlines are needed

-- no idk a better way to do this
local infoboxStyles = {
	furniture = 'c_01',
	medicine = 'c_01',
	structure = 'c_02',
	resource = 'c_03',
	weapon = 'c_06',
	animal = 'c_08',
	food = 'c_08',
	fabric = 'c_09',
	psycast = 'c_09',
	security = 'c_10',
	area = 'c_11',
	exotic = 'c_11',
	metallic = 'c_12',
	stony = 'c_12',
	plant = 'c_14',
	leathery = 'c_22',
	drug = 'c_24',
	production = 'c_25',
}

-- This is used to standardize the order of the sections and properties.
local sectionOrder = {
	-- sectionName is the only named field, all others are integer-indexed
	{
		sectionName = 'baseStats',
		'type', 'type2', 'stuffCategory', 'hp', 'flammability', 'marketValue',
		'beauty', 'massBase', 'stackLimit', 'pathCost', 'rotatable'
	},
	{
		sectionName = 'statModifiers',
		'beautyFactor', 'workToMakeFactor', 'workToBuildFactor',
		'maxHitPointsFactor', 'flammabilityFactor', 'armorSharpFactor',
		'armorBluntFactor', 'armorHeatFactor', 'insulationColdFactor',
		'insulationHeatFactor'
	},
	{
		sectionName = 'Technical',
		'pageVerifiedForVersion',
	},
	{
		sectionName = 'Unused',
		'alwaysHaulable',
	},
}

-- Generates a string for a single row (property-value pair)
function p.infoboxRow(property, value)
    return property .. ' | ' .. value
end

-- Generates a string for a single section
function p.infoboxSection(section, values)
	local sectStr = section.sectionName .. lineSep
	for _i, property in ipairs(section) do
		if values[property] ~= nil then -- So it doesn't error trying to concat nil
			local row = p.infoboxRow(property, values[property])
			sectStr = sectStr .. row .. lineSep
		end
	end
	return sectStr
end

-- Checks if the section should be shown (if there is at least one valid property filled out)
function p.showSection(section, values)
	for _i, property in ipairs(section) do
		if values[property] then
			return true
		end
	end
	return false
end

-- Generates a string for the header section
function p.headerSection(values)
	local sectStr = ''
	if values.name ~= nil then
		sectStr = sectStr .. string.format('<p class="heading">%s</p>', values.name) .. lineSep
	end
	sectStr = sectStr .. '<div class="wrapper">'
	if values.name ~= nil and values.image ~= nil then
		sectStr = sectStr .. string.format('<div class="image_wrapper" title="%s">[[File:%s|250x250px]]</div>', values.name, values.image) .. lineSep
	end
	sectStr = sectStr .. '<p class="text-center" style="display:block; font-size:89%; padding: .5em;">'
	if values.audio ~= nil then
		sectStr = sectStr .. string.format('[[File:%s]]', values.audio)
	end
	if values.description ~= nil then
		sectStr = sectStr .. values.description
	end
	sectStr = sectStr .. '</p>' .. lineSep
	return sectStr
end

-- Generates a string for the whole infobox
function p.infobox(frame)
	local values = frame.args
	local output = string.format('<div class="infobox float:right; %s">', infoboxStyles[values.infoboxStyle])
	output = output .. p.headerSection(values)
	for _i, section in ipairs(sectionOrder) do
		if p.showSection(section, values) then
			local sectStr = p.infoboxSection(section, values)
			output = output .. sectStr
		end
	end
	output = output .. '</div></div>'
	return output
end

return p