13 Articles
Project Nakagami

Module:Recipe: Difference between revisions

(Might work better if I actually return something)
No edit summary
 
(25 intermediate revisions by the same user not shown)
Line 16: Line 16:
     local IMAGE_MAPPING =
     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}}},
         ['Shaped2x2'] = {Img='RecipeBG_Crafting_2x2.png', Exp={X=18, Y=56, W=84, H=91, T='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='/images/3/33/RecipeBG_Crafting_3x3.png'},
         ['Shaped3x3'] = {Img='RecipeBG_Crafting_3x3.png', Exp={X=18, Y=90, W=84, H=91, T='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='/images/7/71/RecipeBG_Crafting_Shapeless.png'},
         ['Shapeless'] = {Img='RecipeBG_Crafting_Shapeless.png', Exp={X=18, Y=102, W=84, H=166, T='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='/images/a/aa/RecipeBG_Furnace.png'},
         ['Furnace'] = {Img='RecipeBG_Furnace.png'},
         ['RC_Worktable'] = {Img='/images/2/29/RecipeBG_Worktable.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)
     local DataTable = mw.loadData('Module:' .. DATA_MODULE)
Line 33: Line 37:


     local ToolData = IMAGE_MAPPING[RecipeTool]
     local ToolData = IMAGE_MAPPING[RecipeTool]
    if not ToolData then return 'RECIPE TOOL INVALID: "' .. RecipeTool .. '"' end
     local RecipeData = DataTable[RecipeTool][RecipeName]
     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')
     local ParentDiv = mw.html.create('div')
     ParentDiv:addClass(DIV_CLASS)
     ParentDiv:addClass(DIV_CLASS)
     ParentDiv:cssText('position: relative; top: 0; left: 0;')
     ParentDiv:cssText('position: relative; top: 0; left: 0; image-rendering: crisp-edges; image-rendering: pixelated;')


     local ContainerImg = ParentDiv:tag('img')
     ParentDiv:tag('div')
    --ContainerImg:addClass()
        :cssText('position: relative; top: 0; left: 0;')
    ContainerImg:attr('src', ToolData.Img)
        :wikitext('[[file:' .. ToolData.Img .. '|link=' .. ']]')
     ContainerImg:cssText('image-rendering: crisp-edges; image-rendering: pixelated; position: relative; top: 0; left: 0;')
        :done()
    ContainerImg:done()
      
    local ExplanationData = ToolData.Exp
    if ExplanationData then
        ParentDiv:tag('div')
            :addClass('item-tooltip-parent')
            :cssText('position: absolute;' .. 'left: ' .. ExplanationData.X .. 'px;' .. 'top: ' .. ExplanationData.Y .. 'px;' .. 'width: ' .. ExplanationData.W .. 'px;' .. 'height: ' .. ExplanationData.H .. 'px;')
            :tag('span')
                :addClass('item-tooltip-text')
                :wikitext(ExplanationData.T)
                :done()
            :done()
    end


     for SlotName,SlotData in pairs(ToolData.Slots) do
     for SlotName,SlotData in pairs(ToolData.Slots) do
         local SlotImg = ParentDiv:tag('img')
         local SlotContent = nil
        SlotImg:attr('src', '/images/6/6d/CopperOre_IC2_256.png')
        if SlotName == 'O' then
        SlotImg:cssText(
            SlotContent = RecipeName:match("^([^^]+)")
             'position: absolute;' ..
            if RecipeData.Q ~= 1 then
            'left: ' .. SlotData.X .. 'px;' ..
                local OutCountDiv = ParentDiv:tag('div')
            'top: ' .. SlotData.Y .. 'px;'
                OutCountDiv:cssText('position: absolute; left: ' .. SlotData.X .. 'px; top:' .. SlotData.Y .. 'px; width: ' .. ICON_SIZE .. 'px; height: ' .. ICON_SIZE .. 'px;')
        )
                OutCountDiv:tag('span'):cssText('position: absolute; right: 0px; bottom: 0px; text-align: right; font-size: 24px; line-height: 24px; font-weight:bold; z-index: 2; text-shadow: 3px 3px black;'):wikitext(RecipeData.Q):done()
        SlotImg:attr('testinfo', RecipeData[SlotName])
            end
        SlotImg:done()
        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, '')
            local IconDiv = ParentDiv:tag('div')
            IconDiv:addClass('item-tooltip-parent')
            IconDiv:cssText(IconCSS)
            IconDiv:node(IconHTML)
           
            local Tooltip = IconDiv:tag('span')
            Tooltip:addClass('item-tooltip-text')
            local ItemText = nil
            if ItemData.Locations[SlotContent] then ItemText = ItemData.Locations[SlotContent].N end
            if not ItemText then ItemText = SlotContent end
            Tooltip:wikitext(ItemText)
        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, '')
                local IconDiv = ParentDiv:tag('div')
                IconDiv:addClass('item-tooltip-parent')
                IconDiv:cssText(IconCSS)
                IconDiv:node(IconHTML)
               
                local Tooltip = IconDiv:tag('span')
                Tooltip:addClass('item-tooltip-text')
                local ItemText = ItemData.Locations[SlotContent].N
                if not ItemText then ItemText = SlotContent end
                Tooltip:wikitext(ItemText)
            end
        end
     end
     end



Latest revision as of 06:09, 16 August 2024

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', Exp={X=18, Y=56, W=84, H=91, T='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', Exp={X=18, Y=90, W=84, H=91, T='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', Exp={X=18, Y=102, W=84, H=166, T='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]
    if not ToolData then return 'RECIPE TOOL INVALID: "' .. RecipeTool .. '"' end
    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()
    
    local ExplanationData = ToolData.Exp
    if ExplanationData then
        ParentDiv:tag('div')
            :addClass('item-tooltip-parent')
            :cssText('position: absolute;' .. 'left: ' .. ExplanationData.X .. 'px;' .. 'top: ' .. ExplanationData.Y .. 'px;' .. 'width: ' .. ExplanationData.W .. 'px;' .. 'height: ' .. ExplanationData.H .. 'px;')
            :tag('span')
                :addClass('item-tooltip-text')
                :wikitext(ExplanationData.T)
                :done()
            :done()
    end

    for SlotName,SlotData in pairs(ToolData.Slots) do
        local SlotContent = nil
        if SlotName == 'O' then
            SlotContent = RecipeName:match("^([^^]+)")
            if RecipeData.Q ~= 1 then
                local OutCountDiv = ParentDiv:tag('div')
                OutCountDiv:cssText('position: absolute; left: ' .. SlotData.X .. 'px; top:' .. SlotData.Y .. 'px; width: ' .. ICON_SIZE .. 'px; height: ' .. ICON_SIZE .. 'px;')
                OutCountDiv:tag('span'):cssText('position: absolute; right: 0px; bottom: 0px; text-align: right; font-size: 24px; line-height: 24px; font-weight:bold; z-index: 2; text-shadow: 3px 3px black;'):wikitext(RecipeData.Q):done()
            end
        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, '')
            local IconDiv = ParentDiv:tag('div')
            IconDiv:addClass('item-tooltip-parent')
            IconDiv:cssText(IconCSS)
            IconDiv:node(IconHTML)
            
            local Tooltip = IconDiv:tag('span')
            Tooltip:addClass('item-tooltip-text')
            local ItemText = nil
            if ItemData.Locations[SlotContent] then ItemText = ItemData.Locations[SlotContent].N end
            if not ItemText then ItemText = SlotContent end
            Tooltip:wikitext(ItemText)
        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, '')
                local IconDiv = ParentDiv:tag('div')
                IconDiv:addClass('item-tooltip-parent')
                IconDiv:cssText(IconCSS)
                IconDiv:node(IconHTML)
                
                local Tooltip = IconDiv:tag('span')
                Tooltip:addClass('item-tooltip-text')
                local ItemText = ItemData.Locations[SlotContent].N
                if not ItemText then ItemText = SlotContent end
                Tooltip:wikitext(ItemText)
            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