So far only Buttons, Images and Windows are supported but it would be very simple to add support for other elements.
A nice feature is its support for "containers". You can add elements as child of other elements. The parent-child link is unlimited.
Here's the code :
Code: Select all
void CBlackNebula::showGUI(stringc gui)
{
IGUIEnvironment * mygui = CDeviceFactory::s_gui;
array<IGUIElement *> parents;
gui.append(".xml");
IXMLReader * xml = CDeviceFactory::s_device->getFileSystem()->createXMLReader(gui.c_str());
while(xml && xml->read())
{
switch(xml->getNodeType())
{
case EXN_ELEMENT:
{
int x1, y1, x2, y2, id;
x1 = xml->getAttributeValueAsInt(L"x1");
y1 = xml->getAttributeValueAsInt(L"y1");
x2 = xml->getAttributeValueAsInt(L"x2");
y2 = xml->getAttributeValueAsInt(L"y2");
id = xml->getAttributeValueAsInt(L"id");
if(stringw("button") == xml->getNodeName())
{
stringw text(xml->getAttributeValueSafe(L"text"));
mygui->addButton(rect<s32>(x1, y1, x2, y2), parents.getLast(), id, text.c_str());
}
if(stringw("image") == xml->getNodeName())
{
stringc texture(xml->getAttributeValueSafe(L"texture"));
ITexture * image = CDeviceFactory::s_driver->getTexture(texture.c_str());
IGUIImage * bg = mygui->addImage(rect<s32>(x1, y1, x2, y2), parents.getLast(), id);
bg->setImage(image);
}
if(stringw("root") == xml->getNodeName())
{
parents.push_back(mygui->getRootGUIElement());
}
if(stringw("window") == xml->getNodeName())
{
bool closable = stringw("true") == xml->getAttributeValueSafe(L"closable") ? true : false;
bool modal = stringw("true") == xml->getAttributeValueSafe(L"modal") ? true : false;
stringw text(xml->getAttributeValueSafe(L"text"));
IGUIWindow * window = mygui->addWindow(rect<s32>(x1, y1, x2, y2), modal, text.c_str(), parents.getLast());
window->getCloseButton()->setEnabled(closable);
parents.push_back(window);
}
}
break;
case EXN_ELEMENT_END:
{
if(stringw("root") == xml->getNodeName())
{
parents.erase(parents.size() - 1);
}
if(stringw("window") == xml->getNodeName())
{
parents.erase(parents.size() - 1);
}
}
break;
}
}
if(xml)
xml->drop();
}
The XML file should be similar to this :
Code: Select all
<root>
<image x1="0" y1="0" x2="800" y2="600" id="-1" texture="background.jpg"/>
<window x1="300" y1="200" x2="500" y2="400" id="-1" modal="false" text="Main" closable="false">
<button x1="16" y1="40" x2="184" y2="64" id="100" text="New Game"/>
<button x1="16" y1="80" x2="184" y2="104" id="101" text="Load Game"/>
<button x1="16" y1="120" x2="184" y2="144" id="102" text="Options"/>
<button x1="16" y1="160" x2="184" y2="184" id="103" text="Exit"/>
</window>
</root>
Feel free to critisize and post your changes!
BTW in the EXN_ELEMENT_END case, the root and window if blocks are identical. I didn't merge them both as I plan to do some changes in the future.