From 5e1975c7627a501dbb30c9daf22412ceed7023f6 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Mon, 11 Sep 2023 20:02:21 +0200 Subject: [PATCH] Support label background colors --- .../RivAnnotationSourceInfo.cpp | 34 ++++++++++++ .../RivAnnotationSourceInfo.h | 10 +++- .../ModelVisualization/RivAnnotationTools.cpp | 54 +++++++++---------- .../Seismic/RivSeismicSectionPartMgr.cpp | 2 + 4 files changed, 72 insertions(+), 28 deletions(-) diff --git a/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.cpp b/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.cpp index 5f07d32aaa..c15ed4d5cf 100644 --- a/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.cpp +++ b/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.cpp @@ -23,6 +23,8 @@ //-------------------------------------------------------------------------------------------------- RivAnnotationSourceInfo::RivAnnotationSourceInfo( const std::string m_text, const std::vector& displayCoords ) : m_text( m_text ) + , m_showColor( false ) + , m_color( cvf::Color3f( cvf::Color3f::BLACK ) ) , m_anchorPointsDisplayCoord( displayCoords ) , m_labelPositionHint( RivAnnotationTools::LabelPositionStrategy::RIGHT ) { @@ -59,3 +61,35 @@ std::vector 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; +} diff --git a/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.h b/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.h index 4de5138e73..d8c3cf82fe 100644 --- a/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.h +++ b/ApplicationLibCode/ModelVisualization/RivAnnotationSourceInfo.h @@ -38,8 +38,16 @@ class RivAnnotationSourceInfo : public cvf::Object std::string text() const; std::vector 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 m_anchorPointsDisplayCoord; + bool m_showColor; + cvf::Color3f m_color; RivAnnotationTools::LabelPositionStrategy m_labelPositionHint; + std::vector m_anchorPointsDisplayCoord; }; diff --git a/ApplicationLibCode/ModelVisualization/RivAnnotationTools.cpp b/ApplicationLibCode/ModelVisualization/RivAnnotationTools.cpp index 5ac7232f66..b325819668 100644 --- a/ApplicationLibCode/ModelVisualization/RivAnnotationTools.cpp +++ b/ApplicationLibCode/ModelVisualization/RivAnnotationTools.cpp @@ -199,13 +199,13 @@ void RivAnnotationTools::addAnnotationLabels( const cvf::Collection& const double anchorLineScalingFactor = computeScalingFactorFromZoom( camera ); - std::vector labelInfo; - for ( auto p : partCollection ) { auto annotationObject = dynamic_cast( p->sourceInfo() ); if ( annotationObject ) { + std::vector labelInfo; + auto strategy = annotationObject->labelPositionStrategyHint(); if ( m_strategy != LabelPositionStrategy::UNDEFINED ) { @@ -262,36 +262,36 @@ void RivAnnotationTools::addAnnotationLabels( const cvf::Collection& labelInfo.push_back( { annotationObject->text(), lineAnchorPosition, labelPosition } ); } } - } - } - for ( const auto& [labelText, lineAnchorPosition, labelPosition] : labelInfo ) - { - // Line part - { - std::vector 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 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() ); + } + } } } @@ -316,7 +316,7 @@ cvf::ref 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 ); diff --git a/ApplicationLibCode/ModelVisualization/Seismic/RivSeismicSectionPartMgr.cpp b/ApplicationLibCode/ModelVisualization/Seismic/RivSeismicSectionPartMgr.cpp index e0c4f239eb..e1673d4739 100644 --- a/ApplicationLibCode/ModelVisualization/Seismic/RivSeismicSectionPartMgr.cpp +++ b/ApplicationLibCode/ModelVisualization/Seismic/RivSeismicSectionPartMgr.cpp @@ -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 ); }