Forrest Posted January 13, 2014 Share Posted January 13, 2014 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
GTX Posted January 13, 2014 Share Posted January 13, 2014 Try: local checkX = ( x > sX/4 and x < sX/4+sX/8 ) local checkY = ( y > sY/4 and y < sY/4+sY/8 ) Link to comment
Forrest Posted January 13, 2014 Author Share Posted January 13, 2014 What I posted already works, but I'm wondering how the maths is worked out and what is what. But thanks. Link to comment
MTA Team 0xCiBeR Posted January 13, 2014 MTA Team Share Posted January 13, 2014 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
Forrest Posted January 13, 2014 Author Share Posted January 13, 2014 Yeah, I understand that part. But how is the screen position INSIDE the rectangle worked out? Thanks for your help. Link to comment
Dealman Posted January 13, 2014 Share Posted January 13, 2014 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 0xCiBeR Posted January 13, 2014 MTA Team Share Posted January 13, 2014 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 @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
Forrest Posted January 13, 2014 Author Share Posted January 13, 2014 Thanks for the help lads, just got in and booted up my dev server and gave it a shot, worked it all out etc. Link to comment
MTA Team 0xCiBeR Posted January 13, 2014 MTA Team Share Posted January 13, 2014 Thanks for the help lads, just got in and booted up my dev server and gave it a shot, worked it all out etc. Your welcome. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now