Pyglet-gui Theme API defines a systematic approach for mapping a set of resources (e.g. “image.png”) and attributes (e.g. color, padding) to lists of vertices and vertex attributes.
The API works as follows:
- The user defines a set of attributes and sources of static resources in a JSON file;
- A set of
Parserstranslate that to
theme.Theme, a nested dictionary, holds these templates with a unique identifier by a path (e.g. [‘button’, ‘up’])
theme.Themeis passed to the
Viewers load concrete graphical elements,
GraphicElement using the path.
This document explains how this API works in detail. It starts by explaining Graphic elements, goes to Templates, Parsers, Theme, and ends in the JSON file.
A graphical element is a subclass of
pyglet_gui.core.Rectangleand an abstract class that represents something with a set of vertices and a set of rules to assign a set of attributes (e.g. color, texture coordinate) to those vertices.
A GraphicalElement is normally instantiated by a
templates.Template. The initialization needs a batch and a group to assign its vertices to a group in the batch.
A graphical element provides three methods for accessing its size:
Returns the tuple (x, y, width, height) with its region.
Returns the tuple (width, height) with the size this element.
Returns the tuple (width, height) with the size required for this element.
After the element is initialized, its size and position can be updated using
update(x, y, width, height)¶
Updates the position and size of the graphics, updating its vertex list in the Batch.
When it is no longer needed, it can be destroyed using
Removes the vertex list from the Batch.
Pyglet-gui provides two concrete implementations of a Graphical element:
A subclass of
GraphicElementrepresenting a rectangle of vertices with a texture.
For generating graphical elements, Pyglet-gui uses the concept of template.
An abstract class that provides the method
generate()to return a new instance of a
elements.GraphicalElement(or subclass of).
A template is normally instantiated by a Parser, when the Theme is being loaded.
generate(color, batch, group)¶
Returns a new instance of a
elements.GraphicalElement. It is an abstract method.
Pyglet-gui provides two concrete implementations of templates:
A parser is a class responsible for parsing elements during the Theme loading. The Theme has a set of parsers and they read “string-keys” and interpret the values of those keys into a
This abstract method receives a string and returns a boolean value when it is able to interpret that key. If two parsers accept the same key, the first in the list of parsers in the Theme is chosen.