lmgl 1.0.0
A lightweight OpenGL graphics engine library written in C++
Loading...
Searching...
No Matches
ui_element.hpp
Go to the documentation of this file.
1
12
13#pragma once
14
15#include <glm/glm.hpp>
16
17#include <memory>
18#include <string>
19#include <vector>
20
21namespace lmgl {
22
30namespace ui {
31
48enum class Anchor {
49 TopLeft,
50 TopCenter,
51 TopRight,
52 CenterLeft,
53 Center,
54 CenterRight,
55 BottomLeft,
56 BottomCenter,
57 BottomRight,
58 Stretch
59};
60
67class UIElement : public std::enable_shared_from_this<UIElement> {
68 public:
74 UIElement(const std::string &name = "UIElement");
75
79 virtual ~UIElement() = default;
80
86 inline void set_position(const glm::vec2 &position) { m_position = position; }
87
93 inline void set_size(const glm::vec2 &size) { m_size = size; }
94
100 inline void set_anchor(Anchor anchor) { m_anchor = anchor; }
101
109 inline void set_render_order(int order) { m_render_order = order; }
110
116 inline void set_visible(bool visible) { m_visible = visible; }
117
123 inline void set_name(const std::string &name) { m_name = name; }
124
130 inline const glm::vec2 &get_position() const { return m_position; }
131
137 inline const glm::vec2 &get_size() const { return m_size; }
138
144 inline Anchor get_anchor() const { return m_anchor; }
145
151 inline int get_render_order() const { return m_render_order; }
152
158 inline bool is_visible() const { return m_visible; }
159
165 inline const std::string &get_name() const { return m_name; }
166
172 inline std::shared_ptr<UIElement> get_parent() const { return m_parent.lock(); }
173
179 void add_child(const std::shared_ptr<UIElement> child);
180
186 void remove_child(const std::shared_ptr<UIElement> child);
187
193 std::vector<std::shared_ptr<UIElement>> get_children() const { return m_children; }
194
202 glm::vec2 get_absolute_position(float canvas_width, float canvas_height) const;
203
211 virtual void render(float canvas_width, float canvas_height, const glm::mat4 &projection) = 0;
212
213 protected:
215 std::string m_name;
216
218 glm::vec2 m_position{0.0f, 0.0f};
219
221 glm::vec2 m_size{100.0f, 100.0f};
222
224 Anchor m_anchor{Anchor::TopLeft};
225
228
230 bool m_visible{true};
231
233 std::weak_ptr<UIElement> m_parent;
235 std::vector<std::shared_ptr<UIElement>> m_children;
236
237 private:
245 glm::vec2 get_anchor_offset(float canvas_width, float canvas_height) const;
246};
247
248} // namespace ui
249
250} // namespace lmgl
std::vector< std::shared_ptr< UIElement > > m_children
Child UI elements.
Definition ui_element.hpp:235
Anchor m_anchor
Anchor preset.
Definition ui_element.hpp:224
std::weak_ptr< UIElement > m_parent
Parent UI element.
Definition ui_element.hpp:233
void set_name(const std::string &name)
Set the name of the UI element.
Definition ui_element.hpp:123
glm::vec2 get_absolute_position(float canvas_width, float canvas_height) const
Compute the absolute screen position based on anchor and parent.
Definition ui_element.cpp:29
virtual void render(float canvas_width, float canvas_height, const glm::mat4 &projection)=0
Virtual render method to be overridden by derived classes.
void set_position(const glm::vec2 &position)
Set the position of the UI element.
Definition ui_element.hpp:86
void set_anchor(Anchor anchor)
Set the anchor of the UI element.
Definition ui_element.hpp:100
void set_size(const glm::vec2 &size)
Set the size of the UI element.
Definition ui_element.hpp:93
Anchor get_anchor() const
Get the anchor of the UI element.
Definition ui_element.hpp:144
std::shared_ptr< UIElement > get_parent() const
Get the parent UI element.
Definition ui_element.hpp:172
virtual ~UIElement()=default
Virtual destructor.
int m_render_order
Render order (z-index).
Definition ui_element.hpp:227
UIElement(const std::string &name="UIElement")
Constructor for UIElement.
Definition ui_element.cpp:10
void set_visible(bool visible)
Set visibility of the UI element.
Definition ui_element.hpp:116
const glm::vec2 & get_size() const
Get the size of the UI element.
Definition ui_element.hpp:137
glm::vec2 m_position
Position in pixels (relative to anchor).
Definition ui_element.hpp:218
std::vector< std::shared_ptr< UIElement > > get_children() const
Get all child UI elements.
Definition ui_element.hpp:193
void add_child(const std::shared_ptr< UIElement > child)
Add a child UI element.
Definition ui_element.cpp:12
const glm::vec2 & get_position() const
Get the position of the UI element.
Definition ui_element.hpp:130
std::string m_name
Name of the UI element.
Definition ui_element.hpp:215
void set_render_order(int order)
Set the render order (z-index).
Definition ui_element.hpp:109
bool m_visible
Visibility flag.
Definition ui_element.hpp:230
bool is_visible() const
Check if the UI element is visible.
Definition ui_element.hpp:158
int get_render_order() const
Get the render order.
Definition ui_element.hpp:151
const std::string & get_name() const
Get the name of the UI element.
Definition ui_element.hpp:165
glm::vec2 m_size
Size in pixels.
Definition ui_element.hpp:221
void remove_child(const std::shared_ptr< UIElement > child)
Remove a child UI element.
Definition ui_element.cpp:19
Definition button.cpp:15
Anchor
Enumeration for UI element anchoring.
Definition ui_element.hpp:48
Forward declarations for Assimp Material structure.
Definition model_loader.cpp:12