GetBrowserVolume: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Client function}} {{New feature/item|3.0151|1.5|| This function gets a specific browser's volume. }} ==Syntax== <syntaxhighlight lang="lua"> bool getBrowserV...")
 
No edit summary
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Client function}}
{{Client function}}
{{New feature/item|3.0151|1.5||
{{New feature/item|3.0151|1.5.1||
This function gets a specific [[Element/Browser|browser]]'s volume.
This function returns a specific [[Element/Browser|browser]]'s volume.
}}
}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool getBrowserVolume ( webBrowser )
float getBrowserVolume ( browser webBrowser )
</syntaxhighlight>
</syntaxhighlight>
{{OOP||[[Element/Browser|browser]]:getVolume||setBrowserVolume}}
{{OOP||[[Element/Browser|browser]]:getVolume||setBrowserVolume}}
Line 18: Line 18:


==Example==
==Example==
Creates a browser in which the volume can be controlled by pressing the page-up & page-down keys
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--TODO
--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)
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{CEF_functions}}
{{CEF_functions}}

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