Exploring Scene2D UI, TableLayout, and skins

LibGDX comes with a great feature set to easily create scene graphs. A scene graph is a hierarchically organized structure of objects similar to files and folders on a hard disk. In LibGDX, such objects are called actors. Actors can be nested to create logical groups. Grouping actors is a very useful feature, as modifications applied to a parent actor will also affect its child actors. Furthermore, each actor has its own local coordinate system, which makes it very easy to define relative offsets inside a group of actors, including position, angle of rotation, and scale.

Scene2D supports hit detection of rotated and scaled actors. LibGDX's flexible event system allows you to handle and route inputs as needed so that the parent actors can intercept inputs before they reach the child actors. Finally, the built-in action system can be used to easily manipulate actors over a period of time, creating complex effects that can execute in sequence, parallel, or in a combination of both. All this described functionality is encapsulated in the Stage class, which contains the hierarchy and distributes user-generated events. Actors can be added to and removed from it at any time. The Stage class and the Actor class both contain an act() method, which takes a delta time as its argument to do a time-based action. Calling act() on a Stage instance will cause a call of act() on every actor in the scene graph. The act() methods of Stage and Actor are basically what we already know as the update() methods, only using a different name. For more information on Scene2D, check out the official documentation at https://github.com/libgdx/libgdx/wiki/Scene2d/.

Until now, we have not used any of Scene2D's functionality in our game. Naturally, we could have implemented the game world, including its game objects, with Scene2D. However, always keep in mind that using a scene graph comes with a certain amount of overhead. LibGDX tries its best to keep the overhead at a bare minimum, such as skipping complex calculations of transformation matrices if objects do not need to be rotated or scaled. So, it really depends on what your requirements are.

As the user interface of the menu that we are going to create is rather complex, we want to make use of LibGDX's scene graph for this task. More precisely, we will use Scene2D UI. This is another implementation in LibGDX that builds on top of Scene2D and extends its functionality by providing a rich set of common and ready-to-use UI elements. In LibGDX, these UI elements are called widgets.

All the widgets currently available in Scene2D UI are Button, ButtonGroup, CheckBox, Dialog, Image, ImageButton, Label, List, ScrollPane, SelectBox, Slider, SplitPane, Stack, Window, TextButton, TextField, TextArea, Touchpad, and Tree.

Scene2D UI also supports the easy creation of new custom widgets. We will discuss a selected number of widgets as we implement them in our menu. For a complete list and description of each widget, check out the official documentation at https://github.com/libgdx/libgdx/wiki/Scene2d.ui.

In addition to Scene2D UI, LibGDX also incorporates a separate project called TableLayout. The TableLayout object makes it very easy to create and maintain dynamic (read: resolution-independent) layouts using tables. It also provides an intuitive API. A Table class provides access to the functionality of TableLayout, which is also implemented as a widget, and therefore integrates seamlessly into the concept of Scene2D UI. It is highly recommended to check out the official documentation at https://github.com/EsotericSoftware/tablelayout/.

One more important feature of Scene2D UI is the support of skins. A skin is a collection of resources used to style and display UI widgets, for example, resources can be texture regions, fonts, and colors. Typically, a skin uses texture regions that come from a texture atlas. The style definition of each widget is stored in a separate file that uses the JSON file format. For more information, check out the official documentation at https://github.com/libgdx/libgdx/wiki/Skin/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset