13 Articles
Project Nakagami

Module:Recipe: Difference between revisions

No edit summary
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 16: Line 16:
     local IMAGE_MAPPING =
     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}}},
         ['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', 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}}},
         ['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', 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}}},
         ['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'},
         ['Furnace'] = {Img='RecipeBG_Furnace.png'},
         ['RC_Worktable'] = {Img='RecipeBG_Worktable.png'}
         ['RC_Worktable'] = {Img='RecipeBG_Worktable.png'}
Line 37: 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]


Line 47: Line 48:
         :wikitext('[[file:' .. ToolData.Img .. '|link=' .. ']]')
         :wikitext('[[file:' .. ToolData.Img .. '|link=' .. ']]')
         :done()
         :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 SlotContent = nil
         local SlotContent = nil
         if SlotName == 'O' then SlotContent = RecipeName:match("^([^^]+)") else SlotContent = RecipeData[SlotName] end
         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
         if SlotContent then
             SlotContent = string.gsub(SlotContent, '%#%*', '#0') -- TODO: This may need to be improved, currently just replacing all "any metadata" with 0.
             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 IconCSS = 'position: absolute;' .. 'left: ' .. SlotData.X .. 'px;' .. 'top: ' .. SlotData.Y .. 'px;'
             local IconHTML = ItemIconModule.AddSpriteInternal(SlotContent, ICON_SIZE, '', '')
             local IconHTML = ItemIconModule.AddSpriteInternal(SlotContent, ICON_SIZE, '')
             local IconDiv = ParentDiv:tag('div')
             local IconDiv = ParentDiv:tag('div')
             IconDiv:addClass('item-tooltip-parent')
             IconDiv:addClass('item-tooltip-parent')
Line 63: Line 83:
             local Tooltip = IconDiv:tag('span')
             local Tooltip = IconDiv:tag('span')
             Tooltip:addClass('item-tooltip-text')
             Tooltip:addClass('item-tooltip-text')
             local ItemText = ItemData.Locations[SlotContent].N
             local ItemText = nil
            if ItemData.Locations[SlotContent] then ItemText = ItemData.Locations[SlotContent].N end
             if not ItemText then ItemText = SlotContent end
             if not ItemText then ItemText = SlotContent end
             Tooltip:wikitext(ItemText)
             Tooltip:wikitext(ItemText)
Line 76: Line 97:
                 SlotContent = string.gsub(SlotContent, '%#%*', '#0') -- TODO: This may need to be improved, currently just replacing all "any metadata" with 0.
                 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 IconCSS = 'position: absolute;' .. 'left: ' .. SlotData.X .. 'px;' .. 'top: ' .. SlotData.Y .. 'px;'
                 local IconHTML = ItemIconModule.AddSpriteInternal(SlotContent, ICON_SIZE, '', IconCSS)
                 local IconHTML = ItemIconModule.AddSpriteInternal(SlotContent, ICON_SIZE, '')
                 ParentDiv:node(IconHTML)
                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