HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
box_constraints.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../geometry/geometry.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <cstdint>
11#include <limits>
12#include <concepts>
13
14hi_export_module(hikogui.layout.box_constraints);
15
16hi_export namespace hi { inline namespace v1 {
17
26 extent2 minimum = {};
27 extent2 preferred = {};
28 extent2 maximum = {};
30
32
33 constexpr box_constraints() noexcept = default;
34 constexpr box_constraints(box_constraints const&) noexcept = default;
35 constexpr box_constraints(box_constraints&&) noexcept = default;
36 constexpr box_constraints& operator=(box_constraints const&) noexcept = default;
37 constexpr box_constraints& operator=(box_constraints&&) noexcept = default;
38 [[nodiscard]] constexpr friend bool operator==(box_constraints const&, box_constraints const&) noexcept = default;
39
40 constexpr box_constraints(
41 extent2 minimum,
42 extent2 preferred,
43 extent2 maximum,
45 hi::margins margins = hi::margins{}) noexcept :
46 minimum(minimum), preferred(preferred), maximum(maximum), margins(margins), alignment(alignment)
47 {
48 hi_axiom(holds_invariant());
49 }
50
51 [[nodiscard]] constexpr box_constraints constrain(extent2 new_minimum, extent2 new_maximum) const noexcept
52 {
53 hi_assert(new_minimum <= new_maximum);
54
55 auto r = *this;
56
57 inplace_max(r.minimum, new_minimum);
58 inplace_min(r.maximum, new_maximum);
59
60 inplace_max(r.preferred, r.minimum);
61 inplace_max(r.maximum, r.preferred);
62 hi_axiom(r.holds_invariant());
63 return r;
64 }
65
66 constexpr box_constraints& operator+=(extent2 const& rhs) noexcept
67 {
68 minimum.width() += rhs.width();
69 preferred.width() += rhs.width();
70 maximum.width() += rhs.width();
71 minimum.height() += rhs.height();
72 preferred.height() += rhs.height();
73 maximum.height() += rhs.height();
74
75 hi_axiom(holds_invariant());
76 return *this;
77 }
78
79 [[nodiscard]] constexpr box_constraints operator+(extent2 const& rhs) const noexcept
80 {
81 auto r = *this;
82 r += rhs;
83 return r;
84 }
85
86 [[nodiscard]] constexpr bool holds_invariant() const noexcept
87 {
89 return false;
90 }
91 if (minimum > preferred or preferred > maximum) {
92 return false;
93 }
94 return true;
95 }
96
97 [[nodiscard]] friend constexpr box_constraints max(box_constraints const& lhs, extent2 const& rhs) noexcept
98 {
99 auto r = lhs;
100 inplace_max(r.minimum, rhs);
101 inplace_max(r.preferred, rhs);
102 inplace_max(r.maximum, rhs);
103
104 hi_axiom(r.holds_invariant());
105 return r;
106 }
107
115 [[nodiscard]] friend constexpr box_constraints max(box_constraints const& lhs, box_constraints const& rhs) noexcept
116 {
117 auto r = lhs;
118 inplace_max(r.minimum, rhs.minimum);
119 inplace_max(r.preferred, rhs.preferred);
120 inplace_max(r.maximum, rhs.maximum);
121 inplace_max(r.margins, rhs.margins);
122
123 hi_axiom(r.holds_invariant());
124 return r;
125 }
126
127 template<std::convertible_to<box_constraints>... Args>
128 [[nodiscard]] friend constexpr box_constraints
129 max(box_constraints const& first, box_constraints const& second, box_constraints const& third, Args const&...args) noexcept
130 {
131 return max(first, max(second, third, args...));
132 }
133};
134
135}} // namespace hi::v1
@ flush
Align the text naturally based on the writing direction of each paragraph.
@ justified
Stretch the text to be flush to both sides.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Horizontal/Vertical alignment combination.
Definition alignment.hpp:244
A high-level geometric extent.
Definition extent2.hpp:32
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:107
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:118
The left, bottom, right and top margins.
Definition margins.hpp:25
2D constraints.
Definition box_constraints.hpp:25
friend constexpr box_constraints max(box_constraints const &lhs, box_constraints const &rhs) noexcept
Makes a constraint that encompasses both given constraints.
Definition box_constraints.hpp:115