Jump to content

absolute to relative in GUIs


darhal

Recommended Posts

Hello,

I need to know how to convert absolute values for GUIs to relative ones ? I see function in guieditor do smthg like this but the resource is too huge and I cant find this function out can anyone help me ?

thx all :)

Try to use sth like this

function ConvertAbsoluteToRelative( X, Y ) 
    local rX, rY = guiGetScreenSize() 
    local x = math.floor(X*rX/1280) 
    local y = math.floor(Y*rY/720) 
    return x, y 
end 

Link to comment

I use always this method:

local sizeX, sizeY = 400, 500 -- Window size 
local x, y = guiGetScreenSize() -- get screen size 
local myGui = guiCreateWindow(x/2-sizeX/2, y/2-sizeY/2, sizeX, sizeY, "My gui", false) -- It's relative! 

Link to comment

I suppose you could try it with a combination of guiGetPosition, guiGetSize and guiGetScreenSize. First, you get the absolute values of the GUI element using guiGetPosition and guiGetSize, then simply divide them by the values you got from guiGetScreenSize to get the relative positions.

Keep in mind that, since you are basically converting absolute values to relative ones, said relatives values will be tailored to the resolution the absolute values where drawn with. So, just because a GUI element was in the top left corner with a resolution of 800x600, it doesn't mean it will be in same corner too when using a resolution of 1366x768.

Link to comment

This is all very simple math. Design your interface in absolute values, and then you divide those absolute values by the resolution you designed it in, for example

local screenW, screenH = guiGetScreenSize() 
  
-- Absolute - created in 1920x1080 resolution 
dxDrawImage(30, 247, 357, 89, "", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
-- Same, but Relative 
dxDrawImage((30/1920)*screenW, (247/1080)*screenH, (357/1920)*screenW, (89/1080)*screenH, "Textures/Button1_Active.dds", 0, 0, 0, tocolor(255, 255, 255, 255), false) 

Of course doing it like this means it would have to do that math every frame, so you could calculate it manually and just do it like this;

dxDrawImage(0.015625*screenW, 0.2287037037037037*screenH, 0.1859375*screenW, 0.0824074074074074*screenH, "Textures/Button1_Active.dds", 0, 0, 0, tocolor(255, 255, 255, 255), false) 

You can use repl.it to quickly test Lua scripts. And you can then use this script on that website to quickly convert it so you can just copy and paste it.

local sX, sY, wX, hY = 30, 247, 357, 89; -- Paste the absolute values here 
local sourceWidth = 1920;    -- Change those to the source values. 
local sourceHeight = 1080;   -- Change those to the source values. 
local nSX, nSY, nWX, nHY = (sX/sourceWidth), (sY/sourceHeight), (wX/sourceWidth), (hY/sourceHeight); 
  
print(tostring(nSX).."*screenW, "..tostring(nSY).."*screenH, "..tostring(nWX).."*screenW, "..tostring(nHY).."*screenH"); 

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