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='/images/6/64/RecipeBG_Crafting_2x2.png', Slots={TL={X=112, Y=66}, TR={X=184, Y=66}, BL={X=112, Y=138}, BR={X=184, Y=138}, O={X=416, Y=104}}},
        ['Shaped3x3'] = {Img='/images/3/33/RecipeBG_Crafting_3x3.png'},
        ['Shapeless'] = {Img='/images/7/71/RecipeBG_Crafting_Shapeless.png'},
        ['Furnace'] = {Img='/images/a/aa/RecipeBG_Furnace.png'},
        ['RC_Worktable'] = {Img='/images/2/29/RecipeBG_Worktable.png'}
    }

    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 IconName = frame:getArgument('icon'):expand()
    --if not IconName then IconName = 'none' end
    --
    --local IconSize = tonumber(frame:getArgument('size'):expand())
    --if not IconSize then IconSize = DEFAULT_ICON_SIZE end
    --
    --local LinkDest = frame:getArgument('link'):expand()
    --if not LinkDest then LinkDest = '' end
    --
    --local LocationFromTable = DataTable.Locations[IconName]
    --if not LocationFromTable then LocationFromTable = { X = 0, Y = 0 } end
    --
    --local SpriteX = LocationFromTable.X
    --local SpriteY = LocationFromTable.Y
    --
    --local IconScale = IconSize / SHEET_ICON_SIZE
    --local ImageXOffset = SpriteX * SHEET_ICON_SIZE * IconScale * -1
    --local ImageYOffset = SpriteY * SHEET_ICON_SIZE * IconScale * -1
    --local ScaledSheetSizeX = SHEET_SIZE_X * IconScale
    --local ScaledSheetSizeY = SHEET_SIZE_Y * IconScale

    local ParentDiv = mw.html.create('div')
    ParentDiv:addClass(DIV_CLASS)
    ParentDiv:cssText('position: relative; top: 0; left: 0;')

    local ContainerImg = ParentDiv:tag('img')
    --ContainerImg:addClass()
    ContainerImg:attr('src', ToolData.Img)
    ContainerImg:cssText('image-rendering: crisp-edges; image-rendering: pixelated; position: relative; top: 0; left: 0;')
    ContainerImg: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
            local SlotImg = ParentDiv:tag('img')
            SlotImg:attr('src', '/images/6/6d/CopperOre_IC2_256.png')
            SlotImg:cssText(
                'position: absolute;' ..
                'left: ' .. SlotData.X .. 'px;' ..
                'top: ' .. SlotData.Y .. 'px;' ..
                'width: ' .. ICON_SIZE .. 'px;' ..
                'height: ' .. ICON_SIZE .. 'px;'
            )
            SlotImg:attr('testinfo', SlotContent)
            SlotImg:done()
        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