Ok. This is the simplest example I could come up with. Basically what we do here is create a new Window type that just draws a single image over it's entire surface; there is no WindowRenderer no LookNFeel, no nothing - this is as raw as it gets
The window will take the name of an image file to load, it will use the same filename as the name of the imageset and will automatically load the file for you, allowing you to set the image without dealing directly with imagesets (though any created imageset does not get cleaned up until the end).
First we have to define the new Window class. This will be a basic affair with a function to set the image that's to be drawn, and an override for the function that does the drawing, and that's all:
Code: Select all
//----------------------------------------------------------------------------//
class ImageWindow : public CEGUI::Window
{
public:
// Used by the window factory template to know the type name.
static const CEGUI::String WidgetTypeName;
ImageWindow(const CEGUI::String& type, const CEGUI::String& name);
// function to set the image to be drawn.
void setImage(const CEGUI::String& filename);
protected:
// override from CEGUI::Window
void populateGeometryBuffer();
// holds ptr to image to draw.
const CEGUI::Image* d_image;
};
Because we use the simplified template system for registering the new type, this requires we provide a member string named WidgetTypeName, so we define that:
Code: Select all
//----------------------------------------------------------------------------//
const CEGUI::String ImageWindow::WidgetTypeName("ImageWindow");
The constructor just initialises the base class and the member variable:
Code: Select all
//----------------------------------------------------------------------------//
ImageWindow::ImageWindow(const CEGUI::String& type, const CEGUI::String& name) :
CEGUI::Window(type, name),
d_image(0)
{
}
The setImage function creates the imageset and invalidates the window so the new image will be drawn next time around:
Code: Select all
//----------------------------------------------------------------------------//
void ImageWindow::setImage(const CEGUI::String& filename)
{
d_image = &CEGUI::ImagesetManager::getSingleton().
createFromImageFile(filename, filename).getImage("full_image");
invalidate();
}
And finally, the populateGeometryBuffer call does the drawing operation. In this case it draws any image set over the entire area of the window's surface:
Code: Select all
//----------------------------------------------------------------------------//
void ImageWindow::populateGeometryBuffer()
{
// draw the image for our entire area
if (d_image)
d_image->draw(*d_geometry, CEGUI::Vector2(0, 0), d_pixelSize, 0);
}
Ok. That covers the new Window type. So how do you use this?
First you have to register the new type with the system:
Code: Select all
using namespace CEGUI;
// Add our new window type:
WindowFactoryManager::addFactory<TplWindowFactory<ImageWindow> >();
And that's pretty much it
Now you can create an instance of the new window type:
Code: Select all
// create an instance of ImageWindow
ImageWindow* wnd = static_cast<ImageWindow*>(
WindowManager::getSingleton().createWindow("ImageWindow"));
Set the image:
Code: Select all
// set the image
wnd->setImage("logo.png");
And set the position and size as you would normally:
Code: Select all
// give window position and size
wnd->setPosition(UVector2(UDim(0.25f, 0), UDim(0.25f, 0)));
wnd->setSize(UVector2(UDim(0.5f, 0), UDim(0.5f, 0)));
Obviously there is no support for an 'Image' property, or such, you can access via XML, but that was not the point; the point was a minimal example
CE