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

Horizontal slider UI element. More...

#include <slider.hpp>

Inheritance diagram for lmgl::ui::Slider:
lmgl::ui::UIElement

Public Member Functions

 Slider (float min=0.0f, float max=1.0f, float initial_value=0.5f, const std::string &name="Slider")
 Constructor for Slider.
 ~Slider () override=default
 Destructor.
void set_value (float value)
 Set the slider value.
float get_value () const
 Get the current value.
void set_range (float min, float max)
 Set the value range.
void set_label (const std::string &label)
 Set the label text.
void set_on_value_changed (std::function< void(float)> callback)
 Set callback for value changes.
void set_show_value (bool show)
 Set whether to show the value as text.
std::shared_ptr< Textget_label_text ()
 Get the label text element (for setting font).
std::shared_ptr< Textget_value_text ()
 Get the value text element (for setting font).
bool contains_point (float x, float y, float canvas_width, float canvas_height) const
 Check if point is inside slider.
bool handle_mouse_button (float x, float y, bool pressed, float canvas_width, float canvas_height)
 Handle mouse button event.
void handle_mouse_drag (float x, float y, float canvas_width, float canvas_height)
 Handle mouse drag.
void render (float canvas_width, float canvas_height, const glm::mat4 &projection) override
 Render the slider.
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

Horizontal slider UI element.

Constructor & Destructor Documentation

◆ Slider()

lmgl::ui::Slider::Slider ( float min = 0.0f,
float max = 1.0f,
float initial_value = 0.5f,
const std::string & name = "Slider" )

Constructor for Slider.

Parameters
minMinimum value.
maxMaximum value.
initial_valueInitial value.
nameOptional name.

Member Function Documentation

◆ contains_point()

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

Check if point is inside slider.

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

◆ get_label_text()

std::shared_ptr< Text > lmgl::ui::Slider::get_label_text ( )
inline

Get the label text element (for setting font).

Returns
Pointer to label text.

◆ get_value()

float lmgl::ui::Slider::get_value ( ) const
inline

Get the current value.

Returns
Current value.

◆ get_value_text()

std::shared_ptr< Text > lmgl::ui::Slider::get_value_text ( )
inline

Get the value text element (for setting font).

Returns
Pointer to value text.

◆ handle_mouse_button()

bool lmgl::ui::Slider::handle_mouse_button ( float x,
float y,
bool pressed,
float canvas_width,
float canvas_height )

Handle mouse button event.

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

◆ handle_mouse_drag()

void lmgl::ui::Slider::handle_mouse_drag ( float x,
float y,
float canvas_width,
float canvas_height )

Handle mouse drag.

Parameters
xMouse X.
yMouse Y.
canvas_widthCanvas width.
canvas_heightCanvas height.

◆ render()

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

Render the slider.

Parameters
canvas_widthCanvas width.
canvas_heightCanvas height.
projectionProjection matrix.

Implements lmgl::ui::UIElement.

◆ set_label()

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

Set the label text.

Parameters
labelLabel text.

◆ set_on_value_changed()

void lmgl::ui::Slider::set_on_value_changed ( std::function< void(float)> callback)
inline

Set callback for value changes.

Parameters
callbackFunction called with new value.

◆ set_range()

void lmgl::ui::Slider::set_range ( float min,
float max )

Set the value range.

Parameters
minMinimum value.
maxMaximum value.

◆ set_show_value()

void lmgl::ui::Slider::set_show_value ( bool show)
inline

Set whether to show the value as text.

Parameters
showTrue to show value.

◆ set_value()

void lmgl::ui::Slider::set_value ( float value)

Set the slider value.

Parameters
valueNew value (will be clamped to min/max).

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