Página 1 de 1

Função para criar menus facilmente em LUA.

Enviado: 15 Ago 2018, 18:14
por Um_Geek
Criar menus tanto em cleo quando em lua é algo até chato, ficar pondo DUMMY em entradas invalidas, cria arquivo gxt ou usar add_text_label sempre complica mais, então para facilitar na criação de menus criei esta função. não notei bugs no uso dela.

Função
Basta copiar a função e por no script que esteja fazendo. Ela é completa, des de pegar o resultado a congelar o player enquanto seleciona uma opção.

Código: Selecionar tudo

-- descrição 
-- table_name: table com entradas do menu.
-- pos_x: posição X
-- pos_y : posição Y
-- return_text: [true] = o menu retornara o texto da opção escolhida, [false] = o menu retornara numero da opção.
-- button: defina o botão que ativa o menu.

function simple_menu(table_name,pos_x,pos_y,return_text,button)
    if button == nil then button = 11 end -- yes
    local labels = {};
    setPlayerControl(PLAYER_HANDLE,false);
    setCameraBehindPlayer();
    for i=1, #table_name do
        if table_name[i] then 
            labels[i] = setFreeGxtEntry(table_name[i]);
        else 
            labels[i] = "DUMMY";
        end
    end
    local menu = createMenu(labels[1],pos_x,pos_y,300.0,1,true,true,1);
    setMenuColumn(menu,0,labels[2],labels[3],labels[4],labels[5],labels[6],labels[7],labels[8],labels[9],labels[10],labels[11],labels[12],labels[13]);
    while not isButtonPressed(PLAYER_HANDLE,button) do
        wait(0);
    end
    local option = getMenuItemSelected(menu);
    deleteMenu(menu);
    while isButtonPressed(PLAYER_HANDLE,button) do
        wait(0);
    end
    setPlayerControl(PLAYER_HANDLE,true);
    restoreCamera();    
    for i=1, #labels do 
        if labels[i]~="DUMMY" then
            clearGxtEntry(labels[i]);
        end
    end
    if return_text then 
        return table_name[option+3];
    end
    return option;
end

Uso
Basta criar uma table com titulo, descrição e opções

Código: Selecionar tudo

local meu_menu = {
    [1] = "meu titulo";
    [2] = "minha descricao";
    [3] = "opt 1";
    [4] = "opt 2"; 
    [5] = "opt 3";
    [6] = "opt 4";        
}

Na função fica assim

Código: Selecionar tudo

function main()
    while true do 
        wait(80);        
        if testCheat('teste') then 
            local result = simple_menu(meu_menu,90.0,90.0,true,15);
            printStringNow(result,1500);
        end
    end
end

**Adicionado o argumento button para definir o botão de confirmar.

Re: Função para criar menus facilmente em LUA.

Enviado: 15 Ago 2018, 23:04
por FilipeS
Tem algo me dizendo pra aprender lua..  :peepo1:

Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 02:55
por Junior_Djjr
Ótimo poder enviar uma variável com os elementos.

Código: Selecionar tudo

while not isButtonPressed(PLAYER_HANDLE,11) do
    setMenuColumn(menu,0,labels[2],labels[3],labels[4],labels[5],labels[6],labels[7],labels[8],labels[9],labels[10],labels[11],labels[12],labels[13]);
    wait(0);
end
Isto aqui não tá errado? Você tá setando as colunas do menu todos os frames, você só precisa setar na criação do menu.

Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 10:40
por Um_Geek
FilipeS escreveu:
15 Ago 2018, 23:04
Tem algo me dizendo pra aprender lua.. :peepo1:
Vale a pena :peepo6:

Junior_Djjr escreveu:
16 Ago 2018, 02:55
Isto aqui não tá errado? Você tá setando as colunas do menu todos os frames, você só precisa setar na criação do menu.
esta parte foi copiada de outro lugar, tem um motivo especial para ser assim, mas no caso não é necessário.


Código: Selecionar tudo

while not isButtonPressed(PLAYER_HANDLE,11) do
Esqueci de mencionar que o menu usa o botão Yes, normalmente usar o de entrar/sair de veículos, mas sempre me encheu aquilo depois de sair de uns menus o CJ sair correndo para o carro mais próximo :v

Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 10:42
por Israel
Um_Geek escreveu:
16 Ago 2018, 10:40
Esqueci de mencionar que o menu usa o botão Yes, normalmente usar o de entrar/sair de veículos, mas sempre me encheu aquilo depois de sair de uns menus o CJ sair correndo para o carro mais próximo :v
É só loopar aguardando soltar o botão e depois liberar os controles do player.

Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 10:53
por Kaneki-ken
Vai me sservi de grande ajuda

Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 10:55
por Um_Geek
É só loopar aguardando soltar o botão e depois liberar os controles do player.

Usar o botão yes é mais pratico. mas quem usar outro melhor acrescentar isto ali no lugar da setinha.

Código: Selecionar tudo

function main()
    while true do 
        wait(80);        
        if testCheat('teste') then 
            local result = simple_menu(meu_menu,90.0,90.0,true);
            printStringNow(result,1500);
            --> Aqui
        end
    end
end


Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 17:20
por Junior_Djjr
Um_Geek escreveu:
16 Ago 2018, 10:55
Usar o botão yes é mais pratico.
Só é mais prático para você. Quebrar um padrão não é prático para as pessoas, o que é prático é seguir um padrão, onde todos os menus se selecionam por Espaço, então o seu também tem que selecionar.

Re: Função para criar menus facilmente em LUA.

Enviado: 16 Ago 2018, 18:45
por Um_Geek
Junior_Djjr escreveu:
16 Ago 2018, 17:20
Um_Geek escreveu:
16 Ago 2018, 10:55
Usar o botão yes é mais pratico.
Só é mais prático para você. Quebrar um padrão não é prático para as pessoas, o que é prático é seguir um padrão, onde todos os menus se selecionam por Espaço, então o seu também tem que selecionar.

Eu faço scripts por passa tempo, ai uma vez ou outra compartilho, então segue o que é pratico para mim :peepo6:

De qualquer forma meus menus usam Y para confirmar uma opção e N para sair sem escolher nada, ou coloco uma opção para sair no menu. Adicionei um argumento que define o botão de confirmar, se for nil vai ser yes mesmo.

Re: Função para criar menus facilmente em LUA.

Enviado: 14 Jan 2020, 05:46
por Grinch_
I think the ability to scroll if there are more than 11 columns would be better.
Something similar to this,
SpoilerAbrir

Código: Selecionar tudo

function simple_menu(table_name,pos_x,pos_y,return_text,button)
    local index = 3 -- first column at 3
    if button == nil then button = 11 end -- yes
    local labels = {};
    setPlayerControl(PLAYER_HANDLE,false);
    setCameraBehindPlayer();
    for i=1, #table_name do
        if table_name[i] then
            labels[i] = setFreeGxtEntry(table_name[i]);
        else
            labels[i] = "DUMMY";
        end
    end
    local menu = createMenu(labels[1],pos_x,pos_y,300.0,1,true,true,1);
    setMenuColumn(menu,0,labels[2],labels[3],labels[4],labels[5],labels[6],labels[7],labels[8],labels[9],labels[10],labels[11],labels[12],labels[13]);

    while not isButtonPressed(PLAYER_HANDLE,button) do
        wait(0);

        local selected_item = getMenuItemSelected(menu)

        if isKeyDown(0x53) or isKeyDown(0x28) then -- S/Down
            if selected_item == 10 then
                if (index+11) <= #labels then
                    index = index+1
                    selected_item = 10
                else
                    if index == (#labels-10) then
                        index = 3
                        selected_item = 0
                    end
                end

                while isKeyDown(0x53) or isKeyDown(0x28) do
                    wait(0);
                end
                setMenuColumn(menu,0,labels[2],labels[index],labels[index+1],labels[index+2],labels[index+3],labels[index+4],labels[index+5],labels[index+6],labels[index+7],labels[index+8],labels[index+9],labels[index+10]);
                setActiveMenuItem(0,selected_item)
            end
        end
        if isKeyDown(0x57) or isKeyDown(0x26) then -- W/Up
            if selected_item == 0 then
                if index > 3 then
                    index = index-1
                    selected_item = 0
                else
                    if index == 3 then
                        index = #labels - 10
                        selected_item = 10
                    end
                end
                while isKeyDown(0x57) or isKeyDown(0x26) do
                    wait(0);
                end
                setMenuColumn(menu,0,labels[2],labels[index],labels[index+1],labels[index+2],labels[index+3],labels[index+4],labels[index+5],labels[index+6],labels[index+7],labels[index+8],labels[index+9],labels[index+10]);
                setActiveMenuItem(0,selected_item)
            end
        end
    end

    local option = getMenuItemSelected(menu);
    deleteMenu(menu);
    while isButtonPressed(PLAYER_HANDLE,button) do
        wait(0);
    end
    setPlayerControl(PLAYER_HANDLE,true);
    restoreCamera();
    for i=1, #labels do
        if labels[i]~="DUMMY" then
            clearGxtEntry(labels[i]);
        end
    end
    if return_text then
        return table_name[option+index];
    end
    return option;
end[/i][/i][/i][/i][/i][/i]


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


I tried to recreate the menu from scratch following how junior did his shinegui.
You can basically create a table and the menu will generate itself.
Imagem
SpoilerAbrir

Código: Selecionar tudo

script_name 'Simple Ui'
script_author("Grinch_")

local main_menu = {
    ["Entry 1"] = {
        ["Help Message Test"] = function() printHelpString("Help Message") end,
        ["Other stuff"] = function()  --[[ do stuff ]] end,
    },
    ["Entry 2"] = {
        ["Sub entry 1"] =
        {
            ["TEST"] = {}, -- shows empty menu
        },
        ["Sub entry 2"] = nil, -- won't appear in menu
    },
}

--------------------------------
-- These are used in runtine
local show_menu = false
local selected_item = 1
local start_index = 1
local current_table = main_menu
local tables = {}
--------------------------------

function getElementCount(table)
    i = 0
    for key,value in pairs(table) do
       i = i + 1
    end
    return i
end

function getStringIndex(name,table)
    local index = 0
    for key,value in pairs(table) do
        index = index + 1
        if key == name then
            return index
        end
    end
end

function getIndexString(index,table)
    local count = 0
    for key,value in pairs(table) do
        count = count + 1
        if count == index then
            return key
        end
    end
end


function SimpleUi(title,posX,posY,width,padding,column_spacing,font,display_text_count,text_rgba,text_active_rgba,box_rgba,h_rgba)

    ------------------------------------------
    -- Default values
    posX = posX or 70.0
    posY = posY or 70.0

    width = width or 150.0

    box_rgba = box_rgba or {0,0,0,150}
    h_rgba = h_rgba or {120, 120, 120,200} -- header rgba

    padding = padding or 10.0
    column_spacing = column_spacing or 15.0

    display_text_count = display_text_count or 15
    font = font or 3

    text_rgba = text_rgba or {200,200,200,255}
    text_active_rgba = text_active_rgba or {100,200,255,255}
    ------------------------------------------

    drawMenu = function()

        local header_height = padding+column_spacing
        local current_spacing = 0

        setSpritesDrawBeforeFade(true)

        -- Header
        drawRect(posX+width/2,posY,width,header_height,h_rgba[1],h_rgba[2],h_rgba[3],h_rgba[4])

        useRenderCommands(true)
        setTextFont(font)
        setTextWrapx(640.0)
        setTextScale(0.5,2)
        setTextDropshadow(1,0,0,0,200)
        displayText(posX+width/2-5*string.len(title),posY-header_height/2,setFreeGxtEntry(title)) -- Header text

        -- Text drawing stuff
        for name,redirect in pairs(current_table) do
            useRenderCommands(true)
            setTextFont(font)
            setTextWrapx(640.0)
            setTextScale(0.3,1.2)
            setTextDropshadow(0,0,0,0,0)

            if getStringIndex(name,current_table) == selected_item then
                setTextColour(text_active_rgba[1],text_active_rgba[2],text_active_rgba[3],text_active_rgba[4])
                setTextEdge(1,0,0,0,255)
            else
                setTextColour(text_rgba[1],text_rgba[2],text_rgba[3],text_rgba[4])
            end

            local gxt = name
            if type(redirect) == "table" then
                gxt = gxt .. " >"
            end

            displayText(posX+padding,posY+column_spacing+header_height/2+current_spacing,setFreeGxtEntry(gxt))
            clearGxtEntry(name)

            current_spacing = current_spacing + column_spacing
        end

        local mainboxY = posY+(header_height+current_spacing)/2 + column_spacing


        -- Main box
        drawRect(posX+width/2,mainboxY,width,current_spacing + column_spacing*2,box_rgba[1],box_rgba[2],box_rgba[3],box_rgba[4])

    end

    drawMenu()

    if isKeyDown(0x53) or isKeyDown(0x28) then -- S/Down
        while isKeyDown(0x53) or isKeyDown(0x28) do
            wait(0)
            drawMenu()
        end

        if (selected_item-start_index+1) == display_text_count then
            if (start_index+display_text_count) < getElementCount(current_table) then
                start_index = start_index + 1
            end
        end

        if selected_item < getElementCount(current_table) then
            selected_item = selected_item + 1
        else
            selected_item = 1
            start_index = 1
        end

    end

    if isKeyDown(0x57) or isKeyDown(0x26) then -- W/Up
        while isKeyDown(0x57) or isKeyDown(0x26) do
            wait(0)
            drawMenu()
        end

        if (selected_item-start_index+1) == 1 then
            if start_index > 1 then
                start_index = start_index - 1
            end
        end

        if selected_item > 1 then
            selected_item = selected_item - 1
        else
            selected_item = getElementCount(current_table)
            start_index = getElementCount(current_table) - display_text_count
        end

    end

    if isKeyDown(0x20) then -- Space
        while isKeyDown(0x20) do
            wait(0)
            drawMenu()
        end

        local table_ = current_table[getIndexString(selected_item,current_table)]

        if type(table_) == "table" then
            table.insert(tables,current_table)
            current_table = table_
            selected_item = 1
            start_index = 1
        else
            if table_ ~= nil then
                table_() -- execute the function
            end
        end
    end

    if isKeyDown(0x0D) then -- Enter
        while isKeyDown(0x0D) do
            wait(0)
            drawMenu()
        end
        current_table = tables[#tables]

        if #tables > 1 then
            table.remove(tables)
        else
            show_menu = false
        end
    end
end

function main()



    while true do
        wait(0);

        if show_menu then
            SimpleUi("Simple Ui")
        end

        if testCheat("Menu") then
            show_menu = not show_menu
        end

    end
end
Download
 

Re: Função para criar menus facilmente em LUA.

Enviado: 04 Jul 2020, 22:17
por Grinch_
Also posting this here as this looks cool af. (by BDC_Osiris)


Re: Função para criar menus facilmente em LUA.

Enviado: 09 Set 2020, 18:54
por Um_Geek
Grinch_ escreveu:
04 Jul 2020, 22:17
Also posting this here as this looks cool af. (by BDC_Osiris)

This tool is really cool.
Imagem

Código: Selecionar tudo

local nat_menu = require "nat_menu"

function main()
	while true do
		if testCheat("UI") then
			setPlayerControl(0,false)
			local v = nat_menu('title','desc',{'opt1','opt2','opt3'},"shop_binco")
			setPlayerControl(0,true)
			printStringNow(v,2000)
		end
		wait(0)
	end
end

nat_menu.zip
(425 Bytes) Baixado 206 vezes


Atualizando a forma padrão, suporta mais de 12 itens.
Imagem

Código: Selecionar tudo

local classic_menu = require "classic_menu"

local opts = {}
for i=1, 40 do
	table.insert(opts, "Option "..i)
end

function main()
	while true do 
		if testCheat("CLS") then
			setPlayerControl(0,false)
			local v = classic_menu.new('title','desc',opts,90,30,240)
			setPlayerControl(0,true)
			printStringNow(v,2000)
		end
		wait(0)
	end
end

function onExitScript()
	classic_menu.clear()
end

classic_menu.zip
(687 Bytes) Baixado 197 vezes