QuickGUI --- Read from XML

gogoplayer

25-04-2008 03:14:11

Now, I'm backing, ^_^.
I writed serialize code use tinyXML base on QuickGUI 0.9.7, now it's only support Sheet and Window.


#include "QuickGUISerializer.h"
#include "QuickGUIWidgetCreator.h"
#include "QuickGUIAttributeSerializer.h"

QuickGUI::WidgetCreator *m_pWidgetCreator;
QuickGUI::AttributeSerializer *m_pAttributeSerializer;
QuickGUI::Serializer *m_pSerializer;

void createGUI()
{
m_pWidgetCreator = new QuickGUI::WidgetCreator(mGUIManager);
m_pAttributeSerializer = new QuickGUI::AttributeSerializer(mGUIManager);
m_pSerializer = new QuickGUI::Serializer(mGUIManager);

m_pSerializer->ReadFromFile("Test.xml");
}


XML File:

<?xml version="1.0" ?>
<QuickGUILayout ActiveSheet="MainSheet">
<Widget Type="Sheet" Name="MainSheet" Skin="qgui">
<Widget Type="Window" Name="MainWindow">
<Property Dimension="10 10 320 180" />
</Widget>
<Widget Type="Window" Name="TestWindow">
<Property Dimension="10 220 320 180" />
</Widget>
</Widget>
</QuickGUILayout>


ScreenShoot:

Demo Download:
http://gogoplayer.cnblogs.com/Files/gogoplayer/Ogre/QuickGUIDemo.rar

kungfoomasta

25-04-2008 04:57:09

Welcome back! :)

I wish I knew how to do the XML with that format, when I played around with it it didn't look as readable. Not that XML is meant to be readable, but the easier to read the better!

gogoplayer

25-04-2008 05:13:12

Welcome back! :)

I wish I knew how to do the XML with that format, when I played around with it it didn't look as readable. Not that XML is meant to be readable, but the easier to read the better!


^_^, I think this format is readable, widget can contains sub widget, here is serializer code, you will see I recursive read widgets. How about it?



bool Serializer::ReadFromFile(const Ogre::String& file)
{
TiXmlDocument doc(file.c_str());
if (!doc.LoadFile())
return false;

TiXmlHandle hDoc(&doc);
TiXmlElement* pElem;
TiXmlHandle hRoot(0);

pElem = hDoc.FirstChildElement().Element();

// should always have a valid root but handle gracefully if it does
if (!pElem)
return false;

// save this for later
hRoot=TiXmlHandle(pElem);

pElem = hRoot.FirstChild("Widget").Element();

// read top level widget
ReadWidget(pElem, 0, 0);

return true;
}

void Serializer::ReadWidget(TiXmlElement *pElement, Widget *pParentWidget, WidgetNode *pNode)
{
// Read widget attributes to attribute dictionary
std::string value = pElement->Value();
std::string type = pElement->Attribute("Type");
std::string name = pElement->Attribute("Name");

WidgetNode *pChildWidgetNode;

// if tree is null
if (pNode == 0)
{
m_WidgetTree.Create(pElement->Attribute("Name"));
pChildWidgetNode = m_WidgetTree.GetRoot();
}
else
{
pChildWidgetNode = new WidgetNode(type, name, pNode);
pNode->AddChild(pChildWidgetNode);
}

AttributeDictionary ad;
ad.Add("Type", type);
ad.Add("Name", name);
ReadWidgetAllProperty(pElement, ad);

// Create widget with attribute dictionary
WidgetCreator *pWidgetCreator = WidgetCreator::getSingletonPtr();
Widget *pWidget = pWidgetCreator->CreateWidget(ad, pParentWidget);
AttributeSerializer::getSingletonPtr()->DictionaryToWidgetAttribute(ad, pWidget);

// Create all child widget
TiXmlElement *pElem = TiXmlHandle(pElement).FirstChild("Widget").Element();
for( ; pElem; pElem = pElem->NextSiblingElement())
ReadWidget(pElem, pWidget, pChildWidgetNode);
}

void Serializer::ReadWidgetAllProperty(TiXmlElement *pWidgetElement, AttributeDictionary &ad)
{
TiXmlElement *pElement = pWidgetElement->FirstChildElement("Property");
for( ; pElement; pElement = pElement->NextSiblingElement("Property"))
{
std::string attributeName = pElement->FirstAttribute()->Name();
std::string attributeValue = pElement->FirstAttribute()->Value();
ad.Add(attributeName, attributeValue);
}
}

kungfoomasta

25-04-2008 08:04:32

Well I didn't want to say too much.... but I have serialization working in my code base. 8)

Here is a sample of the output:


Sheet DefaultSheet.DefaultSheet
{
DescClass SheetDesc
ConsumeKeyboardEvents false
Enabled true
Dimensions 0 0 800 600
Dragable false
HorizontalAnchor ANCHOR_HORIZONTAL_LEFT
MaxSize 0 0
MinSize 0 0
Name DefaultSheet
Propogate_WIDGET_EVENT_ENABLED_CHANGED false
Propogate_WIDGET_EVENT_POSITION_CHANGED false
Propogate_WIDGET_EVENT_SCROLL_CHANGED false
Propogate_WIDGET_EVENT_SIZE_CHANGED false
Propogate_WIDGET_EVENT_VISIBLE_CHANGED false
Propogate_WIDGET_EVENT_CHARACTER_KEY false
Propogate_WIDGET_EVENT_DRAGGED false
Propogate_WIDGET_EVENT_DROPPED false
Propogate_WIDGET_EVENT_KEY_DOWN false
Propogate_WIDGET_EVENT_KEY_UP false
Propogate_WIDGET_EVENT_MOUSE_BUTTON_DOWN false
Propogate_WIDGET_EVENT_MOUSE_BUTTON_UP false
Propogate_WIDGET_EVENT_MOUSE_CLICK false
Propogate_WIDGET_EVENT_MOUSE_CLICK_DOUBLE false
Propogate_WIDGET_EVENT_MOUSE_CLICK_TRIPLE false
Propogate_WIDGET_EVENT_MOUSE_ENTER false
Propogate_WIDGET_EVENT_MOUSE_LEAVE false
Propogate_WIDGET_EVENT_MOUSE_MOVE false
Propogate_WIDGET_EVENT_MOUSE_WHEEL false
Scrollable true
Type transparent
VerticalAnchor ANCHOR_VERTICAL_TOP
Visible true

Child0 Panel DefaultSheet.MainPanel

Window0 DefaultSheet.Window1
}

Window DefaultSheet.Window1
{
DescClass WindowDesc
ConsumeKeyboardEvents false
Enabled true
Dimensions 300 200 200 200
Dragable false
HorizontalAnchor ANCHOR_HORIZONTAL_LEFT
MaxSize 0 0
MinSize 0 0
Name Window1
Propogate_WIDGET_EVENT_ENABLED_CHANGED false
Propogate_WIDGET_EVENT_POSITION_CHANGED false
Propogate_WIDGET_EVENT_SCROLL_CHANGED false
Propogate_WIDGET_EVENT_SIZE_CHANGED false
Propogate_WIDGET_EVENT_VISIBLE_CHANGED false
Propogate_WIDGET_EVENT_CHARACTER_KEY false
Propogate_WIDGET_EVENT_DRAGGED false
Propogate_WIDGET_EVENT_DROPPED false
Propogate_WIDGET_EVENT_KEY_DOWN false
Propogate_WIDGET_EVENT_KEY_UP false
Propogate_WIDGET_EVENT_MOUSE_BUTTON_DOWN false
Propogate_WIDGET_EVENT_MOUSE_BUTTON_UP false
Propogate_WIDGET_EVENT_MOUSE_CLICK false
Propogate_WIDGET_EVENT_MOUSE_CLICK_DOUBLE false
Propogate_WIDGET_EVENT_MOUSE_CLICK_TRIPLE false
Propogate_WIDGET_EVENT_MOUSE_ENTER false
Propogate_WIDGET_EVENT_MOUSE_LEAVE false
Propogate_WIDGET_EVENT_MOUSE_MOVE false
Propogate_WIDGET_EVENT_MOUSE_WHEEL false
Scrollable true
Type transparent
VerticalAnchor ANCHOR_VERTICAL_TOP
Visible true
TitleBar true
TitleBarCloseButton true
TitleBarType qgui

Child0 Panel DefaultSheet.Window1Panel
}

. . .


Its very easy to maintain and very easy to modify, and doesn't have any nested definition of widgets. When I say easy to maintain, this means Users can create their own custom widgets and they only have to write one function for the serialization to work.