lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
lmgl::ui::Canvas Class Reference

Canvas for rendering 2D UI elements. More...

#include <canvas.hpp>

Public Member Functions

 Canvas (int width=1920, int height=1080)
 Constructor for Canvas.
 ~Canvas ()=default
 Destructor for Canvas.
void resize (int width, int height)
 Resize the canvas to match window dimensions.
void add_element (std::shared_ptr< UIElement > element)
 Add a UI element to the canvas.
void remove_element (std::shared_ptr< UIElement > element)
 Remove a UI element from the canvas.
const std::vector< std::shared_ptr< UIElement > > & get_elements () const
 Get all UI elements in the canvas.
void render ()
 Render all UI elements.
const glm::mat4 & get_projection_matrix () const
 Get the orthographic projection matrix.
int get_width () const
 Get the canvas width.
int get_height () const
 Get the canvas height.
void set_visible (bool visible)
 Set visibility of the entire canvas.
bool is_visible () const
 Check if the canvas is visible.

Detailed Description

Canvas for rendering 2D UI elements.

The Canvas class provides a 2D rendering surface that sits on top of the 3D world. It manages orthographic projection, automatic window resizing, blending for transparency, and proper render ordering of UI elements.

Constructor & Destructor Documentation

◆ Canvas()

lmgl::ui::Canvas::Canvas ( int width = 1920,
int height = 1080 )

Constructor for Canvas.

Parameters
widthInitial width of the canvas in pixels.
heightInitial height of the canvas in pixels.

Member Function Documentation

◆ add_element()

void lmgl::ui::Canvas::add_element ( std::shared_ptr< UIElement > element)

Add a UI element to the canvas.

Parameters
elementShared pointer to the UI element.

◆ get_elements()

const std::vector< std::shared_ptr< UIElement > > & lmgl::ui::Canvas::get_elements ( ) const
inline

Get all UI elements in the canvas.

Returns
Vector of shared pointers to UI elements.

◆ get_height()

int lmgl::ui::Canvas::get_height ( ) const
inline

Get the canvas height.

Returns
Height in pixels.

◆ get_projection_matrix()

const glm::mat4 & lmgl::ui::Canvas::get_projection_matrix ( ) const
inline

Get the orthographic projection matrix.

Maps pixels (0, 0) to (width, height) to normalized device coordinates (-1, -1) to (1, 1).

Returns
Orthographic projection matrix.

◆ get_width()

int lmgl::ui::Canvas::get_width ( ) const
inline

Get the canvas width.

Returns
Width in pixels.

◆ is_visible()

bool lmgl::ui::Canvas::is_visible ( ) const
inline

Check if the canvas is visible.

Returns
True if visible, false otherwise.

◆ remove_element()

void lmgl::ui::Canvas::remove_element ( std::shared_ptr< UIElement > element)

Remove a UI element from the canvas.

Parameters
elementShared pointer to the UI element.

◆ render()

void lmgl::ui::Canvas::render ( )

Render all UI elements.

Enables blending, sets up orthographic projection, and renders all UI elements in back-to-front order based on their render order.

◆ resize()

void lmgl::ui::Canvas::resize ( int width,
int height )

Resize the canvas to match window dimensions.

Updates the orthographic projection matrix to match the new dimensions.

Parameters
widthNew width in pixels.
heightNew height in pixels.

◆ set_visible()

void lmgl::ui::Canvas::set_visible ( bool visible)
inline

Set visibility of the entire canvas.

Parameters
visibleTrue to show, false to hide.

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