sandbox
Loading...
Searching...
No Matches
element.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_UI_ELEMENT_HPP_
3#define LIBSBX_UI_ELEMENT_HPP_
4
5#include <vector>
6#include <memory>
7#include <algorithm>
8
9#include <libsbx/math/vector2.hpp>
10#include <libsbx/math/vector3.hpp>
11#include <libsbx/math/matrix4x4.hpp>
12#include <libsbx/math/color.hpp>
13
14#include <libsbx/graphics/images/image.hpp>
15
16#include <libsbx/sprites/sprites_module.hpp>
17
18#include <libsbx/ui/layout.hpp>
19
20namespace sbx::ui {
21
22class element {
23
24public:
25
26 math::vector2 anchor_min{0.0f, 0.0f};
27 math::vector2 anchor_max{0.0f, 0.0f};
28 math::vector2 offset_min{0.0f, 0.0f};
29 math::vector2 offset_max{0.0f, 0.0f};
30 math::vector2 pivot{0.5f, 0.5f};
31
32 math::color color{1.0f, 1.0f, 1.0f, 1.0f};
33 std::int32_t sort_order{0};
34 bool is_enabled{true};
35 bool is_interactive{false};
36
37 size_hints sizing{};
38
39 element() = default;
40
41 virtual ~element() = default;
42
43 auto add_child(std::unique_ptr<element> child) -> element&;
44
45 auto remove_child(element& child) -> void;
46
47 [[nodiscard]] auto children() const -> const std::vector<std::unique_ptr<element>>&;
48
49 [[nodiscard]] auto children() -> std::vector<std::unique_ptr<element>>&;
50
51 [[nodiscard]] auto computed_rectangle() const -> const rectangle&;
52
53 template<typename Type, typename... Args>
54 requires (std::is_base_of_v<ui::layout, Type>)
55 auto set_layout(Args&&... args) -> Type& {
56 auto layout = std::make_unique<Type>(std::forward<Args>(args)...);
57 auto& reference = *layout;
58
59 _layout = std::move(layout);
60
61 return reference;
62 }
63
64 auto clear_layout() -> void;
65
66 [[nodiscard]] auto has_layout() const -> bool;
67
68 auto resolve_layout(const rectangle& parent_rectangle) -> void;
69
70 auto resolve_as_arranged(const rectangle& arranged_rect) -> void;
71
72 auto submit_tree(const math::vector2& screen_size) -> void;
73
74 auto process_input_tree(const math::vector2& mouse_position, bool is_down, bool was_down) -> bool;
75
76protected:
77
78 virtual auto submit(const math::vector2& screen_size) -> void { }
79
80 virtual auto process_input(const math::vector2& mouse_position, bool is_down, bool was_down) -> bool { return false; }
81
82 auto submit_quad(const math::vector2& screen_size, const math::vector2& quad_position, const math::vector2& quad_size, const math::vector2& quad_pivot, const math::color& quad_color, std::uint32_t albedo_index, std::int32_t quad_sort_order, const math::vector2& uv_min, const math::vector2& uv_max, std::uint32_t flags, std::float_t sdf_px_range = 0.0f) -> void;
83
84private:
85
86 auto _resolve_self(const rectangle& parent_rectangle) -> void;
87
88 element* _parent{nullptr};
89 std::vector<std::unique_ptr<element>> _children;
90 rectangle _computed_rectangle{};
91 std::unique_ptr<ui::layout> _layout{};
92
93}; // class element
94
95} // namespace sbx::ui
96
97#endif // LIBSBX_UI_ELEMENT_HPP_
A vector in two-dimensional space.
Definition: vector2.hpp:28
RGBA color value type.
Definition: color.hpp:48
Definition: element.hpp:22
Definition: layout.hpp:39
RGBA color representation and utilities.
Definition: layout.hpp:15
Definition: layout.hpp:32