Skinning

As well as defining its own style for the widgets included, Nuklear supports skinning—loading a theme to change how applications look. This is a powerful feature—very similar to the themes that we saw with GTK+ and Qt, but selected by the application instead of the end user. Any nk application developer looking to set up skinning for their application may find that it isn't easy to do—this is due to the way that most configuration is expose through C structures from the underlying Nuklear API. While these elements are mostly available through the Go API binding, it requires a lot of pointer conversion and unsafe assignments that could affect the stability of your application. It would be possible, however, to write some C code and include it in your application using CGo.

The following C code is extracted from a Nuklear skinning example in case a developer wishes to include a custom skin in their application and is willing to embed C in their Go code. The example uses a single texture image that defines all of the different images that together define the theme. Firstly, the texture must be loaded into the current OpenGL context and then the individual areas identified within the loaded texture, as follows:

   glEnable(GL_TEXTURE_2D);
media.skin = image_load("../skins/gwen.png");
media.check = nk_subimage_id(media.skin, 512,512, nk_rect(464,32,15,15));
media.check_cursor = nk_subimage_id(media.skin, 512,512, nk_rect(450,34,11,11));

...

The preceding code excerpt specifies only two sub-textures where, in real-life usage, there would be many more. After the textures are loaded, we define a style struct pointer that matches the widget to theme (here, we're skinning the checkbox). The value of this pointer is then set to the location of the loaded style configuration (this is where it becomes very difficult to re-create in pure Go code). For each field in the struct, an appropriate image or color should be set:

 {struct nk_style_toggle *toggle;
toggle = &ctx.style.checkbox;
toggle->normal = nk_style_item_image(media.check);
toggle->hover = nk_style_item_image(media.check);
toggle->active = nk_style_item_image(media.check);
toggle->cursor_normal = nk_style_item_image(media.check_cursor);
toggle->cursor_hover = nk_style_item_image(media.check_cursor);
toggle->text_normal = nk_rgb(95,95,95);
toggle->text_hover = nk_rgb(95,95,95);
toggle->text_active = nk_rgb(95,95,95);}

The same technique should be applied for all widgets that will be used in the application to be skinned. This is a lot of work and even the toolkit author warns against it at this time due to its time-consuming nature! Following is the style texture for the Gwen skin and a screenshot of an application with this theme loaded:

  

The Gwen skin is used to demonstrate Nuklear skin capabilities (left); The Gwen skin in action (right)

A complete implementation can be found in the examples repository at https://github.com/vurtun/nuklear/blob/master/example/skinning.c.