lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
slider.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
12#include "lmgl/ui/panel.hpp"
13#include "lmgl/ui/text.hpp"
14
15#include <functional>
16#include <glm/glm.hpp>
17#include <memory>
18#include <string>
19
20namespace lmgl {
21
22namespace ui {
23
27class Slider : public UIElement {
28 public:
37 Slider(float min = 0.0f, float max = 1.0f, float initial_value = 0.5f, const std::string &name = "Slider");
38
42 ~Slider() override = default;
43
49 void set_value(float value);
50
56 inline float get_value() const { return m_value; }
57
64 void set_range(float min, float max);
65
71 void set_label(const std::string &label);
72
78 inline void set_on_value_changed(std::function<void(float)> callback) { m_on_value_changed = callback; }
79
85 inline void set_show_value(bool show) { m_show_value = show; }
86
92 inline std::shared_ptr<Text> get_label_text() { return m_label_text; }
93
99 inline std::shared_ptr<Text> get_value_text() { return m_value_text; }
100
110 bool contains_point(float x, float y, float canvas_width, float canvas_height) const;
111
122 bool handle_mouse_button(float x, float y, bool pressed, float canvas_width, float canvas_height);
123
132 void handle_mouse_drag(float x, float y, float canvas_width, float canvas_height);
133
141 void render(float canvas_width, float canvas_height, const glm::mat4 &projection) override;
142
143 private:
145 float m_value;
146
148 float m_min;
149 float m_max;
150
152 bool m_dragging{false};
153
155 std::string m_label;
156
158 bool m_show_value{true};
159
161 float m_track_height{4.0f};
162
164 float m_handle_size{16.0f};
165
167 std::shared_ptr<Panel> m_track_bg;
168
170 std::shared_ptr<Panel> m_track_fill;
171
173 std::shared_ptr<Panel> m_handle;
174
176 std::shared_ptr<Text> m_label_text;
177
179 std::shared_ptr<Text> m_value_text;
180
182 std::function<void(float)> m_on_value_changed;
183
185 glm::vec4 m_track_color{0.3f, 0.3f, 0.3f, 1.0f};
186 glm::vec4 m_fill_color{0.0f, 0.6f, 1.0f, 1.0f};
187 glm::vec4 m_handle_color{1.0f, 1.0f, 1.0f, 1.0f};
188 glm::vec4 m_text_color{1.0f, 1.0f, 1.0f, 1.0f};
189
197 void update_value_from_mouse(float mouse_x, float canvas_width, float canvas_height);
198};
199
200} // namespace ui
201
202} // namespace lmgl
void handle_mouse_drag(float x, float y, float canvas_width, float canvas_height)
Handle mouse drag.
Definition slider.cpp:82
std::shared_ptr< Text > get_label_text()
Get the label text element (for setting font).
Definition slider.hpp:92
void render(float canvas_width, float canvas_height, const glm::mat4 &projection) override
Render the slider.
Definition slider.cpp:97
std::shared_ptr< Text > get_value_text()
Get the value text element (for setting font).
Definition slider.hpp:99
float get_value() const
Get the current value.
Definition slider.hpp:56
bool handle_mouse_button(float x, float y, bool pressed, float canvas_width, float canvas_height)
Handle mouse button event.
Definition slider.cpp:68
~Slider() override=default
Destructor.
void set_value(float value)
Set the slider value.
Definition slider.cpp:36
void set_on_value_changed(std::function< void(float)> callback)
Set callback for value changes.
Definition slider.hpp:78
bool contains_point(float x, float y, float canvas_width, float canvas_height) const
Check if point is inside slider.
Definition slider.cpp:62
void set_range(float min, float max)
Set the value range.
Definition slider.cpp:51
void set_label(const std::string &label)
Set the label text.
Definition slider.cpp:57
Slider(float min=0.0f, float max=1.0f, float initial_value=0.5f, const std::string &name="Slider")
Constructor for Slider.
Definition slider.cpp:21
void set_show_value(bool show)
Set whether to show the value as text.
Definition slider.hpp:85
UIElement(const std::string &name="UIElement")
Constructor for UIElement.
Definition ui_element.cpp:10
Definition button.cpp:15
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12
Panel UI element for displaying colored rectangles.
Text UI element for rendering text strings.
Base class for all UI elements.