Jump to content

dx calculations


Wei

Recommended Posts

  
example = { 
{"test1"}; 
{"test2"}; 
} 
  
function draw() 
    for index, value in ipairs (example) do 
        dxDrawRectangle( pos1, pos2, 100, 50 ) 
    end 
end 
addEventHandler("onClientRender", root, draw) 

Can someone explain me how I can get pos1 and pos2 to allways be in middle of screen ?

I loosing my mind with this calculations -.-

Thanks,

Wei.

Link to comment
local sx, sy = guiGetScreenSize ( ) 
local example = 
    { 
        { "test1" }, 
        { "test2" } 
    } 
  
function draw ( ) 
    local width, height = 100, 50 
    local x, y = ( sx / 2 - width / 2 ), ( sy / 2 - height / 2 ) 
    for _, value in ipairs ( example ) do 
        dxDrawRectangle ( x, y, width, height ) 
    end 
end 
addEventHandler ( "onClientRender", root, draw ) 

Link to comment
local sx, sy = guiGetScreenSize ( ) 
local example = 
    { 
        { "test1" }, 
        { "test2" } 
    } 
  
function draw ( ) 
    local width, height = 100, 50 
    local x, y = ( sx / 2 - width / 2 ), ( sy / 2 - height / 2 ) 
    for _, value in ipairs ( example ) do 
        dxDrawRectangle ( x, y, width, height ) 
        y = ( y + 10 ) 
    end 
end 
addEventHandler ( "onClientRender", root, draw ) 

Link to comment
  • Moderators

Hummm okay, I'm a bit late again xD but the code of Solidsnake doesn't center all rectangle as a whole. So if you add 5 rectangles in your table, then the 1st one will still be rendered from the middle and the last rectangles will probably rendered out of the screen.

Here is my version with a screenshot with explainations:

local sx, sy = guiGetScreenSize ( ) 
local example = 
    { 
        { "test1" }, 
        { "test2" } 
    } 
  
function draw ( ) 
    local width, height = 100, 50 
    local offsetY = 10 -- seperation between each rectangles 
    local totalHeight = (50+offsetY)*(#example)-offsetY --(-offsetY to remove seperation of the last rectangle) 
    local x = ( sx/2 - width/2 ) 
    for k, value in ipairs ( example ) do 
        local y = (sy/2 - totalHeight/2) + (height+offsetY) * (k-1) 
        dxDrawRectangle ( x, y, width, height ) 
    end 
end 
addEventHandler ( "onClientRender", root, draw ) 

The result with extra explainations:

ftr0.png

EDIT: Alright ! seems that the image can't be displayed from gyazo, is there a whitelist on that forum ? I'm gonna upload it somewhere else.

EDIT2: Changed img tag

Edited by Guest
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...