Jump to content

[HELP] Clicking on a dx Text


Recommended Posts

Posted

So I made this dx window and I've drawn an "X" to be used as a button to close the window.

function isCursorOverText(posX, posY, sizeX, sizeY) 
    if(isCursorShowing()) then 
        local cX, cY = getCursorPosition() 
        local screenWidth, screenHeight = guiGetScreenSize() 
        local cX, cY = (cX*screenWidth), (cY*screenHeight) 
        if(cX >= posX and cX <= posX+(sizeX - posX)) and (cY >= posY and cY <= posY+(sizeY - posY)) then 
            return true 
        else 
            return false 
        end 
    else 
        return false     
    end 
end    
  
function renderCmdsPanel() 
    dxDrawText("X", s[1]*1265/1920, s[2]*239/1080, s[1]*1318/1920, s[2]*260/1080, tocolor(255, 255, 255, 255), textFont, "bankgothic", "center", "center", false, false, false, false, false) 
end 
addEventHandler("onClientRender", getRootElement(), renderCmdsPanel) 
addEventHandler("onClientClick", getRootElement(), closeXButton) 
         
function closeXButton(button, state) 
    local xButton = isCursorOverText(s[1]*1265/1920, s[2]*239/1080, s[1]*1318/1920, s[2]*260/1080) 
    if (button == "left" and state == "down" and xButton) then 
        guiSetVisible(tabPanel, false) 
        showCursor (false) 
        guiSetInputEnabled(false) 
        tabPanel = nil 
        removeEventHandler("onClientRender", getRootElement(), renderCmdsPanel) 
        removeEventHandler("onClientClick", getRootElement(), closeXButton) 
    end 
end 

I've used the same method for for my other windows and it works, but here, it only works when I have gui editor active, as if the "onClientClick" event is triggered only when in guieditor...

Any ideas?

Thanks.

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted

You added the event handler before the function was even defined.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

That was just the main things I have in the code, but it goes like this:

function closeXButton(button, state) 
    local xButton = isCursorOverText(s[1]*1265/1920, s[2]*239/1080, s[1]*1318/1920, s[2]*260/1080) 
    if (button == "left" and state == "down" and xButton) then 
        guiSetVisible(tabPanel, false) 
        showCursor (false) 
        guiSetInputEnabled(false) 
        tabPanel = nil 
        removeEventHandler("onClientRender", getRootElement(), renderCmdsPanel) 
        removeEventHandler("onClientClick", getRootElement(), closeXButton) 
    end 
end 
  
function commandsHelp() 
    local loggedIn = getElementData(sourcePlayer, "loggedin") 
    if (loggedIn == 1) then 
        guiSetInputEnabled(true) 
        addEventHandler("onClientRender", getRootElement(), renderCmdsPanel) 
        addEventHandler("onClientClick", getRootElement(), closeXButton)   
............. 

So I don't think that would be the problem.

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted

Well, if you post incomplete code, how I'm supposed to know?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

[quote name=..:D&G:..]

local s = {guiGetScreenSize()} 
  
function renderCmdsPanel() 
    textFont2 = 1.30/1920*s[1] 
    textFont = 1.00/1920*s[1] 
    local r, g, b = exports.gui:getPlayerBaseColor(getLocalPlayer())  
    dxDrawText("X", s[1]*(1265 - 1)/1920, s[2]*(239 - 1)/1080, s[1]*(1318 - 1)/1920, s[2]*(260 - 1)/1080, tocolor(0, 0, 0, 255), textFont, "bankgothic", "center", "center", false, false, false, false, false) 
    dxDrawText("X", s[1]*(1265 + 1)/1920, s[2]*(239 - 1)/1080, s[1]*(1318 + 1)/1920, s[2]*(260 - 1)/1080, tocolor(0, 0, 0, 255), textFont, "bankgothic", "center", "center", false, false, false, false, false) 
    dxDrawText("X", s[1]*(1265 - 1)/1920, s[2]*(239 + 1)/1080, s[1]*(1318 - 1)/1920, s[2]*(260 + 1)/1080, tocolor(0, 0, 0, 255), textFont, "bankgothic", "center", "center", false, false, false, false, false) 
    dxDrawText("X", s[1]*(1265 + 1)/1920, s[2]*(239 + 1)/1080, s[1]*(1318 + 1)/1920, s[2]*(260 + 1)/1080, tocolor(0, 0, 0, 255), textFont, "bankgothic", "center", "center", false, false, false, false, false) 
    dxDrawText("X", s[1]*1265/1920, s[2]*239/1080, s[1]*1318/1920, s[2]*260/1080, tocolor(255, 255, 255, 255), textFont, "bankgothic", "center", "center", false, false, false, false, false) 
    dxDrawText("Welcome to the commands panel!", s[1]*574/1920, s[2]*271/1080, s[1]*1243/1920, s[2]*318/1080, tocolor(255, 255, 255, 255), textFont2, "default-bold", "center", "center", false, false, false, false, false) 
    dxDrawText("Commands Panel", s[1]*849/1920, s[2]*239/1080, s[1]*969/1920, s[2]*261/1080, tocolor(255, 255, 255, 255), textFont, "default-bold", "center", "center", false, false, false, false, false) 
    dxDrawText("© World Gaming", s[1]*497/1920, s[2]*239/1080, s[1]*665/1920, s[2]*261/1080, tocolor(255, 255, 255, 255), textFont, "default", "center", "center", false, false, false, false, false) 
end 
  
function closeXButton(button, state) 
    local xButton = isCursorOverText(s[1]*1265/1920, s[2]*239/1080, s[1]*1318/1920, s[2]*260/1080) 
    if (button == "left" and state == "down" and xButton) then 
        guiSetVisible(tabPanel, false) 
        showCursor (false) 
        guiSetInputEnabled(false) 
        tabPanel = nil 
        removeEventHandler("onClientRender", getRootElement(), renderCmdsPanel) 
        removeEventHandler("onClientClick", getRootElement(), closeXButton) 
    end 
end 
  
function commandsHelp() 
    local loggedIn = getElementData(sourcePlayer, "loggedin") 
    if (loggedIn == 1) then 
        guiSetInputEnabled(true) 
        addEventHandler("onClientRender", getRootElement(), renderCmdsPanel) 
        addEventHandler("onClientClick", getRootElement(), closeXButton)   
............. 

This IS the complete code. The bit with dots is just a gridlist ...

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted

Apart from guiSetInputEnabled, did you use showCursor?

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted

Also, showCursor is a resource based, if you use showCursor in another resource you have to hide it from the same resource else it won't be hidden.

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...