lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
button.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
27enum class ButtonState { Normal, Hovered, Pressed };
28
35class Button : public UIElement {
36 public:
43 Button(const std::string &label = "Button", const std::string &name = "Button");
44
48 ~Button() override = default;
49
55 void set_label(const std::string &label);
56
62 inline const std::string &get_label() const { return m_label; }
63
69 inline void set_on_click(std::function<void()> callback) { m_on_click = callback; }
70
78 void set_colors(const glm::vec4 &normal, const glm::vec4 &hovered, const glm::vec4 &pressed);
79
85 void set_text_color(const glm::vec4 &color);
86
92 inline std::shared_ptr<Text> get_text() { return m_text; }
93
103 bool contains_point(float x, float y, float canvas_width, float canvas_height) const;
104
115 bool handle_mouse_button(float x, float y, bool pressed, float canvas_width, float canvas_height);
116
125 void handle_mouse_move(float x, float y, float canvas_width, float canvas_height);
126
134 void render(float canvas_width, float canvas_height, const glm::mat4 &projection) override;
135
136 private:
138 std::string m_label;
139
141 std::shared_ptr<Panel> m_panel;
142
144 std::shared_ptr<Text> m_text;
145
147 std::function<void()> m_on_click;
148
150 ButtonState m_state{ButtonState::Normal};
151
153 glm::vec4 m_color_normal{0.3f, 0.3f, 0.3f, 1.0f};
154 glm::vec4 m_color_hovered{0.4f, 0.4f, 0.4f, 1.0f};
155 glm::vec4 m_color_pressed{0.2f, 0.2f, 0.2f, 1.0f};
156
158 glm::vec4 m_text_color{1.0f, 1.0f, 1.0f, 1.0f};
159};
160
161} // namespace ui
162
163} // namespace lmgl
void handle_mouse_move(float x, float y, float canvas_width, float canvas_height)
Handle mouse movement event.
Definition button.cpp:70
void set_text_color(const glm::vec4 &color)
Set text color.
Definition button.cpp:38
~Button() override=default
Destructor.
void render(float canvas_width, float canvas_height, const glm::mat4 &projection) override
Render the button.
Definition button.cpp:80
bool handle_mouse_button(float x, float y, bool pressed, float canvas_width, float canvas_height)
Handle mouse button event.
Definition button.cpp:50
Button(const std::string &label="Button", const std::string &name="Button")
Constructor for Button.
Definition button.cpp:17
void set_label(const std::string &label)
Set the button label text.
Definition button.cpp:25
const std::string & get_label() const
Get the button label text.
Definition button.hpp:62
std::shared_ptr< Text > get_text()
Get the internal text element (for setting font).
Definition button.hpp:92
void set_colors(const glm::vec4 &normal, const glm::vec4 &hovered, const glm::vec4 &pressed)
Set button colors for different states.
Definition button.cpp:32
bool contains_point(float x, float y, float canvas_width, float canvas_height) const
Check if point is inside button.
Definition button.cpp:45
void set_on_click(std::function< void()> callback)
Set the click callback.
Definition button.hpp:69
UIElement(const std::string &name="UIElement")
Constructor for UIElement.
Definition ui_element.cpp:10
Definition button.cpp:15
ButtonState
Button states for visual feedback.
Definition button.hpp:27
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.