Fixed background color setting for legends and refactored Legend class hierarchy

* Added a new cafTitledOverlayFrame as a base-class for all ResInsight legend classes
  * Inherits cvf::OverlayItem
This commit is contained in:
Gaute Lindkvist
2018-04-05 15:25:33 +02:00
parent 7a09b9a146
commit 507229f84b
14 changed files with 315 additions and 390 deletions

View File

@@ -12,6 +12,8 @@ add_library( ${PROJECT_NAME}
cafBoxManipulatorPartManager.h
cafBoxManipulatorGeometryGenerator.cpp
cafBoxManipulatorGeometryGenerator.h
cafTitledOverlayFrame.cpp
cafTitledOverlayFrame.h
cafCategoryLegend.cpp
cafCategoryLegend.h
cafOverlayScalarMapperLegend.h

View File

@@ -39,15 +39,8 @@ namespace caf {
///
//--------------------------------------------------------------------------------------------------
CategoryLegend::CategoryLegend(Font* font, const CategoryMapper* categoryMapper)
: m_sizeHint(200, 200),
m_textColor(Color3::BLACK),
m_lineColor(Color3::BLACK),
m_backgroundColor(1.0f, 1.0f, 1.0f, 0.8f),
m_backgroundFrameColor(0.0f, 0.0f, 0.0f, 0.5f),
m_isBackgroundEnabled(true),
m_lineWidth(1),
m_font(font),
m_categoryMapper(categoryMapper)
: TitledOverlayFrame(font, 200, 200)
, m_categoryMapper(categoryMapper)
{
CVF_ASSERT(font);
CVF_ASSERT(!font->isEmpty());
@@ -61,48 +54,6 @@ CategoryLegend::~CategoryLegend()
// Empty destructor to avoid errors with undefined types when cvf::ref's destructor gets called
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec2ui CategoryLegend::sizeHint()
{
return m_sizeHint;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setSizeHint(const Vec2ui& size)
{
m_sizeHint = size;
}
//--------------------------------------------------------------------------------------------------
/// Set color of the text
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setTextColor(const Color3f& color)
{
m_textColor = color;
}
//--------------------------------------------------------------------------------------------------
/// Set the title (text that will be rendered above the legend)
///
/// The legend supports multi-line titles. Separate each line with a '\n' character
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setTitle(const String& title)
{
// Title
if (title.isEmpty())
{
m_titleStrings.clear();
}
else
{
m_titleStrings = title.split("\n");
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -184,7 +135,7 @@ void CategoryLegend::renderGeneric(OpenGLContext* oglContext,
// Set up text drawer
float maxLegendRightPos = 0;
TextDrawer textDrawer(m_font.p());
TextDrawer textDrawer(this->font());
setupTextDrawer(&textDrawer, &layout, &maxLegendRightPos);
Vec2f backgroundSize(CVF_MIN(maxLegendRightPos + 3.0f, (float)size.x()), (float)size.y());
@@ -192,21 +143,21 @@ void CategoryLegend::renderGeneric(OpenGLContext* oglContext,
// Do the actual rendering
if (software)
{
if (m_isBackgroundEnabled) InternalLegendRenderTools::renderBackgroundImmediateMode(oglContext,
if (this->backgroundEnabled()) InternalLegendRenderTools::renderBackgroundImmediateMode(oglContext,
backgroundSize,
m_backgroundColor,
m_backgroundFrameColor);
this->backgroundColor(),
this->backgroundFrameColor());
renderLegendImmediateMode(oglContext, &layout);
textDrawer.renderSoftware(oglContext, camera);
}
else
{
const MatrixState matrixState(camera);
if (m_isBackgroundEnabled) InternalLegendRenderTools::renderBackgroundUsingShaders(oglContext,
if (this->backgroundEnabled()) InternalLegendRenderTools::renderBackgroundUsingShaders(oglContext,
matrixState,
backgroundSize,
m_backgroundColor,
m_backgroundFrameColor);
this->backgroundColor(),
this->backgroundFrameColor());
renderLegendUsingShaders(oglContext, &layout, matrixState);
textDrawer.render(oglContext, camera);
}
@@ -231,7 +182,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
float legendRight = 0.0f;
textDrawer->setVerticalAlignment(TextDrawer::CENTER);
textDrawer->setTextColor(m_textColor);
textDrawer->setTextColor(this->textColor());
m_visibleCategoryLabels.clear();
@@ -274,7 +225,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
Vec2f pos(textX, textY);
textDrawer->addText(displayText, pos);
float neededRightPos = pos.x() + m_font->textExtent(displayText).x();
float neededRightPos = pos.x() + this->font()->textExtent(displayText).x();
legendRight = legendRight >= neededRightPos ? legendRight :neededRightPos;
lastVisibleTextY = textY;
@@ -282,12 +233,12 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer,
}
float titleY = static_cast<float>(layout->size.y()) - layout->margins.y() - layout->charHeight / 2.0f;
for (size_t it = 0; it < m_titleStrings.size(); it++)
for (size_t it = 0; it < this->titleStrings().size(); it++)
{
Vec2f pos(layout->margins.x(), titleY);
textDrawer->addText(m_titleStrings[it], pos);
textDrawer->addText(this->titleStrings()[it], pos);
float neededRightPos = pos.x() + m_font->textExtent(m_titleStrings[it]).x();
float neededRightPos = pos.x() + this->font()->textExtent(this->titleStrings()[it]).x();
legendRight = legendRight >= neededRightPos ? legendRight :neededRightPos;
titleY -= layout->lineSpacing;
@@ -311,7 +262,7 @@ void CategoryLegend::renderLegendUsingShaders(OpenGLContext* oglContext,
RenderStateDepth depth(false);
depth.applyOpenGL(oglContext);
RenderStateLine line(static_cast<float>(m_lineWidth));
RenderStateLine line(static_cast<float>(this->lineWidth()));
line.applyOpenGL(oglContext);
// All vertices. Initialized here to set Z to zero once and for all.
@@ -393,7 +344,7 @@ void CategoryLegend::renderLegendUsingShaders(OpenGLContext* oglContext,
v2[1] = v3[1] = layout->legendRect.max().y() - 0.5f;
static const ushort frameConnects[] = { 0, 1, 1, 3, 3, 2, 2, 0 };
UniformFloat uniformColor("u_color", Color4f(m_lineColor));
UniformFloat uniformColor("u_color", Color4f(this->lineColor()));
shaderProgram->applyUniform(oglContext, uniformColor);
#ifdef CVF_OPENGL_ES
@@ -497,7 +448,7 @@ void CategoryLegend::renderLegendImmediateMode(OpenGLContext* oglContext, Overla
v0[1] = v1[1] = layout->legendRect.min().y() - 0.5f;
v2[1] = v3[1] = layout->legendRect.max().y() - 0.5f;
glColor3fv(m_textColor.ptr());
glColor3fv(this->textColor().ptr());
glBegin(GL_LINES);
glVertex3fv(v0);
glVertex3fv(v1);
@@ -528,13 +479,13 @@ void CategoryLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
{
CVF_TIGHT_ASSERT(layout);
ref<Glyph> glyph = m_font->getGlyph(L'A');
ref<Glyph> glyph = this->font()->getGlyph(L'A');
layout->charHeight = static_cast<float>(glyph->height());
layout->lineSpacing = layout->charHeight*1.5f;
layout->margins = Vec2f(4.0f, 4.0f);
float legendWidth = 25.0f;
float legendHeight = static_cast<float>(layout->size.y()) - 2 * layout->margins.y() - static_cast<float>(m_titleStrings.size())*layout->lineSpacing - layout->lineSpacing;
float legendHeight = static_cast<float>(layout->size.y()) - 2 * layout->margins.y() - static_cast<float>(this->titleStrings().size())*layout->lineSpacing - layout->lineSpacing;
layout->legendRect = Rectf(layout->margins.x(), layout->margins.y() + layout->charHeight / 2.0f, legendWidth, legendHeight);
if (layout->legendRect.width() < 1 || layout->legendRect.height() < 1)
@@ -547,46 +498,6 @@ void CategoryLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
layout->tickX = layout->x1 + 5;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setLineColor(const Color3f& lineColor)
{
m_lineColor = lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setLineWidth(int lineWidth)
{
m_lineWidth = lineWidth;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setBackgroundColor(const Color4f& backgroundColor)
{
m_backgroundColor = backgroundColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::setBackgroundFrameColor(const Color4f& backgroundFrameColor)
{
m_backgroundFrameColor = backgroundFrameColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryLegend::enableBackground(bool enable)
{
m_isBackgroundEnabled = enable;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include "cafTitledOverlayFrame.h"
#include "cvfBase.h"
#include "cvfArray.h"
#include "cvfCamera.h"
@@ -23,27 +24,15 @@ class CategoryMapper;
//
//
//==================================================================================================
class CategoryLegend : public cvf::OverlayItem
class CategoryLegend : public caf::TitledOverlayFrame
{
public:
CategoryLegend(cvf::Font* font, const CategoryMapper* categoryMapper);
virtual ~CategoryLegend();
void setSizeHint(const cvf::Vec2ui& size);
void setTextColor(const cvf::Color3f& color);
void setLineColor(const cvf::Color3f& lineColor);
void setLineWidth(int lineWidth);
void setTitle(const cvf::String& title);
void enableBackground(bool enable);
void setBackgroundColor(const cvf::Color4f& backgroundColor);
void setBackgroundFrameColor(const cvf::Color4f& backgroundFrameColor);
size_t categoryCount() const;
protected:
cvf::Vec2ui sizeHint() override;
void render(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size) override;
void renderSoftware(cvf::OpenGLContext* oglContext, const cvf::Vec2i& position, const cvf::Vec2ui& size) override;
bool pick(int oglXCoord, int oglYCoord, const cvf::Vec2i& position, const cvf::Vec2ui& size) override;
@@ -92,19 +81,7 @@ protected:
protected:
std::vector<bool> m_visibleCategoryLabels; // Skip labels ending up on top of previous visible label
cvf::Vec2ui m_sizeHint; // Pixel size of the color legend area
cvf::Color3f m_textColor;
cvf::Color3f m_lineColor;
int m_lineWidth;
std::vector<cvf::String> m_titleStrings;
cvf::ref<cvf::Font> m_font;
bool m_isBackgroundEnabled;
cvf::Color4f m_backgroundColor;
cvf::Color4f m_backgroundFrameColor;
cvf::cref<CategoryMapper> m_categoryMapper;
};

View File

@@ -83,16 +83,9 @@ using namespace cvf;
/// Constructor
//--------------------------------------------------------------------------------------------------
OverlayScalarMapperLegend::OverlayScalarMapperLegend(Font* font)
: m_sizeHint(200, 200),
m_textColor(Color3::BLACK),
m_lineColor(Color3::BLACK),
m_backgroundColor(1.0f, 1.0f, 1.0f, 0.8f),
m_backgroundFrameColor(0.0f, 0.0f, 0.0f, 0.5f),
m_isBackgroundEnabled(true),
m_lineWidth(1),
m_font(font),
m_tickNumberPrecision(4),
m_numberFormat(AUTO)
: TitledOverlayFrame(font, 200, 200)
, m_tickNumberPrecision(4)
, m_numberFormat(AUTO)
{
CVF_ASSERT(font);
CVF_ASSERT(!font->isEmpty());
@@ -112,16 +105,6 @@ OverlayScalarMapperLegend::~OverlayScalarMapperLegend()
// Empty destructor to avoid errors with undefined types when cvf::ref's destructor gets called
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec2ui OverlayScalarMapperLegend::sizeHint()
{
return m_sizeHint;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -138,43 +121,6 @@ void OverlayScalarMapperLegend::setScalarMapper(const ScalarMapper* scalarMapper
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setSizeHint(const Vec2ui& size)
{
m_sizeHint = size;
}
//--------------------------------------------------------------------------------------------------
/// Set color of the text
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setTextColor(const Color3f& color)
{
m_textColor = color;
}
//--------------------------------------------------------------------------------------------------
/// Set the title (text that will be rendered above the legend)
///
/// The legend supports multi-line titles. Separate each line with a '\n' character
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setTitle(const String& title)
{
// Title
if (title.isEmpty())
{
m_titleStrings.clear();
}
else
{
m_titleStrings = title.split("\n");
}
}
//--------------------------------------------------------------------------------------------------
/// Hardware rendering using shader programs
//--------------------------------------------------------------------------------------------------
@@ -243,7 +189,7 @@ void OverlayScalarMapperLegend::renderGeneric(OpenGLContext* oglContext, const V
// Set up text drawer
float maxLegendRightPos = 0;
TextDrawer textDrawer(m_font.p());
TextDrawer textDrawer(this->font());
setupTextDrawer(&textDrawer, &layout, &maxLegendRightPos);
Vec2f backgroundSize(CVF_MIN(maxLegendRightPos + 3.0f, (float)size.x()), (float)size.y());
@@ -251,14 +197,14 @@ void OverlayScalarMapperLegend::renderGeneric(OpenGLContext* oglContext, const V
// Do the actual rendering
if (software)
{
if (m_isBackgroundEnabled) InternalLegendRenderTools::renderBackgroundImmediateMode(oglContext, backgroundSize, m_backgroundColor, m_backgroundFrameColor);
if (this->backgroundEnabled()) InternalLegendRenderTools::renderBackgroundImmediateMode(oglContext, backgroundSize, this->backgroundColor(), this->backgroundFrameColor());
renderLegendImmediateMode(oglContext, &layout);
textDrawer.renderSoftware(oglContext, camera);
}
else
{
const MatrixState matrixState(camera);
if (m_isBackgroundEnabled) InternalLegendRenderTools::renderBackgroundUsingShaders(oglContext, matrixState, backgroundSize, m_backgroundColor, m_backgroundFrameColor);
if (this->backgroundEnabled()) InternalLegendRenderTools::renderBackgroundUsingShaders(oglContext, matrixState, backgroundSize, this->backgroundColor(), this->backgroundFrameColor());
renderLegendUsingShaders(oglContext, &layout, matrixState);
textDrawer.render(oglContext, camera);
}
@@ -277,7 +223,7 @@ void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const Ov
float legendRight = 0.0f;
textDrawer->setVerticalAlignment(TextDrawer::CENTER);
textDrawer->setTextColor(m_textColor);
textDrawer->setTextColor(this->textColor());
m_visibleTickLabels.clear();
@@ -330,7 +276,7 @@ void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const Ov
Vec2f pos(textX, textY);
textDrawer->addText(valueString, pos);
float neededRightPos = pos.x() + m_font->textExtent(valueString).x();
float neededRightPos = pos.x() + this->font()->textExtent(valueString).x();
legendRight = legendRight >= neededRightPos ? legendRight :neededRightPos;
lastVisibleTextY = textY;
@@ -338,12 +284,12 @@ void OverlayScalarMapperLegend::setupTextDrawer(TextDrawer* textDrawer, const Ov
}
float titleY = static_cast<float>(layout->size.y()) - layout->margins.y() - layout->charHeight/2.0f;
for (it = 0; it < m_titleStrings.size(); it++)
for (it = 0; it < this->titleStrings().size(); it++)
{
Vec2f pos(layout->margins.x(), titleY);
textDrawer->addText(m_titleStrings[it], pos);
textDrawer->addText(this->titleStrings()[it], pos);
float neededRightPos = pos.x() + m_font->textExtent(m_titleStrings[it]).x();
float neededRightPos = pos.x() + this->font()->textExtent(this->titleStrings()[it]).x();
legendRight = legendRight >= neededRightPos ? legendRight :neededRightPos;
@@ -367,7 +313,7 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
RenderStateDepth depth(false);
depth.applyOpenGL(oglContext);
RenderStateLine line(static_cast<float>(m_lineWidth));
RenderStateLine line(static_cast<float>(this->lineWidth()));
line.applyOpenGL(oglContext);
// All vertices. Initialized here to set Z to zero once and for all.
@@ -449,7 +395,7 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
v2[1] = v3[1] = layout->legendRect.max().y()-0.5f;
static const ushort frameConnects[] = { 0, 1, 1, 3, 3, 2, 2, 0};
UniformFloat uniformColor("u_color", Color4f(m_lineColor));
UniformFloat uniformColor("u_color", Color4f(this->lineColor()));
shaderProgram->applyUniform(oglContext, uniformColor);
#ifdef CVF_OPENGL_ES
@@ -482,7 +428,7 @@ void OverlayScalarMapperLegend::renderLegendUsingShaders(OpenGLContext* oglConte
// Dynamic coordinates for tickmarks-lines
v0[1] = v1[1] = v2[1] = v3[1] = v4[1] = y0;
UniformFloat uniformColor("u_color", Color4f(m_lineColor));
UniformFloat uniformColor("u_color", Color4f(this->lineColor()));
shaderProgram->applyUniform(oglContext, uniformColor);
const ushort * linesConnects;
@@ -597,7 +543,7 @@ void OverlayScalarMapperLegend::renderLegendImmediateMode(OpenGLContext* oglCont
v0[1] = v1[1] = layout->legendRect.min().y()-0.5f;
v2[1] = v3[1] = layout->legendRect.max().y()-0.5f;
glColor3fv(m_textColor.ptr());
glColor3fv(this->textColor().ptr());
glBegin(GL_LINES);
glVertex3fv(v0);
glVertex3fv(v1);
@@ -631,7 +577,7 @@ void OverlayScalarMapperLegend::renderLegendImmediateMode(OpenGLContext* oglCont
// Dynamic coordinates for tickmarks-lines
v0[1] = v1[1] = v2[1] = v3[1] = v4[1] = y0;
glColor3fv(m_textColor.ptr());
glColor3fv(this->textColor().ptr());
glBegin(GL_LINES);
if ( m_visibleTickLabels[ic])
{
@@ -664,13 +610,13 @@ void OverlayScalarMapperLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
{
CVF_TIGHT_ASSERT(layout);
ref<Glyph> glyph = m_font->getGlyph(L'A');
ref<Glyph> glyph = this->font()->getGlyph(L'A');
layout->charHeight = static_cast<float>(glyph->height());
layout->lineSpacing = layout->charHeight*1.5f;
layout->margins = Vec2f(4.0f, 4.0f);
float legendWidth = 25.0f;
float legendHeight = static_cast<float>(layout->size.y()) - 2*layout->margins.y() - static_cast<float>(m_titleStrings.size())*layout->lineSpacing - layout->lineSpacing;
float legendHeight = static_cast<float>(layout->size.y()) - 2*layout->margins.y() - static_cast<float>(this->titleStrings().size())*layout->lineSpacing - layout->lineSpacing;
layout->legendRect = Rectf(layout->margins.x(), layout->margins.y() + layout->charHeight/2.0f, legendWidth, legendHeight);
if (layout->legendRect.width() < 1 || layout->legendRect.height() < 1)
@@ -704,46 +650,6 @@ void OverlayScalarMapperLegend::layoutInfo(OverlayColorLegendLayoutInfo* layout)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setLineColor(const Color3f& lineColor)
{
m_lineColor = lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setBackgroundColor(const Color4f& backgroundColor)
{
m_backgroundColor = backgroundColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setBackgroundFrameColor(const Color4f& backgroundFrameColor)
{
m_backgroundFrameColor = backgroundFrameColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::enableBackground(bool enable)
{
m_isBackgroundEnabled = enable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void OverlayScalarMapperLegend::setLineWidth(int lineWidth)
{
m_lineWidth = lineWidth;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -37,6 +37,7 @@
#pragma once
#include "cafTitledOverlayFrame.h"
#include "cvfOverlayItem.h"
#include "cvfArray.h"
#include "cvfCamera.h"
@@ -59,7 +60,7 @@ namespace caf {
// Overlay color legend
//
//==================================================================================================
class OverlayScalarMapperLegend : public cvf::OverlayItem
class OverlayScalarMapperLegend : public caf::TitledOverlayFrame
{
using Font = cvf::Font;
using ScalarMapper = cvf::ScalarMapper;
@@ -81,25 +82,11 @@ public:
void setScalarMapper(const ScalarMapper* scalarMapper);
void setSizeHint(const Vec2ui& size);
void setTextColor(const Color3f& color);
void setLineColor(const Color3f& lineColor);
void setLineWidth(int lineWidth);
void setTickPrecision(int precision);
enum NumberFormat { AUTO, SCIENTIFIC, FIXED};
void setTickFormat(NumberFormat format);
void enableBackground(bool enable);
void setBackgroundColor(const Color4f& backgroundColor);
void setBackgroundFrameColor(const Color4f& backgroundFrameColor);
void setTitle(const String& title);
protected:
Vec2ui sizeHint() override;
void render(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size) override;
void renderSoftware(OpenGLContext* oglContext, const Vec2i& position, const Vec2ui& size) override;
bool pick(int oglXCoord, int oglYCoord, const Vec2i& position, const Vec2ui& size) override;
@@ -147,19 +134,6 @@ protected:
int m_tickNumberPrecision;
NumberFormat m_numberFormat;
Vec2ui m_sizeHint; // Pixel size of the color legend area
Color3f m_textColor;
Color3f m_lineColor;
bool m_isBackgroundEnabled;
Color4f m_backgroundColor;
Color4f m_backgroundFrameColor;
int m_lineWidth;
std::vector<String> m_titleStrings;
cvf::ref<Font> m_font;
cvf::cref<ScalarMapper> m_scalarMapper;
};

View File

@@ -0,0 +1,167 @@
#include "cafTitledOverlayFrame.h"
#include "cafCategoryMapper.h"
#include "cvfFont.h"
using namespace cvf;
namespace caf {
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TitledOverlayFrame::TitledOverlayFrame(Font* font, unsigned int width, unsigned int height)
: m_font(font)
, m_sizeHint(width, height)
, m_textColor(Color3::BLACK)
, m_lineColor(Color3::BLACK)
, m_lineWidth(1)
, m_isBackgroundEnabled(true)
, m_backgroundColor(1.0f, 1.0f, 1.0f, 0.8f)
, m_backgroundFrameColor(0.0f, 0.0f, 0.0f, 0.5f)
{
}
TitledOverlayFrame::~TitledOverlayFrame()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setSizeHint(const Vec2ui& size)
{
m_sizeHint = size;
}
//--------------------------------------------------------------------------------------------------
/// Set color of the text
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setTextColor(const Color3f& color)
{
m_textColor = color;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setLineColor(const Color3f& lineColor)
{
m_lineColor = lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setLineWidth(int lineWidth)
{
m_lineWidth = lineWidth;
}
//--------------------------------------------------------------------------------------------------
/// Set the title (text that will be rendered above the legend)
///
/// The legend supports multi-line titles. Separate each line with a '\n' character
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setTitle(const String& title)
{
// Title
if (title.isEmpty())
{
m_titleStrings.clear();
}
else
{
m_titleStrings = title.split("\n");
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::enableBackground(bool enable)
{
m_isBackgroundEnabled = enable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setBackgroundColor(const Color4f& backgroundColor)
{
m_backgroundColor = backgroundColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void TitledOverlayFrame::setBackgroundFrameColor(const Color4f& backgroundFrameColor)
{
m_backgroundFrameColor = backgroundFrameColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec2ui TitledOverlayFrame::sizeHint()
{
return m_sizeHint;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color3f TitledOverlayFrame::textColor() const
{
return m_textColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color3f TitledOverlayFrame::lineColor() const
{
return m_lineColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int TitledOverlayFrame::lineWidth() const
{
return m_lineWidth;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool TitledOverlayFrame::backgroundEnabled() const
{
return m_isBackgroundEnabled;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color4f TitledOverlayFrame::backgroundColor() const
{
return m_backgroundColor;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color4f TitledOverlayFrame::backgroundFrameColor() const
{
return m_backgroundFrameColor;
}
std::vector<cvf::String>& TitledOverlayFrame::titleStrings()
{
return m_titleStrings;
}
cvf::Font* TitledOverlayFrame::font()
{
return m_font.p();
}
}

View File

@@ -0,0 +1,62 @@
#pragma once
#include "cvfBase.h"
#include "cvfColor3.h"
#include "cvfColor4.h"
#include "cvfOverlayItem.h"
#include "cvfString.h"
namespace cvf
{
class Font;
}
namespace caf {
//==================================================================================================
// Base class for ApplicationFwk Legends.
// Abstract because the pure virtual render methods from cvf::OverlayItem are not implemented.
//==================================================================================================
class TitledOverlayFrame : public cvf::OverlayItem
{
public:
TitledOverlayFrame(cvf::Font* font, unsigned int width = 100, unsigned int height = 200);
virtual ~TitledOverlayFrame();
virtual void setSizeHint(const cvf::Vec2ui& size);
void setTextColor(const cvf::Color3f& color);
void setLineColor(const cvf::Color3f& lineColor);
void setLineWidth(int lineWidth);
void setTitle(const cvf::String& title);
void enableBackground(bool enable);
void setBackgroundColor(const cvf::Color4f& backgroundColor);
void setBackgroundFrameColor(const cvf::Color4f& backgroundFrameColor);
virtual cvf::Vec2ui sizeHint() override;
protected:
cvf::Color3f textColor() const;
cvf::Color3f lineColor() const;
int lineWidth() const;
bool backgroundEnabled() const;
cvf::Color4f backgroundColor() const;
cvf::Color4f backgroundFrameColor() const;
std::vector<cvf::String>& titleStrings();
cvf::Font* font();
private:
cvf::Vec2ui m_sizeHint; // The desired pixel size of the color legend area
cvf::Color3f m_textColor;
cvf::Color3f m_lineColor;
int m_lineWidth;
bool m_isBackgroundEnabled;
cvf::Color4f m_backgroundColor;
cvf::Color4f m_backgroundFrameColor;
std::vector<cvf::String> m_titleStrings;
cvf::ref<cvf::Font> m_font;
};
}