Module:Recipe: Difference between revisions
SlyAceZeta (talk | contribs) (attempting to move icon creation to its own function; adding big width for machine and output; attempting to fix icon properties) |
SlyAceZeta (talk | contribs) (Smarter handling of icon heights) |
||
Line 12: | Line 12: | ||
local getArgs = require('Dev:Arguments').getArgs |
local getArgs = require('Dev:Arguments').getArgs |
||
local width = "50px" |
local width = "50px" |
||
local widthBig = " |
local widthBig = "75px" |
||
function p.getItemID(id, data) |
function p.getItemID(id, data) |
||
Line 41: | Line 41: | ||
iconWidth = widthBig |
iconWidth = widthBig |
||
end |
end |
||
local iconHeight = iconWidth |
|||
local name = data[item].name |
local name = data[item].name |
||
Line 52: | Line 53: | ||
local iconBGType = data[item].icon.bg or "default" |
local iconBGType = data[item].icon.bg or "default" |
||
iconBG = iconBGSize .. " " .. iconBGType |
iconBG = iconBGSize .. " " .. iconBGType |
||
if iconBGSize == "1x2" then |
|||
iconHeight = iconWidth * 2 |
|||
elseif iconBGSize == "3x2" then |
|||
iconHeight = (iconWidth / 3) * 2 |
|||
elseif iconBGSize == "2x3" then |
|||
iconHeight = (iconWidth / 2) * 3 |
|||
end |
|||
else |
else |
||
iconBG = "1x1 default" |
iconBG = "1x1 default" |
||
Line 57: | Line 65: | ||
end |
end |
||
iconOut = |
iconOut = "<div style='position:relative;width:" .. iconWidth .. ";'>" |
||
if iconBG then |
if iconBG then |
||
Line 63: | Line 71: | ||
end |
end |
||
iconOut = iconOut .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. image .. "|" .. iconWidth .. "|link=" .. name .. "|" .. name .. "]]</span><span style='visibility:hidden;width:" .. iconWidth .. ";display:block;'><span style='height:" .. |
iconOut = iconOut .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. image .. "|" .. iconWidth .. "|link=" .. name .. "|" .. name .. "]]</span><span style='visibility:hidden;width:" .. iconWidth .. ";display:block;'><span style='height:" .. iconHeight .. ";width:" .. iconWidth .. ";display:inline-block;'></span><br>x0</span>" |
||
if quantity > 1 then |
if quantity > 1 then |
||
iconOut = iconOut .. "<span style='display:block;position:absolute;top:" .. |
iconOut = iconOut .. "<span style='display:block;position:absolute;top:" .. iconHeight .. ";text-align:center;width:100%;'>x" .. quantity .. "</span>" |
||
end |
end |
||
Revision as of 02:07, 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"
local widthBig = "75px"
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.createIcon(item, quantity, data, mid)
local iconOut = ""
local iconWidth = width
if not mid then
iconWidth = widthBig
end
local iconHeight = iconWidth
local name = data[item].name
local image = data[item].image
local iconBG
if not image then
image = name .. " Icon.png"
if data[item].icon then
local iconBGSize = data[item].icon.size or "1x1"
local iconBGType = data[item].icon.bg or "default"
iconBG = iconBGSize .. " " .. iconBGType
if iconBGSize == "1x2" then
iconHeight = iconWidth * 2
elseif iconBGSize == "3x2" then
iconHeight = (iconWidth / 3) * 2
elseif iconBGSize == "2x3" then
iconHeight = (iconWidth / 2) * 3
end
else
iconBG = "1x1 default"
end
end
iconOut = "<div style='position:relative;width:" .. iconWidth .. ";'>"
if iconBG then
iconOut = iconOut .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. iconBG .. " bg.png|" .. iconWidth .. "|link=]]</span>"
end
iconOut = iconOut .. "<span style='position:absolute;left:0;top:0;'>[[File:" .. image .. "|" .. iconWidth .. "|link=" .. name .. "|" .. name .. "]]</span><span style='visibility:hidden;width:" .. iconWidth .. ";display:block;'><span style='height:" .. iconHeight .. ";width:" .. iconWidth .. ";display:inline-block;'></span><br>x0</span>"
if quantity > 1 then
iconOut = iconOut .. "<span style='display:block;position:absolute;top:" .. iconHeight .. ";text-align:center;width:100%;'>x" .. quantity .. "</span>"
end
return iconOut .. "</div>"
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 icon = p.createIcon(v[1], v[2], dataSN, true)
output = output .. icon
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>