Canvas: Fix removing text elements.

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.
This commit is contained in:
Thomas Geymayer 2013-07-04 14:11:54 +02:00
parent 943efbb3ed
commit d0ff144753
5 changed files with 87 additions and 53 deletions

View 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;
}
//----------------------------------------------------------------------------

View 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;

View 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() );
}
}

View 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;

View 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();