Changes

Jump to navigation Jump to search

platform.withGC

1,791 bytes added, 21:53, 6 June 2012
Created page with "platform.'''withGC''' is a function that is part of the platform library. Executes ''function(args)'' within a dummy graphics context and returns all retu..."
platform.'''withGC''' is a function that is part of the [[:Category:platform|platform]] library.

Executes ''function(args)'' within a dummy graphics context and returns all return values from ''function()''. <br />
It is typically used to measure pixel lengths and heights of strings when a normal graphics context is not available. This may be the case when creating new text elements when the script app is initialized. A graphics context is available only during paint events, and that may be too late to create and size the containers for text fields. <br />
This graphics context should not be used to draw graphics since it is not associated with a window. 
<br />

{{Since|3.2}}

== Syntax ==
platform.'''withGC'''(func, funcArgs...)

{| class="wikitable"
|-
! Parameter !! Type !! Description
|-
| <u><center>func</center></u> || function || name of function to call
|-
| <u><center>funcArgs</center></u> || (any) || The arguments of the function ''func''
|-
|}

== Example ==
Here is an example of using withGC() to get the pixel length and height of a string :
<syntaxhighlight>function setFont(family, style, size, gc)
return gc:setFont(family, style, size) -- Sets the font to use
end
function getHeightWidth(str, gc)
width = gc:getStringWidth(str) -- Gets the pixel length of str
height = gc:getStringHeight(str) -- Gets the pixel height of str
return height, width
end
platform.withGC(setFont, 'serif', 'b', 12)
height, width = platform.withGC(getHeightWidth, 'Hello World')</syntaxhighlight>
Note: You could combine the two functions above into a single one in order to avoid calling withGC() twice, but that is not required since the dummy graphics context remembers its state.


== See also ==
*[[on.paint]](gc)


[[Category:platform]]

Navigation menu