Jump to content

Detecting a click on a DX element


Recommended Posts

Hi,

I'm currently using a method to detect when I click on a DX element by checking if the cursor is within the DX elements coordinates.

I'm wanting to know if there is a better way of doing this? I thought about just putting gui buttons over something that would be clicked on and using GUI functions, but there should be either an easier or better way of doing that right?

And also, I saw a post that someone said that onClientClick executes twice which is annoying because I'm using that to detect the click on a DX button and when I want it to output something, it outputs it twice. Which is another reason why I'm wanting to know if there is another way.

And another reason is for DX gridlists. Since the DX elements would change position when you move a slider up and down, it wouldn't work with mu current method.

Here is an example of what I am currently doing:

function inside(x1, y1, x2, y2, w2, h2) 
   return not (x1 < x2  or x2+w2 < x1 or y1 < y2 or y2+h2 < y1) 
end 
  
addEventHandler("onClientClick", root,  
    function(_, _, x, y) 
            --Check if the cursor is within the Close button 
            if (inside(x, y, screenW * 0.5182, screenH * 0.8620, screenW * 0.0781, screenH * 0.0491)) then 
                    outputChatBox("Closed", 255, 255, 0) 
            end 
    end 
) 
  

Link to comment

onClientClick executes twice because it executes when you press down a mouse button and when release it. The button state is the second parameter value for the callback function.

button: This refers the button used to click on the mouse, can be left, right, or middle

state: This can be used to tell if the user released or pressed the mouse button, where up is passed if the button is released, and down is passed if the button is pushed

https://wiki.multitheftauto.com/wiki/OnClientClick

You just have to check if button == "left" and state == "down" to make your function only detect a click when your left mouse button is pressed down.

Link to comment
  
function isMouseInPosition ( x, y, width, height ) 
    if ( not isCursorShowing ( ) ) then 
        return false 
    end 
  
    local sx, sy = guiGetScreenSize ( ) 
    local cx, cy = getCursorPosition ( ) 
    local cx, cy = ( cx * sx ), ( cy * sy ) 
    if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then 
        return true 
    else 
        return false 
    end 
end 
  

Link to comment
  
function isMouseInPosition ( x, y, width, height ) 
    if ( not isCursorShowing ( ) ) then 
        return false 
    end 
  
    local sx, sy = guiGetScreenSize ( ) 
    local cx, cy = getCursorPosition ( ) 
    local cx, cy = ( cx * sx ), ( cy * sy ) 
    if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then 
        return true 
    else 
        return false 
    end 
end 
  
I already have that kind of function which is in my first post. 
  
Do you know how something like this would be used in a DX gridlist? 
Thats really the only thing im trying to figure out is how to make a working DX gridlist. 
  

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