GuiGetScreenSize: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(19 intermediate revisions by 13 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
{{Client function}}
This fake function is for use with blah & blah and does blahblahblabhalbhl
This function retrieves the local screen size according to the resolution they are using.


==Syntax==  
==Syntax==  
<!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
float float guiGetScreenSize ( )
float float guiGetScreenSize()
</syntaxhighlight>  
</syntaxhighlight>
{{OOP||GuiElement.getScreenSize||}}
 
===Returns===
This returns two floats representing the player's screen resolution, ''width'' and ''height''.
 
==Example==
This example checks whether a player is using a low resolution, and warns them that GUI may appear incorrect.
<syntaxhighlight lang="lua">
--setup a function when the resource starts
function checkResolutionOnStart ()
local x,y = guiGetScreenSize() --get their screen size
if ( x <= 640 ) and ( y <= 480 ) then --if their resolution is lower or equal to 640x480
--warn them about GUI problems.
outputChatBox ( "WARNING: You are running on a low resolution.  Some GUI may be placed or appear incorrectly." )
end
end
--attach the function to the event handler
addEventHandler ( "onClientResourceStart", resourceRoot, checkResolutionOnStart )
</syntaxhighlight>
 
 
==Using guiGetScreenSize to fit GUI & DX drawing in all resolutions==
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.


===Required Arguments===
For example, there is a DX text. It fits on 1024x768 resolution.
<!-- List each argument one per line. This should be the argument's name as in the argument list above, NOT the argument's data type -->
<syntaxhighlight lang="lua">
*'''argumentName:''' description
function DXtext ()
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)
end


<!-- Only include this section below if there are optional arguments -->
addEventHandler ( "onClientRender", getRootElement(), DXtext )
===Optional Arguments===
</syntaxhighlight>
{{OptionalArg}}
Now if you want it to fit on all resolutions. Then follow these steps:
*'''argumentName2:''' description
*'''argumentName3:''' description


===Returns===
1. Add ''width'' and ''height'' variables to get GUI's screen size, here we use sWidth and sHeight.
<!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check -->
<syntaxhighlight lang="lua">
Returns ''true'' if blah, ''false'' otherwise.
local sWidth,sHeight = guiGetScreenSize() -- The variables
*x
dxDrawText( "Hello World!",684.0,731.0,732.0,766.0,tocolor(0,255,255,175),1.0,"bankgothic","left","top",false,false,false)
*y
</syntaxhighlight>
==Example==
 
<!-- Explain what the example is in a single sentance -->
2. Divide each of the DX text's position values by the screen size manually (remembering the resolution is 1024x768):
This example does...
 
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized -->
*'''Left''' position value is 684, 684/1024 = 0.668
*'''Top''' position value is 731, 731/768 = 0.952
*'''Right''' position values is 732, 732/1024 = 0.715
*'''Bottom''' position value is 766, 766/768 = 0.997
 
You may want to use a calculator to help you count.
 
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:
<syntaxhighlight lang="lua">
local sWidth,sHeight = guiGetScreenSize() -- The variables
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)
</syntaxhighlight>
 
So the final results will be a DX text which will fit on all resolutions which will be:
<syntaxhighlight lang="lua">
function DXtext ()
local sWidth,sHeight = guiGetScreenSize() -- The variables
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)
end
 
addEventHandler ( "onClientRender", getRootElement(), DXtext )
</syntaxhighlight>
 
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.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--This line does...
local sx, sy = guiGetScreenSize( )
blabhalbalhb --abababa
 
--This line does this...
addEventHandler( "onClientRender", root,
mooo
function( )
    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 )
end
)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
<!-- Change FunctionArea to the area that this function is in on the main function list page, e.g. Server, Player, Vehicle etc -->
{{GUI_functions}}
{{FunctionArea_functions}}
{{GUI_events}}
[[Category:Need_Syntax]]  -- leave this until syntax is available. Cannot document the function or event without syntax.
[[Category:Incomplete]] -- leave this unless you complete the function

Latest revision as of 05:37, 6 November 2023

This function retrieves the local screen size according to the resolution they are using.

Syntax

float float guiGetScreenSize()

OOP Syntax Help! I don't understand this!

Method: GuiElement.getScreenSize(...)


Returns

This returns two floats representing the player's screen resolution, width and height.

Example

This example checks whether a player is using a low resolution, and warns them that GUI may appear incorrect.

--setup a function when the resource starts
function checkResolutionOnStart ()
	local x,y = guiGetScreenSize() --get their screen size
	if ( x <= 640 ) and ( y <= 480 ) then --if their resolution is lower or equal to 640x480
		--warn them about GUI problems.
		outputChatBox ( "WARNING: You are running on a low resolution.  Some GUI may be placed or appear incorrectly." )
	end
end
--attach the function to the event handler
addEventHandler ( "onClientResourceStart", resourceRoot, checkResolutionOnStart )


Using guiGetScreenSize to fit GUI & DX drawing in all resolutions

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.

For example, there is a DX text. It fits on 1024x768 resolution.

function DXtext ()
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)
end

addEventHandler ( "onClientRender", getRootElement(), DXtext )

Now if you want it to fit on all resolutions. Then follow these steps:

1. Add width and height variables to get GUI's screen size, here we use sWidth and sHeight.

local sWidth,sHeight = guiGetScreenSize() -- The variables
dxDrawText( "Hello World!",684.0,731.0,732.0,766.0,tocolor(0,255,255,175),1.0,"bankgothic","left","top",false,false,false)

2. Divide each of the DX text's position values by the screen size manually (remembering the resolution is 1024x768):

  • Left position value is 684, 684/1024 = 0.668
  • Top position value is 731, 731/768 = 0.952
  • Right position values is 732, 732/1024 = 0.715
  • Bottom position value is 766, 766/768 = 0.997

You may want to use a calculator to help you count.

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:

local sWidth,sHeight = guiGetScreenSize() -- The variables
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)

So the final results will be a DX text which will fit on all resolutions which will be:

function DXtext ()
local sWidth,sHeight = guiGetScreenSize() -- The variables
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)
end

addEventHandler ( "onClientRender", getRootElement(), DXtext )

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.

local sx, sy = guiGetScreenSize( )

addEventHandler( "onClientRender", root,
	function( )
	     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 ) 
	end
)

See Also

General functions

Browsers

Buttons

Checkboxes

Comboboxes

Edit Boxes

Gridlists

Memos

Progressbars

Radio Buttons

Scrollbars

Scrollpanes

Static Images

Tab Panels

Tabs

Text Labels

Windows

Input

GUI