Module:Recipe: Difference between revisions

From Subnautica Wiki
(adding more styling; adding width control to the top to easily change icon widths)
(removing link from background icon image; adding link to item image; removing background icon when full image is provided; adding hidden element to force widths and heights adapted to text size)
Line 50: Line 50:
-- Recipe is found, start creating the output
-- Recipe is found, start creating the output
for i,v in ipairs(dataSN[itemID].recipe) do
for i,v in ipairs(dataSN[itemID].recipe) do
local name = dataSN[v[1]].name
local image = dataSN[v[1]].image
local image = dataSN[v[1]].image
local iconBG
local iconBG
Line 62: Line 64:
end
end
end
end
local quantity = ""
local quantity = ""
if v[2] > 1 then
if v[2] > 1 then
quantity = "<span style='display:block;position:absolute;top:" .. width .. ";text-align:center;width:100%;'>x" .. v[2] .. "</span>"
quantity = "<span style='display:block;position:absolute;top:" .. width .. ";text-align:center;width:100%;'>x" .. v[2] .. "</span>"
end
end
output = output .. "<div style='position:relative;width:" .. width .. ";'><span style='position:absolute;left:0;top:0;'>[[File:" .. iconBG .. " bg.png|" .. width .. "]]</span><span style='position:absolute;left:0;top:0;'>[[File:" .. image .. "|" .. width .. "]]</span>" .. quantity .. "</div>"
output = output .. "<div style='position:relative;width:" .. width .. ";'>"
if iconBG then
output = output .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. iconBG .. " bg.png|" .. width .. "|link=]]</span>"
end
output = output .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. image .. "|" .. width .. "|link=" .. name .. "|" .. name .. "]]</span><span style='visibility:hidden;width:" .. width .. ";display:block;'><span style='height:" .. width .. ";width:" .. width .. ";display:inline-block;'></span><br>x0</span>" .. quantity .. "</div>"
end
end
return output .. "</div>"
return output .. "</div>"

Revision as of 01:24, 10 June 2022

This module standardizes the display of crafting recipes and makes it easy to display a specified item's recipe or all recipes that use a specified ingredient. See Template:RecipeNew for more information. Thanks to BryghtShadow for helping out with the loops!


--[[
	This module standardizes the display of crafting recipes
	and makes it easy to display a specified item's recipe
	or all recipes that use a specified ingredient.
	Thanks to User:BryghtShadow for helping out with the loops!
--]]

-- <nowiki>
local p = {}
local dataSN = mw.loadData('Module:Recipe/SN')
-- local dataBZ = mw.loadData('Module:Recipe/BZ')
local getArgs = require('Dev:Arguments').getArgs
local width = "50px"

function p.getItemID(id, data)
	-- removing disambiguations from pagenames
	id = string.gsub(id, " %(Subnautica%)", "")
	id = string.gsub(id, " %(Below Zero%)", "")
	
	-- if item ID is provided directly...
	if data[id] then
		return id
	end
	
	-- ...otherwise try to find by item name
	for k, v in pairs(data) do
		if v.name == id then -- item found
			return k
		end
	end
	
	return nil -- Could not find item
end

function p.SN(frame)
	local args = getArgs(frame)
	local output = "<div style='display:flex;flex-wrap:wrap;'>"
	
	-- First, get the ID of the product item provided by template; pagename used by default
	local id = args[1] or mw.title.getCurrentTitle().text
	local itemID = p.getItemID(id, dataSN)
	if not itemID then error("Could not find recipe for item '" .. id .. "'") end
	
	-- Item is found, start processing it
	if dataSN[itemID].original then
		-- If the item is made as part of a different item's recipe
		error ("No standalone recipe for item '" .. itemID .. "' (use '" .. dataSN[itemID].original .. "' instead)")
	else
		if dataSN[itemID].recipe then
			-- Recipe is found, start creating the output
			for i,v in ipairs(dataSN[itemID].recipe) do
				local name = dataSN[v[1]].name
				
				local image = dataSN[v[1]].image
				local iconBG
				if not image then
					image = dataSN[v[1]].name .. " Icon.png"
					if dataSN[v[1]].icon then
						local iconBGSize = dataSN[v[1]].icon[size] or "1x1"
						local iconBGType = dataSN[v[1]].icon[bg] or "default"
						iconBG = iconBGSize .. " " .. iconBGType
					else
						iconBG = "1x1 default"
					end
				end
				
				local quantity = ""
				if v[2] > 1 then
					quantity = "<span style='display:block;position:absolute;top:" .. width .. ";text-align:center;width:100%;'>x" .. v[2] .. "</span>"
				end
				
				output = output .. "<div style='position:relative;width:" .. width .. ";'>"
				
				if iconBG then
					output = output .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. iconBG .. " bg.png|" .. width .. "|link=]]</span>"
				end
				
				output = output .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. image .. "|" .. width .. "|link=" .. name .. "|" .. name .. "]]</span><span style='visibility:hidden;width:" .. width .. ";display:block;'><span style='height:" .. width .. ";width:" .. width .. ";display:inline-block;'></span><br>x0</span>" .. quantity .. "</div>"
			end
			return output .. "</div>"
		else
			-- If there is no recipe for the item
			error ("Cannot craft item '" .. itemID .. "' (use Template:Uses instead)")
		end
	end
end

return p
-- </nowiki>