wPlot2D version: 0.1.0
A lightweight C++ library for clear and customizable 2D scientific plots.
Loading...
Searching...
No Matches
wPlot2D::LegendEntity Class Reference

Represents a legend box that describes functions and data plots. More...

#include <wLegendEntity.hpp>

Inheritance diagram for wPlot2D::LegendEntity:
Inheritance graph

Public Member Functions

 LegendEntity (const sf::Font &font, const sf::Vector2f &position, bool hasFrame=true)
 Constructs a LegendEntity.
virtual ~LegendEntity ()=default
 Virtual destructor.
void addItem (const std::string &label, FunctionEntity *function)
 Adds a new legend item associated with a function.
void addItem (const std::wstring &label, FunctionEntity *function)
 Adds a new legend item associated with a function (wide string).
void addItem (const std::string &label, DataPlotEntity *plot)
 Adds a new legend item associated with a data plot.
void addItem (const std::wstring &label, DataPlotEntity *plot)
 Adds a new legend item associated with a data plot (wide string).
void setFrameEnabled (bool enabled)
 Enables or disables the surrounding frame of the legend.
void setFrameFillColor (const sf::Color &color)
 Sets the fill color of the legend frame.
void setFrameOutlineColor (const sf::Color &color)
 Sets the outline color of the legend frame.
void setFrameThickness (float thickness)
 Sets the outline thickness of the legend frame.
void setPadding (const sf::Vector2f &padding)
 Sets the internal padding between items and the frame borders.
void setFont (const sf::Font &font)
 Updates the font used for all legend labels.
void setCharacterSize (unsigned int size)
 Sets the character size of the legend text.
void setTextColor (const sf::Color &color)
 Sets the color of the legend labels.
void render (sf::RenderWindow &window)
 Renders the legend (all items and optional frame) to the target window.
Public Member Functions inherited from wEngine::Entity
 Entity ()
virtual ~Entity ()
unsigned int getEntityID () const
 Returns the unique ID associated with this entity.
void clearComponents ()
 Removes all components currently attached to the entity.
template<typename T, typename... Args>
std::shared_ptr< T > addComponent (Args &&... args)
 Adds a new component of type T to the entity.
template<typename T>
void removeComponent ()
 Removes the component of type T from the entity.
template<typename T>
bool hasComponent () const noexcept
 Checks whether the entity has a component of type T.
template<typename T>
std::shared_ptr< T > getComponent () const
 Retrieves the component of type T attached to the entity.
template<typename T>
std::shared_ptr< T > requireComponent (const std::string &context="") const
 Retrieves the component of type T and throws if it's missing.
template<typename Interface>
std::shared_ptr< Interface > getInterfaceComponent () const
 Returns the first component that implements the specified interface.

Additional Inherited Members

Static Public Member Functions inherited from wEngine::Entity
static void resetEntityIDCounter ()
 Resets the global entity ID counter to zero.

Detailed Description

Represents a legend box that describes functions and data plots.

A LegendEntity provides a visual legend for plotted entities such as FunctionEntity and DataPlotEntity. Each legend item is composed of:

  • A sample line (LineEntity) with the same style, thickness, and color as the source entity.
  • A text label (sf::Text) describing the entity.

The legend can optionally be surrounded by a frame (FrameEntity) with configurable padding, outline color, thickness, and fill color.

Components and Features:

  • Configurable font and text size.
  • Support for UTF-8 and wide string labels.
  • Dynamic addition of items from existing plotted entities.
  • Automatic alignment of line + text pairs inside the legend box.
See also
FunctionEntity, DataPlotEntity, LineEntity, FrameEntity
Note
The font passed in the constructor must remain valid during the lifetime of the legend, as SFML does not copy font data internally.
Author
Wilfried Koch

Constructor & Destructor Documentation

◆ LegendEntity()

wPlot2D::LegendEntity::LegendEntity ( const sf::Font & font,
const sf::Vector2f & position,
bool hasFrame = true )

Constructs a LegendEntity.

Initializes the legend with a given font, anchor position, and optional frame.

Parameters
fontReference to the font used for labels (must remain valid).
positionAnchor position of the legend box in window coordinates.
hasFrameWhether to display a surrounding frame (default: true).

◆ ~LegendEntity()

virtual wPlot2D::LegendEntity::~LegendEntity ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ addItem() [1/4]

void wPlot2D::LegendEntity::addItem ( const std::string & label,
FunctionEntity * function )

Adds a new legend item associated with a function.

Parameters
labelLabel text (UTF-8 string).
functionPointer to the source FunctionEntity.

◆ addItem() [2/4]

void wPlot2D::LegendEntity::addItem ( const std::wstring & label,
FunctionEntity * function )

Adds a new legend item associated with a function (wide string).

Parameters
labelLabel text (wide string).
functionPointer to the source FunctionEntity.

◆ addItem() [3/4]

void wPlot2D::LegendEntity::addItem ( const std::string & label,
DataPlotEntity * plot )

Adds a new legend item associated with a data plot.

Parameters
labelLabel text (UTF-8 string).
plotPointer to the source DataPlotEntity.

◆ addItem() [4/4]

void wPlot2D::LegendEntity::addItem ( const std::wstring & label,
DataPlotEntity * plot )

Adds a new legend item associated with a data plot (wide string).

Parameters
labelLabel text (wide string).
plotPointer to the source DataPlotEntity.

◆ setFrameEnabled()

void wPlot2D::LegendEntity::setFrameEnabled ( bool enabled)

Enables or disables the surrounding frame of the legend.

Parameters
enabledTrue to display the frame, false to hide it.

◆ setFrameFillColor()

void wPlot2D::LegendEntity::setFrameFillColor ( const sf::Color & color)

Sets the fill color of the legend frame.

Parameters
colorSFML color applied to the frame's background.

◆ setFrameOutlineColor()

void wPlot2D::LegendEntity::setFrameOutlineColor ( const sf::Color & color)

Sets the outline color of the legend frame.

Parameters
colorSFML color applied to the frame's border.

◆ setFrameThickness()

void wPlot2D::LegendEntity::setFrameThickness ( float thickness)

Sets the outline thickness of the legend frame.

Parameters
thicknessThickness in pixels (positive for outside expansion).

◆ setPadding()

void wPlot2D::LegendEntity::setPadding ( const sf::Vector2f & padding)

Sets the internal padding between items and the frame borders.

Padding defines horizontal and vertical margins in pixels.

Parameters
paddingVector (x, y) where:
  • x = horizontal padding (left/right),
  • y = vertical padding (top/bottom).

◆ setFont()

void wPlot2D::LegendEntity::setFont ( const sf::Font & font)

Updates the font used for all legend labels.

Note
The font must remain valid during the legend’s lifetime, as SFML does not copy font data internally.
Parameters
fontReference to an externally managed sf::Font.

◆ setCharacterSize()

void wPlot2D::LegendEntity::setCharacterSize ( unsigned int size)

Sets the character size of the legend text.

Parameters
sizeFont size in pixels.

◆ setTextColor()

void wPlot2D::LegendEntity::setTextColor ( const sf::Color & color)

Sets the color of the legend labels.

Parameters
colorSFML color applied to all legend text.

◆ render()

void wPlot2D::LegendEntity::render ( sf::RenderWindow & window)

Renders the legend (all items and optional frame) to the target window.

Each item is drawn with its sample line and label text, aligned inside the legend box. The optional frame is drawn behind all items.

Parameters
windowThe target SFML render window.

The documentation for this class was generated from the following files: