Page 1 of 1

Patch: User strings in layout files for CEGUI 0.7.5

Posted: Tue Sep 13, 2011 03:32
by Garthy
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. :)

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

Posted: Tue Sep 13, 2011 10:24
by Hanmac
this feature will be added into 0.8 but not with this patch directly, because i rewrite all xmlHandlers

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

Posted: Tue Sep 13, 2011 17:16
by Kulik
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.

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

Posted: Wed Sep 14, 2011 03:32
by Garthy
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.

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

Posted: Sat Sep 17, 2011 21:48
by Kulik
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.

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

Posted: Sun Sep 18, 2011 08:07
by Garthy
Good news. :) Hopefully CrazyEddie is keen on it too. Thanks for checking it out!

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

Posted: Tue Sep 20, 2011 09:08
by CrazyEddie
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.

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

Posted: Tue Sep 20, 2011 09:18
by Garthy
Excellent! :) No problem. Renaming the shared variables sounds good too.