Jump to content

Rectangles positioning


Recommended Posts

Hi, I have two rectangles and I want them to be on the center of the screen on different resolutions e.g. I have it on center on 1600x900 but when I change res to other the rectangles move. How do I keep them on center?

Code:

font = guiCreateFont("font.ttf", 10) 
fonttimes = guiCreateFont("font.ttf", 9) 
  
-- 
-- toptimes_client.lua 
-- 
  
Created = {} 
CToptimes = {} 
CToptimes.__index = CToptimes 
CToptimes.instances = {} 
g_Settings = {} 
serverInfo = {} 
px = 0 
  
sX,sY = guiGetScreenSize() 
  
toptimes = { 
    anim = 360, 
    size = {380,20}, 
    mapname = "N/A", 
    label = {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"}, 
    time = {"","","","","","","","","","",""}, 
    data = {"","","","","","","","","","",""}, 
    country = {"","","","","","","","","","",""}, 
    personal = {false,pos = 9,name="",time="",data="",country=""}, 
    state = false, 
    animState = "closed", 
    tick = getTickCount() 
} 
  
function drawToptimesIntreface() 
    if toptimes.animState == "closed" then 
        local tick = getTickCount() - toptimes.tick 
        local progress = tick/1000 
        toptimes.anim = interpolateBetween(0,0,0,390,0,0,progress,"InOutBack") 
    else 
        local tick = getTickCount() - toptimes.tick 
        local progress = tick/1000 
        toptimes.anim = interpolateBetween(390,0,0,0,0,0,progress,"OutBounce") 
    end 
    local clanColor = {128,255,0} 
    if toptimes.state then 
        local sX = sX+toptimes.anim 
        dxDrawRectangle(sX-toptimes.size[1]-980,(sY/300)-(toptimes.size[2]),toptimes.size[1],200/8,tocolor(128,255,0,200)) 
        dxDrawRectangle(sX-toptimes.size[1]-980,(sY/300)-(toptimes.size[2])+(200/8),toptimes.size[1],toptimes.size[2],tocolor(15,15,15,150)) 
        for i=1,1 do 
            dxDrawText(i..")     "..toptimes.label[i],sX-toptimes.size[1]+(toptimes.size[1]*0.03)-980,(sY/300)-(toptimes.size[2])+(200/8)+((i-1)*20),sX-(toptimes.size[1]*0.1)-10,(sY/300)-(toptimes.size[2])+(200/8)+((i)*20),tocolor(255,255,255,255),1,"default-bold","left","center",true,false,false,true) 
            dxDrawText(toptimes.time[i],sX-toptimes.size[1]+(toptimes.size[1]*0.52)-980,(sY/300)-(toptimes.size[2])+(200/8)+((i-1)*20),sX-(toptimes.size[1]*0.1)-10,(sY/300)-(toptimes.size[2])+(200/8)+((i)*20),tocolor(255,255,255,255),1,"default-bold","left","center",true,false,false,true) 
            if toptimes.country[i] ~= "" then 
                if fileExists(":admin/client/images/flags/"..toptimes.country[i]..".png") then 
                    dxDrawImage(sX-toptimes.size[1]+(toptimes.size[1]*0.75)-980,(sY/300)-(toptimes.size[2])+(200/8)+((i-1)*20)+3,16,11,":admin/client/images/flags/"..toptimes.country[i]..".png",tocolor(255,255,255,255)) 
                    dxDrawText(toptimes.country[i],sX-toptimes.size[1]+(toptimes.size[1]*0.75)-960,(sY/300)-(toptimes.size[2])+(200/8)+((i-1)*20),sX-(toptimes.size[1]*0.1)-10,(sY/300)-(toptimes.size[2])+(200/8)+((i)*20),tocolor(255,255,255,255),1,"default-bold","left","center",true,false,false,true) 
                    dxDrawImage(sX-toptimes.size[1]+(toptimes.size[1]*0.75)-940,(sY/300)-(toptimes.size[2])+(200/8)+((i-1)*20)+1,18,18,":record_on_map/1.png",tocolor(255,255,255,255)) 
                    dxDrawImage(sX-toptimes.size[1]+(toptimes.size[1]*0.75)-1272,(sY/300)-(toptimes.size[2])+(200/8)+((i-1)*20)+1,50,30,":record_on_map/2.png",tocolor(255,255,255,255)) 
                end 
            end 
        end 
    end 
end 
addEventHandler("onClientRender",getRootElement(),drawToptimesIntreface) 
  
  
  
  
--------------------------------------------------------------------------- 
-- Client 
-- Handle events from Race 
-- 
-- This is the 'interface' from Race 
-- 
--------------------------------------------------------------------------- 
  
addEventHandler('onClientResourceStart', g_ResRoot, 
    function() 
        triggerServerEvent('onLoadedAtClient_tt', g_Me) 
        topTimePersonalLabel = false 
        chatBlock = false 
    end 
) 
  
addEvent('onClientMapStarting', true) 
addEventHandler('onClientMapStarting', getRootElement(), 
    function(mapinfo) 
        outputDebug( 'TOPTIMES', 'onClientMapStarting' ) 
        if g_CToptimes then 
            g_CToptimes:onMapStarting(mapinfo) 
            chatBlock = false 
        end 
    end 
) 
  
addEvent('onClientMapStopping', true) 
addEventHandler('onClientMapStopping', getRootElement(), 
    function() 
        outputDebug( 'TOPTIMES', 'onClientMapStopping' ) 
        if g_CToptimes then 
            g_CToptimes:onMapStopping() 
        end 
    end 
) 
  
addEvent('onClientPlayerFinish', true) 
addEventHandler('onClientPlayerFinish', getRootElement(), 
    function() 
        outputDebug( 'TOPTIMES', 'onClientPlayerFinish' ) 
        if g_CToptimes then 
            openToptimes() 
            setTimer(closeToptimes,12000,1) 
        end 
    end 
) 
  
addEvent('onClientSetMapName', true) 
addEventHandler('onClientSetMapName', getRootElement(), 
    function(manName) 
        if g_CToptimes then 
            g_CToptimes:setWindowTitle(manName) 
        end 
    end 
) 
  
  
function updateSettings(settings, playeradmin) 
     
end 
  
  
--------------------------------------------------------------------------- 
-- 
-- CToptimes:create() 
-- 
-- 
-- 
--------------------------------------------------------------------------- 
function CToptimes:create() 
    local id = #CToptimes.instances + 1 
    CToptimes.instances[id] = setmetatable( 
        { 
            id = id, 
            hasTimes        = false          
        }, 
        self 
    ) 
  
    return CToptimes.instances[id] 
end 
  
function CToptimes:setWindowTitle( mapName ) 
    toptimes.mapname = mapName 
end 
  
function string.findIgnoreCase(haystack,needle) 
    return string.find(string.lower(haystack),string.lower(needle),1,true) 
end 
  
  
function CToptimes:onMapStarting(mapinfo) 
    self:setWindowTitle( mapinfo.name ) 
    self.hasTimes = true 
  
    if self.hasTimes then 
        self:doToggleToptimes( true ) 
        openToptimes() 
        setTimer(closeToptimes,12000,1) 
    else 
        self:doToggleToptimes( false ) 
        closeToptimes() 
    end 
end 
  
function CToptimes:onMapStopping() 
    self:setWindowTitle( '' ) 
    closeToptimes() 
end 
  
function CToptimes:doAutoShow() 
    self.bAutoShow = true 
    self:updateShow() 
end 
  
function CToptimes:updateShow(bOn) 
    self:enableToptimeUpdatesFromServer(bOn) 
end 
  
function CToptimes:enableToptimeUpdatesFromServer( bOn ) 
    triggerServerEvent('onClientRequestToptimesUpdates', g_Me, bOn, self.clientRevision ) 
end 
  
function CToptimes:doOnServerSentToptimes( data, serverRevision, playerPosition ) 
    toptimes.personal[1] = false 
    for i=1,1 do 
        toptimes.label[i] = data[i].playerName 
        local timeText = data[i].timeText 
        if timeText:sub(1,1) == '0' then 
            timeText = '  ' .. timeText:sub(2) 
        end 
        toptimes.time[i] = timeText 
        toptimes.data[i] = string.sub(data[i].dateRecorded,1,10) 
        toptimes.country[i] = data[i].country 
    end 
    if playerPosition then 
        if not chatBlock then 
            chatBlock = true 
        end 
        if playerPosition > 8 then 
            toptimes.personal[1] = true 
            toptimes.personal.pos = playerPosition 
            toptimes.personal.name = data[playerPosition].playerName 
            local timeText = data[playerPosition].timeText 
            if timeText:sub(1,1) == '0' then 
                timeText = '  ' .. timeText:sub(2) 
            end 
            toptimes.personal.time = timeText 
            toptimes.personal.data = string.sub(data[playerPosition].dateRecorded,1,10) 
            toptimes.personal.country = data[playerPosition].extra 
        end 
    end 
    if not chatBlock then 
        chatBlock = true 
    end 
end 
  
  
function onServerSentToptimes( data, serverRevision, playerPosition ) 
    g_CToptimes:doOnServerSentToptimes( data, serverRevision, playerPosition ) 
end 
  
function CToptimes:doToggleToptimes(bOn) 
    self:updateShow(bOn) 
    toptimes.state = bOn 
end 
  
  
  
  
--------------------------------------------------------------------------- 
-- Global instance 
--------------------------------------------------------------------------- 
g_CToptimes = CToptimes:create() 

Link to comment
X = 545 --Size X of your image or text or etc. 
Y = 545 --Size Y of your image or text or etc. 
sWidth, sHeight = guiGetScreenSize() --Get the client's screen size 
pX = (sWidth/2)-(X/2) --Half the screen - Half the size 
pY = (sHeight/2)-(Y/2) --Half the screen - Half the size 

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