wPlot2D version: 0.1.0
A lightweight C++ library for clear and customizable 2D scientific plots.
Loading...
Searching...
No Matches
wGraphicsEntity.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_GRAPHICS_ENTITY_HPP
9#define W_GRAPHICS_ENTITY_HPP
10
12
15
16#include "wAxisEntity.hpp"
17#include "wTitleEntity.hpp"
18#include "wFunctionEntity.hpp"
19#include "wDataPlotEntity.hpp"
20#include "wLegendEntity.hpp"
21#include "wLineEntity.hpp"
22
23#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wfloat-equal"
25#pragma GCC diagnostic ignored "-Wswitch-default"
26#include <SFML/Graphics.hpp>
27#pragma GCC diagnostic pop
28
29namespace wPlot2D
30{
31
36 enum class TitleAlignment
37 {
40 };
41
65 {
66 public:
78 const std::string& windowTitle = "wPlot2D",
79 const sf::Vector2u& windowSize = { 1600, 1600 },
80 const sf::Vector2f& originFactor = { 0.5f, 0.5f },
81 const sf::Vector2f& scaleFactor = { 0.1f, 0.1f } );
82
86 virtual ~GraphicsEntity( ) = default;
87
92 [[nodiscard]] sf::RenderWindow& getWindow( );
93
98 [[nodiscard]] sf::Vector2u getWindowSize( ) const;
99
104 void setWindowSize( const sf::Vector2u& newSize );
105
110 void setWindowTitle( const std::string& title );
111
116 void setBackgroundColor( const sf::Color& color );
117
123 void addFont( const std::string& name, const std::string& fileName );
124
131 sf::Font& getFont( const std::string name );
132
138 [[nodiscard]] sf::Vector2f getOrigin( ) const;
139
145 void setOrigin( sf::Vector2f originFactor );
146
152 [[nodiscard]] sf::Vector2f getScale( ) const;
153
158 void setScale( sf::Vector2f scaleFactor );
159
165 [[nodiscard]] sf::Vector2f getOffset( ) const;
166
171 void setOffset( sf::Vector2f offset );
172
179 [[nodiscard]] AxisEntity* addAxis( AxisType type, sf::Vector2f axisRange );
180
187 [[nodiscard]] TitleEntity* addTitle( const std::string& title, TitleAlignment alignment = TitleAlignment::Bottom );
188
195 [[nodiscard]] TitleEntity* addTitle( const std::wstring& title, TitleAlignment alignment = TitleAlignment::Bottom );
196
205 [[nodiscard]] FunctionEntity* addFunction( std::function< double( double )> func,
206 double startX, double endX, size_t nbPoints = 1000 );
207
213 [[nodiscard]] DataPlotEntity* addDataPlot( const std::vector< sf::Vector2f >& dataPoints );
214
221 [[nodiscard]] LegendEntity* addLegend( const sf::Vector2f& position, bool hasFrame = true );
222
229 [[nodiscard]] TitleEntity* addText( const std::string& text, sf::Vector2f position );
230
237 [[nodiscard]] TitleEntity* addText( const std::wstring& text, sf::Vector2f position );
238
246 [[nodiscard]] LineEntity* addLine( const sf::Vector2f& start, const sf::Vector2f& end, bool withArrow = false );
247
253 void saveToFile( const std::string& filename );
254 private:
255 sf::RenderWindow mWindow;
256 wEngine::AssetManager mAssets;
257 std::unique_ptr< AxisEntity > mAxisX;
258 std::unique_ptr< AxisEntity > mAxisY;
259 std::unique_ptr< TitleEntity > mTitle;
260 TitleAlignment mAlignment;
261
262 template < typename T >
263 TitleEntity* addTitleImpl( const T& title, TitleAlignment alignment );
264
265 template < typename T >
266 TitleEntity* initText( const T& title, sf::Vector2f position );
267
268 struct FunctionData
269 {
270 std::unique_ptr< FunctionEntity > entity;
271 double startX;
272 double endX;
273 size_t nbPoints;
274 };
275 std::vector< FunctionData > mFunctions;
276
277 struct DataPlotData
278 {
279 std::unique_ptr< DataPlotEntity > entity;
280 };
281 std::vector< DataPlotData > mDataPlots;
282
283 std::unique_ptr< LegendEntity > mLegend;
284
285 struct TextData
286 {
287 std::unique_ptr< TitleEntity > entity;
288 };
289 std::vector< TextData > mTexts;
290
291 struct LineData
292 {
293 std::unique_ptr< LineEntity > entity;
294 };
295 std::vector< LineData > mLines;
296
313 void render( );
314
325 void validateNormalizedFactor( const sf::Vector2f& factor ) const;
326
338 [[nodiscard]] sf::Vector2f convertNormalizedToPixels( const sf::Vector2f& factor ) const;
339 };
340
341}//End of namespace wPlot2D
342
343#endif
Manages graphical assets such as fonts for reuse across the application.
Definition wAssetManager.hpp:50
Represents an entity in the ECS (Entity-Component System) architecture.
Definition wEntity.hpp:60
Represents a visual axis (X or Y) in a 2D plot with optional notches and title.
Definition wAxisEntity.hpp:67
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
sf::Vector2f getOrigin() const
Returns the current logical origin (in pixels).
Definition wGraphicsEntity.cpp:102
void setWindowTitle(const std::string &title)
Updates the window title.
Definition wGraphicsEntity.cpp:70
LegendEntity * addLegend(const sf::Vector2f &position, bool hasFrame=true)
Adds a legend box at a given position.
Definition wGraphicsEntity.cpp:248
sf::RenderWindow & getWindow()
Gives access to the internal SFML window.
Definition wGraphicsEntity.cpp:53
void setScale(sf::Vector2f scaleFactor)
Sets new scale factors (normalized).
Definition wGraphicsEntity.cpp:122
LineEntity * addLine(const sf::Vector2f &start, const sf::Vector2f &end, bool withArrow=false)
Adds a line segment to the scene.
Definition wGraphicsEntity.cpp:295
void setOrigin(sf::Vector2f originFactor)
Sets a new logical origin (normalized).
Definition wGraphicsEntity.cpp:108
virtual ~GraphicsEntity()=default
Virtual destructor.
AxisEntity * addAxis(AxisType type, sf::Vector2f axisRange)
Adds an axis (X or Y) to the scene.
Definition wGraphicsEntity.cpp:146
sf::Vector2f getOffset() const
Returns the current logical offset.
Definition wGraphicsEntity.cpp:128
void setWindowSize(const sf::Vector2u &newSize)
Sets a new window size.
Definition wGraphicsEntity.cpp:63
void addFont(const std::string &name, const std::string &fileName)
Adds a font to the AssetManager.
Definition wGraphicsEntity.cpp:82
void setBackgroundColor(const sf::Color &color)
Clears the window with a background color.
Definition wGraphicsEntity.cpp:75
DataPlotEntity * addDataPlot(const std::vector< sf::Vector2f > &dataPoints)
Adds a raw data plot (connected points).
Definition wGraphicsEntity.cpp:231
void saveToFile(const std::string &filename)
Saves a screenshot of the current window.
Definition wGraphicsEntity.cpp:313
FunctionEntity * addFunction(std::function< double(double)> func, double startX, double endX, size_t nbPoints=1000)
Adds a mathematical function to the scene.
Definition wGraphicsEntity.cpp:214
sf::Vector2f getScale() const
Returns the scale factors (pixels per logical unit).
Definition wGraphicsEntity.cpp:116
TitleEntity * addTitle(const std::string &title, TitleAlignment alignment=TitleAlignment::Bottom)
Adds a main plot title (top or bottom).
Definition wGraphicsEntity.cpp:166
void setOffset(sf::Vector2f offset)
Sets the logical offset applied to the axes.
Definition wGraphicsEntity.cpp:134
sf::Font & getFont(const std::string name)
Retrieves a previously loaded font.
Definition wGraphicsEntity.cpp:88
GraphicsEntity(const std::string &windowTitle="wPlot2D", const sf::Vector2u &windowSize={ 1600, 1600 }, const sf::Vector2f &originFactor={ 0.5f, 0.5f }, const sf::Vector2f &scaleFactor={ 0.1f, 0.1f })
Constructs the graphics entity and initializes the rendering window and components.
Definition wGraphicsEntity.cpp:26
sf::Vector2u getWindowSize() const
Retrieves the current window size.
Definition wGraphicsEntity.cpp:58
TitleEntity * addText(const std::string &text, sf::Vector2f position)
Adds arbitrary text to the scene.
Definition wGraphicsEntity.cpp:264
Represents a legend box that describes functions and data plots.
Definition wLegendEntity.hpp:56
Entity representing a straight line segment with optional arrowhead.
Definition wLineEntity.hpp:43
Represents a textual label (typically an axis title or main plot title) in a 2D plot.
Definition wTitleEntity.hpp:45
Definition wAxisEntity.cpp:17
AxisType
Enum representing the type of axis to render.
Definition wAxisEntity.hpp:35
TitleAlignment
Defines the vertical placement of the main plot title.
Definition wGraphicsEntity.hpp:37
@ Bottom
Definition wGraphicsEntity.hpp:39
@ Top
Definition wGraphicsEntity.hpp:38