モジュール:Gui

提供:Minecraft Japan Wiki
ナビゲーションに移動 検索に移動

このモジュールについての説明文ページを モジュール:Gui/doc に作成できます

local p = {}
function p.base(f)
    local args = f
    if f == mw.getCurrentFrame() then
        args = require('Module:ProcessArgs').merge(true)
    else
        f = mw.getCurrentFrame()
    end

    local jsprite = require('Module:JSprite')
    local animate = require('Module:AnimateFrame')

    local gui_tables = mw.loadData('Module:GUISheet') or {}

    local table_name = string.gsub(string.lower(args[1]), " ", "-")

    local table_setting

    for i, v in pairs(gui_tables) do
        if v.name == table_name then
            table_setting = v
            break
        end
    end

    if not table_setting then
        error("Module:GUI: '" .. args[1] .. "' has not found !!")
    end

    ------ mw.text.split is very slow -------
    local function split(inputstr, sep)
        if sep == nil then
            sep = "%s"
        end
        local t = {}
        for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
            table.insert(t, str)
        end
        return t
    end

    ------ mw.text.trim is very slow -------
    local function trim(s)
        return (s:gsub('^[\t\r\n\f ]+', ''):gsub('[\t\r\n\f ]+$', ''))
    end


    ------ generate sprite -------
    local function generateSlot(slot, num)
        local amount = num or 1

        if string.match(slot, '^.+%*%-?%d+$') then
            local tmp = split(slot, "*")
            if #tmp == 2 then
                slot = tmp[1]
                amount = tonumber(tmp[2])
            end
        end

        if mw.text.trim(slot or '') ~= 'blank' then

            if mw.ustring.match(slot, "[%w%s'(),:]+") ~= slot then
                slot = mw.text.trim(slot or '')
            else
                local sprite = { args = {} }

                local sprite_other = string.find(slot, ":")
                local isPotion = false --string.find(string.lower(slot), "potion of")

                if sprite_other or isPotion then
                    if sprite_other then
                        sprite.title = string.sub(slot, 1, sprite_other - 1)
                        sprite.args[1] = string.sub(slot, sprite_other + 1)
                    else
                        sprite.title = "EnchantSprite"
                        sprite.args[1] = slot
                    end

                    if not mw.title.new("Template:" .. sprite.title).exists and mw.title.new("Template:" .. sprite.title .. "Sprite").exists then
                        sprite.title = sprite.title .. "Sprite"
                    end
                    slot = f:expandTemplate(sprite)
                else
                    sprite.args[1] = slot
                    sprite.args.sheet = "InvSprite"

                    slot = jsprite.base(sprite.args)
                end
            end
        else
            slot = ''
        end

        local sub = mw.html.create("span")
        sub:addClass("gui-slot")
        sub:wikitext(slot)

        if amount ~= 1 or args['num'] then
            local sub_overlay = sub:tag("span")
            sub_overlay:addClass('gui-nfr')

            local num_len = string.len(tostring(amount))

            for j = 1, num_len, 1 do
            	local chr = string.sub(tostring(amount), j, j)
                local num = tonumber(chr) or 10
                local overlay_num = sub_overlay:tag("span")
                overlay_num
                    :addClass('sprite')
                    :addClass('gui-num')
                    :css("background-position", "-" .. tostring(num * 12) .. "px -0px")
            end
        end

        return sub
    end

    -------------------------

    div = mw.html.create("span")
    div:addClass("gui-frm")
    if args['nopad'] or (table_setting.nopad and not args['nopad']) then
        div:css("padding", '0')
    end
    if args['width'] then
        div:css("width", args['width'])
    end
    if args['height'] then
        div:css("height", args['height'])
    end

    local gui = div:tag("span")
    gui
        :addClass('sprite')
        :addClass('gui-sheet')
        :css("width", tostring(table_setting.width * 2) .. "px")
        :css("height", tostring(table_setting.height * 2) .. "px")

    if table_setting.x ~= 0 or table_setting.y ~= 0 then
        gui:css("background-position",
            "-" .. tostring(table_setting.x * 2) .. "px -" .. tostring(table_setting.y * 2) .. "px")
    end

    if table_setting.gif then
        local gif = gui:tag("span")
        gif
            :addClass("gui-slot")
            :css("padding", "0")
            :css("left", tostring(table_setting.gif.x * 2) .. "px")
            :css("top", tostring(table_setting.gif.y * 2) .. "px")
            :wikitext(f:preprocess('[[File:' .. table_setting.gif.img .. '|link=|]]'))
    end

    if args['shapeless'] and table_setting.sgif then
        local gif = gui:tag("span")
        gif
            :addClass("gui-slot")
            :css("padding", "0")
            :css("left", tostring(table_setting.sgif.x * 2) .. "px")
            :css("top", tostring(table_setting.sgif.y * 2) .. "px")
            :css("cursor", "help")
            :wikitext(f:preprocess('[[File:' .. table_setting.sgif.img .. '|link= |配置不問]]'))
    end

    local slots = table_setting.slots
    local slot_count = table_setting.slotcount or 1

    local hasAnimate = false
    local max_animation = 1
    local slotlist = {}
    local textlist = {}

    ------ アニメーションの最大フレーム数を数える --------
    for i = 1, slot_count, 1 do
        local slot = args["slot" .. tostring(i)]
        slotlist[i] = {}
        if i == slot_count and not slot then
            slot = args["target"]
        end
        if table_setting.blank_icons and (not slot) and table_setting.blank_icons[i] then
            slot = "SlotSprite:" .. table_setting.blank_icons[i]
        end
        if slot then
            if not slot:match("<.*>") and string.find(slot, ";") then
                slotlist[i] = split(slot, ";")
                if # slotlist[i] > max_animation then
                    hasAnimate = true
                    max_animation = # slotlist[i]
                end
            else
                slotlist[i] = { slot }
            end
        end
    end

    if table_setting.textpos then
        local title = (args["text"] or args["target"]) or args["slot1"]
        if title and mw.ustring.match(title, "^[%w%s'(),;]+$") then
            local items = split(title, ";")

            for i, v in pairs(items) do
                textlist[i] = trim(items[i] or '')
                if # textlist > max_animation then
                    hasAnimate = true
                    max_animation = # textlist
                end
            end
        end
    end

    -------- 描画開始 ----------

    -- anvil text ---

    if table_setting.textpos and #textlist == 1 then
        local title = textlist[1]
        local text = gui:tag("span")
        text
            :addClass("gui-text")
            :addClass("minetext")
            :css("left", tostring(table_setting.textpos.x * 2) .. "px")
            :css("top", tostring(table_setting.textpos.y * 2) .. "px")
            :wikitext(title)
    end

    -- slots

    for i = 1, slot_count, 1 do
        if #slotlist[i] == 1 then
            local slot = args["slot" .. tostring(i)]

            if i == slot_count and not slot then
                slot = args["target"]
            end

            if mw.text.trim(slot or '') ~= 'blank' then

                if table_setting.blank_icons and (not slot) and table_setting.blank_icons[i] then
                    slot = "SlotSprite:" .. table_setting.blank_icons[i]
                end

                if slot then
                    local amount = 1
                    if i == slot_count then
                        amount = tonumber(args["amount"]) or 1
                    end

                    local sub = generateSlot(slot, amount)

                    if slots[i].x ~= 0 then
                        sub:css("left", tostring(slots[i].x * 2) .. "px")
                    end

                    if slots[i].y ~= 0 then
                        sub:css("top", tostring(slots[i].y * 2) .. "px")
                    end

                    gui:node(sub)
                end
            end
        end
    end

    --------------------------------------
    if hasAnimate then
        local anim_frames = {}

        for frame_no = 1, max_animation, 1 do
            anim_frames[frame_no] = ""

            ------ Anvil text -------

            if table_setting.textpos and #textlist > 1 then
                local length = # textlist

                local idx = math.floor(length * ((frame_no - 1) / max_animation)) + 1
                if max_animation % length == 0 then
                    idx = ((frame_no-1) % length) + 1
                end

                if textlist[idx] then
                    local text = mw.html.create("span")
                    text
                        :addClass("gui-text")
                        :addClass("minetext")
                        :css("left", tostring(table_setting.textpos.x * 2) .. "px")
                        :css("top", tostring(table_setting.textpos.y * 2) .. "px")

                    text:wikitext(
                        textlist[idx]
                    )

                    anim_frames[frame_no] = anim_frames[frame_no] .. tostring(text)
                end
            end

            ------- Slot --------

            for i = 1, slot_count, 1 do
                local length = # slotlist[i]

                if length > 1 then
                    -- スロットの数が異なる場合でも、最大値のアニメーション数の割合でフレーム番号を算出
                    -- 対象フレーム番号 = 対象スロットの最大アニメーション * (現在のフレーム番号 ÷ 最大値のアニメーション数)
                    local idx = math.floor(length * ((frame_no - 1) / max_animation)) + 1
                    if max_animation % length == 0 then
                        idx = ((frame_no-1) % length) + 1
                    end

                    slot = slotlist[i][idx]

                    if slot then
                        local amount = 1
                        if i == slot_count then
                            amount = tonumber(args["amount"]) or 1
                        end

                        local sub = generateSlot(slot, amount)

                        if slots[i].x ~= 0 then
                            sub:css("left", tostring(slots[i].x * 2) .. "px")
                        end

                        if slots[i].y ~= 0 then
                            sub:css("top", tostring(slots[i].y * 2) .. "px")
                        end

                        anim_frames[frame_no] = anim_frames[frame_no] .. tostring(sub)
                    end
                end
            end
        end

        gui:wikitext(animate.base(anim_frames))
    end

    return tostring(div)
end

return p
Cookieは私達のサービスを提供するのに役立ちます。このサービスを使用することにより、お客様はCookieの使用に同意するものとします。