Jump to content

Interactive dX Elements


Forrest

Recommended Posts

I've been contemplating learning how to script interactive dX GUI's for a while due to the better customization than the original MTA GUI's.

I found a small snippet floating around on the forum and decided to check it out, however the mathematic equation to check whether the cursor is within the rectangle has slightly confused me, if someone could help, that would be great.

local sX, sY = guiGetScreenSize() 
dxDrawRectangle(sX/4, sY/4, sX/8, sY/8, tocolor(0,255,0,255), false) 
  
local checkX = ( x > sX/4 and x < 2*sX/4-sX/8 ) 
local checkY = ( y > sY/4 and y < 2*sY/4-sY/8 ) 

The checkX and checkY math equations are what I'm having trouble with, if someone could explain how it works that would be great, thanks.

Link to comment
  • MTA Team

You are not posting the full script, anyway, what it is doing there is checking with "onClientClick" this variables:

string button, string state, int absoluteX, int absoluteY, float worldX, float worldY, float worldZ, element clickedWorld 

The X and Y positions in your script refer to the absoluteX and absoluteY positions in the event variables.

Example this would be the same thing but, with the onClientClick Variables.:

  
local sX, sY = guiGetScreenSize() 
addEventHandler ( "onClientClick", getRootElement(), 
function (button,state,absoluteX,absoluteY) 
local checkX = ( absoluteX > sX/4 and absoluteX < 2*sX/4-sX/8 ) 
local checkY = ( absoluteY > sY/4 and absoluteY < 2*sY/4-sY/8 ) 
end) 
function draw() 
dxDrawRectangle(sX/4, sY/4, sX/8, sY/8, tocolor(0,255,0,255), false) 
end 
addEventHandler ( "onClientRender", getRootElement(), draw ) 

So what it actually does, is that when you click on somewhere in the screen, the event handler triggers a function that checks the position of that click and resolves if you are actually clicking the dxRectangle or not.

Link to comment

If you're like me, and hate math. You could simply make use of a similar workaround as I do.

I simply create a regular GUI with the same shape as my DX Drawings, then set its alpha to 0. That way I can manipulate my DX Drawings using the already available GUI Functions/Events. :)

Link to comment
  • MTA Team
If you're like me, and hate math. You could simply make use of a similar workaround as I do.

I simply create a regular GUI with the same shape as my DX Drawings, then set its alpha to 0. That way I can manipulate my DX Drawings using the already available GUI Functions/Events. :)

+1 :D

@Forrest:

  
local checkX = ( absoluteX > sX/4 and absoluteX < 2*sX/4-sX/8 ) 
local checkY = ( absoluteY > sY/4 and absoluteY < 2*sY/4-sY/8 ) 

The sX/4 and sX/8 and sY/4 and sY/8 are the position and boundaries of the dxRectangle. So that's why you use it in your check function.What you do, is remove the position used to create the dxRectangle.

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