Jump to content

Dx rectangle Click


Snow-Man

Recommended Posts

how can i make Dx Rectangle button

i know functions but i need example

Example would basically be the whole code.

You need to get the cursor position and check if its on the button.

Not that hard.

If it's on the button u could change the alpha or something so players get a response.

Link to comment

Just like bonsai said ,

you could make an button over you rectangel , set its alpha to 0 so you cant see it ,

and just handle it like a normal button.

  
button = guiCreateButton(x,y,w,h, "button", false) 
guiSetAlpha(button, 0) 
dxDrawRectangle(x,y,w,h) 
  
function buttonclick() 
--your code 
end 
addEventHandler("onClientGUIClick", button , buttonclick) 
  
  

Link to comment
  
function DrawRectangle() 
x, y = guiGetScreenSize() 
local check = isMouseWithinRangeOf(x*0,x*0.5,y*0,y*0.5) -- start x, width, start y, height 
if check then 
dxDrawRectangle(x*0,y*0,x*0.5,y*0.5,tocolor(0,0,0,180)) 
end 
end 
addEventHandler("onClientRender",resourceRoot,DrawRectangle) 
  
function isMouseWithinRangeOf(psx,pssx,psy,pssy) 
  if not isCursorShowing() then 
    return false 
  end 
  local cx,cy = getCursorPosition() 
  cx,cy = cx*x,cy*y 
  if cx >= psx and cx <= psx+pssx and cy >= psy and cy <= psy+pssy then 
    return true,cx,cy 
  else 
    return false 
  end 
end 
  

Gallardo gave me this code and it works perfect :-)

Link to comment

and you can add a click checker to the code above:

function onClick() 
  local check = isMouseWithinRangeOf(x*0,x*0.5,y*0,y*0.5) -- start x, width, start y, height 
  if check then 
    outputChatBox("you have pressed inside the rectangle") 
  end 
end 
addEventHandler("onClientClick",getRootElement(),onClick) 

Link to comment

you can also do this with onClientRender, if you want for example smooth move camera

  
local x1 = 50 
local y1 = 50 
local z1 = 50 
  
function onClick() 
local x,y  = guiGetScreenSize() 
 setCameraMatrix(x1,y1,z1,50,50,50) 
  local check = isMouseWithinRangeOf(x*0,x*0.5,y*0,y*0.5) -- start x, width, start y, height 
           if getKeyState("mouse1") == true then 
                   if check then 
                       x1 = x1 + 0.2 
                        y1 = y1 - 0.02 
              end 
end 
addEventHandler("onClienRender",getRootElement(),onClick) 
  

Edit: Gallardo you are fast, I noticed that and changed it after 30 seconds.

Edited by Guest
Link to comment

You can use examples from these resources that i've made that have DX buttons:

http://www.mediafire.com/download/gbkal ... -login.zip

https://community.multitheftauto.com/ind ... ls&id=7970

Here's a basic example:

local rectangleData = { 
    x = 0, 
    y = 0, 
    width = 100, 
    height = 20 
} 
addEventHandler ( "onClientRender", root, function ( ) 
    dxDrawRectangle ( rectangleData.x, rectangleData.y, rectangleData.width, rectangleData.height, tocolor ( 0, 0, 0, 120 ) ) 
end ) 
  
addEventHandler ( "onClientClick", root, function ( _, _, x, y ) 
    if ( x >= rectangleData.x and x <= rectangleData.x + rectangleData.width and y >= rectangleData.y and y <= rectangleData.y + rectangleData.height ) then 
        outputChatBox ( "On rectangle click!" ) 
    end 
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...