xyzii Posted March 15, 2017 Share Posted March 15, 2017 (edited) So, I'm in middle of making a dx GUI script. Currently working on window, got it done except that I can't figure out the math to position the text on top of the GUI. I need to calculate the middle position (so it works for every size) of a DX Rectangle by using the starting point of the rectangle, and its' length. I guess screen size could be used with this but I can't figure it out. Start X = 901, Width X = 119 Thanks. Edited March 15, 2017 by xyzii Link to comment
Administrators Lpsd Posted March 15, 2017 Administrators Share Posted March 15, 2017 (edited) local screenX,screenY = guiGetScreenSize() local dxWidth,dxHeight = 901,119 local centerX, centerY = (screenX / 2) - (dxWidth / 2), (screenY / 2) - (dxHeight / 2) Here's what we're doing in this example. screenX, and screenY hold the width and height of the users screen. If we divide both those values by 2, we find the center of the users screen. Next we need your dx/GUI windows width & height, as you can see it's stored in dxWidth and dxHeight Now we calculate the center position for the GUI. If we were just to use screenX and screenY (both halved) to position the GUI window, it would be offset slightly from the center. We get the center of the users screen (screenX), and take away half the width of the GUI (dxWidth), this removes the offset of the GUI window. We do the same with screenY and dxHeight. These are stored in centerX, and centerY. Example below, to explain the 'offset' and why we have to do the maths. For your purposes to place it on the top, instead of in the exact center, you could slightly adjust the code as so: local screenX,screenY = guiGetScreenSize() local dxWidth,dxHeight = 901,119 local multi = 1.5 local centerX, centerY = (screenX / 2) - (dxWidth / 2), (screenY / 2) - (dxHeight * multi) Now you should be able to use centerX and centerY for the x and y positioning of your GUI. Play around with the value in 'multi' to make the GUI higher/lower on the screen. Hope this helps! Edited March 15, 2017 by LopSided_ 1 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