| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_RENDERING_TEXTLABELBASE_H |
| 7 |
#define AVOGADRO_RENDERING_TEXTLABELBASE_H |
| 8 |
|
| 9 |
#include "avogadrorenderingexport.h" |
| 10 |
#include "drawable.h" |
| 11 |
|
| 12 |
#include <avogadro/rendering/textproperties.h> |
| 13 |
|
| 14 |
#include <avogadro/core/array.h> |
| 15 |
|
| 16 |
#include <string> |
| 17 |
|
| 18 |
namespace Avogadro { |
| 19 |
namespace Rendering { |
| 20 |
class TextRenderStrategy; |
| 21 |
|
| 22 |
/** |
| 23 |
* @class TextLabelBase textlabelbase.h <avogadro/rendering/textlabelbase.h> |
| 24 |
* @brief The TextLabelBase class provides a generic implementation of a text |
| 25 |
* drawable. |
| 26 |
*/ |
| 27 |
class AVOGADRORENDERING_EXPORT TextLabelBase : public Drawable |
| 28 |
{ |
| 29 |
public: |
| 30 |
TextLabelBase(); |
| 31 |
TextLabelBase(const TextLabelBase& other); |
| 32 |
~TextLabelBase() override; |
| 33 |
|
| 34 |
TextLabelBase& operator=(TextLabelBase other); |
| 35 |
friend void swap(TextLabelBase& lhs, TextLabelBase& rhs); |
| 36 |
|
| 37 |
void render(const Camera& camera) override; |
| 38 |
|
| 39 |
/** |
| 40 |
* Render the string to the internal texture buffer. |
| 41 |
* @param tren The text rendering strategy to use. |
| 42 |
*/ |
| 43 |
void buildTexture(const TextRenderStrategy& tren); |
| 44 |
|
| 45 |
/** |
| 46 |
* The text that will be rendered. |
| 47 |
* @{ |
| 48 |
*/ |
| 49 |
void setText(const std::string& str); |
| 50 |
const std::string& text() const; |
| 51 |
/** @} */ |
| 52 |
|
| 53 |
/** |
| 54 |
* The properties of the rendered text. |
| 55 |
* @{ |
| 56 |
*/ |
| 57 |
void setTextProperties(const TextProperties& tprop); |
| 58 |
const TextProperties& textProperties() const; |
| 59 |
/** @} */ |
| 60 |
|
| 61 |
/** |
| 62 |
* Clear the texture, forcing it to be regenerated on the next render. |
| 63 |
*/ |
| 64 |
void resetTexture(); |
| 65 |
|
| 66 |
protected: |
| 67 |
std::string m_text; |
| 68 |
TextProperties m_textProperties; |
| 69 |
Vector2i m_imageDimensions; |
| 70 |
Core::Array<unsigned char> m_imageRgba; |
| 71 |
|
| 72 |
// Subclasses use this to update the text position: |
| 73 |
void setAnchorInternal(const Vector3f& anchor); |
| 74 |
Vector3f getAnchorInternal() const; |
| 75 |
|
| 76 |
// ...and the radius. |
| 77 |
void setRadiusInternal(float radius); |
| 78 |
float getRadiusInternal() const; |
| 79 |
|
| 80 |
void markDirty(); |
| 81 |
|
| 82 |
private: |
| 83 |
// Container for rendering cache: |
| 84 |
class RenderImpl; |
| 85 |
RenderImpl* const m_render; |
| 86 |
}; |
| 87 |
|
| 88 |
inline TextLabelBase& TextLabelBase::operator=(TextLabelBase other) |
| 89 |
{ |
| 90 |
using std::swap; |
| 91 |
swap(*this, other); |
| 92 |
return *this; |
| 93 |
} |
| 94 |
|
| 95 |
inline void swap(TextLabelBase& lhs, TextLabelBase& rhs) |
| 96 |
{ |
| 97 |
using std::swap; |
| 98 |
swap(static_cast<Drawable&>(lhs), static_cast<Drawable&>(rhs)); |
| 99 |
swap(lhs.m_text, rhs.m_text); |
| 100 |
swap(lhs.m_textProperties, rhs.m_textProperties); |
| 101 |
swap(lhs.m_imageDimensions, rhs.m_imageDimensions); |
| 102 |
swap(lhs.m_imageRgba, rhs.m_imageRgba); |
| 103 |
lhs.markDirty(); |
| 104 |
rhs.markDirty(); |
| 105 |
} |
| 106 |
|
| 107 |
} // namespace Rendering |
| 108 |
} // namespace Avogadro |
| 109 |
|
| 110 |
#endif // AVOGADRO_RENDERING_TEXTLABELBASE_H |