ShayF2 Posted February 2, 2018 Share Posted February 2, 2018 I want to center the dx elements in a certain space via math with text length and max text length. Here is my code as well as a picture to show its status. local messagesList = { '----------- Coming Soon!', 'Visit the site! http://---------/', 'Let your voice be heard! http://---------/forum/', 'READ F9!' } local sx,sy = guiGetScreenSize() local sw,sh = 1360,768 local currentMessage = 1 function dxRect(...) arg[1],arg[2],arg[3],arg[4] = arg[1]/sw*sx,arg[2]/sh*sy,arg[3]/sw*sx,arg[4]/sh*sy return dxDrawRectangle(unpack(arg)) end function dxText(...) arg[2],arg[3],arg[4],arg[5],arg[7] = arg[2]/sw*sx,arg[3]/sh*sy,(arg[4]+arg[2])/sw*sx,(arg[5]+arg[3])/sh*sy,(arg[7] or 1)/sw*sx return dxDrawText(unpack(arg)) end addEventHandler('onClientPreRender',root,function() local width = dxGetTextWidth(messagesList[currentMessage]) dxRect(1068-8,107,width+16,21,tocolor(0,0,0,192),false) dxText(messagesList[currentMessage],1068,107,width,21,tocolor(0,255,255,255),1,'default-bold','center','center',false,false,false) end) setTimer(function() if currentMessage ~= #messagesList then currentMessage = currentMessage+1 else currentMessage = 1 end end,5000,0) Link to comment
NeXuS™ Posted February 2, 2018 Share Posted February 2, 2018 What happens if you try to output the string? Link to comment
ShayF2 Posted February 2, 2018 Author Share Posted February 2, 2018 2 minutes ago, NeXuS™ said: What happens if you try to output the string? dont worry about the debug errers i took care of dem, i just cant get the dam thing centered under the hud with custom width calculated by text length. Link to comment
NeXuS™ Posted February 2, 2018 Share Posted February 2, 2018 In your place, I'd calculate the center without any text, and then just minus the textwidth/2-some pixels. Link to comment
ShayF2 Posted February 2, 2018 Author Share Posted February 2, 2018 14 minutes ago, NeXuS™ said: In your place, I'd calculate the center without any text, and then just minus the textwidth/2-some pixels. show me example that is tested to work. Link to comment
NeXuS™ Posted February 2, 2018 Share Posted February 2, 2018 Give me a second. addEventHandler('onClientPreRender',root,function() local width = dxGetTextWidth(messagesList[currentMessage]) dxRect(1068-8-width/2,250,width+16,21,tocolor(0,0,0,192),false) dxText(messagesList[currentMessage],1068-width/2,250,width,21,tocolor(0,255,255,255),1,'default-bold','center','center',false,false,false) end) This'll be centered at 1068-8. 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