Ir para conteúdo

Featured Replies

Postado

Resumo

A maneira atual para implementar Modal Window é um pouco complicada. Atualmente precisamos cria-la em algum lugar, registrar o evento, adicionar os botões em uma ordem específica, definir o ID da janela, dos botões e da escolha. Isso não é o ideal, então esta biblioteca foi criada pelo Non Sequitur para ajudar nisso. E eu estou trazendo para a OtServBrasil.

Exemplo/ Tutorial Usando Modal Window

 

Instalando

  • Adicionar em data/lib/lib.lua
dofile('data/lib/modalwindow.lua')
  • Crie o arquivo modalwindow.lua com o seguinte conteúdo em data/lib
if not modalWindows then
    modalWindows = {
        modalWindowConstructor = ModalWindow,
        nextFreeId = 500,
 
        windows = {}
    }
end
 
local MT = {}
MT.__index = MT
 
function ModalWindow(...)
    local args = {...}
    if type(args[1]) == 'table' then
        local self = setmetatable(args[1], MT)
        local id = modalWindows.nextFreeId        
        self.id = id
        self.buttons = {}
        self.choices = {}
        self.players = {}
        self.created = false
 
        modalWindows.nextFreeId = id + 1
        table.insert(modalWindows.windows, self)
        return self
    end
 
    return modalWindows.modalWindowConstructor(...)
end
 
function MT:setDefaultCallback(callback)
    self.defaultCallback = callback
end
 
function MT:addButton(text, callback)
    local button = {text = tostring(text), callback = callback}
    table.insert(self.buttons, button)
    return button
end
 
function MT:addButtons(...)
    for _, text in ipairs({...}) do
        table.insert(self.buttons, {text = tostring(text)})
    end
end
 
function MT:addChoice(text)
    local choice = {text = tostring(text)}
    table.insert(self.choices, choice)
    return choice
end
 
function MT:addChoices(...)
    for _, text in ipairs({...}) do
        table.insert(self.choices, {text = tostring(text)})
    end
end
 
function MT:setDefaultEnterButton(text)
    self.defaultEnterButton = text
end
 
function MT:setDefaultEscapeButton(text)
    self.defaultEscapeButton = text
end
 
function MT:setTitle(title)
    self.title = tostring(title)
end
 
function MT:setMessage(message)
    self.message = tostring(message)
end
 
local buttonOrder = {
    [4] = {3, 4, 2, 1},
    [3] = {2, 3, 1},
    [2] = {1, 2},
    [1] = {1}
}
function MT:create()
    local modalWindow = modalWindows.modalWindowConstructor(self.id, self.title, self.message)
    local order = buttonOrder[math.min(#self.buttons, 4)]
 
    if order then
        for _, i in ipairs(order) do
            local button = self.buttons[i]
            modalWindow:addButton(i, button.text)
            button.id = i
 
            if button.text == self.defaultEnterButton then
                modalWindow:setDefaultEnterButton(i)
            elseif button.text == self.defaultEscapeButton then
                modalWindow:setDefaultEscapeButton(i)
            end
        end
    end
 
    for _, choice in ipairs(self.choices) do
        modalWindow:addChoice(_, choice.text)
        choice.id = _
    end
 
    self.modalWindow = modalWindow
end
 
function MT:sendToPlayer(player)
    if not self.modalWindow then
        self:create()
    end
 
    player:registerEvent('ModalWindowHelper')
    self.players[player:getId()] = true
    return self.modalWindow:sendToPlayer(player)
end
  • Adicionar em data/creaturescripts/craturescripts.xml
<event type="modalwindow" name="ModalWindowHelper" script="modalwindowhelper.lua" />
  • Crie o arquivo modalwindowhelper.lua com o seguinte conteúdo em data/creaturescripts/scripts/
function onModalWindow(player, modalWindowId, buttonId, choiceId)
    local modalWindow
    for _, window in ipairs(modalWindows.windows) do
        if window.id == modalWindowId then
            modalWindow = window
            break
        end
    end
 
    if not modalWindow then
        return true
    end
 
    local playerId = player:getId()
    if not modalWindow.players[playerId] then
        return true
    end
    modalWindow.players[playerId] = nil
 
    local choice = modalWindow.choices[choiceId]
 
    for _, button in ipairs(modalWindow.buttons) do
        if button.id == buttonId then
            local callback = button.callback or modalWindow.defaultCallback
            if callback then
                callback(button, choice)
                break
            end
        end
    end
 
    return true
end

 

Pronto!

 

Espero que gostem. Posteriormente irei postar um tutorial de como usar/ aplicar e alguns scripts utilizando a Biblioteca.

  • Respostas 8
  • Visualizações 2.3k
  • Created
  • Última resposta

Top Posters In This Topic

Most Popular Posts

  • Esse sistema é incrível, boa contribuição @EddyHavoc   Não queria estragar mas tive que dar um rep

Posted Images

Postado

Parabéns, seu tópico de conteúdo foi aprovado!
Muito obrigado pela sua contribuição, nós do Tibia King agradecemos.
Seu conteúdo com certeza ajudará à muitos outros, você recebeu +1 REP.

Mostrar conteúdo oculto

 

vodkart_logo.png

[*Ninguém será digno do sucesso se não usar suas derrotas para conquistá-lo.*]

 

DISCORDvodkart#6090

 

Postado
  Em 23/05/2018 em 00:59, EddyHavoc disse:

Resumo

A maneira atual para implementar Modal Window é um pouco complicada. Atualmente precisamos cria-la em algum lugar, registrar o evento, adicionar os botões em uma ordem específica, definir o ID da janela, dos botões e da escolha. Isso não é o ideal, então esta biblioteca foi criada pelo Non Sequitur para ajudar nisso. E eu estou trazendo para a OtServBrasil.

Exemplo/ Tutorial Usando Modal Window

 

Instalando

  • Adicionar em data/lib/lib.lua

dofile('data/lib/modalwindow.lua')
  • Crie o arquivo modalwindow.lua com o seguinte conteúdo em data/lib

if not modalWindows then
    modalWindows = {
        modalWindowConstructor = ModalWindow,
        nextFreeId = 500,
 
        windows = {}
    }
end
 
local MT = {}
MT.__index = MT
 
function ModalWindow(...)
    local args = {...}
    if type(args[1]) == 'table' then
        local self = setmetatable(args[1], MT)
        local id = modalWindows.nextFreeId        
        self.id = id
        self.buttons = {}
        self.choices = {}
        self.players = {}
        self.created = false
 
        modalWindows.nextFreeId = id + 1
        table.insert(modalWindows.windows, self)
        return self
    end
 
    return modalWindows.modalWindowConstructor(...)
end
 
function MT:setDefaultCallback(callback)
    self.defaultCallback = callback
end
 
function MT:addButton(text, callback)
    local button = {text = tostring(text), callback = callback}
    table.insert(self.buttons, button)
    return button
end
 
function MT:addButtons(...)
    for _, text in ipairs({...}) do
        table.insert(self.buttons, {text = tostring(text)})
    end
end
 
function MT:addChoice(text)
    local choice = {text = tostring(text)}
    table.insert(self.choices, choice)
    return choice
end
 
function MT:addChoices(...)
    for _, text in ipairs({...}) do
        table.insert(self.choices, {text = tostring(text)})
    end
end
 
function MT:setDefaultEnterButton(text)
    self.defaultEnterButton = text
end
 
function MT:setDefaultEscapeButton(text)
    self.defaultEscapeButton = text
end
 
function MT:setTitle(title)
    self.title = tostring(title)
end
 
function MT:setMessage(message)
    self.message = tostring(message)
end
 
local buttonOrder = {
    [4] = {3, 4, 2, 1},
    [3] = {2, 3, 1},
    [2] = {1, 2},
    [1] = {1}
}
function MT:create()
    local modalWindow = modalWindows.modalWindowConstructor(self.id, self.title, self.message)
    local order = buttonOrder[math.min(#self.buttons, 4)]
 
    if order then
        for _, i in ipairs(order) do
            local button = self.buttons[i]
            modalWindow:addButton(i, button.text)
            button.id = i
 
            if button.text == self.defaultEnterButton then
                modalWindow:setDefaultEnterButton(i)
            elseif button.text == self.defaultEscapeButton then
                modalWindow:setDefaultEscapeButton(i)
            end
        end
    end
 
    for _, choice in ipairs(self.choices) do
        modalWindow:addChoice(_, choice.text)
        choice.id = _
    end
 
    self.modalWindow = modalWindow
end
 
function MT:sendToPlayer(player)
    if not self.modalWindow then
        self:create()
    end
 
    player:registerEvent('ModalWindowHelper')
    self.players[player:getId()] = true
    return self.modalWindow:sendToPlayer(player)
end
  • Adicionar em data/creaturescripts/craturescripts.xml

<event type="modalwindow" name="ModalWindowHelper" script="modalwindowhelper.lua" />
  • Crie o arquivo modalwindowhelper.lua com o seguinte conteúdo em data/creaturescripts/scripts/

function onModalWindow(player, modalWindowId, buttonId, choiceId)
    local modalWindow
    for _, window in ipairs(modalWindows.windows) do
        if window.id == modalWindowId then
            modalWindow = window
            break
        end
    end
 
    if not modalWindow then
        return true
    end
 
    local playerId = player:getId()
    if not modalWindow.players[playerId] then
        return true
    end
    modalWindow.players[playerId] = nil
 
    local choice = modalWindow.choices[choiceId]
 
    for _, button in ipairs(modalWindow.buttons) do
        if button.id == buttonId then
            local callback = button.callback or modalWindow.defaultCallback
            if callback then
                callback(button, choice)
                break
            end
        end
    end
 
    return true
end

 

Pronto!

 

Espero que gostem. Posteriormente irei postar um tutorial de como usar/ aplicar e alguns scripts utilizando a Biblioteca.

image.thumb.png.13a736650e79eed3e6f2b703413373ae.pngpoderia me ajudar ? meu servidor é 10.10

Participe da conversa

Você pode postar agora e se cadastrar mais tarde. Se você tem uma conta, faça o login para postar com sua conta.

Visitante
Responder

Quem Está Navegando 0

  • Nenhum usuário registrado visualizando esta página.

Estatísticas dos Fóruns

  • Tópicos 96.9k
  • Posts 519.7k

Informação Importante

Confirmação de Termo