GetBrowserVolume: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{Needs_Example}}
{{New feature/item|3.0151|1.5.1||
{{New feature/item|3.0151|1.5.1||
This function returns a specific [[Element/Browser|browser]]'s volume.
This function returns a specific [[Element/Browser|browser]]'s volume.

Latest revision as of 18:50, 17 March 2018

This function returns a specific browser's volume.

Syntax

float getBrowserVolume ( browser webBrowser )

OOP Syntax Help! I don't understand this!

Method: browser:getVolume(...)
Counterpart: setBrowserVolume


Required Arguments

  • webBrowser: A browser element

Returns

Returns a specific browser's volume, or false if the browser element passed to the function is invalid.

Example

Creates a browser in which the volume can be controlled by pressing the page-up & page-down keys

--In order to render the browser on the full screen, we need to know the dimensions.
local screenWidth, screenHeight = guiGetScreenSize()
 
--Let's create a new browser in remote mode.
local webBrowser = createBrowser(screenWidth, screenHeight, false, false)

-- How much we increase/decrease the volume by each time
local volumeStep = 0.1 

-- The min/max value for the browser volume. Change these if you like, but note that the minimum value is 0 and the maximum value is 1.
local MIN_VOLUME = 0
local MAX_VOLUME = 1
 
--Function to render the browser.
function webBrowserRender()
	--Render the browser on the full size of the screen.
	dxDrawImage(0, 0, screenWidth, screenHeight, webBrowser, 0, 0, 0, tocolor(255,255,255,255), true)
end

--The event onClientBrowserCreated will be triggered, after the browser has been initialized.
--After this event has been triggered, we will be able to load our URL and start drawing.
addEventHandler("onClientBrowserCreated", webBrowser, 
	function()
		--After the browser has been initialized, we can load www.youtube.com
		loadBrowserURL(webBrowser, "http://www.youtube.com")
		--Now we can start to render the browser.
		addEventHandler("onClientRender", root, webBrowserRender)
	end
)

-- Now we create a function attached to an onClientKey event handler to catch user input.
-- Here we can utilize the getBrowserVolume function to lower or increase the volume of the browser based on the clients input
function handleVolumeKeys(button, press)
    if (press) then -- Is key pressed?
        if (button == "pgup") then
		local volume = getBrowserVolume(webBrowser) + volumeStep -- Get the current browser volume and add our volume step
		if (volume) > MAX_VOLUME then return end -- Check if the current volume is bigger than max value
		setBrowserVolume(webBrowser, volume) -- Set the browser volume			
	elseif (button == "pgdn") then -- Same as previous condition except we subtract the volume step and make sure the volume value doesn't fall below minimum.
		local volume = getBrowserVolume(webBrowser) - volumeStep
		if (volume) < MIN_VOLUME then return end
		setBrowserVolume(webBrowser, volume) 		
	end
    end
end
addEventHandler("onClientKey", root, handleVolumeKeys)

See Also