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

Toggle/Checkbox UI element with on/off state. More...

#include <toggle.hpp>

Inheritance diagram for lmgl::ui::Toggle:
lmgl::ui::UIElement

Public Member Functions

 Toggle (const std::string &label="Toggle", const std::string &name="Toggle")
 Constructor for Toggle.
 ~Toggle () override=default
 Destructor.
void set_checked (bool checked)
 Set the toggle state.
bool is_checked () const
 Get the toggle state.
void set_label (const std::string &label)
 Set the label text.
std::shared_ptr< Textget_text ()
 Get the internal text element (for setting font).
void set_on_toggle (std::function< void(bool)> callback)
 Set the callback for state changes.
void set_box_size (float size)
 Set checkbox size (default 20x20).
bool contains_point (float x, float y, float canvas_width, float canvas_height) const
 Check if point is inside toggle.
bool handle_click (float x, float y, float canvas_width, float canvas_height)
 Handle mouse click.
void render (float canvas_width, float canvas_height, const glm::mat4 &projection) override
 Render the toggle.
Public Member Functions inherited from lmgl::ui::UIElement
 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.

Additional Inherited Members

Protected Attributes inherited from lmgl::ui::UIElement
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

Toggle/Checkbox UI element with on/off state.

Constructor & Destructor Documentation

◆ Toggle()

lmgl::ui::Toggle::Toggle ( const std::string & label = "Toggle",
const std::string & name = "Toggle" )

Constructor for Toggle.

Parameters
labelText label for the toggle.
nameOptional name for the element.

Member Function Documentation

◆ contains_point()

bool lmgl::ui::Toggle::contains_point ( float x,
float y,
float canvas_width,
float canvas_height ) const

Check if point is inside toggle.

Parameters
xX coordinate.
yY coordinate.
canvas_widthCanvas width.
canvas_heightCanvas height.
Returns
True if inside.

◆ get_text()

std::shared_ptr< Text > lmgl::ui::Toggle::get_text ( )
inline

Get the internal text element (for setting font).

Returns
Pointer to text element.

◆ handle_click()

bool lmgl::ui::Toggle::handle_click ( float x,
float y,
float canvas_width,
float canvas_height )

Handle mouse click.

Parameters
xMouse X.
yMouse Y.
canvas_widthCanvas width.
canvas_heightCanvas height.
Returns
True if handled.

◆ is_checked()

bool lmgl::ui::Toggle::is_checked ( ) const
inline

Get the toggle state.

Returns
True if checked, false otherwise.

◆ render()

void lmgl::ui::Toggle::render ( float canvas_width,
float canvas_height,
const glm::mat4 & projection )
overridevirtual

Render the toggle.

Parameters
canvas_widthCanvas width.
canvas_heightCanvas height.
projectionProjection matrix.

Implements lmgl::ui::UIElement.

◆ set_box_size()

void lmgl::ui::Toggle::set_box_size ( float size)
inline

Set checkbox size (default 20x20).

Parameters
sizeSize of the checkbox.

◆ set_checked()

void lmgl::ui::Toggle::set_checked ( bool checked)

Set the toggle state.

Parameters
checkedTrue for on, false for off.

◆ set_label()

void lmgl::ui::Toggle::set_label ( const std::string & label)

Set the label text.

Parameters
labelNew label.

◆ set_on_toggle()

void lmgl::ui::Toggle::set_on_toggle ( std::function< void(bool)> callback)
inline

Set the callback for state changes.

Parameters
callbackFunction called with new state.

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