NodeAttributes

Version 1 (admin, 20/03/2012 14:29)

1 1 admin
h1. Node Attributes
2 1 admin
3 1 admin
{{>toc}}
4 1 admin
5 1 admin
libavg gives you full access to all nodes you defined using _attributes_. The syntax will appear very familiar to people who have done some html/javascript scripting.
6 1 admin
7 1 admin
h1. Attribute Example
8 1 admin
9 1 admin
If you modify minimal.py from the [[HelloWorld|Hello World]] page to change the attribute of a node, the display will change as well:
10 1 admin
11 1 admin
_attributes.py:_
12 1 admin
13 1 admin
<pre><code class="python">
14 1 admin
#!/usr/bin/env python
15 1 admin
# -*- coding: utf-8 -*-
16 1 admin
17 1 admin
from libavg import avg
18 1 admin
19 1 admin
player = avg.Player.get()
20 1 admin
21 1 admin
canvas = player.createMainCanvas(size=(640,480))
22 1 admin
rootNode = canvas.getRootNode()
23 1 admin
node = avg.WordsNode(pos=(10,10), font="arial", 
24 1 admin
        text="Hello World", parent=rootNode)
25 1 admin
print node.x
26 1 admin
node.x = 200
27 1 admin
player.play()
28 1 admin
</code></pre>
29 1 admin
30 1 admin
If you run this script, the x position of the node will be output on the console and the text will be moved 190 pixels to the right. You can change almost all attributes of all node types in this way. We're moving the text before starting playback, so there is no movement visible on the display. To make things happen during playback, you need to react to events as described under [[Events]].
31 1 admin
32 1 admin
h1. Common Attributes
33 1 admin
34 1 admin
There are several attribute types which are common to all or almost all nodes. For instance, the x and y attributes determine the position of a @node@. @Width@ and @height@ attributes can be used to specify a size (if none are given, the native size of the medium is used instead). Alternatively, the @pos@ and @size@ attributes can be used. @opacity@ is a number between 0 and 1 that determines how transparently the node is rendered: 0 is completely transparent, 1 completely opaque. Nodes can also have a rotation attribute that rotates the medium. The id attribute of a node can be used to access it from python by using the @Player.getElementByID()@ function.
35 1 admin
36 1 admin
One warning: python allows you to add any attributes you want to objects. This includes libavg nodes. That means that if you misspell a name when setting an attribute, you will not get an error. Instead, there will be a new attribute with a strange name - in effect, your code will be silently ignored.
37 1 admin
38 1 admin
The properties defined for the various node types are descibed in the "libavg reference":http://www.libavg.de/reference/svn/.
39 1 admin
40 1 admin
h1. Image Nodes
41 1 admin
42 1 admin
Image nodes are used to put a bitmap on the screen. The href attribute of an image node contains the filename of a bitmap to load on start. This file can be in a variety of formats, including png, tiff, bmp and jpeg. Alpha channels are fully supported, so images with transparency are displayed with elements below them visible according to the transparency information in the image.
43 1 admin
44 1 admin
h1. Words Nodes
45 1 admin
46 1 admin
Words nodes support simple text as well as formatted text. UTF-8 and right-to-left languages are fully supported, so text in Japanese, Russian and Arabic should pose no problems. Formatted text supports full paragraph layout and is authored like this:
47 1 admin
48 1 admin
_Words Formatting_
49 1 admin
50 1 admin
<pre><code class="python">
51 1 admin
avg.WordsNode(pos=(10,10), width=70, text="Text in <i>italics</i>",
52 1 admin
        parent=rootNode)
53 1 admin
</code></pre>
54 1 admin
55 1 admin
All text attributes - font, size, bold, italic, super- and subscripting, as well as some others - can be changed in mid-paragraph. A full description of the formatting language is at http://developer.gnome.org/pango/stable/PangoMarkupFormat.html.