Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_layout.hpp
1#pragma once
2
3#include "structure/design_pattern/spk_cached_data.hpp"
4#include "structure/graphics/spk_extend_2d.hpp"
5#include "structure/math/spk_vector2.hpp"
6#include "structure/widget/spk_resizable_element.hpp"
7
8#include <any>
9#include <limits>
10#include <vector>
11
12namespace spk
13{
23 class Layout : public ResizableElement
24 {
25 public:
31 {
36 enum Value
37 {
38 Desired, // Take the "Desired" size of the element, forcing it.
39 Extend, // Take as much space as possible, while lower or equal to the maximal size hint.
40 Standard, // Take as much space as possible, but after the extended widget have taken position.
41 Minimum, // Take up the minimal space requested by the element, with no resize.
42 };
43
52 };
53
54 private:
55 spk::Vector2UInt _padding{};
56
57 protected:
62 struct Element
63 {
67 ResizableElement *element;
75 std::any extraData;
76 };
77
81 using ElementContainer = std::vector<Element>;
82
87
92 const ElementContainer &_elements() const;
93
94 public:
95 Layout() = default;
96
101 void setPadding(const spk::Vector2UInt &p_padding);
106 const spk::Vector2UInt &padding() const;
107
111 void clear();
112
120 template <typename TExtra>
121 void addElement(ResizableElement *p_element, const SizePolicy &p_sizePolicy, TExtra &&p_extra)
122 {
123 if (p_element == nullptr)
124 {
125 throw std::runtime_error("Can't add a nullptr to a layout");
126 }
127 _elementContainer.push_back(Element{p_element, p_sizePolicy, std::forward<TExtra>(p_extra)});
128 sizeHint().release();
129 }
130
136 void addElement(ResizableElement *p_element, const SizePolicy &p_sizePolicy);
137 };
138
139 std::string toString(const Layout::SizePolicy::Value &p_value);
140 std::wstring toWstring(const Layout::SizePolicy::Value &p_value);
141 std::string toString(const Layout::SizePolicy &p_sizePolicy);
142 std::wstring toWstring(const Layout::SizePolicy &p_sizePolicy);
143
144 std::ostream &operator<<(std::ostream &p_stream, const Layout::SizePolicy::Value &p_value);
145 std::wostream &operator<<(std::wostream &p_stream, const Layout::SizePolicy::Value &p_value);
146 std::ostream &operator<<(std::ostream &p_stream, const Layout::SizePolicy &p_sizePolicy);
147 std::wostream &operator<<(std::wostream &p_stream, const Layout::SizePolicy &p_sizePolicy);
148}
void addElement(ResizableElement *p_element, const SizePolicy &p_sizePolicy, TExtra &&p_extra)
Adds an element with extra layout data.
Definition spk_layout.hpp:121
const ElementContainer & _elements() const
Returns the stored elements.
Definition spk_layout.cpp:33
ElementContainer _elementContainer
Stored layout elements.
Definition spk_layout.hpp:86
void setPadding(const spk::Vector2UInt &p_padding)
Sets layout padding between elements.
Definition spk_layout.cpp:5
const spk::Vector2UInt & padding() const
Returns layout padding.
Definition spk_layout.cpp:11
std::vector< Element > ElementContainer
Container type used to store elements.
Definition spk_layout.hpp:81
void clear()
Removes all elements from the layout.
Definition spk_layout.cpp:16
void release()
Releases all cached size hint values.
Definition spk_resizable_element.cpp:36
Base interface for elements that can be sized by layouts.
Definition spk_resizable_element.hpp:17
SizeHint & sizeHint()
Returns mutable size hint data.
Definition spk_resizable_element.cpp:86
Layout element entry with sizing policy and extra data.
Definition spk_layout.hpp:63
SizePolicy sizePolicy
Size policy for the element.
Definition spk_layout.hpp:71
ResizableElement * element
Pointer to the resizable element.
Definition spk_layout.hpp:67
std::any extraData
Extra data used by derived layouts.
Definition spk_layout.hpp:75
Defines sizing policies for layout elements.
Definition spk_layout.hpp:31
Value
Policy value controlling sizing behavior.
Definition spk_layout.hpp:37
Value horizontal
Horizontal size policy.
Definition spk_layout.hpp:47
Value vertical
Vertical size policy.
Definition spk_layout.hpp:51