ClanLib

Creating a custom GUI style

ClanGUI contains a powerful mechanism to code custom themes, or styles as we call them in ClanGUI. Styles are very flexible, yet very easy to implement. This document will guide you through creation of a custom style.

Styles overview

Each component in ClanGUI is clearly separated into logic and presentation. When creating a new style for a component (for example CL_Button), you create a new class which does the drawing of a component - the logic is still the same between different styles.

A CL_StyleManager is used to attach styles to each component available. So in order to create a new style you need two parts - a stylemanager, and then the style-classes for each component you want a new look for.

When do I need to create my own GUI style?

The standard GUI style in ClanGUI very much resembles any desktop-gui used in applications. This doesn't always fit with games.

Some of the components in the default GUI style supports surfaces, like CL_Button. So, if you only wants some nice-looking buttons in your game which use predrawed pictures, you can still use the default style. But if you want a custom-looking inputbox or scrollbar, you need to create a custom style.

Creating a stylemanager

Create a new file called stylemanager_coolstyle.h, and insert the following code into it:

The new stylemanager (CoolTheme) inherits the CL_StyleManager_Boring. This means it will use the default style for any components you don't define a new style for. Note that it is not neccessary to inherit from CL_StyleManager_Boring - you could also inherit from CL_StyleManager, but if you then try to use a component for which you haven't created a style for, nothing will be drawn for the component. You could also use the CL_StyleManager_Silver for a slightly fancier default style.

The connect_styles() is the function which attaches the styles to the various components. Make sure you inherit this function.

Next, create a new file called stylemanager_coolstyle.cpp, and insert the following code into it:

A file called button_coolstyle.h is included here; we'll get back to that in the next section.

The connect_styles() function has two parameters - type and component.

The type parameter contains the name of the component to be styled - examples are label, button, frame, inputbox, scrollbar, radiobutton, listbox, window, etc. For each component you want to put a style on, make a new test for the type, and attach the style using the set_style() function in the component. Notice how its done through creating a new style class called CL_Button_CoolTheme.

Any components we don't attach, we just pass on to the default stylemanager. You can skip doing this if you have styled all the component you want to use, or don't want to use the default stylemanager at all.

Creating a styled component

Create a new file called button_coolstyle.h, and insert the following code into it:

Each component-style inherits from the class CL_ComponentStyle. The CL_Button object you get in the constructor is the logic class for the button. You can query it for states (is_down()) and size etc.

In this simple style, we only prepare to hook into the sig_paint signal - therefore the slot_paint and on_paint() code. Read on for more specifics on this.

Create a new file called button_coolstyle.cpp, and insert the following code into it:

In the constructor we hook into the sig_paint signal of the CL_Button. This allows us to do custom drawing of the button.

The on_paint() function fetches the size of the button, and draws some simple rects, based on the button state - if its pressed down or not.

Testing the new style

We have now created a very simple style, using only one themed component - a CL_Button. Lets try to test it, and see if it behaves as we expect it to do.

Remember to copy the Resources/GUIStyleBoring files into your project, so it has some fonts and resources to use. These can be replaced later with your own resources.

Some example GUI styles

Silver

ClanGUIs default theme Silver

Boring

ClanGUIs default theme Boring

SupaPlex

The game Supaplex

Race

The game Race

Race Editor

The gameeditor in Race

APOC

The game APOC

Questions or comments, write to the ClanLib mailing list.