13 Articles
Project Nakagami

Module:Recipe

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

local p = {} --package

-- Allowed Arguments:
-- icon: The name of the icon to show, defined in the image index.
-- size: The size, in pixels, to use for the icon on the page.
-- link: The page to link to if the icon is clicked


-- recipe: The name of the recipe to show, defined in Module:RecipeData
-- tool: The name of the tool used to execute the recipe, listed below and equal to the sections in Module:RecipeData
function p.AddRecipe(frame)
    local ICON_SIZE = 64 -- how big item icons should be
    local DIV_CLASS = 'recipe-container' -- the class attribute on the parent div container
    local DATA_MODULE = 'RecipeData' -- the Module page containing the data table
    local DEFAULT_TOOL = 'Shaped3x3'
    local IMAGE_MAPPING =
    {
        ['Shaped2x2'] = {Img='RecipeBG_Crafting_2x2.png', Alt='Regular 2x2 Crafting Recipe', Slots={TL={X=112, Y=32}, TR={X=184, Y=32}, BL={X=112, Y=104}, BR={X=184, Y=104}, O={X=416, Y=70}}},
        ['Shaped3x3'] = {Img='RecipeBG_Crafting_3x3.png', Alt='Regular 3x3 Crafting Recipe', Slots={TL={X=112, Y=32}, TC={X=184, Y=32}, TR={X=256, Y=32}, ML={X=112, Y=104}, MC={X=184, Y=104}, MR={X=256, Y=104}, BL={X=112, Y=176}, BC={X=184, Y=176}, BR={X=256, Y=176}, O={X=488, Y=104}}},
        ['Shapeless'] = {Img='RecipeBG_Crafting_Shapeless.png', Alt='Regular Shapeless Crafting Recipe', SlotsIndexed={[1]={X=112, Y=32}, [2]={X=196, Y=32}, [3]={X=280, Y=32}, [4]={X=112, Y=116}, [5]={X=196, Y=116}, [6]={X=280, Y=116}, [7]={X=112, Y=200}, [8]={X=196, Y=200}, [9]={X=280, Y=200}}, Slots={O={X=512, Y=116}}},
        ['Furnace'] = {Img='RecipeBG_Furnace.png'},
        ['RC_Worktable'] = {Img='RecipeBG_Worktable.png'}
    }

    local ItemIconModule = require('Module:ItemIcon')
    local ItemData = mw.loadData('Module:ItemIconData')
    if not ItemData then ItemData = {} end

    local DataTable = mw.loadData('Module:' .. DATA_MODULE)
    if not DataTable then DataTable = {} end

    local RecipeName = frame:getArgument('recipe'):expand()
    if not RecipeName then RecipeName = 'none' end

    local RecipeTool = frame:getArgument('tool'):expand()
    if not RecipeTool then RecipeTool = DEFAULT_TOOL end

    local ToolData = IMAGE_MAPPING[RecipeTool]
    local RecipeData = DataTable[RecipeTool][RecipeName]

    local ParentDiv = mw.html.create('div')
    ParentDiv:addClass(DIV_CLASS)
    ParentDiv:cssText('position: relative; top: 0; left: 0; image-rendering: crisp-edges; image-rendering: pixelated;')

    ParentDiv:tag('div')
        :cssText('position: relative; top: 0; left: 0;')
        :wikitext('[[file:' .. ToolData.Img .. '|link=' .. ']]')
        :done()

    for SlotName,SlotData in pairs(ToolData.Slots) do
        local SlotContent = nil
        if SlotName == 'O' then SlotContent = RecipeName:match("^([^^]+)") else SlotContent = RecipeData[SlotName] end

        if SlotContent then
            SlotContent = string.gsub(SlotContent, '%#%*', '#0') -- TODO: This may need to be improved, currently just replacing all "any metadata" with 0.
            local IconCSS = 'position: absolute;' .. 'left: ' .. SlotData.X .. 'px;' .. 'top: ' .. SlotData.Y .. 'px;'
            local IconHTML = ItemIconModule.AddSpriteInternal(SlotContent, ICON_SIZE, '', IconCSS)
            local IconDiv = ParentDiv:tag('div')
            IconDiv:class('item-tooltip-parent')
            IconDiv:node(IconHTML)
            
            local Tooltip = IconDiv:tag('span')
            Tooltip:class('item-tooltip-text')
            Tooltip:wikitext('Hello this is a tooltip')
        end
    end

    if ToolData.SlotsIndexed then
        for SlotNumber,SlotData in pairs(ToolData.SlotsIndexed) do
            local SlotContent = RecipeData.I[SlotNumber]

            if SlotContent then
                SlotContent = string.gsub(SlotContent, '%#%*', '#0') -- TODO: This may need to be improved, currently just replacing all "any metadata" with 0.
                local IconCSS = 'position: absolute;' .. 'left: ' .. SlotData.X .. 'px;' .. 'top: ' .. SlotData.Y .. 'px;'
                local IconHTML = ItemIconModule.AddSpriteInternal(SlotContent, ICON_SIZE, '', IconCSS)
                ParentDiv:node(IconHTML)
            end
        end
    end

    return tostring(ParentDiv)

    --if LinkDest ~= '' then
    --    if LinkDest:find('//') then -- External Link
    --        return '[' .. LinkDest .. ' ' .. tostring(SpriteHTML) .. ']'
    --    else -- Internal Link
    --        return '[[' .. LinkDest .. '|' .. tostring(SpriteHTML) .. ']]'
    --    end
    --else
    --    return tostring(SpriteHTML)
    --end
end

return p