wPlot2D version: 0.1.0
A lightweight C++ library for clear and customizable 2D scientific plots.
Loading...
Searching...
No Matches
wLegendEntity.hpp
Go to the documentation of this file.
1/*
2+---------------------------------------------------------------------------------------------------------------------------------------------------+
3Created by Wilfried Koch.
4Copyright @ 2025 Wilfried Koch. All rights reserved.
5+---------------------------------------------------------------------------------------------------------------------------------------------------+
6*/
7
8#ifndef W_LEGEND_ENTITY_HPP
9#define W_LEGEND_ENTITY_HPP
10
12#include "wLineEntity.hpp"
13#include "wFrameEntity.hpp"
14
15#include "wFunctionEntity.hpp"
16#include "wDataPlotEntity.hpp"
17
18#pragma GCC diagnostic push
19#pragma GCC diagnostic ignored "-Wfloat-equal"
20#pragma GCC diagnostic ignored "-Wswitch-default"
21#include <SFML/Graphics.hpp>
22#pragma GCC diagnostic pop
23
24namespace wPlot2D
25{
26
27
56 {
57 public:
67 LegendEntity( const sf::Font& font, const sf::Vector2f& position, bool hasFrame = true );
68
72 virtual ~LegendEntity( ) = default;
73
79 void addItem( const std::string& label, FunctionEntity* function );
80
86 void addItem( const std::wstring& label, FunctionEntity* function );
87
93 void addItem( const std::string& label, DataPlotEntity* plot );
94
100 void addItem( const std::wstring& label, DataPlotEntity* plot );
101
106 void setFrameEnabled( bool enabled );
107
112 void setFrameFillColor( const sf::Color& color );
113
118 void setFrameOutlineColor( const sf::Color& color );
119
124 void setFrameThickness( float thickness );
125
135 void setPadding( const sf::Vector2f& padding );
136
143 void setFont( const sf::Font& font );
144
149 void setCharacterSize( unsigned int size );
150
155 void setTextColor( const sf::Color& color );
156
165 void render( sf::RenderWindow& window );
166 private:
171 struct LegendItem
172 {
173 std::string label;
174 std::unique_ptr< LineEntity > line;
175 sf::Text labelText;
176
177 LegendItem( std::unique_ptr< LineEntity > line, sf::Text&& txt )
178 : line{ std::move( line ) },
179 labelText{ std::move( txt ) }
180 {
181
182 }
183 };
184
197 template < typename LabelT, typename SourceT >
198 void addItemGeneric( const LabelT& label, SourceT* source );
199
211 template< typename T >
212 void createItem( const T& label, std::unique_ptr< LineEntity > line );
213
214 std::vector< LegendItem > mItems;
215 const sf::Font& mFont;
216 unsigned int mCharacterSize = 30;
217 FrameEntity mFrame;
218 };
219
220} // namespace wPlot2D
221
222#endif
Represents an entity in the ECS (Entity-Component System) architecture.
Definition wEntity.hpp:60
Entity for plotting raw data points as a connected polyline.
Definition wDataPlotEntity.hpp:42
Represents a mathematical function as a drawable entity in a 2D plot.
Definition wFunctionEntity.hpp:43
void render(sf::RenderWindow &window)
Renders the legend (all items and optional frame) to the target window.
Definition wLegendEntity.cpp:150
void setFont(const sf::Font &font)
Updates the font used for all legend labels.
Definition wLegendEntity.cpp:111
virtual ~LegendEntity()=default
Virtual destructor.
void setCharacterSize(unsigned int size)
Sets the character size of the legend text.
Definition wLegendEntity.cpp:117
void setFrameEnabled(bool enabled)
Enables or disables the surrounding frame of the legend.
Definition wLegendEntity.cpp:86
LegendEntity(const sf::Font &font, const sf::Vector2f &position, bool hasFrame=true)
Constructs a LegendEntity.
Definition wLegendEntity.cpp:21
void setPadding(const sf::Vector2f &padding)
Sets the internal padding between items and the frame borders.
Definition wLegendEntity.cpp:106
void setFrameFillColor(const sf::Color &color)
Sets the fill color of the legend frame.
Definition wLegendEntity.cpp:91
void setFrameThickness(float thickness)
Sets the outline thickness of the legend frame.
Definition wLegendEntity.cpp:101
void addItem(const std::string &label, FunctionEntity *function)
Adds a new legend item associated with a function.
Definition wLegendEntity.cpp:36
void setTextColor(const sf::Color &color)
Sets the color of the legend labels.
Definition wLegendEntity.cpp:126
void setFrameOutlineColor(const sf::Color &color)
Sets the outline color of the legend frame.
Definition wLegendEntity.cpp:96
Definition wAxisEntity.cpp:17