7#include "box_constraints.hpp"
8#include "box_shape.hpp"
10#include "../geometry/module.hpp"
11#include "../utility/module.hpp"
19namespace hi {
inline namespace v1 {
26 size_t first_column = 0;
28 size_t last_column = 0;
30 bool beyond_maximum =
false;
31 value_type value = {};
46 std::convertible_to<value_type>
auto&& value) noexcept :
47 first_column(first_column),
49 last_column(last_column),
51 beyond_maximum(beyond_maximum),
58 constexpr void set_constraints(
box_constraints const& constraints)
noexcept
60 _constraints = constraints;
63 template<hi::axis Axis>
64 [[nodiscard]]
constexpr size_t first()
const noexcept
66 if constexpr (Axis == axis::x) {
68 }
else if constexpr (Axis == axis::y) {
75 template<hi::axis Axis>
76 [[nodiscard]]
constexpr size_t last()
const noexcept
78 if constexpr (Axis == axis::x) {
80 }
else if constexpr (Axis == axis::y) {
87 template<hi::axis Axis>
88 [[nodiscard]]
constexpr size_t span()
const noexcept
90 hi_axiom(first<Axis>() < last<Axis>());
91 return last<Axis>() - first<Axis>();
94 template<hi::axis Axis>
95 [[nodiscard]]
constexpr auto alignment()
const noexcept
97 if constexpr (Axis == axis::x) {
98 return _constraints.alignment.horizontal();
99 }
else if constexpr (Axis == axis::y) {
100 return _constraints.alignment.vertical();
106 template<hi::axis Axis>
107 [[nodiscard]]
constexpr float minimum()
const noexcept
109 if constexpr (Axis == axis::x) {
110 return _constraints.minimum.
width();
111 }
else if constexpr (Axis == axis::y) {
112 return _constraints.minimum.
height();
118 template<hi::axis Axis>
119 [[nodiscard]]
constexpr float preferred()
const noexcept
121 if constexpr (Axis == axis::x) {
122 return _constraints.preferred.
width();
123 }
else if constexpr (Axis == axis::y) {
124 return _constraints.preferred.
height();
130 template<hi::axis Axis>
131 [[nodiscard]]
constexpr float maximum()
const noexcept
133 if constexpr (Axis == axis::x) {
134 return _constraints.maximum.
width();
135 }
else if constexpr (Axis == axis::y) {
136 return _constraints.maximum.
height();
142 template<hi::axis Axis>
143 [[nodiscard]]
constexpr float margin_before(
bool forward)
const noexcept
145 if constexpr (Axis == axis::x) {
147 return _constraints.margins.left();
149 return _constraints.margins.right();
151 }
else if constexpr (Axis == axis::y) {
153 return _constraints.margins.bottom();
155 return _constraints.margins.top();
162 template<hi::axis Axis>
163 [[nodiscard]]
constexpr float margin_after(
bool forward)
const noexcept
165 if constexpr (Axis == axis::x) {
167 return _constraints.margins.right();
169 return _constraints.margins.left();
171 }
else if constexpr (Axis == axis::y) {
173 return _constraints.margins.top();
175 return _constraints.margins.bottom();
182 template<hi::axis Axis>
183 [[nodiscard]]
constexpr float padding_before(
bool forward)
const noexcept
185 if constexpr (Axis == axis::x) {
187 return _constraints.padding.left();
189 return _constraints.padding.right();
191 }
else if constexpr (Axis == axis::y) {
193 return _constraints.padding.bottom();
195 return _constraints.padding.top();
202 template<hi::axis Axis>
203 [[nodiscard]]
constexpr float padding_after(
bool forward)
const noexcept
205 if constexpr (Axis == axis::x) {
207 return _constraints.padding.right();
209 return _constraints.padding.left();
211 }
else if constexpr (Axis == axis::y) {
213 return _constraints.padding.top();
215 return _constraints.padding.bottom();
226template<hi::axis Axis,
typename T>
231 using value_type = T;
232 using alignment_type = std::conditional_t<axis == axis::y, vertical_alignment, horizontal_alignment>;
292 using iterator = constraint_vector::iterator;
293 using const_iterator = constraint_vector::const_iterator;
294 using reverse_iterator = constraint_vector::reverse_iterator;
295 using reference = constraint_vector::reference;
296 using const_reference = constraint_vector::const_reference;
304 [[nodiscard]] constexpr friend
bool
315 _constraints(num), _forward(forward)
317 for (
hilet& cell : cells) {
318 construct_simple_cell(cell);
322 for (
hilet& cell : cells) {
323 construct_span_cell(cell);
328 [[nodiscard]]
constexpr float margin_before() const noexcept
330 return empty() ? 0 : _forward ?
front().margin_before :
back().margin_before;
333 [[nodiscard]]
constexpr float margin_after() const noexcept
335 return empty() ? 0 : _forward ?
back().margin_after :
front().margin_after;
338 [[nodiscard]]
constexpr float padding_before() const noexcept
340 return empty() ? 0 : _forward ?
front().padding_before :
back().padding_before;
343 [[nodiscard]]
constexpr float padding_after() const noexcept
345 return empty() ? 0 : _forward ?
back().padding_after :
front().padding_after;
365 [[nodiscard]]
constexpr float position(cell_type
const& cell)
const noexcept
367 return position(cell.first<
axis>(), cell.last<
axis>());
370 [[nodiscard]]
constexpr float extent(cell_type
const& cell)
const noexcept
372 return extent(cell.first<
axis>(), cell.last<
axis>());
375 [[nodiscard]]
constexpr std::optional<float> guideline(cell_type
const& cell)
const noexcept
377 if (cell.span<
axis>() == 1) {
378 return guideline(cell.first<
axis>());
406 constexpr void layout(
float new_position,
float new_extent, std::optional<float> external_guideline,
float guideline_width)
noexcept
409 for (
auto& constraint : _constraints) {
410 constraint.extent = constraint.preferred;
414 auto [total_extent, count] = layout_shrink(
begin(),
end());
415 while (total_extent > new_extent and count != 0) {
417 std::tie(total_extent, count) = layout_shrink(
begin(),
end(), total_extent - new_extent, count);
422 while (total_extent < new_extent and count != 0) {
424 std::tie(total_extent, count) = layout_expand(
begin(),
end(), new_extent - total_extent, count);
428 if (total_extent < new_extent) {
431 return item.beyond_maximum;
434 auto expand = new_extent - total_extent;
437 for (
auto& constraint : _constraints) {
439 if (constraint.beyond_maximum) {
440 constraint.extent += expand_this;
441 expand -= expand_this;
449 if (total_extent < new_extent and not
empty()) {
451 front().extent += new_extent - total_extent;
455 layout_position(
begin(),
end(), new_position, guideline_width);
457 layout_position(
rbegin(),
rend(), new_position, guideline_width);
460 if (external_guideline and
size() == 1) {
463 front().guideline = *external_guideline;
469 [[nodiscard]]
constexpr size_t size() const noexcept
471 return _constraints.
size();
476 [[nodiscard]]
constexpr bool empty() const noexcept
478 return _constraints.
empty();
483 [[nodiscard]]
constexpr iterator
begin() noexcept
485 return _constraints.
begin();
490 [[nodiscard]]
constexpr const_iterator
begin() const noexcept
492 return _constraints.
begin();
497 [[nodiscard]]
constexpr const_iterator
cbegin() const noexcept
499 return _constraints.
cbegin();
504 [[nodiscard]]
constexpr iterator
end() noexcept
506 return _constraints.
end();
511 [[nodiscard]]
constexpr const_iterator
end() const noexcept
513 return _constraints.
end();
518 [[nodiscard]]
constexpr const_iterator
cend() const noexcept
520 return _constraints.
cend();
525 [[nodiscard]]
constexpr reverse_iterator
rbegin() noexcept
527 return _constraints.
rbegin();
532 [[nodiscard]]
constexpr reverse_iterator
rend() noexcept
534 return _constraints.
rend();
543 [[nodiscard]]
constexpr reference
operator[](
size_t index)
noexcept
546 return _constraints[index];
555 [[nodiscard]]
constexpr const_reference
operator[](
size_t index)
const noexcept
558 return _constraints[index];
566 [[nodiscard]]
constexpr reference
front() noexcept
569 return _constraints.
front();
577 [[nodiscard]]
constexpr const_reference
front() const noexcept
580 return _constraints.
front();
588 [[nodiscard]]
constexpr reference
back() noexcept
591 return _constraints.
back();
599 [[nodiscard]]
constexpr const_reference
back() const noexcept
602 return _constraints.
back();
614 constraint_vector _constraints = {};
618 bool _forward =
true;
637 layout_shrink(const_iterator first, const_iterator last,
float shrink = 0.0f,
size_t count = 1) noexcept
646 auto new_extent = 0.0f;
647 auto new_count = 0_uz;
648 for (
auto it = first_; it != last_; ++it) {
649 hilet shrink_this =
std::max({shrink_per, shrink, it->extent - it->minimum});
650 it->extent -= shrink_this;
651 shrink -= shrink_this;
654 new_extent += it->margin_before;
656 new_extent += it->extent;
658 if (it->extent > it->minimum) {
663 return {new_extent, new_count};
683 layout_expand(const_iterator first, const_iterator last,
float expand = 0.0f,
size_t count = 1) noexcept
693 auto new_extent = 0.0f;
694 auto new_count = 0_uz;
695 for (
auto it = first_; it != last_; ++it) {
696 hilet expand_this =
std::min({expand_per, expand, it->maximum - it->extent});
697 it->extent += expand_this;
698 expand -= expand_this;
701 new_extent += it->margin_before;
703 new_extent += it->extent;
705 if (it->extent < it->maximum) {
710 return {new_extent, new_count};
713 constexpr void layout_position(
auto first,
auto last,
float start_position,
float guideline_width)
noexcept
715 auto position = start_position;
716 for (
auto it = first; it != last; ++it) {
717 it->position = position;
719 it->alignment, position, position + it->extent, it->padding_before, it->padding_after, guideline_width);
721 position += it->extent;
722 position += it->margin_after;
733 constexpr void construct_simple_cell(cell_type
const& cell)
noexcept
735 inplace_max(_constraints[cell.first<axis>()].margin_before, cell.margin_before<axis>(_forward));
736 inplace_max(_constraints[cell.last<axis>() - 1].margin_after, cell.margin_after<axis>(_forward));
737 inplace_max(_constraints[cell.first<axis>()].padding_before, cell.padding_before<axis>(_forward));
738 inplace_max(_constraints[cell.last<axis>() - 1].padding_after, cell.padding_after<axis>(_forward));
740 for (
auto i = cell.first<axis>(); i != cell.last<
axis>(); ++i) {
741 _constraints[i].beyond_maximum |= cell.beyond_maximum;
744 if (cell.span<axis>() == 1) {
745 inplace_max(_constraints[cell.first<axis>()].alignment, cell.alignment<axis>());
746 inplace_max(_constraints[cell.first<axis>()].minimum, cell.minimum<axis>());
747 inplace_max(_constraints[cell.first<axis>()].preferred, cell.preferred<axis>());
748 inplace_min(_constraints[cell.first<axis>()].maximum, cell.maximum<axis>());
758 constexpr void construct_span_cell(cell_type
const& cell)
noexcept
760 auto num_cells = narrow_cast<float>(cell.span<axis>());
762 if (cell.span<axis>() > 1) {
764 if (
hilet extra = cell.minimum<axis>() - span_minimum; extra > 0) {
766 for (
auto i = cell.first<axis>(); i != cell.last<
axis>(); ++i) {
767 _constraints[i].minimum += extra_per_cell;
771 if (
hilet extra = cell.preferred<axis>() - span_preferred; extra > 0) {
773 for (
auto i = cell.first<axis>(); i != cell.last<
axis>(); ++i) {
774 _constraints[i].preferred += extra_per_cell;
778 if (
hilet extra = cell.maximum<axis>() - span_preferred; extra < 0) {
780 for (
auto i = cell.first<axis>(); i != cell.last<
axis>(); ++i) {
782 _constraints[i].maximum += extra_per_cell;
792 constexpr void construct_fixup() noexcept
794 for (
auto it =
begin(); it !=
end(); ++it) {
796 if (it + 1 !=
end()) {
797 it->margin_after = (it + 1)->margin_before =
std::max(it->margin_after, (it + 1)->margin_before);
801 inplace_max(it->preferred, it->minimum);
802 inplace_max(it->maximum, it->preferred);
805 if (it->padding_before + it->padding_after > it->minimum) {
806 hilet padding_diff = it->padding_after - it->padding_before;
807 hilet middle = std::clamp(it->minimum / 2.0f + padding_diff, 0.0f, it->minimum);
808 it->padding_after =
middle;
809 it->padding_before = it->minimum -
middle;
824 auto r_minimum = 0.0f;
825 auto r_preferred = 0.0f;
826 auto r_maximum = 0.0f;
827 auto r_margin = 0.0f;
830 r_minimum = first->minimum;
831 r_preferred = first->preferred;
832 r_maximum = first->maximum;
833 for (
auto it = first + 1; it != last; ++it) {
834 r_margin += it->margin_before;
835 r_minimum += it->minimum;
836 r_preferred += it->preferred;
837 r_maximum += it->maximum;
840 return {r_minimum + r_margin, r_preferred + r_margin, r_maximum + r_margin};
865 [[nodiscard]]
constexpr float position(const_iterator first, const_iterator last)
const noexcept
869 return first->position;
871 return (last - 1)->position;
882 [[nodiscard]]
constexpr float position(
size_t first,
size_t last)
const noexcept
896 [[nodiscard]]
constexpr float extent(const_iterator first, const_iterator last)
const noexcept
901 for (
auto it = first + 1; it != last; ++it) {
902 r += it->margin_before;
916 [[nodiscard]]
constexpr float extent(
size_t first,
size_t last)
const noexcept
923 [[nodiscard]]
constexpr std::optional<float> guideline(const_iterator it)
const noexcept
925 return it->guideline;
928 [[nodiscard]]
constexpr std::optional<float> guideline(
size_t i)
const noexcept
930 return guideline(
cbegin() + i);
944 using value_type = T;
946 using cell_type = detail::grid_layout_cell<value_type>;
948 using iterator = cell_vector::iterator;
949 using const_iterator = cell_vector::const_iterator;
950 using reference = cell_vector::reference;
951 using const_reference = cell_vector::const_reference;
959 [[nodiscard]]
constexpr friend bool operator==(
grid_layout const&,
grid_layout const&)
noexcept =
default;
961 [[nodiscard]]
constexpr bool empty()
const noexcept
963 return _cells.
empty();
966 [[nodiscard]]
constexpr size_t size()
const noexcept
968 return _cells.
size();
971 [[nodiscard]]
constexpr size_t num_columns()
const noexcept
976 [[nodiscard]]
constexpr size_t num_rows()
const noexcept
981 [[nodiscard]]
constexpr iterator begin()
noexcept
983 return _cells.
begin();
986 [[nodiscard]]
constexpr iterator end()
noexcept
991 [[nodiscard]]
constexpr const_iterator begin()
const noexcept
993 return _cells.
begin();
996 [[nodiscard]]
constexpr const_iterator end()
const noexcept
1001 [[nodiscard]]
constexpr const_iterator cbegin()
const noexcept
1006 [[nodiscard]]
constexpr const_iterator cend()
const noexcept
1008 return _cells.
cend();
1011 [[nodiscard]]
constexpr const_reference operator[](
size_t i)
const noexcept
1016 [[nodiscard]]
constexpr reference operator[](
size_t i)
noexcept
1029 [[nodiscard]]
constexpr bool cell_in_use(
size_t first_column,
size_t first_row,
size_t last_column,
size_t last_row)
noexcept
1032 hi_axiom(first_column < last_column);
1035 for (
hilet& cell : _cells) {
1036 if (first_column >= cell.last_column) {
1039 if (last_column <= cell.first_column) {
1042 if (first_row >= cell.last_row) {
1045 if (last_row <= cell.first_row) {
1063 template<forward_of<value_type> Value>
1065 size_t first_column,
1070 bool beyond_maximum =
false) noexcept
1076 auto& r = _cells.
emplace_back(first_column, first_row, last_column, last_row, beyond_maximum, std::forward<Value>(value));
1077 update_after_insert_or_delete();
1089 template<forward_of<value_type> Value>
1090 constexpr reference
add_cell(
size_t column,
size_t row, Value&& value,
bool beyond_maximum =
false) noexcept
1092 return add_cell(column, row, column + 1, row + 1, std::forward<Value>(value), beyond_maximum);
1095 constexpr void clear() noexcept
1098 update_after_insert_or_delete();
1101 [[nodiscard]]
constexpr box_constraints constraints(
bool left_to_right)
const noexcept
1104 _row_constraints = {_cells, num_rows(),
false};
1105 _column_constraints = {_cells, num_columns(), left_to_right};
1107 auto r = box_constraints{};
1108 std::tie(r.minimum.width(), r.preferred.width(), r.maximum.width()) = _column_constraints.update_constraints();
1109 r.margins.left() = _column_constraints.margin_before();
1110 r.margins.right() = _column_constraints.margin_after();
1111 r.padding.left() = _column_constraints.padding_before();
1112 r.padding.right() = _column_constraints.padding_after();
1114 std::tie(r.minimum.height(), r.preferred.height(), r.maximum.height()) = _row_constraints.update_constraints();
1115 r.margins.bottom() = _row_constraints.margin_after();
1116 r.margins.top() = _row_constraints.margin_before();
1117 r.padding.bottom() = _row_constraints.padding_after();
1118 r.padding.top() = _row_constraints.padding_before();
1121 if (num_rows() == 1 and num_columns() == 1) {
1123 }
else if (num_rows() == 1) {
1125 }
else if (num_columns() == 1) {
1143 _column_constraints.
layout(shape.x(), shape.width(), shape.centerline, 0);
1144 _row_constraints.
layout(shape.y(), shape.height(), shape.baseline, baseline_adjustment);
1147 for (
auto& cell : _cells) {
1148 cell.shape.rectangle = {
1149 _column_constraints.position(cell),
1150 _row_constraints.position(cell),
1151 _column_constraints.extent(cell),
1152 _row_constraints.extent(cell)};
1153 cell.shape.centerline = _column_constraints.guideline(cell);
1154 cell.shape.baseline = _row_constraints.guideline(cell);
1159 cell_vector _cells = {};
1160 size_t _num_rows = 0;
1161 size_t _num_columns = 0;
1162 mutable detail::grid_layout_axis_constraints<axis::y, value_type> _row_constraints = {};
1163 mutable detail::grid_layout_axis_constraints<axis::x, value_type> _column_constraints = {};
1169 constexpr void sort_cells() noexcept
1171 std::sort(_cells.begin(), _cells.end(), [](cell_type
const& lhs, cell_type
const& rhs) {
1172 if (lhs.first_row != rhs.first_row) {
1173 return lhs.first_row < rhs.first_row;
1175 return lhs.first_column < rhs.first_column;
1182 constexpr void update_after_insert_or_delete() noexcept
1188 for (
hilet& cell : _cells) {
1189 inplace_max(_num_rows, cell.last_row);
1190 inplace_max(_num_columns, cell.last_column);
Utilities for parsing spreadsheet addresses.
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:323
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
constexpr std::optional< float > make_guideline(vertical_alignment alignment, float bottom, float top, float padding_bottom, float padding_top, float guideline_width)
Create a guideline between two points.
Definition alignment.hpp:57
axis
An enumeration of the 3 axis for 3D geometry.
Definition axis.hpp:18
@ middle
Align to the vertical-middle.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Horizontal/Vertical alignment combination.
Definition alignment.hpp:239
constexpr float & width() noexcept
Access the x-as-width element from the extent.
Definition extent2.hpp:101
constexpr float & height() noexcept
Access the y-as-height element from the extent.
Definition extent2.hpp:112
2D constraints.
Definition box_constraints.hpp:22
Definition box_shape.hpp:15
Definition grid_layout.hpp:23
Definition grid_layout.hpp:227
constexpr reference operator[](size_t index) noexcept
Get element.
Definition grid_layout.hpp:543
constexpr size_t size() const noexcept
Number of cell on this axis.
Definition grid_layout.hpp:469
constexpr const_reference back() const noexcept
Get the last element.
Definition grid_layout.hpp:599
constexpr bool empty() const noexcept
Check if this axis is empty.
Definition grid_layout.hpp:476
constexpr reference front() noexcept
Get the first element.
Definition grid_layout.hpp:566
constexpr reverse_iterator rend() noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:532
constexpr reverse_iterator rbegin() noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:525
constexpr reference back() noexcept
Get the last element.
Definition grid_layout.hpp:588
constexpr const_iterator end() const noexcept
Iterator to beyond the last cell on this axis.
Definition grid_layout.hpp:511
constexpr const_reference operator[](size_t index) const noexcept
Get element.
Definition grid_layout.hpp:555
constexpr const_iterator cbegin() const noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:497
constexpr iterator end() noexcept
Iterator to beyond the last cell on this axis.
Definition grid_layout.hpp:504
constexpr const_iterator begin() const noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:490
constexpr const_iterator cend() const noexcept
Iterator to beyond the last cell on this axis.
Definition grid_layout.hpp:518
constexpr iterator begin() noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:483
constexpr void layout(float new_position, float new_extent, std::optional< float > external_guideline, float guideline_width) noexcept
Layout each cell along an axis.
Definition grid_layout.hpp:406
constexpr const_reference front() const noexcept
Get the first element.
Definition grid_layout.hpp:577
constexpr std::tuple< float, float, float > constraints(cell_type const &cell) const noexcept
Get the minimum, preferred, maximum size of the span.
Definition grid_layout.hpp:360
Definition grid_layout.hpp:236
float margin_after
The right/bottom margin of the cells.
Definition grid_layout.hpp:255
float extent
Size of the cell.
Definition grid_layout.hpp:283
float padding_before
The left/top padding of the cells.
Definition grid_layout.hpp:259
float padding_after
The right/bottom padding of the cells.
Definition grid_layout.hpp:263
float minimum
The minimum width/height of the cells.
Definition grid_layout.hpp:239
bool beyond_maximum
Allow this cell to be resized beyond the maximum constraint.
Definition grid_layout.hpp:271
float maximum
The maximum width/height of the cells.
Definition grid_layout.hpp:247
float margin_before
The left/top margin of the cells.
Definition grid_layout.hpp:251
float preferred
The preferred width/height of the cells.
Definition grid_layout.hpp:243
std::optional< float > guideline
The before-position within this cell where to align to.
Definition grid_layout.hpp:289
float position
The position of the cell.
Definition grid_layout.hpp:277
Grid layout algorithm.
Definition grid_layout.hpp:942
constexpr bool cell_in_use(size_t first_column, size_t first_row, size_t last_column, size_t last_row) noexcept
Check if the cell on the grid is already in use.
Definition grid_layout.hpp:1029
constexpr reference add_cell(size_t first_column, size_t first_row, size_t last_column, size_t last_row, Value &&value, bool beyond_maximum=false) noexcept
Check if the cell on the grid is already in use.
Definition grid_layout.hpp:1064
constexpr reference add_cell(size_t column, size_t row, Value &&value, bool beyond_maximum=false) noexcept
Check if the cell on the grid is already in use.
Definition grid_layout.hpp:1090
constexpr void set_layout(box_shape const &shape, float baseline_adjustment) noexcept
Layout the cells based on the width and height.
Definition grid_layout.hpp:1140
T emplace_back(T... args)