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

Base class for all UI elements. More...

#include <ui_element.hpp>

Inheritance diagram for lmgl::ui::UIElement:
lmgl::ui::Button lmgl::ui::Image lmgl::ui::Panel lmgl::ui::Slider lmgl::ui::Text lmgl::ui::Toggle

Public Member Functions

 UIElement (const std::string &name="UIElement")
 Constructor for UIElement.
virtual ~UIElement ()=default
 Virtual destructor.
void set_position (const glm::vec2 &position)
 Set the position of the UI element.
void set_size (const glm::vec2 &size)
 Set the size of the UI element.
void set_anchor (Anchor anchor)
 Set the anchor of the UI element.
void set_render_order (int order)
 Set the render order (z-index).
void set_visible (bool visible)
 Set visibility of the UI element.
void set_name (const std::string &name)
 Set the name of the UI element.
const glm::vec2 & get_position () const
 Get the position of the UI element.
const glm::vec2 & get_size () const
 Get the size of the UI element.
Anchor get_anchor () const
 Get the anchor of the UI element.
int get_render_order () const
 Get the render order.
bool is_visible () const
 Check if the UI element is visible.
const std::string & get_name () const
 Get the name of the UI element.
std::shared_ptr< UIElementget_parent () const
 Get the parent UI element.
void add_child (const std::shared_ptr< UIElement > child)
 Add a child UI element.
void remove_child (const std::shared_ptr< UIElement > child)
 Remove a child UI element.
std::vector< std::shared_ptr< UIElement > > get_children () const
 Get all child UI elements.
glm::vec2 get_absolute_position (float canvas_width, float canvas_height) const
 Compute the absolute screen position based on anchor and parent.
virtual void render (float canvas_width, float canvas_height, const glm::mat4 &projection)=0
 Virtual render method to be overridden by derived classes.

Protected Attributes

std::string m_name
 Name of the UI element.
glm::vec2 m_position {0.0f, 0.0f}
 Position in pixels (relative to anchor).
glm::vec2 m_size {100.0f, 100.0f}
 Size in pixels.
Anchor m_anchor {Anchor::TopLeft}
 Anchor preset.
int m_render_order {0}
 Render order (z-index).
bool m_visible {true}
 Visibility flag.
std::weak_ptr< UIElementm_parent
 Parent UI element.
std::vector< std::shared_ptr< UIElement > > m_children
 Child UI elements.

Detailed Description

Base class for all UI elements.

The UIElement class provides common functionality for UI components, including position, size, anchoring, visibility, and hierarchical parent-child relationships.

Constructor & Destructor Documentation

◆ UIElement()

lmgl::ui::UIElement::UIElement ( const std::string & name = "UIElement")

Constructor for UIElement.

Parameters
nameOptional name for the UI element.

Member Function Documentation

◆ add_child()

void lmgl::ui::UIElement::add_child ( const std::shared_ptr< UIElement > child)

Add a child UI element.

Parameters
childShared pointer to the child element.

◆ get_absolute_position()

glm::vec2 lmgl::ui::UIElement::get_absolute_position ( float canvas_width,
float canvas_height ) const

Compute the absolute screen position based on anchor and parent.

Parameters
canvas_widthWidth of the canvas in pixels.
canvas_heightHeight of the canvas in pixels.
Returns
Absolute position in pixels.

◆ get_anchor()

Anchor lmgl::ui::UIElement::get_anchor ( ) const
inline

Get the anchor of the UI element.

Returns
Anchor preset.

◆ get_children()

std::vector< std::shared_ptr< UIElement > > lmgl::ui::UIElement::get_children ( ) const
inline

Get all child UI elements.

Returns
Vector of shared pointers to child elements.

◆ get_name()

const std::string & lmgl::ui::UIElement::get_name ( ) const
inline

Get the name of the UI element.

Returns
Name of the element.

◆ get_parent()

std::shared_ptr< UIElement > lmgl::ui::UIElement::get_parent ( ) const
inline

Get the parent UI element.

Returns
Shared pointer to the parent element, or nullptr if none.

◆ get_position()

const glm::vec2 & lmgl::ui::UIElement::get_position ( ) const
inline

Get the position of the UI element.

Returns
Position in pixels.

◆ get_render_order()

int lmgl::ui::UIElement::get_render_order ( ) const
inline

Get the render order.

Returns
Render order value.

◆ get_size()

const glm::vec2 & lmgl::ui::UIElement::get_size ( ) const
inline

Get the size of the UI element.

Returns
Size in pixels.

◆ is_visible()

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

Check if the UI element is visible.

Returns
True if visible, false otherwise.

◆ remove_child()

void lmgl::ui::UIElement::remove_child ( const std::shared_ptr< UIElement > child)

Remove a child UI element.

Parameters
childShared pointer to the child element.

◆ render()

virtual void lmgl::ui::UIElement::render ( float canvas_width,
float canvas_height,
const glm::mat4 & projection )
pure virtual

Virtual render method to be overridden by derived classes.

Parameters
canvas_widthWidth of the canvas in pixels.
canvas_heightHeight of the canvas in pixels.
projectionProjection matrix for UI rendering.

Implemented in lmgl::ui::Button, lmgl::ui::Image, lmgl::ui::Panel, lmgl::ui::Slider, lmgl::ui::Text, and lmgl::ui::Toggle.

◆ set_anchor()

void lmgl::ui::UIElement::set_anchor ( Anchor anchor)
inline

Set the anchor of the UI element.

Parameters
anchorAnchor preset.

◆ set_name()

void lmgl::ui::UIElement::set_name ( const std::string & name)
inline

Set the name of the UI element.

Parameters
nameNew name for the element.

◆ set_position()

void lmgl::ui::UIElement::set_position ( const glm::vec2 & position)
inline

Set the position of the UI element.

Parameters
positionPosition in pixels (x, y).

◆ set_render_order()

void lmgl::ui::UIElement::set_render_order ( int order)
inline

Set the render order (z-index).

Higher values render on top of lower values.

Parameters
orderRender order value.

◆ set_size()

void lmgl::ui::UIElement::set_size ( const glm::vec2 & size)
inline

Set the size of the UI element.

Parameters
sizeSize in pixels (width, height).

◆ set_visible()

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

Set visibility of the UI element.

Parameters
visibleTrue to show, false to hide.

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