lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
toggle.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 <glm/glm.hpp>
16#include <functional>
17#include <memory>
18#include <string>
19
20namespace lmgl {
21
22namespace ui {
23
27class Toggle : public UIElement {
28 public:
35 Toggle(const std::string &label = "Toggle", const std::string &name = "Toggle");
36
40 ~Toggle() override = default;
41
47 void set_checked(bool checked);
48
54 inline bool is_checked() const { return m_checked; }
55
61 void set_label(const std::string &label);
62
68 inline std::shared_ptr<Text> get_text() { return m_text; }
69
75 inline void set_on_toggle(std::function<void(bool)> callback) { m_on_toggle = callback; }
76
82 inline void set_box_size(float size) { m_box_size = size; }
83
93 bool contains_point(float x, float y, float canvas_width, float canvas_height) const;
94
104 bool handle_click(float x, float y, float canvas_width, float canvas_height);
105
113 void render(float canvas_width, float canvas_height, const glm::mat4 &projection) override;
114
115 private:
117 std::string m_label;
118
120 bool m_checked{false};
121
123 float m_box_size{20.0f};
124
126 std::shared_ptr<Panel> m_box_bg;
127
129 std::shared_ptr<Panel> m_checkmark;
130
132 std::shared_ptr<Text> m_text;
133
135 std::function<void(bool)> m_on_toggle;
136
138 glm::vec4 m_bg_color{0.2f, 0.2f, 0.2f, 1.0f};
139 glm::vec4 m_check_color{0.0f, 1.0f, 0.0f, 1.0f};
140 glm::vec4 m_text_color{1.0f, 1.0f, 1.0f, 1.0f};
141};
142
143} // namespace ui
144
145} // namespace lmgl
~Toggle() override=default
Destructor.
bool is_checked() const
Get the toggle state.
Definition toggle.hpp:54
bool contains_point(float x, float y, float canvas_width, float canvas_height) const
Check if point is inside toggle.
Definition toggle.cpp:44
Toggle(const std::string &label="Toggle", const std::string &name="Toggle")
Constructor for Toggle.
Definition toggle.cpp:17
void set_box_size(float size)
Set checkbox size (default 20x20).
Definition toggle.hpp:82
void set_label(const std::string &label)
Set the label text.
Definition toggle.cpp:37
void set_checked(bool checked)
Set the toggle state.
Definition toggle.cpp:27
std::shared_ptr< Text > get_text()
Get the internal text element (for setting font).
Definition toggle.hpp:68
bool handle_click(float x, float y, float canvas_width, float canvas_height)
Handle mouse click.
Definition toggle.cpp:49
void set_on_toggle(std::function< void(bool)> callback)
Set the callback for state changes.
Definition toggle.hpp:75
void render(float canvas_width, float canvas_height, const glm::mat4 &projection) override
Render the toggle.
Definition toggle.cpp:61
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.