Changeset 5961


Ignore:
Timestamp:
Jan 19, 2016, 11:45:44 AM (9 years ago)
Author:
ldelgass
Message:

Add backing quad to placard annotation

Location:
geovis/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • geovis/trunk/Picker.cpp

    r5944 r5961  
    203203        osgEarth::Symbology::Style style;
    204204        osgEarth::Symbology::TextSymbol *ts = style.getOrCreateSymbol<osgEarth::Symbology::TextSymbol>();
    205         ts->halo()->color() = osgEarth::Symbology::Color(0, 0, 0);
    206         ts->halo()->width() = 2;
    207         ts->fill()->color() = osgEarth::Symbology::Color(1, 1, 1);
     205        //ts->halo()->color() = osgEarth::Symbology::Color(1, 1, 1);
     206        //ts->halo()->width() = 2;
     207        ts->fill()->color() = osgEarth::Symbology::Color(0, 0, 0);
    208208        ts->size() = 14;
    209209        //ts->alignment() = alignment;
     
    261261    for (unsigned int i = 0; i < nodes->getNumChildren(); i++) {
    262262        osg::Node *node = nodes->getChild(i);
    263         if (dynamic_cast<osgEarth::Annotation::LabelNode *>(node) != NULL) {
     263        if (dynamic_cast<osgEarth::Annotation::OrthoNode *>(node) != NULL) {
    264264            toRemove.push_back(node);
    265265        }
  • geovis/trunk/Placard.cpp

    r5952 r5961  
    99#include <sstream>
    1010
     11#include <osg/Depth>
     12#include <osgText/Text>
     13#include <osgEarthAnnotation/AnnotationUtils>
     14#include <osgEarth/Registry>
     15#include <osgEarth/ShaderGenerator>
     16
    1117#include "Placard.h"
    1218#include "Trace.h"
    1319
     20#define LC "[PlacardNode] "
     21
    1422using namespace GeoVis;
    1523
    16 PlacardNode::PlacardNode(osgEarth::MapNode *mapNode,
    17                          const osgEarth::GeoPoint& position,
    18                          const Placard& placardConf,
    19                          const osgEarth::Features::AttributeTable &attrs,
    20                          const osgEarth::Symbology::Style& style) :
     24PlacardLabelNode::PlacardLabelNode(osgEarth::MapNode *mapNode,
     25                                   const osgEarth::GeoPoint& position,
     26                                   const Placard& placardConf,
     27                                   const osgEarth::Features::AttributeTable &attrs,
     28                                   const osgEarth::Symbology::Style& style) :
    2129    osgEarth::Annotation::LabelNode(mapNode, position, style),
    2230    _placardConf(placardConf),
     
    2735
    2836void
    29 PlacardNode::setConfig(const Placard& placardConf)
     37PlacardLabelNode::setConfig(const Placard& placardConf)
    3038{
    3139    _placardConf = placardConf;
     
    6270    setText(oss.str());
    6371}
     72
     73PlacardNode::PlacardNode(osgEarth::MapNode *mapNode,
     74                         const osgEarth::GeoPoint& position,
     75                         const Placard& placardConf,
     76                         const osgEarth::Features::AttributeTable &attrs,
     77                         const osgEarth::Symbology::Style& style) :
     78    osgEarth::Annotation::OrthoNode(mapNode, position),
     79    _backdropColor(0.75, 0.75, 0.75, 0.75),
     80    _placardConf(placardConf),
     81    _attrs(attrs)
     82{
     83    setConfig(placardConf);
     84    init(style);
     85}
     86
     87void
     88PlacardNode::setConfig(const Placard& placardConf)
     89{
     90    _placardConf = placardConf;
     91    std::ostringstream oss;
     92    for (size_t i = 0; i < placardConf.getNumEntries(); i++) {
     93        std::string name, label;
     94        placardConf.getEntry(i, name, label);
     95        oss << label << ": ";
     96        INFO(" attr: %s '%s'", name.c_str(), label.c_str());
     97        osgEarth::Features::AttributeTable::const_iterator itr = _attrs.find(name);
     98        if (itr == _attrs.end())
     99            continue;
     100        switch (itr->second.first) {
     101        case osgEarth::Features::ATTRTYPE_STRING:
     102            INFO(" value: %s", itr->second.getString().c_str());
     103            oss << itr->second.getString() << std::endl;
     104            break;
     105        case osgEarth::Features::ATTRTYPE_INT:
     106            INFO(" value: %d", itr->second.getInt());
     107            oss << itr->second.getInt() << std::endl;
     108            break;
     109        case osgEarth::Features::ATTRTYPE_DOUBLE:
     110            INFO(" value: %g", itr->second.getDouble());
     111            oss << itr->second.getDouble() << std::endl;
     112            break;
     113        case osgEarth::Features::ATTRTYPE_BOOL:
     114            INFO(" value: %s", itr->second.getBool() ? "true" : "false");
     115            oss << (itr->second.getBool() ? "true" : "false") << std::endl;
     116            break;
     117        default:
     118            break;
     119        }
     120    }
     121    setText(oss.str());
     122}
     123
     124void
     125PlacardNode::init( const osgEarth::Symbology::Style& style )
     126{
     127    _backdropGeode = new osg::Geode();
     128    _backdropGeode->setComputeBoundingSphereCallback(new osgEarth::Annotation::ControlPointCallback());
     129    getAttachPoint()->addChild(_backdropGeode.get());
     130
     131    osg::StateSet* stateSet = _backdropGeode->getOrCreateStateSet();
     132    stateSet->setAttributeAndModes( new osg::Depth(osg::Depth::ALWAYS, 0, 1, false), 1 );
     133
     134    _geode = new osg::Geode();
     135
     136    // ensure that (0,0,0) is the bounding sphere control/center point.
     137    // useful for things like horizon culling.
     138    _geode->setComputeBoundingSphereCallback(new osgEarth::Annotation::ControlPointCallback());
     139
     140    getAttachPoint()->addChild( _geode.get() );
     141
     142    stateSet = _geode->getOrCreateStateSet();
     143    stateSet->setAttributeAndModes( new osg::Depth(osg::Depth::ALWAYS, 0, 1, false), 1 );
     144
     145    setStyle( style );
     146}
     147
     148void
     149PlacardNode::setText( const std::string& text )
     150{
     151    if ( !_dynamic && getNumParents() > 0 )
     152    {
     153        OE_WARN << LC << "Illegal state: cannot change a LabelNode that is not dynamic" << std::endl;
     154        return;
     155    }
     156
     157    _text = text;
     158    if (!_geode.valid())
     159        return;
     160
     161    osgText::Text* d = dynamic_cast<osgText::Text*>(_geode->getDrawable(0));
     162    if ( d )
     163    {
     164        d->setText( text );
     165        d->dirtyDisplayList();
     166    }
     167
     168    if (!_backdropGeode.valid())
     169        return;
     170
     171    osg::Geometry *bd = dynamic_cast<osg::Geometry*>(_backdropGeode->getDrawable(0));
     172    if (bd) {
     173        osg::BoundingBox bbox = d->getBoundingBox();
     174        const osgEarth::Symbology::TextSymbol* symbol = _style.get<osgEarth::Symbology::TextSymbol>();
     175        double charSize = symbol && symbol->size().isSet() ? symbol->size()->eval() : 16.0;
     176        float padding = (float)(charSize / 2.0);
     177        _backdropGeode->removeDrawables( 0, _backdropGeode->getNumDrawables() );
     178        osg::Drawable *bd = osgEarth::Annotation::AnnotationUtils::create2DQuad(bbox, padding, _backdropColor);
     179        _backdropGeode->addDrawable(bd);
     180    }
     181}
     182
     183void
     184PlacardNode::setStyle( const osgEarth::Symbology::Style& style )
     185{
     186    if ( !_dynamic && getNumParents() > 0 )
     187    {
     188        OE_WARN << LC << "Illegal state: cannot change a LabelNode that is not dynamic" << std::endl;
     189        return;
     190    }
     191
     192    this->clearDecoration();
     193
     194    _backdropGeode->removeDrawables( 0, _backdropGeode->getNumDrawables() );
     195
     196    _geode->removeDrawables( 0, _geode->getNumDrawables() );
     197
     198    _style = style;
     199
     200    const osgEarth::Symbology::TextSymbol* symbol = _style.get<osgEarth::Symbology::TextSymbol>();
     201
     202    if ( _text.empty() )
     203        _text = symbol->content()->eval();
     204
     205    osg::Drawable* t = osgEarth::Annotation::AnnotationUtils::createTextDrawable( _text, symbol, osg::Vec3(0,0,0) );
     206    _geode->addDrawable(t);
     207    _geode->setCullingActive(false);
     208
     209    double charSize = symbol && symbol->size().isSet() ? symbol->size()->eval() : 16.0;
     210    osg::BoundingBox bbox = t->getBoundingBox();
     211    float padding = (float)(charSize / 2.0);
     212    osg::Drawable *bd = osgEarth::Annotation::AnnotationUtils::create2DQuad(bbox, padding, _backdropColor);
     213    _backdropGeode->addDrawable(bd);
     214    _backdropGeode->setCullingActive(false);
     215
     216    applyStyle( _style );
     217
     218    setLightingIfNotSet( false );
     219
     220    osgEarth::Registry::shaderGenerator().run(
     221        this,
     222        "osgEarth.PlacardNode",
     223        osgEarth::Registry::stateSetCache() );
     224}
     225
     226void
     227PlacardNode::setAnnotationData( osgEarth::Annotation::AnnotationData* data )
     228{
     229    osgEarth::Annotation::OrthoNode::setAnnotationData( data );
     230
     231    // override this method so we can attach the anno data to the drawables.
     232    for(unsigned i=0; i<_geode->getNumDrawables(); ++i)
     233    {
     234        _geode->getDrawable(i)->setUserData( data );
     235    }
     236}
     237
     238void
     239PlacardNode::setDynamic( bool dynamic )
     240{
     241    osgEarth::Annotation::OrthoNode::setDynamic( dynamic );
     242
     243    osgText::Text* d = dynamic_cast<osgText::Text*>(_geode->getDrawable(0));
     244    if ( d )
     245    {
     246        d->setDataVariance( dynamic ? osg::Object::DYNAMIC : osg::Object::STATIC );
     247    }
     248    osg::Geometry *bd = dynamic_cast<osg::Geometry*>(_backdropGeode->getDrawable(0));
     249    if (bd) {
     250        bd->setDataVariance( dynamic ? osg::Object::DYNAMIC : osg::Object::STATIC );
     251    }
     252}
     253
     254osgEarth::Config
     255PlacardNode::getConfig() const
     256{
     257    osgEarth::Config conf( "placard" );
     258    conf.add   ( "text",   _text );
     259    conf.addObj( "style",  _style );
     260    conf.addObj( "position", getPosition() );
     261
     262    return conf;
     263}
  • geovis/trunk/Placard.h

    r5952 r5961  
    5858};
    5959
    60 class PlacardNode : public osgEarth::Annotation::LabelNode {
     60class PlacardLabelNode : public osgEarth::Annotation::LabelNode {
     61public:
     62    META_Node(osgEarthAnnotation, PlacardLabelNode);
     63    virtual bool accept(osgEarth::Annotation::Decoration* ds, bool enable)
     64    { return ds->apply(*this, enable); }
     65
     66    PlacardLabelNode() :
     67        osgEarth::Annotation::LabelNode()
     68    {}
     69    PlacardLabelNode(const PlacardLabelNode& other, const osg::CopyOp& op = osg::CopyOp::DEEP_COPY_ALL) :
     70        osgEarth::Annotation::LabelNode(other, op),
     71        _placardConf(other._placardConf)
     72    {
     73    }
     74    PlacardLabelNode(osgEarth::MapNode *mapNode,
     75                     const osgEarth::GeoPoint& position,
     76                     const Placard& placardConf,
     77                const osgEarth::Features::AttributeTable &attrs,
     78                     const osgEarth::Symbology::Style& style = osgEarth::Symbology::Style());
     79
     80    virtual ~PlacardLabelNode()
     81    {}
     82
     83    void setConfig(const Placard& placardConf);
     84
     85private:
     86    Placard _placardConf;
     87    osgEarth::Features::AttributeTable _attrs;
     88};
     89
     90class PlacardNode : public osgEarth::Annotation::OrthoNode {
    6191public:
    6292    META_Node(osgEarthAnnotation, PlacardNode);
     
    6595
    6696    PlacardNode() :
    67         osgEarth::Annotation::LabelNode()
     97        osgEarth::Annotation::OrthoNode()
    6898    {}
    6999    PlacardNode(const PlacardNode& other, const osg::CopyOp& op = osg::CopyOp::DEEP_COPY_ALL) :
    70         osgEarth::Annotation::LabelNode(other, op),
     100        osgEarth::Annotation::OrthoNode(other, op),
    71101        _placardConf(other._placardConf)
    72102    {
     
    83113    void setConfig(const Placard& placardConf);
    84114
    85 private:
     115    /**
     116     * Sets the text content.
     117     */
     118    void setText( const std::string& text );
     119    const std::string& text() const { return _text; }
     120    virtual const std::string& getText() const { return text(); }
     121
     122    /**
     123     * Gets a copy of the text style.
     124     */
     125    const osgEarth::Symbology::Style& getStyle() const { return _style; }
     126
     127    /**
     128     * Sets a new text style
     129     */
     130    void setStyle( const osgEarth::Symbology::Style& style );
     131
     132public: // OrthoNode override
     133
     134    virtual void setAnnotationData( osgEarth::Annotation::AnnotationData* data );
     135
     136    virtual void setDynamic( bool value );
     137
     138    virtual osgEarth::Config getConfig() const;
     139
     140protected:
     141    void init(const osgEarth::Symbology::Style& style);
     142
     143    std::string                _text;
     144    osgEarth::Symbology::Style _style;
     145    osg::ref_ptr<osg::Geode>   _geode;
     146    osg::ref_ptr<osg::Geode>   _backdropGeode;
     147    osg::Vec4 _backdropColor;
     148
    86149    Placard _placardConf;
    87150    osgEarth::Features::AttributeTable _attrs;
Note: See TracChangeset for help on using the changeset viewer.