Patch: User strings in layout files for CEGUI 0.7.5

If you found a bug in our library or on our website, please report it in this section. In this forum you can also make concrete suggestions or feature requests.

Moderators: CEGUI MVP, CEGUI Team

Garthy
Just popping in
Just popping in
Posts: 15
Joined: Thu Dec 02, 2010 02:06

Patch: User strings in layout files for CEGUI 0.7.5

Postby Garthy » 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:

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. :)

Hanmac
Just popping in
Just popping in
Posts: 7
Joined: Thu Jul 07, 2011 19:45

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby Hanmac » Tue Sep 13, 2011 10:24

this feature will be added into 0.8 but not with this patch directly, because i rewrite all xmlHandlers

User avatar
Kulik
CEGUI Team
Posts: 1382
Joined: Mon Jul 26, 2010 18:47
Location: Czech Republic
Contact:

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby Kulik » Tue Sep 13, 2011 17:16

Nice work! The patch is very high quality. In my opinion this should go into 0.8, not 0.7 branch. I propose to apply it before Hanmac rewrites it (though I have no idea if/why he is going to rewrite it).

I will check how much work is needed to get it applied onto 0.8 and report back.

Garthy
Just popping in
Just popping in
Posts: 15
Joined: Thu Dec 02, 2010 02:06

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby Garthy » Wed Sep 14, 2011 03:32

Thanks. :) Happy to have been able to contribute.

I'm presently using 0.7.5, no *short*-term plans to upgrade, but I will probably move to a more recent version in the longer term. As such, I don't know an awful lot about the 0.8 branch. :} Hopefully the patch can be applied easily to the 0.8 series. If it doesn't apply neatly, the process to create the patch was essentially to base all bits on the corresponding "Property" code (renaming things as needed), share the same member variables for storage, remove the Property callback (and associated code), and call setUserString() in both handlers for the short and long form. I think that covers the vast majority of the patch.

User avatar
Kulik
CEGUI Team
Posts: 1382
Joined: Mon Jul 26, 2010 18:47
Location: Czech Republic
Contact:

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby Kulik » Sat Sep 17, 2011 21:48

According to my tests it applies more or less cleanly, the only thing left to do is to discuss this with CrazyEddie whether it can go in.

Garthy
Just popping in
Just popping in
Posts: 15
Joined: Thu Dec 02, 2010 02:06

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby Garthy » Sun Sep 18, 2011 08:07

Good news. :) Hopefully CrazyEddie is keen on it too. Thanks for checking it out!

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby CrazyEddie » Tue Sep 20, 2011 09:08

We discussed this, and I think it's a fine addition - thanks for the contribution. We'll just rename the d_propertyName member variable to something else (this way it's use in two places will look like we planned it that way all along :mrgreen:)

CE.

Garthy
Just popping in
Just popping in
Posts: 15
Joined: Thu Dec 02, 2010 02:06

Re: Patch: User strings in layout files for CEGUI 0.7.5

Postby Garthy » Tue Sep 20, 2011 09:18

Excellent! :) No problem. Renaming the shared variables sounds good too.


Return to “Bug Reports, Suggestions, Feature Requests”

Who is online

Users browsing this forum: No registered users and 1 guest