Nodes¶
The Node Tree¶
libavg layout is based on nodes that represent things to display on the screen. These nodes are organized in a hierarchial structure - a tree. At the root of the tree is an avg node
. This is very similar to html layout. In html, nodes like div
structure the page and nodes like p
or img
display content. libavg also has div nodes, but the content nodes are centered around graphics and include image, video, text and camera nodes as well as nodes that draw vector graphics. The non-div nodes are named leaves.
This tree (also called the scene graph) describes the logical and spatial layout of the nodes. It can be initialized either by using an xml screen layout file or by creating nodes in python. Parents of nodes influence the rendering of the child nodes. Again, this is very similar to html layout: div nodes structure the rendering area and determine where the children are rendered. This concept is similar to what is called the DOM (Document Object Model) in xml and the scene graph in 3D engines.
One very positive consequence of this design is that the system always knows what to display. The scene graph tells the libavg engine what media elements should be displayed and what their properties are. The user of the library does not need to specify what should to be displayed unless it changes. Besides making development easier, this design allows major speed optimizations in the rendering engine, because the engine has a global view of what needs to be done. The technical term for this behaviour is retained-mode graphics: The system retains the state of the display.
Creating nodes¶
Besides specifying the libavg nodes to use in an xml file like in the Hello World example, nodes can be created in python. There are several ways to do this. The simplest one uses standard python constructors:
1newNode = avg.WordsNode(text='Hello libavg', pos=(10,30),
2 parent=player.getRootNode())
If you add this code to the minimal.py example, you should see the new text after you run the script. parent can be any div node. In this case, it's the root of the scenegraph.
You can also create nodes using xml:
Alternative createNode:
1newNode = player.createNode("""<words text="Hello libavg" pos="(10,30)"
2 />""")
Using the second syntax, whole node trees can be constructed with one call to createNode
.