Jump to content

dxButton by AsuS


Recommended Posts

Bueno como veo que la gente del foro esta todo el rato pidiendo esta funcion pues decidí darles la funcion :fadein:

Uso una parecida en mi servidor esta la eh creado solo para postearla aqui:

--Client-side

local buttons = {} 
local sx, sy = guiGetScreenSize() 
  
--Funcion exportadora 
function dxCreateButton(x, y, w, h, text, relative) 
    if x and y and w and h and text then 
        local button = createElement ( "dxButton" ) 
        if button then 
            data = {} 
            data.x = x 
            data.y = y 
            data.w = w 
            data.h = h 
            data.text = text 
            data.relative = relative or false 
            data.font = "default-bold" 
            data.hover = false 
            data.textColor = {255, 255, 255, 255} 
            data.dxColor = {255, 255, 255, 255} 
            buttons[button] = data 
            return button 
        else 
            outputDebugString("dxButton: Imposible crear el elemento.") 
        end 
    else 
        outputDebugString("dxButton: Argumentos no definidos") 
    end 
end 
  
--Funcion de dibujo 
  
function drawButtons () 
    for i,button in pairs(getElementsByType("dxButton")) do 
        if button then 
            local data = buttons[button] 
            local r, g, b = unpack(data.dxColor) 
            local r2, g2, b2 = unpack(data.textColor) 
            if data.relative then 
                if isCursorHover((data.x*sx), (data.y*sy), (data.w*sx), (data.h*sy)) then 
                    if not data.hover then 
                        data.hover = true 
                    end 
                else 
                    if data.hover then 
                        data.hover = false 
                    end 
                end 
                if getKeyState("mouse1") and d.hover then 
                    dxDrawRectangle ( (data.x*sx), (data.y*sy), (data.w*sx), (data.h*sy), tocolor(r, g, b, 150)) 
                    dxDrawText(tostring(data.text),(data.x*sx),(data.y*sy),(data.x+data.w)*sx,(data.y+data.h)*sy,tocolor(r2, g2, b2, 150),1,data.font,"center","center",true,false) 
                elseif d.hover then 
                    dxDrawRectangle ( (data.x*sx), (data.y*sy), (data.w*sx), (data.h*sy), tocolor(r, g, b, 200)) 
                    dxDrawText(tostring(data.text),(data.x*sx),(data.y*sy),(data.x+data.w)*sx,(data.y+data.h)*sy,tocolor(r2, g2, b2, 200),1,data.font,"center","center",true,false) 
                else 
                    dxDrawRectangle ( (data.x*sx), (data.y*sy), (data.w*sx), (data.h*sy), tocolor(r, g, b, 255)) 
                    dxDrawText(tostring(data.text),(data.x*sx),(data.y*sy),(data.x+data.w)*sx,(data.y+data.h)*sy,tocolor(r2, g2, b2, 255),1,data.font,"center","center",true,false) 
                end 
            else 
                if isCursorHover((data.x*sx), (data.y*sy), (data.w*sx), (data.h*sy)) then 
                    if not data.hover then 
                        data.hover = true 
                    end 
                else 
                    if data.hover then 
                        data.hover = false 
                    end 
                end 
                if getKeyState("mouse1") and data.hover then 
                    dxDrawRectangle ( data.x, data.y, data.w, data.h, tocolor(r, g, b, 150)) 
                    dxDrawText(tostring(data.text),(data.x),(data.y),(data.x+data.w),(data.y+data.h),tocolor(r2, g2, b2, 150),1,data.font,"center","center",true,false) 
                elseif data.hover then 
                    dxDrawRectangle ( data.x, data.y, data.w, data.h, tocolor(r, g, b, 200)) 
                    dxDrawText(tostring(data.text),(data.x),(data.y),(data.x+data.w),(data.y+data.h),tocolor(r2, g2, b2, 200),1,data.font,"center","center",true,false) 
                else 
                    dxDrawRectangle ( data.x, data.y, data.w, data.h, tocolor(r, g, b, 255)) 
                    dxDrawText(tostring(data.text),(data.x),(data.y),(data.x+data.w),(data.y+data.h),tocolor(r2, g2, b2, 255),1,data.font,"center","center",true,false) 
                end 
            end 
            buttons[button] = data 
        end 
    end 
end 
addEventHandler ( "onClientRender", root, drawButtons ) 
  
function dxClientClick(button,state) 
    if button == "left" and state == "up" then 
        for i,element in pairs(getElementsByType("dxButton")) do 
            if element then 
                local button = buttons[element] 
                if button.hover == true then 
                    button.hover = false 
                    buttons[element] = button 
                    triggerEvent("onClientDXClick",element) 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientClick",getRootElement(), dxClientClick) 
  
function isCursorHover(posX,posY,sizeX,sizeY) 
    if posX and posY and sizeX and sizeY then 
        if isCursorShowing() then 
            local x,y = getCursorPosition() 
            local x,y = x*sx,y*sy 
            if x>=posX and x<=posX+sizeX and y>=posY and y<=posY+sizeY then 
                return true 
            end 
        else 
            return false 
        end 
    else 
        return false 
    end 
end 
  
--Event 
addEvent("onClientDXClick",true) 

-meta.xml

function="dxCreateButton" type="client" /> 

Lo del meta.xml es lo que hace posible exportar esta funcion de otros scripts.

Ejemplo de uso:

exports.NombreDelScriptdxButton:dxCreateButton(0, 0, 200, 50, "Test", false) 

Aquí dejo algunas funciones extra que pueden servirles

--Abajo del todo en el mismo archivo del button

--Funciones extra 
function getButtonFont(button) 
    if button and getElementType(button) == "dxButton" then 
        return tostring(buttons[button].font) 
    end 
    return false 
end 
  
function setButtonFont(button, font) 
    if button and getElementType(button) == "dxButton" and type(font) == "string" then 
        buttons[button].font = font 
        return true 
    end 
    return false 
end 
  
function getButtonColor(button) 
    if button and getElementType(button) == "dxButton" then 
        local r, g, b = unpack(buttons[button].dxColor) 
        return r, g, b 
    end 
    return false 
end 
  
function setButtonColor(button, r, g, b) 
    if button and getElementType(button) == "dxButton" and type(r) == "number" and type(g) == "number" and type(b) == "number" then 
        buttons[button].dxColor = {r, g, b} 
        return true 
    end 
    return false 
end 
  
function getButtonTextColor(button) 
    if button and getElementType(button) == "dxButton" then 
        local r, g, b = unpack(buttons[button].textColor) 
        return r, g, b 
    end 
    return false 
end 
  
function setButtonTextColor(button, r, g, b) 
    if button and getElementType(button) == "dxButton" and type(r) == "number" and type(g) == "number" and type(b) == "number" then 
        buttons[button].textColor = {r, g, b} 
        return true 
    end 
    return false 
end 
  
function getButtonText(button) 
    if button and getElementType(button) == "dxButton" then 
        return buttons[button].text 
    end 
    return false 
end 
  
function setButtonText(button, text) 
    if button and getElementType(button) == "dxButton" and type(text) == "string" then 
        buttons[button].text = text 
        return true 
    end 
    return false 
end 

--meta.xml

  
function="getButtonFont" type="client" /> 
function="setButtonFont" type="client" /> 
function="getButtonColor" type="client" /> 
function="setButtonColor" type="client" /> 
function="getButtonTextColor" type="client" /> 
function="setButtonTextColor" type="client" /> 
function="getButtonText" type="client" /> 
function="setButtonText" type="client" /> 
  
  

Si quieren que siga haciendo componentes dxGUI solo decírmelo por comentario :)

Si encuentran algún error avisarme por comentario también :arrowdown::arrowdown:

Edited by Guest
Link to comment
  • 2 weeks later...

Por lo que veo funciona sin el evento "onClientRender", si no me equivoco ¿La mención "dx" no era para todas aquellas funciones que solo servían si se ejecutaban con este evento?

Al parecer funciona como un gui, yo también cree mis propias "librerías" hace algún tiempo atrás, pero no las usé por que encontraba que era mas eficiente trabajar sin ellas.

Aun así, buen aporte para la gente que le acomoda mas trabajar así.

Link to comment
  • 3 months later...
Por lo que veo funciona sin el evento "onClientRender", si no me equivoco ¿La mención "dx" no era para todas aquellas funciones que solo servían si se ejecutaban con este evento?

Al parecer funciona como un gui, yo también cree mis propias "librerías" hace algún tiempo atrás, pero no las usé por que encontraba que era mas eficiente trabajar sin ellas.

Aun así, buen aporte para la gente que le acomoda mas trabajar así.

Me parece que no leíste bien el code o.O

Link to comment

Asus, anoche cuando lo probé el script tira un error en la línea 71, dice que 'd' es un valor nil, yo lo que hice fue cambiarlo pro 'data' y funcionó, aunque creo que no está al 100% funcionando porque el texto no se ve y no funciona el evento, haber si hice algo mal xD

  
local button = exports.dxButton:dxCreateButton(500, 500, 300, 100, "Test", false) 
addEventHandler("onClientDXClick",button,function() 
outputChatBox("Clicked") 
end 
) 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...