]> git.mxchange.org Git - simgear.git/commitdiff
Canvas: Fix removing text elements.
authorThomas Geymayer <tomgey@gmail.com>
Thu, 4 Jul 2013 12:11:54 +0000 (14:11 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 4 Jul 2013 12:11:54 +0000 (14:11 +0200)
Removing text elements failed due to prefering removing the
style property with the same name. Add a flag to each style
indicating whether it can be inherited. The text property
is only applicable to text elements and can not be inherited.
Now groups can not have a text property, avoiding the name
clash with the text element.

simgear/canvas/elements/CanvasElement.cxx
simgear/canvas/elements/CanvasElement.hxx
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx
simgear/canvas/elements/CanvasText.cxx

index 85327b2a45c5e01790c4bf02f49721db6953fb78..8d33eff2ce308b72bdf2e9d9276209067a9d54a3 100644 (file)
@@ -342,11 +342,10 @@ namespace canvas
         _transform_dirty = true;
         return;
       }
-      else if( StyleSetter const* setter =
-                 getStyleSetter(child->getNameString()) )
+      else if( StyleInfo const* style = getStyleInfo(child->getNameString()) )
       {
-        setStyle(getParentStyle(child), setter);
-        return;
+        if( setStyle(getParentStyle(child), style) )
+          return;
       }
     }
 
@@ -360,7 +359,7 @@ namespace canvas
     if( parent == _node )
     {
       const std::string& name = child->getNameString();
-      if( StyleSetter const* setter = getStyleSetter(name) )
+      if( StyleInfo const* style_info = getStyleInfo(name) )
       {
         SGPropertyNode const* style = child;
         if( isStyleEmpty(child) )
@@ -368,7 +367,7 @@ namespace canvas
           child->clearValue();
           style = getParentStyle(child);
         }
-        setStyle(style, setter);
+        setStyle(style, style_info);
         return;
       }
       else if( name == "update" )
@@ -391,9 +390,9 @@ namespace canvas
 
   //----------------------------------------------------------------------------
   bool Element::setStyle( const SGPropertyNode* child,
-                          const StyleSetter* setter )
+                          const StyleInfo* style_info )
   {
-    return canApplyStyle(child) && setStyleImpl(child, setter);
+    return canApplyStyle(child) && setStyleImpl(child, style_info);
   }
 
   //----------------------------------------------------------------------------
@@ -520,7 +519,7 @@ namespace canvas
 
     if( !isInit<Element>() )
     {
-      addStyle("clip", "", &Element::setClip);
+      addStyle("clip", "", &Element::setClip, false);
     }
   }
 
@@ -543,10 +542,10 @@ namespace canvas
 
   //----------------------------------------------------------------------------
   bool Element::setStyleImpl( const SGPropertyNode* child,
-                              const StyleSetter* setter )
+                              const StyleInfo* style_info )
   {
-    const StyleSetter* style_setter = setter
-                                    ? setter
+    const StyleSetter* style_setter = style_info
+                                    ? &style_info->setter
                                     : getStyleSetter(child->getNameString());
     while( style_setter )
     {
@@ -558,14 +557,22 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  const Element::StyleSetter*
-  Element::getStyleSetter(const std::string& name) const
+  const Element::StyleInfo*
+  Element::getStyleInfo(const std::string& name) const
   {
     StyleSetters::const_iterator setter = _style_setters.find(name);
     if( setter == _style_setters.end() )
       return 0;
 
-    return &setter->second.setter;
+    return &setter->second;
+  }
+
+  //----------------------------------------------------------------------------
+  const Element::StyleSetter*
+  Element::getStyleSetter(const std::string& name) const
+  {
+    const StyleInfo* info = getStyleInfo(name);
+    return info ? &info->setter : 0;
   }
 
   //----------------------------------------------------------------------------
index a624bb22f8364eac49fd5983cf16b235f06d5ad9..07e876911ae900019d8a94409d31daebc7fc6a73 100644 (file)
@@ -71,6 +71,8 @@ namespace canvas
       {
         StyleSetter setter; ///!< Function(s) for setting this style
         std::string type;   ///!< Interpolation type
+        bool inheritable;   ///!< Whether children can inherit this style from
+                            ///   their parents
       };
 
       /**
@@ -118,7 +120,7 @@ namespace canvas
       virtual void valueChanged(SGPropertyNode * child);
 
       virtual bool setStyle( const SGPropertyNode* child,
-                             const StyleSetter* setter = 0 );
+                             const StyleInfo* style_info = 0 );
 
       /**
        * Set clipping shape
@@ -240,7 +242,8 @@ namespace canvas
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
-                const boost::function<void (Derived&, T2)>& setter )
+                const boost::function<void (Derived&, T2)>& setter,
+                bool inheritable = true )
       {
         StyleInfo& style_info = _style_setters[ name ];
         if( !type.empty() )
@@ -256,6 +259,8 @@ namespace canvas
 
           style_info.type = type;
         }
+        // TODO check if changed?
+        style_info.inheritable = inheritable;
 
         StyleSetter* style = &style_info.setter;
         while( style->next )
@@ -280,9 +285,10 @@ namespace canvas
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
-                const boost::function<void (Derived&, T)>& setter )
+                const boost::function<void (Derived&, T)>& setter,
+                bool inheritable = true )
       {
-        return addStyle<T, T>(name, type, setter);
+        return addStyle<T, T>(name, type, setter, inheritable);
       }
 
       template<
@@ -292,13 +298,15 @@ namespace canvas
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
-                void (Derived::*setter)(T) )
+                void (Derived::*setter)(T),
+                bool inheritable = true )
       {
         return addStyle<T, T>
         (
           name,
           type,
-          boost::function<void (Derived&, T)>(setter)
+          boost::function<void (Derived&, T)>(setter),
+          inheritable
         );
       }
 
@@ -310,13 +318,15 @@ namespace canvas
       StyleSetterFunc
       addStyle( const std::string& name,
                 const std::string& type,
-                void (Derived::*setter)(T2) )
+                void (Derived::*setter)(T2),
+                bool inheritable = true )
       {
         return addStyle<T1>
         (
           name,
           type,
-          boost::function<void (Derived&, T2)>(setter)
+          boost::function<void (Derived&, T2)>(setter),
+          inheritable
         );
       }
 
@@ -326,13 +336,15 @@ namespace canvas
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
-                void (Derived::*setter)(const std::string&) )
+                void (Derived::*setter)(const std::string&),
+                bool inheritable = true )
       {
         return addStyle<const char*, const std::string&>
         (
           name,
           type,
-          boost::function<void (Derived&, const std::string&)>(setter)
+          boost::function<void (Derived&, const std::string&)>(setter),
+          inheritable
         );
       }
 
@@ -346,9 +358,16 @@ namespace canvas
       addStyle( const std::string& name,
                 const std::string& type,
                 void (Other::*setter)(T),
-                OtherRef Derived::*instance_ref )
+                OtherRef Derived::*instance_ref,
+                bool inheritable = true )
       {
-        return addStyle<T, T>(name, type, bindOther(setter, instance_ref));
+        return addStyle<T, T>
+        (
+          name,
+          type,
+          bindOther(setter, instance_ref),
+          inheritable
+        );
       }
 
       template<
@@ -362,9 +381,16 @@ namespace canvas
       addStyle( const std::string& name,
                 const std::string& type,
                 void (Other::*setter)(T2),
-                OtherRef Derived::*instance_ref )
+                OtherRef Derived::*instance_ref,
+                bool inheritable = true )
       {
-        return addStyle<T1>(name, type, bindOther(setter, instance_ref));
+        return addStyle<T1>
+        (
+          name,
+          type,
+          bindOther(setter, instance_ref),
+          inheritable
+        );
       }
 
       template<
@@ -378,9 +404,16 @@ namespace canvas
       addStyle( const std::string& name,
                 const std::string& type,
                 const boost::function<void (Other&, T2)>& setter,
-                OtherRef Derived::*instance_ref )
+                OtherRef Derived::*instance_ref,
+                bool inheritable = true )
       {
-        return addStyle<T1>(name, type, bindOther(setter, instance_ref));
+        return addStyle<T1>
+        (
+          name,
+          type,
+          bindOther(setter, instance_ref),
+          inheritable
+        );
       }
 
       template<
@@ -392,14 +425,16 @@ namespace canvas
       addStyle( const std::string& name,
                 const std::string& type,
                 void (Other::*setter)(const std::string&),
-                OtherRef Derived::*instance_ref )
+                OtherRef Derived::*instance_ref,
+                bool inheritable = true )
       {
         return addStyle<const char*, const std::string&>
         (
           name,
           type,
           boost::function<void (Other&, const std::string&)>(setter),
-          instance_ref
+          instance_ref,
+          inheritable
         );
       }
 
@@ -445,8 +480,9 @@ namespace canvas
       bool isStyleEmpty(const SGPropertyNode* child) const;
       bool canApplyStyle(const SGPropertyNode* child) const;
       bool setStyleImpl( const SGPropertyNode* child,
-                         const StyleSetter* setter = 0 );
+                         const StyleInfo* style_info = 0 );
 
+      const StyleInfo* getStyleInfo(const std::string& name) const;
       const StyleSetter* getStyleSetter(const std::string& name) const;
       const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
 
index 7199a97689b346b5e9c05f48f1094790b37f73b5..4b60ca45b6f74503a89e0e1b0554149fac27b480 100644 (file)
@@ -175,20 +175,16 @@ namespace canvas
 
   //----------------------------------------------------------------------------
   bool Group::setStyle( const SGPropertyNode* style,
-                        const StyleSetter* setter )
+                        const StyleInfo* style_info )
   {
     if( !canApplyStyle(style) )
       return false;
 
-    // Don't propagate styles directly applicable to this group
-    if( setStyleImpl(style, setter) )
-      return true;
-
-    bool handled = false;
-    for(size_t i = 0; i < _transform->getNumChildren(); ++i)
+    bool handled = setStyleImpl(style, style_info);
+    if( style_info->inheritable )
     {
-      if( getChildByIndex(i)->setStyle(style, setter) )
-        handled = true;
+      for(size_t i = 0; i < _transform->getNumChildren(); ++i)
+        handled |= getChildByIndex(i)->setStyle(style, style_info);
     }
 
     return handled;
@@ -247,12 +243,9 @@ namespace canvas
       return;
     }
 
-    if( !Element::setStyle(child) )
-    {
-      // Only add style if not applicable to group itself
+    StyleInfo const* style = getStyleInfo(child->getNameString());
+    if( style && style->inheritable )
       _style[ child->getNameString() ] = child;
-      setStyle(child);
-    }
   }
 
   //----------------------------------------------------------------------------
@@ -285,9 +278,7 @@ namespace canvas
     }
     else
     {
-      Style::iterator style = _style.find(node->getNameString());
-      if( style != _style.end() )
-        _style.erase(style);
+      _style.erase( node->getNameString() );
     }
   }
 
index cf40e8efb517be331651b0e00cb492453610e454..5ee417ea3b6e5b30ca11eb857514a5f854c19ca6 100644 (file)
@@ -93,7 +93,7 @@ namespace canvas
       virtual bool traverse(EventVisitor& visitor);
 
       virtual bool setStyle( const SGPropertyNode* child,
-                             const StyleSetter* setter = 0 );
+                             const StyleInfo* style_info = 0 );
 
       virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
 
index 1ed7be493c26a84cbfba09e6ee18b1623679e311..1aca55dbe16a2b8b524c380cad64fd325433a505 100644 (file)
@@ -292,7 +292,7 @@ namespace canvas
       addStyle("max-width", "numeric", &TextOSG::setMaximumWidth, text);
       addStyle("font", "", &Text::setFont);
       addStyle("alignment", "", &Text::setAlignment);
-      addStyle("text", "", &Text::setText);
+      addStyle("text", "", &Text::setText, false);
     }
 
     setupStyle();