Use your custom widget in place of CEGUI standard one

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search

Written for CEGUI 0.7


Works with versions 0.7.x (obsolete)

Introduction

This article describes how to inherit CEGUI widget, enhance it the way you like, and then make sure it is used in place of CEGUI standard one when met in a layout file.

We will enhance CEGUI::MultiColumnList class. It's very powerful, but it's nice to have an easier interface for row addition.

CustomMCL

This is our enhanced version of CEGUI::MultiColumnList.

Header:

class CustomMCL : public CEGUI::MultiColumnList {
    public:
        CustomMCL(const CEGUI::String &type, const CEGUI::String &name);
 
        void clear();
        void createItem(const CEGUI::String &text);
        void createRow();
 
        // Unique widget name throughout CEGUI.
        static CEGUI::String WidgetTypeName;
 
    private:
        int mRowID;
        int mColID;
};

Source:

CEGUI::String CustomMCL::WidgetTypeName = "CustomMCL";
 
CustomMCL::CustomMCL(const CEGUI::String &type, const CEGUI::String &name) :
        CEGUI::MultiColumnList(type, name),
        mRowID(-1) { }
 
void CustomMCL::clear() {
    resetList();
    mRowID = -1;
}
 
void CustomMCL::createItem(const CEGUI::String &text) {
    CEGUI::ListboxTextItem *item = new CEGUI::ListboxTextItem(text);
    setItem(item, mColID++, mRowID);
}
 
void CustomMCL::createRow() {
    mRowID++;
    mColID = 0;
    addRow(mRowID);
}

Custom widget must contain the following public static variable:

static CEGUI::String WidgetTypeName;

with the name unique among all CEGUI widgets. It's a name our custom widget will be registered under. We named it 'CustomMCL'.

Register the widget:

CEGUI::WindowFactoryManager::getSingleton().addFactory<CEGUI::TplWindowFactory<CustomMCL> >();

Make sure our class is used in place of CEGUI one when met in a layout:

CEGUI::WindowFactoryManager::getSingleton().addWindowTypeAlias("CEGUI/MultiColumnList", "CustomMCL");

Example

Download example source code (requires OGRE and OIS additionally)

Screenshot:

Cegui custom widget.png