https://wiki.multitheftauto.com/api.php?action=feedcontributions&user=Thiaguz&feedformat=atomMulti Theft Auto: Wiki - User contributions [en]2024-03-28T10:22:46ZUser contributionsMediaWiki 1.39.3https://wiki.multitheftauto.com/index.php?title=GuiGetScreenSize&diff=64756GuiGetScreenSize2019-12-03T15:40:42Z<p>Thiaguz: </p>
<hr />
<div>__NOTOC__<br />
{{Client function}}<br />
This function retrieves the local screen size according to the resolution they are using.<br />
<br />
==Syntax== <br />
<syntaxhighlight lang="lua"><br />
float float guiGetScreenSize()<br />
</syntaxhighlight><br />
{{OOP||[[GUI widgets|GuiElement]].getScreenSize||}}<br />
<br />
===Returns===<br />
This returns two floats representing the player's screen resolution, ''width'' and ''height''.<br />
<br />
==Example== <br />
This example checks whether a player is using a low resolution, and warns them that GUI may appear incorrect.<br />
<syntaxhighlight lang="lua"><br />
--setup a function when the resource starts<br />
function checkResolutionOnStart ()<br />
local x,y = guiGetScreenSize() --get their screen size<br />
if ( x <= 640 ) and ( y <= 480 ) then --if their resolution is lower or equal to 640x480<br />
--warn them about GUI problems.<br />
outputChatBox ( "WARNING: You are running on a low resolution. Some GUI may be placed or appear incorrectly." )<br />
end<br />
end<br />
--attach the function to the event handler<br />
addEventHandler ( "onClientResourceStart", resourceRoot, checkResolutionOnStart )<br />
</syntaxhighlight><br />
<br />
<br />
==Using guiGetScreenSize to fit GUI & DX drawing in all resolutions==<br />
To get the precise coordinates of a GUI element or DX drawings, you need to decide which edges of the screen you want to have them positioned against, then you just need to find the difference between your screen size and your position values.<br />
<br />
For example, there is a DX text. It fits on 1024x768 resolution.<br />
<syntaxhighlight lang="lua"><br />
function DXtext ()<br />
dxDrawText(tostring "Hello World!",684.0,731.0,732.0,766.0,tocolor(0,255,255,175),1.0,"bankgothic","left","top",false,false,false)<br />
end<br />
<br />
addEventHandler ( "onClientRender", getRootElement(), DXtext )<br />
</syntaxhighlight><br />
Now if you want it to fit on all resolutions. Then follow these steps:<br />
<br />
1. Add ''width'' and ''height'' variables to get GUI's screen size, here we use sWidth and sHeight.<br />
<syntaxhighlight lang="lua"><br />
local sWidth,sHeight = guiGetScreenSize() -- The variables<br />
dxDrawText( "Hello World!",684.0,731.0,732.0,766.0,tocolor(0,255,255,175),1.0,"bankgothic","left","top",false,false,false)<br />
</syntaxhighlight><br />
<br />
2. Divide each of the DX text's position values by the screen size manually (remembering the resolution is 1024x768):<br />
<br />
*'''Left''' position value is 684, 684/1024 = 0.668<br />
*'''Top''' position value is 731, 731/768 = 0.952<br />
*'''Right''' position values is 732, 732/1024 = 0.715<br />
*'''Bottom''' position value is 766, 766/768 = 0.997<br />
<br />
You may want to use a calculator to help you count.<br />
<br />
3. Now with the answer above remove all of the position values and replace it with the width or height variable multiplied by the answer. Which would be:<br />
<syntaxhighlight lang="lua"><br />
local sWidth,sHeight = guiGetScreenSize() -- The variables<br />
dxDrawText("Hello World!",sWidth*0.668, sHeight*0.952, sWidth*0.715, sHeight*0.997,tocolor(0,255,255,175),1.0,"bankgothic","left","top",false,false,false)<br />
</syntaxhighlight><br />
<br />
So the final results will be a DX text which will fit on all resolutions which will be:<br />
<syntaxhighlight lang="lua"><br />
function DXtext ()<br />
local sWidth,sHeight = guiGetScreenSize() -- The variables<br />
dxDrawText("Hello World!",sWidth*0.668, sHeight*0.952, sWidth*0.715, sHeight*0.997,tocolor(0,255,255,175),1.0,"bankgothic","left","top",false,false,false)<br />
end<br />
<br />
addEventHandler ( "onClientRender", getRootElement(), DXtext )<br />
</syntaxhighlight><br />
<br />
We can also do it by ourself doing some calculations in the code. This works in all resolution, adjusting the scale to all screen-sizes.<br />
<syntaxhighlight lang="lua"><br />
local sx, sy = guiGetScreenSize( )<br />
<br />
addEventHandler( "onClientRender", root,<br />
function( )<br />
dxDrawText( "Hello World!", sx*( 684/1024 ), sy*( 731/768 ), sx*( 732/1024 ), sy*( 766/768 ), tocolor(0,255,255,175), sx/1000*1.0,"bankgothic","left","top",false,false,false ) <br />
end<br />
)<br />
</syntaxhighlight><br />
<br />
==See Also==<br />
{{GUI_functions}}<br />
{{GUI_events}}</div>Thiaguz