Skip to content

Commit

Permalink
Support label background colors
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Sep 11, 2023
1 parent f5b60f3 commit 5e1975c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
34 changes: 34 additions & 0 deletions ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
//--------------------------------------------------------------------------------------------------
RivAnnotationSourceInfo::RivAnnotationSourceInfo( const std::string m_text, const std::vector<cvf::Vec3d>& displayCoords )
: m_text( m_text )
, m_showColor( false )
, m_color( cvf::Color3f( cvf::Color3f::BLACK ) )
, m_anchorPointsDisplayCoord( displayCoords )
, m_labelPositionHint( RivAnnotationTools::LabelPositionStrategy::RIGHT )
{
Expand Down Expand Up @@ -59,3 +61,35 @@ std::vector<cvf::Vec3d> RivAnnotationSourceInfo::anchorPointsDisplayCoords() con
{
return m_anchorPointsDisplayCoord;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RivAnnotationSourceInfo::showColor() const
{
return m_showColor;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivAnnotationSourceInfo::setShowColor( bool showColor )
{
m_showColor = showColor;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color3f RivAnnotationSourceInfo::color() const
{
return m_color;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivAnnotationSourceInfo::setColor( const cvf::Color3f& color )
{
m_color = color;
}
10 changes: 9 additions & 1 deletion ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ class RivAnnotationSourceInfo : public cvf::Object
std::string text() const;
std::vector<cvf::Vec3d> anchorPointsDisplayCoords() const;

bool showColor() const;
void setShowColor( bool showColor );

cvf::Color3f color() const;
void setColor( const cvf::Color3f& color );

private:
std::string m_text;
std::vector<cvf::Vec3d> m_anchorPointsDisplayCoord;
bool m_showColor;
cvf::Color3f m_color;
RivAnnotationTools::LabelPositionStrategy m_labelPositionHint;
std::vector<cvf::Vec3d> m_anchorPointsDisplayCoord;
};
54 changes: 27 additions & 27 deletions ApplicationLibCode/ModelVisualization/RivAnnotationTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ void RivAnnotationTools::addAnnotationLabels( const cvf::Collection<cvf::Part>&

const double anchorLineScalingFactor = computeScalingFactorFromZoom( camera );

std::vector<LabelInfo> labelInfo;

for ( auto p : partCollection )
{
auto annotationObject = dynamic_cast<RivAnnotationSourceInfo*>( p->sourceInfo() );
if ( annotationObject )
{
std::vector<LabelInfo> labelInfo;

auto strategy = annotationObject->labelPositionStrategyHint();
if ( m_strategy != LabelPositionStrategy::UNDEFINED )
{
Expand Down Expand Up @@ -262,36 +262,36 @@ void RivAnnotationTools::addAnnotationLabels( const cvf::Collection<cvf::Part>&
labelInfo.push_back( { annotationObject->text(), lineAnchorPosition, labelPosition } );
}
}
}
}

for ( const auto& [labelText, lineAnchorPosition, labelPosition] : labelInfo )
{
// Line part
{
std::vector<cvf::Vec3d> points = { lineAnchorPosition, labelPosition };

auto anchorLineColor = cvf::Color3f::BLACK;
auto part = RivAnnotationTools::createPartFromPolyline( "AnnotationObjectAnchorPoints", anchorLineColor, points );
if ( part.notNull() )
for ( const auto& [labelText, lineAnchorPosition, labelPosition] : labelInfo )
{
model->addPart( part.p() );
}
}
// Line part
{
std::vector<cvf::Vec3d> points = { lineAnchorPosition, labelPosition };

auto anchorLineColor = cvf::Color3f::BLACK;
auto part = RivAnnotationTools::createPartFromPolyline( "AnnotationObjectAnchorPoints", anchorLineColor, points );
if ( part.notNull() )
{
model->addPart( part.p() );
}
}

// Text part
{
auto textColor = cvf::Color3f::BLACK;
auto backgroundColor = cvf::Color3f::LIGHT_GRAY;
auto fontSize = 10;
auto font = RiaFontCache::getFont( fontSize );
// Text part
{
auto backgroundColor = annotationObject->showColor() ? annotationObject->color() : cvf::Color3f::LIGHT_GRAY;
auto textColor = RiaColorTools::contrastColor( backgroundColor );
auto fontSize = 10;
auto font = RiaFontCache::getFont( fontSize );

auto drawableText = createDrawableText( font.p(), textColor, backgroundColor, labelText, cvf::Vec3f( labelPosition ) );
auto drawableText = createDrawableText( font.p(), textColor, backgroundColor, labelText, cvf::Vec3f( labelPosition ) );

auto part = createPart( drawableText.p() );
part->setName( "RivAnnotationTools: " + labelText );
auto part = createPart( drawableText.p() );
part->setName( "RivAnnotationTools: " + labelText );

model->addPart( part.p() );
model->addPart( part.p() );
}
}
}
}

Expand All @@ -316,7 +316,7 @@ cvf::ref<cvf::DrawableText> RivAnnotationTools::createDrawableText( cvf::Font*
drawableText->setDrawBackground( true );
drawableText->setVerticalAlignment( cvf::TextDrawer::BASELINE );
drawableText->setBackgroundColor( backgroundColor );
drawableText->setBorderColor( RiaColorTools::computeOffsetColor( backgroundColor, 0.3f ) );
drawableText->setBorderColor( RiaColorTools::computeOffsetColor( backgroundColor, 0.5f ) );
drawableText->setTextColor( textColor );

cvf::String cvfString( text );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ void RivSeismicSectionPartMgr::appendSurfaceIntersectionLines( cvf::ModelBasicLi
// Set the source info once, as this data is only used for display of labels
auto annoObj = new RivAnnotationSourceInfo( surface->fullName().toStdString(), completePolyLine );
annoObj->setLabelPositionStrategyHint( RivAnnotationTools::LabelPositionStrategy::RIGHT );
annoObj->setShowColor( true );
annoObj->setColor( surface->color() );

firstPart->setSourceInfo( annoObj );
}
Expand Down

0 comments on commit 5e1975c

Please sign in to comment.