Patch: User strings in layout files for CEGUI 0.7.5
Posted: Tue Sep 13, 2011 03:32
Patch: User strings in layout files for CEGUI 0.7.5
Heya.
Unless I'm mistaken, I don't *think* it is possible to specify user strings in layout files in CEGUI 0.7.5. This is a really useful thing to be able to do, and this patch gives you the ability to do so. You can specify user strings with "UserString" much as you would use "Property". For example, in a layout file:
It's heavily based on the code that handles "Property". I hijacked the class members that "Property" uses (eg. "d_propertyName"), but it could be changed fairly easily to use its own if preferred.
I hope it is useful.
PS. It'd be neat if it were possible to attach files to posts, that'd make patch submission easier.
Heya.
Unless I'm mistaken, I don't *think* it is possible to specify user strings in layout files in CEGUI 0.7.5. This is a really useful thing to be able to do, and this patch gives you the ability to do so. You can specify user strings with "UserString" much as you would use "Property". For example, in a layout file:
Code: Select all
<UserString Name="MyUserString" Value="MyValue" />
It's heavily based on the code that handles "Property". I hijacked the class members that "Property" uses (eg. "d_propertyName"), but it could be changed fairly easily to use its own if preferred.
I hope it is useful.
Code: Select all
diff -Naur old/cegui/include/CEGUIGUILayout_xmlHandler.h new/cegui/include/CEGUIGUILayout_xmlHandler.h
--- old/cegui/include/CEGUIGUILayout_xmlHandler.h 2010-11-19 11:19:03.000000000 +0000
+++ new/cegui/include/CEGUIGUILayout_xmlHandler.h 2011-09-13 01:46:01.000000000 +0000
@@ -101,12 +101,15 @@
static const String GUILayoutElement; //!< Tag name for GUILayout elements.
static const String WindowElement; //!< Tag name for Window elements.
static const String AutoWindowElement; //!< Tag name for AutoWindow elements.
+ static const String UserStringElement; //!< Tag name for UserString elements.
static const String PropertyElement; //!< Tag name for Property elements.
static const String LayoutImportElement; //!< Tag name for LayoutImport elements.
static const String EventElement; //!< Tag name for Event elements.
static const String WindowTypeAttribute; //!< Attribute name that stores the type of Window to create.
static const String WindowNameAttribute; //!< Attribute name that stores the name of the window to create.
static const String AutoWindowNameSuffixAttribute; //!< Attribute name that stores the name suffix of the auto window to get.
+ static const String UserStringNameAttribute; //!< Attribute name that stores the name of the user string.
+ static const String UserStringValueAttribute; //!< Attribute name that stores the value to set the user string to.
static const String PropertyNameAttribute; //!< Attribute name that stores the name of the property to set.
static const String PropertyValueAttribute; //!< Attribute name that stores the value to pass to the property.
static const String LayoutParentAttribute; //!< Attribute name that stores the name of the window to attach the layout to.
@@ -136,6 +139,12 @@
/*!
\brief
+ Method that handles the UserString XML element.
+ */
+ void elementUserStringStart(const XMLAttributes& attributes);
+
+ /*!
+ \brief
Method that handles the Property XML element.
*/
void elementPropertyStart(const XMLAttributes& attributes);
@@ -172,6 +181,12 @@
/*!
\brief
+ Method that handles the closing of a UserString XML element.
+ */
+ void elementUserStringEnd();
+
+ /*!
+ \brief
Method that handles the closing of a property XML element.
*/
void elementPropertyEnd();
@@ -189,8 +204,8 @@
const String& d_namingPrefix; //!< Prefix that is to prepend all names of created windows.
PropertyCallback* d_propertyCallback; //!< Callback for every property loaded
void* d_userData; //!< User data for the property callback
- String d_propertyName; //!< Use for long property value
- String d_propertyValue; //!< Use for long property value
+ String d_propertyName; //!< Use for long property or user string value
+ String d_propertyValue; //!< Use for long property or user string value
};
diff -Naur old/cegui/src/CEGUIGUILayout_xmlHandler.cpp new/cegui/src/CEGUIGUILayout_xmlHandler.cpp
--- old/cegui/src/CEGUIGUILayout_xmlHandler.cpp 2010-11-19 11:19:12.000000000 +0000
+++ new/cegui/src/CEGUIGUILayout_xmlHandler.cpp 2011-09-13 01:45:04.000000000 +0000
@@ -43,12 +43,15 @@
const String GUILayout_xmlHandler::GUILayoutElement( "GUILayout" );
const String GUILayout_xmlHandler::WindowElement( "Window" );
const String GUILayout_xmlHandler::AutoWindowElement( "AutoWindow" );
+const String GUILayout_xmlHandler::UserStringElement( "UserString" );
const String GUILayout_xmlHandler::PropertyElement( "Property" );
const String GUILayout_xmlHandler::LayoutImportElement( "LayoutImport" );
const String GUILayout_xmlHandler::EventElement( "Event" );
const String GUILayout_xmlHandler::WindowTypeAttribute( "Type" );
const String GUILayout_xmlHandler::WindowNameAttribute( "Name" );
const String GUILayout_xmlHandler::AutoWindowNameSuffixAttribute( "NameSuffix" );
+const String GUILayout_xmlHandler::UserStringNameAttribute( "Name" );
+const String GUILayout_xmlHandler::UserStringValueAttribute( "Value" );
const String GUILayout_xmlHandler::PropertyNameAttribute( "Name" );
const String GUILayout_xmlHandler::PropertyValueAttribute( "Value" );
const String GUILayout_xmlHandler::LayoutParentAttribute( "Parent" );
@@ -75,6 +78,11 @@
{
elementAutoWindowStart(attributes);
}
+ // handle UserString element (set user string for window at top of stack)
+ else if (element == UserStringElement)
+ {
+ elementUserStringStart(attributes);
+ }
// handle Property element (set property for window at top of stack)
else if (element == PropertyElement)
{
@@ -114,6 +122,11 @@
{
elementAutoWindowEnd();
}
+ // handle UserString element
+ else if (element == UserStringElement)
+ {
+ elementUserStringEnd();
+ }
// handle Property element
else if (element == PropertyElement)
{
@@ -266,6 +279,51 @@
}
/*************************************************************************
+ Method that handles the UserString XML element.
+*************************************************************************/
+void GUILayout_xmlHandler::elementUserStringStart(const XMLAttributes& attributes)
+{
+ // Get user string name
+ String userStringName(attributes.getValueAsString(UserStringNameAttribute));
+
+ // Get user string value
+ String userStringValue;
+ if (attributes.exists(UserStringValueAttribute))
+ {
+ userStringValue = attributes.getValueAsString(UserStringValueAttribute);
+ }
+
+ // Short user string
+ if (!userStringValue.empty())
+ {
+ d_propertyName.clear();
+ CEGUI_TRY
+ {
+ // need a window to be able to set properties!
+ if (!d_stack.empty())
+ {
+ // get current window being defined.
+ Window* curwindow = d_stack.back().first;
+
+ curwindow->setUserString(userStringName, userStringValue);
+ }
+ }
+ CEGUI_CATCH (Exception&)
+ {
+ // Don't do anything here, but the error will have been logged.
+ }
+ }
+ // Long user string
+ else
+ {
+ // Store name for later use
+ d_propertyName = userStringName;
+ // reset the property (user string) value buffer
+ d_propertyValue.clear();
+ }
+}
+
+/*************************************************************************
Method that handles the Property XML element.
*************************************************************************/
void GUILayout_xmlHandler::elementPropertyStart(const XMLAttributes& attributes)
@@ -416,6 +474,33 @@
}
/*************************************************************************
+ Method that handles the closing UserString XML element.
+*************************************************************************/
+void GUILayout_xmlHandler::elementUserStringEnd()
+{
+ // only do something if this is a "long" user string
+ if (d_propertyName.empty())
+ {
+ return;
+ }
+ CEGUI_TRY
+ {
+ // need a window to be able to set user strings!
+ if (!d_stack.empty())
+ {
+ // get current window being defined.
+ Window* curwindow = d_stack.back().first;
+
+ curwindow->setUserString(d_propertyName, d_propertyValue);
+ }
+ }
+ CEGUI_CATCH (Exception&)
+ {
+ // Don't do anything here, but the error will have been logged.
+ }
+}
+
+/*************************************************************************
Method that handles the closing Property XML element.
*************************************************************************/
void GUILayout_xmlHandler::elementPropertyEnd()
PS. It'd be neat if it were possible to attach files to posts, that'd make patch submission easier.