The android APIs are a strange and confusing place: onThis(), onThat(), onTheOther(), the ubiquitous Context and the multipurpose Intent, the lack of javadoc-style usage information in the docs, etc, etc. It all got too much for me, and so I scurried off through GLSurfaceView and android.opengl and pretended I was safe, back in nice familiar LWJGL.
However, I still need some kind of interface for user preferences and the like. I’m buggered if I’m going to write/integrate an OpenGL widget library when PreferenceActivity is right there, but:
- Hand writing XML every time I add a variable is not my idea of fun
- It’s not clear how you get at the results of hierarchical preference structures
- What if I want more than primitive and String types?
So I sallied forth into unknown territory and carved out an automagical PreferenceActivity generator. Annotate your object graph to form a tree structure of variables and call configure. Your variable tree will be reflectively extracted, encoded, and used to create a PreferenceActivity where you can change values and load and save configurations.
When you’re done with the preferences, control will be returned to your activity and the configuration will be applied via the onActivityResult callback. There’s also a mechanism to save and load configurations programmatically.
See here for an example of annotation usage, and here for launch and callback usage. The resulting gui looks like this
http://preflect.googlecode.com/svn/trunk/PreflectTest/images/device.png
with nice appropriate widgets/input methods
Out of the box, it’ll handle ints, floats, Strings, enums, packed-int colours, Points and trigger void-return/no-argument methods. It’ll also handle encapsulated variables if you annotate the get and set methods.
You’ve got full control over the order and grouping of widgets
If you want to control a more complex datatype in a single widget, you’ll have to create a VariableType implementation. For instance, here is a VariableType to control a Vector2f.
You can also override variable type for the purposes of widget selection: I store colours as packed integers, but it’s not very helpful to edit them as such. Instead I use WidgetHint and ColourVarType to let me type in r,g,b,a quads.
Project is over here, javadoc is over here, BSD-licence-free to a good home.
In addition: the android libraries aren’t so scary any more
Edit: I’ve moved this project into its own repository, and named it. Tremble at the feet of Preflect!