GuiGetAlpha: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
 
(6 intermediate revisions by 3 users not shown)
Line 5: Line 5:
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
int guiGetAlpha ( element guiElement )
float guiGetAlpha ( element guiElement )
</syntaxhighlight>  
</syntaxhighlight>
{{OOP||[[GUI widgets|GuiElement]]:getAlpha|alpha|guiSetAlpha}}


===Required Arguments===  
===Required Arguments===  
Line 15: Line 16:


==Example==  
==Example==  
This example provides a ''fadeElement'' function, which fades roughly over a period of 4 seconds.  The user may fade in or fade out an element
This example provides a ''guiFade'' function, which allows you to fade in/out a GUI.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function fadeElement ( guiElement, state )
function guiFade( gui, state )
if state == "out" then --if the user specifies he wants to fade out an element
if state == "in" then -- This state will fade IN the GUI
local currentAlpha = guiGetAlpha ( guiElement )--get the current alpha
fadeIn = setTimer(guiFade, 50, 1, gui, state) -- We loop the function to make it lower the alpha each 50 ms
local newAlpha = currentAlpha - 4 --set the alpha to 4 less (more transparent)
alpha = guiGetAlpha(gui) -- We get the GUI's actual alpha after each loop
--ensure that the alpha is not below 0, if it is, set it to 0
guiSetAlpha(gui, alpha - 0.1) -- We set the GUI's actual alpha after each loop
if newAlpha < 0 then newAlpha = 0 end
if alpha == 0 then -- If the loop reached "0"...
--set the new alpha
guiSetVisible(gui, false) -- We set the GUI visibility to 0 so it won't be clickable or editable
guiSetAlpha ( guiElement, newAlpha )
killTimer(fadeIn) -- ... We kill the timer
--if the new alpha is not completely invisible already
fadeIn = nil -- And to make sure it doesn't exist anymore, we set it to nil
if newAlpha ~= 0 then
--call this function to fade out some more 50ms later
setTimer ( fadeElement, 50, 1, guiElement, state )
end
end
elseif state == "in" then --else, if the user specifies he wants to fade out an element
elseif state == "out" then -- This state will fade OUT the GUI
local currentAlpha = guiGetAlpha ( guiElement )--get the current alpha
guiSetVisible(gui, true) -- Since the GUI will still be click-able, we'll set it's visibility to "false"
local newAlpha = currentAlpha + 4 --set the alpha to 4 more(less transparent)
fadeOut = setTimer(guiFade, 50, 1, gui, state) -- We loop the function to make it higher the alpha each 50 ms
--ensure that the alpha is not above 255, if it is, set it to 255
alpha = guiGetAlpha(gui) -- We get the GUI's actual alpha after each loop
if newAlpha > 255 then newAlpha = 255 end
guiSetAlpha(gui, alpha + 0.1) -- We set the GUI's actual alpha after each loop
--set the new alpha
if alpha == 1 then -- If the loop reached "1"...
guiSetAlpha ( guiElement, newAlpha )
killTimer(fadeOut) -- ... We kill the timer
--if the new alpha is not completely opaque already
fadeOut = nil -- And to make sure it doesn't exist anymore, we set it to nil
if newAlpha ~= 255 then
--call this function to fade in some more 50ms later
setTimer ( fadeElement, 50, 1, guiElement, state )
end
end
end
end
end
end
-- NOTE: If you're using a button to pop up the GUI-Element. Use "guiSetEnabled" along with the button. You'll have also to set a timer in order to disable it by the time it fades, either way it will bug.
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{GUI_functions}}
{{GUI_functions}}
{{GUI_events}}

Latest revision as of 17:15, 21 November 2018

Alpha represents the transparency of a gui element. This function allows retrieval of a gui element's current alpha.

Syntax

float guiGetAlpha ( element guiElement )

OOP Syntax Help! I don't understand this!

Method: GuiElement:getAlpha(...)
Variable: .alpha
Counterpart: guiSetAlpha


Required Arguments

  • guiElement: The gui element in which you want to retrieve the alpha of.

Returns

This function returns a positive integer in between 0 and 1 of the gui element's current alpha, or false if it could not be retrieved.

Example

This example provides a guiFade function, which allows you to fade in/out a GUI.

function guiFade( gui, state )
	if state == "in" then -- This state will fade IN the GUI
	fadeIn = setTimer(guiFade, 50, 1, gui, state) -- We loop the function to make it lower the alpha each 50 ms
	alpha = guiGetAlpha(gui) -- We get the GUI's actual alpha after each loop
	guiSetAlpha(gui, alpha - 0.1) -- We set the GUI's actual alpha after each loop
	if alpha == 0 then -- If the loop reached "0"...
		guiSetVisible(gui, false) -- We set the GUI visibility to 0 so it won't be clickable or editable
		killTimer(fadeIn) -- ... We kill the timer
		fadeIn = nil -- And to make sure it doesn't exist anymore, we set it to nil
		end
	elseif state == "out" then -- This state will fade OUT the GUI
		guiSetVisible(gui, true) -- Since the GUI will still be click-able, we'll set it's visibility to "false"
		fadeOut = setTimer(guiFade, 50, 1, gui, state) -- We loop the function to make it higher the alpha each 50 ms
		alpha = guiGetAlpha(gui) -- We get the GUI's actual alpha after each loop
		guiSetAlpha(gui, alpha + 0.1) -- We set the GUI's actual alpha after each loop
		if alpha == 1 then -- If the loop reached "1"...
			killTimer(fadeOut) -- ... We kill the timer
			fadeOut = nil -- And to make sure it doesn't exist anymore, we set it to nil
		end
	end
end

-- NOTE: If you're using a button to pop up the GUI-Element. Use "guiSetEnabled" along with the button. You'll have also to set a timer in order to disable it by the time it fades, either way it will bug.

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