Sign in to follow this  
JeViCo

Clickable dx rectangle/text

Recommended Posts

i am looking for example of dx button (rectangle/text). I tried to find info at forum/wiki but nothing understood((

Example

(some code show/hide dx window+img+rectangle+text using key "i") --have it

(code that spawns car/changes skin on clicked dx rectangle/text --Nothing of it((

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 ) 

I found this code but it is long and execute twice in game

(noob lvl of lua + russian=poor english+stupid questions sry :3 )

Share this post


Link to post
function onClick(button, state)
	if (button == "left") and (state == "up") then
		-- check if dx-rectangle is showing here
		if isMouseInPosition( x, y, width, height ) then
			outputChatBox ( "On rectangle click!" ) 
		end
	end
end
addEventHandler( "onClientClick", root, onClick )

isMouseInPosition: https://wiki.multitheftauto.com/wiki/IsMouseInPosition

Share this post


Link to post
2 hours ago, JeViCo said:

isMouseInPosition has a nil value(

you need add this function in your script 

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

 

  • Thanks 1

Share this post


Link to post

bump. Now i this button activates even when dx window closed.

i think

if (button == "left") and (state == "up") then

should check it but nope\\\ Full code below

local screenW, screenH = guiGetScreenSize()
local x,y,z = getElementPosition ( getLocalPlayer ( ) )
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

function togglePanel()
  if (dxVisible == false) then
    dxVisible = true
	showCursor ( true )
  else
    dxVisible = false
	showCursor ( false )
  end
end
bindKey("i","down",togglePanel)

addEventHandler("onClientRender", root,
  function ()
    if (dxVisible == true) then
		dxDrawImage((screenW - 389) / 2, (screenH - 450) / 2, 389, 450, ":u_panel/sinin_art.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
        dxDrawText("Скин", 489 - 1, 288 - 1, 605 - 1, 327 - 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "center", "top", false, false, false, false, false)
        dxDrawText("Скин", 489 + 1, 288 - 1, 605 + 1, 327 - 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "center", "top", false, false, false, false, false)
        dxDrawText("Скин", 489 - 1, 288 + 1, 605 - 1, 327 + 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "center", "top", false, false, false, false, false)
        dxDrawText("Скин", 489 + 1, 288 + 1, 605 + 1, 327 + 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "center", "top", false, false, false, false, false)
        dxDrawText("Скин", 489, 288, 605, 327, tocolor(2, 218, 7, 255), 1.20, "bankgothic", "center", "top", false, false, false, false, false)
        dxDrawText("Панель Управления", 538 + 1, 163 + 1, 817 + 1, 199 + 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
        dxDrawText("Панель Управления", 538, 163, 817, 199, tocolor(0, 221, 15, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
        dxDrawText("Авто", 762 - 1, 289 - 1, 878 - 1, 327 - 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
        dxDrawText("Авто", 762 + 1, 289 - 1, 878 + 1, 327 - 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
        dxDrawText("Авто", 762 - 1, 289 + 1, 878 - 1, 327 + 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
        dxDrawText("Авто", 762 + 1, 289 + 1, 878 + 1, 327 + 1, tocolor(0, 0, 0, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
        dxDrawText("Авто", 762, 289, 878, 327, tocolor(2, 218, 7, 255), 1.20, "bankgothic", "left", "top", false, false, false, false, false)
	 end
  end
)

function onClick(button, state)
	if (button == "left") and (state == "up") then
		-- check if dx-rectangle is showing here
		if isMouseInPosition( 490, 284, 116, 47 ) then
			outputChatBox ( "Машина заспавнена!" ) --car spawn
					createVehicle(434,x,y,z)
					y = y + 5
		elseif isMouseInPosition( 737, 284, 116, 47 ) then
			outputChatBox ( "Вы сменили скин!" ) --skin change
					setElementModel ( player, 25) 
			end
		end
	end

addEventHandler( "onClientClick", root, onClick )

also i have problem with car spawn(car locked and i can't get it) and skin. I don't know how to change it(

Share this post


Link to post

You need to add the dxVisible var to your clicking event so it only executes if dxVisible is true.

Like this:

function onClick(button, state)
	if dxVisible and (button == "left") and (state == "up") then
		-- check if dx-rectangle is showing here
		if isMouseInPosition( 490, 284, 116, 47 ) then
			outputChatBox ( "Машина заспавнена!" ) --car spawn
			createVehicle(434,x,y,z)
			y = y + 5
		elseif isMouseInPosition( 737, 284, 116, 47 ) then
			outputChatBox ( "Вы сменили скин!" ) --skin change
			setElementModel ( player, 25) 
		end
	end
end
addEventHandler( "onClientClick", root, onClick )

 

  • Thanks 1

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.