Toggle menu
66
63
2
559
Fisk's Superheroes Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Fabricator

From Fisk's Superheroes Wiki

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

local p = {}

local function isUrl(value)
    return type(value) == "string" and value:match("^https?://")
end

local function isHtmlImg(value)
    return type(value) == "string" and value:match("^<img")
end

local function resolveImage(value)
    if not value then return nil end

    if isUrl(value) then
        if not value:match("[?&]dl=1") then
            if value:match("%?") then
                value = value .. "&dl=1"
            else
                value = value .. "?dl=1"
            end
        end
        return value
    end

    if isHtmlImg(value) then
        return value
    end

    return mw.getCurrentFrame():callParserFunction("filepath", value)
end

local function split(str, sep)
    if (str == nil) then return {} end
    local result = {}
    for match in (str..sep):gmatch("([^"..sep.."]+)") do
        table.insert(result, match)
    end
    return result
end

function p.gui(frame)
    local args = frame.args
    local scale = tonumber(args.scale) or 1
    
    local html = mw.html.create("div")
        :attr("class", "frame")
        :css{
            position = "relative",
            width = 166 * scale .. "px",
            height = 48 * scale .. "px",
            display = "inline-block",
            overflow = "hidden"
        }

    html:tag("img")
        :attr("src", resolveImage("Fabricator.png"))
        :css{
            ["image-rendering"] = "pixelated",
            width = "100%",
            height = "100%",
            display = "block"
        }

    for i = 1, 10 do
        local item = args["I"..i]

        if item then
            local itemParts = split(item, ";")
            local amount = itemParts[2] or 1
            local filename = itemParts[1]

            local xOffset
            if i == 1 or i == 6 then
                xOffset = 30 * scale
            else
                xOffset = (30 + ((i - 1) % 5) * 18) * scale
            end

            local yOffset = (i > 5) and (25 * scale) or (7 * scale)

            local container = mw.html.create("div")
                :attr("class", "item")
                :css{
                    position = "absolute",
                    left = xOffset .. "px",
                    top = yOffset .. "px",
                    width = 16 * scale .. "px",
                    height = 16 * scale .. "px"
                }

            container:tag("img")
                :attr("src", resolveImage(filename))
                :css{
                    ["image-rendering"] = "pixelated",
                    width = 16 * scale .. "px",
                    height = 16 * scale .. "px"
                }

            container:tag("span")
                :attr("class", "micro-5-regular")
                :wikitext(amount)
                :css{
                    position = "absolute",
                    right = 0,
                    bottom = 0,
                    ["font-size"] = (12 * scale) .. "px",
                    ["line-height"] = (12 * scale) - (scale * 4) .. "px",
                    color = "#FFFFFF",
                    ["-webkit-text-stroke"] = math.max(1, scale / 2) .. "px #000000",
                }

            html:node(container)
        end
    end

    if args.output then
        html:tag("img")
            :attr("src", resolveImage(args.output))
            :css{
                position = "absolute",
                left = 142 * scale .. "px",
                top = 16 * scale .. "px",
                width = 16 * scale .. "px",
                height = 16 * scale .. "px",
                ["image-rendering"] = "pixelated",
            }
    end

    html:tag("img")
        :attr("src", resolveImage(args.drive or "Suit_Data_Drive_Black_Active.gif"))
        :css{
            position = "absolute",
            left = 8 * scale .. "px",
            top = 7 * scale .. "px",
            width = 16 * scale .. "px",
            height = 16 * scale .. "px",
            ["image-rendering"] = "pixelated",
        }

    html:tag("img")
        :attr("src", resolveImage(args.core or "Suit_Core.png"))
        :css{
            position = "absolute",
            left = 8 * scale .. "px",
            top = 25 * scale .. "px",
            width = 16 * scale .. "px",
            height = 16 * scale .. "px",
            ["image-rendering"] = "pixelated",
        }

    local tooltip = mw.html.create("div")
        :attr("id", "item-tooltip")
        :css{ display = "none" }

    tooltip:tag("span")
        :attr("class", "micro-5-regular")
        :css{
            ["font-size"] = (12 * scale) .. "px",
            ["line-height"] = (12 * scale) - (scale * 4) .. "px",
            color = "#FFFFFF"
        }
        :wikitext("Test")

    html:node(tooltip)

    return frame:extensionTag("html", tostring(html))
end

return p