scene2d changes

Mario and I have been refactoring the scene2d package. This is our 2D scene graph, which is useful for simple games and complex GUIs. If you already use scene2d, updating to the latest SVN may cause a few compilation errors in your projects. We don’t make API changes lightly, so please bear with us as the library improves.

Most of the changes are for the input system, which is now easier to use. Actor has these methods:

When a touch goes down, the Group class calls hit() on each child. If a child returns non-null then it was hit and touchDown() is called on the child. If this returns true, it means that the event has been consumed and no other children should be tried. It also means that if no other actor is “focused”, the child gains focus. Being focused means that the usual system just described will be bypassed and the focused widget receives all touch events. When a touchUp occurs, the focus is lost. This mechanism allows individual actors to easily handle complex touch events.

Another important change happened to Group. This class is an Actor that has children. When a group is rotated and scaled, its children are rotated and scaled. This is the main reason to use a scene graph and it is very powerful. The cost is that every frame each group does matrix transformations which use between 27 and 108 multiplications. This is not an issue for most games, as there is rarely a need for more than a few nested groups. However, when using the scene graph to create GUIs, groups are used to layout their children and it is easy to have a dozen or more nested groups. Since it is rare to use rotation and scale in a GUI, we added a boolean named “transform” to Group. When set to false, no matrix transformations will be done. Just be sure not to use rotation or scale on a group that has transform set to false!

As a result of the Group change, TableLayout is now more efficient. Feel free to nest tables to your heart’s content!

Skin saw some major changes. It used to be serialized by hand written XML (reading only). It is now serialized with our fancy automatic JSON serialization (reading and writing), with a fraction of the old code. Besides being good exercise for the JSON stuff, now that skin supports both read and write, some sort of editor may be in the works. A skin file now looks like this:

You’ll notice this is only similar to JSON (has no quotes). This is because our JSON stuff now has multiple modes: JSON, JavaScript, and the “minimal” format above. The minimal format is easier on the eyes, and if the data is only going to be read by libgdx, it really doesn’t matter if it follows the JSON spec.

Anyway, there are two parts to a skin. The “resources” section describes a bunch of colors, fonts, texture regions, nine patches, etc. The “styles” section describes a bunch of objects that use the resources. Above you can see a BitmapFont named “default-font” is defined, as well as a few Colors. Next, a LabelStyle named “default” is defined that references the font “default-font” and the color “white”. Here is some scene2d code to use this stuff:

The skin is completely extensible, you can use your own classes in the skin file to define any kind of resources or styles you want. The serialization happens automatically, thanks to the Json class, so you don’t have to add any special code to your classes.

The TableLayout SVN repo is now linked via the libgdx repo. This means a couple package names have changed. There are also a few new additions. GestureDetector detects tap, long press, fling, pan, and zoom touch events. FlickScrollPane is a scrollable area that works great on touch screens.

TL;DR: Return true from touchDown() if you handled the event and want to receive touchDragged and touchUp events. Use Group.transform = false if your group doesn’t use rotation or scale. Skin changed and is awesome. Use the JSON shit, it rocks!

Update to the nightlies!

Update: please update the nightlies again, i found another bug related to my fix yesterday. All should be well now. Sorry folks :/

I found two pretty nasty bugs today:

  • The capacity of direct ByteBuffer instances returned to a Pixmap from native code was calculated incorrectly. This doesn’t influence your apps behaviour nor does it require more memory. It’s still nasty.
  • I forgot to set the unpack alignment to 1. That is indeed nasty. If you used RGB888 and images looked strange, that’s the reason. Sorry for that

I just finished fixing those things. I’ll put my fixes through extensive tests tomorrow and will update this post if anything bad turns up.

Make sure to get the latest nightlies and don’t forget to update your Android natives in libs/armeabi and libs/armeabi-v7a in your Android project!

On a related note: due to popular request you can now fetch the latest nightlies from this link
http://libgdx.l33tlabs.org/libgdx-latest-nightlies.zip

This should make writting scripts to update to the latest and greatest a lot easier than having to mock around with the other link that contains a date. The thing is just a symlink, the old link is still there as well of course.

I’ll probably release libgdx 0.9.2 this weekend. For real. There are quite a few improvements in the latest trunk version.

Sorry for the fuck up :/

p.s.: i’m checking the forums every 4th day now. I try my best to answer most questions. If it seems that libgdx is dormant and its creators have vanished you have the wrong impression. The oposite is true. It’s just that at the moment i’m really fucking busy with my job. Hope you understand.

Community Wiki & All good things have to end eventually

A couple of nice guys over at the forums started a community Wiki. It is not supervised by us and anyone can join! If you want to help libgdx flurish, sign up and go write some nice articles! More info here.

I just checked the ranking of my book on Amazon and it finally fell down the ranking list. I guess all good things have to end eventually, which is fine by me. Let’s see how much i actually made. So far i didn’t get any info on that…