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

Central entity responsible for graphical rendering in wPlot2D. More...

#include <wGraphicsEntity.hpp>

Inheritance diagram for wPlot2D::GraphicsEntity:
Inheritance graph

Public Member Functions

 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.
virtual ~GraphicsEntity ()=default
 Virtual destructor.
sf::RenderWindow & getWindow ()
 Gives access to the internal SFML window.
sf::Vector2u getWindowSize () const
 Retrieves the current window size.
void setWindowSize (const sf::Vector2u &newSize)
 Sets a new window size.
void setWindowTitle (const std::string &title)
 Updates the window title.
void setBackgroundColor (const sf::Color &color)
 Clears the window with a background color.
void addFont (const std::string &name, const std::string &fileName)
 Adds a font to the AssetManager.
sf::Font & getFont (const std::string name)
 Retrieves a previously loaded font.
sf::Vector2f getOrigin () const
 Returns the current logical origin (in pixels).
void setOrigin (sf::Vector2f originFactor)
 Sets a new logical origin (normalized).
sf::Vector2f getScale () const
 Returns the scale factors (pixels per logical unit).
void setScale (sf::Vector2f scaleFactor)
 Sets new scale factors (normalized).
sf::Vector2f getOffset () const
 Returns the current logical offset.
void setOffset (sf::Vector2f offset)
 Sets the logical offset applied to the axes.
AxisEntityaddAxis (AxisType type, sf::Vector2f axisRange)
 Adds an axis (X or Y) to the scene.
TitleEntityaddTitle (const std::string &title, TitleAlignment alignment=TitleAlignment::Bottom)
 Adds a main plot title (top or bottom).
TitleEntityaddTitle (const std::wstring &title, TitleAlignment alignment=TitleAlignment::Bottom)
 Adds a main plot title (top or bottom).
FunctionEntityaddFunction (std::function< double(double)> func, double startX, double endX, size_t nbPoints=1000)
 Adds a mathematical function to the scene.
DataPlotEntityaddDataPlot (const std::vector< sf::Vector2f > &dataPoints)
 Adds a raw data plot (connected points).
LegendEntityaddLegend (const sf::Vector2f &position, bool hasFrame=true)
 Adds a legend box at a given position.
TitleEntityaddText (const std::string &text, sf::Vector2f position)
 Adds arbitrary text to the scene.
TitleEntityaddText (const std::wstring &text, sf::Vector2f position)
 Adds arbitrary text to the scene.
LineEntityaddLine (const sf::Vector2f &start, const sf::Vector2f &end, bool withArrow=false)
 Adds a line segment to the scene.
void saveToFile (const std::string &filename)
 Saves a screenshot of the current 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

Central entity responsible for graphical rendering in wPlot2D.

The GraphicsEntity manages the creation and control of the SFML rendering window. It provides high-level methods to add and configure graphical entities:

  • Axes (X and Y),
  • Titles (main or custom text),
  • Functions and raw data plots,
  • Legends,
  • Lines (with or without arrows).

It also handles window configuration (title, size, background color) and allows exporting the final rendering to an image file.

Note
This class is intended to be the main entry point for user interaction with the rendering system.
Author
Wilfried Koch

Constructor & Destructor Documentation

◆ GraphicsEntity()

wPlot2D::GraphicsEntity::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.

Parameters
windowTitleThe title displayed on the window (default: "wPlot2D").
windowSizeWindow dimensions in pixels (default: 1600×1600).
originFactorNormalized factor in [0,1]x[0,1] specifying the origin’s relative position (default: ( 0.5f, 0.5f ).
scaleFactorNormalized factor specifying the size of one logical unit relative to window dimensions (default: ( 0.1f, 0.1f )).
Exceptions
std::invalid_argumentif originFactor not in [0,1].

◆ ~GraphicsEntity()

virtual wPlot2D::GraphicsEntity::~GraphicsEntity ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ getWindow()

sf::RenderWindow & wPlot2D::GraphicsEntity::getWindow ( )
nodiscard

Gives access to the internal SFML window.

Returns
Reference to the internal sf::RenderWindow.

◆ getWindowSize()

sf::Vector2u wPlot2D::GraphicsEntity::getWindowSize ( ) const
nodiscard

Retrieves the current window size.

Returns
Window size in pixels.

◆ setWindowSize()

void wPlot2D::GraphicsEntity::setWindowSize ( const sf::Vector2u & newSize)

Sets a new window size.

Parameters
newSizeWindow dimensions in pixels.

◆ setWindowTitle()

void wPlot2D::GraphicsEntity::setWindowTitle ( const std::string & title)

Updates the window title.

Parameters
titleNew window title.

◆ setBackgroundColor()

void wPlot2D::GraphicsEntity::setBackgroundColor ( const sf::Color & color)

Clears the window with a background color.

Parameters
colorBackground fill color.

◆ addFont()

void wPlot2D::GraphicsEntity::addFont ( const std::string & name,
const std::string & fileName )

Adds a font to the AssetManager.

Parameters
nameIdentifier string for the font.
fileNamePath to the font file.

◆ getFont()

sf::Font & wPlot2D::GraphicsEntity::getFont ( const std::string name)

Retrieves a previously loaded font.

Parameters
nameIdentifier of the font.
Returns
Reference to the sf::Font.
Exceptions
std::runtime_errorif the font is not found.

◆ getOrigin()

sf::Vector2f wPlot2D::GraphicsEntity::getOrigin ( ) const
nodiscard

Returns the current logical origin (in pixels).

Returns
Origin position in pixel coordinates.
Exceptions
std::runtime_errorif the PositionComponent is missing.

◆ setOrigin()

void wPlot2D::GraphicsEntity::setOrigin ( sf::Vector2f originFactor)

Sets a new logical origin (normalized).

Parameters
originFactorin [0,1]×[0,1] new relative origin.
Exceptions
std::invalid_argumentif originFactor is outside [0,1].

◆ getScale()

sf::Vector2f wPlot2D::GraphicsEntity::getScale ( ) const
nodiscard

Returns the scale factors (pixels per logical unit).

Returns
Scaling vector.
Exceptions
std::runtime_errorif the ScaleComponent is missing.

◆ setScale()

void wPlot2D::GraphicsEntity::setScale ( sf::Vector2f scaleFactor)

Sets new scale factors (normalized).

Parameters
scaleFactornew scaling factor

◆ getOffset()

sf::Vector2f wPlot2D::GraphicsEntity::getOffset ( ) const
nodiscard

Returns the current logical offset.

Returns
Offset vector in logical units.
Exceptions
std::runtime_errorif the OffsetComponent is missing.

◆ setOffset()

void wPlot2D::GraphicsEntity::setOffset ( sf::Vector2f offset)

Sets the logical offset applied to the axes.

Parameters
offsetDisplacement in logical units.

◆ addAxis()

AxisEntity * wPlot2D::GraphicsEntity::addAxis ( AxisType type,
sf::Vector2f axisRange )
nodiscard

Adds an axis (X or Y) to the scene.

Parameters
typeAxis type.
axisRangeLogical range for the axis.
Returns
Pointer to the created AxisEntity.

◆ addTitle() [1/2]

TitleEntity * wPlot2D::GraphicsEntity::addTitle ( const std::string & title,
TitleAlignment alignment = TitleAlignment::Bottom )
nodiscard

Adds a main plot title (top or bottom).

Parameters
titleTitle text (UTF-8).
alignmentVertical alignment (default: bottom).
Returns
Pointer to the created TitleEntity.

◆ addTitle() [2/2]

TitleEntity * wPlot2D::GraphicsEntity::addTitle ( const std::wstring & title,
TitleAlignment alignment = TitleAlignment::Bottom )
nodiscard

Adds a main plot title (top or bottom).

Parameters
titleTitle text (UTF-16/32).
alignmentVertical alignment (default: bottom).
Returns
Pointer to the created TitleEntity.

◆ addFunction()

FunctionEntity * wPlot2D::GraphicsEntity::addFunction ( std::function< double(double)> func,
double startX,
double endX,
size_t nbPoints = 1000 )
nodiscard

Adds a mathematical function to the scene.

Parameters
funcFunction of type double(double).
startXDomain start (logical).
endXDomain end (logical).
nbPointsSampling resolution (default 1000).
Returns
Pointer to the created FunctionEntity.

◆ addDataPlot()

DataPlotEntity * wPlot2D::GraphicsEntity::addDataPlot ( const std::vector< sf::Vector2f > & dataPoints)
nodiscard

Adds a raw data plot (connected points).

Parameters
dataPointsVector of (x,y) coordinates.
Returns
Pointer to the created DataPlotEntity.

◆ addLegend()

LegendEntity * wPlot2D::GraphicsEntity::addLegend ( const sf::Vector2f & position,
bool hasFrame = true )
nodiscard

Adds a legend box at a given position.

Parameters
positionNormalized position inside window [0,1]x[0,1].
hasFrameWhether the legend frame is visible (default true).
Returns
Pointer to the created LegendEntity.

◆ addText() [1/2]

TitleEntity * wPlot2D::GraphicsEntity::addText ( const std::string & text,
sf::Vector2f position )
nodiscard

Adds arbitrary text to the scene.

Parameters
textText string (UTF-8).
positionNormalized position in [0,1]x[0,1].
Returns
Pointer to the created TitleEntity.

◆ addText() [2/2]

TitleEntity * wPlot2D::GraphicsEntity::addText ( const std::wstring & text,
sf::Vector2f position )
nodiscard

Adds arbitrary text to the scene.

Parameters
textText string (UTF-16/32).
positionNormalized position in [0,1]x[0,1].
Returns
Pointer to the created TitleEntity.

◆ addLine()

LineEntity * wPlot2D::GraphicsEntity::addLine ( const sf::Vector2f & start,
const sf::Vector2f & end,
bool withArrow = false )
nodiscard

Adds a line segment to the scene.

Parameters
startStart point in logical coordinates.
endEnd point in logical coordinates.
withArrowWhether to render an arrowhead at the end.
Returns
Pointer to the created LineEntity.

◆ saveToFile()

void wPlot2D::GraphicsEntity::saveToFile ( const std::string & filename)

Saves a screenshot of the current window.

Parameters
filenameOutput file path (supported: png, bmp, tga, jpg).
Exceptions
std::runtime_errorif saving fails.

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