HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
grid_layout.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 "box_constraints.hpp"
8#include "box_shape.hpp"
10#include "../geometry/module.hpp"
11#include "../utility/module.hpp"
12#include <cstdint>
13#include <numeric>
14#include <vector>
15#include <algorithm>
16#include <utility>
17#include <cmath>
18
19namespace hi { inline namespace v1 {
20namespace detail {
21
22template<typename T>
24 using value_type = T;
25
26 size_t first_column = 0;
27 size_t first_row = 0;
28 size_t last_column = 0;
29 size_t last_row = 0;
30 bool beyond_maximum = false;
31 value_type value = {};
32 box_shape shape = {};
33
34 constexpr grid_layout_cell() noexcept = default;
35 constexpr grid_layout_cell(grid_layout_cell const&) noexcept = default;
36 constexpr grid_layout_cell(grid_layout_cell&&) noexcept = default;
37 constexpr grid_layout_cell& operator=(grid_layout_cell const&) noexcept = default;
38 constexpr grid_layout_cell& operator=(grid_layout_cell&&) noexcept = default;
39
40 constexpr grid_layout_cell(
41 size_t first_column,
42 size_t first_row,
43 size_t last_column,
44 size_t last_row,
45 bool beyond_maximum,
46 std::convertible_to<value_type> auto&& value) noexcept :
47 first_column(first_column),
48 first_row(first_row),
49 last_column(last_column),
50 last_row(last_row),
51 beyond_maximum(beyond_maximum),
52 value(hi_forward(value))
53 {
54 hi_assert(first_column < last_column);
55 hi_assert(first_row < last_row);
56 }
57
58 constexpr void set_constraints(box_constraints const& constraints) noexcept
59 {
60 _constraints = constraints;
61 }
62
63 template<hi::axis Axis>
64 [[nodiscard]] constexpr size_t first() const noexcept
65 {
66 if constexpr (Axis == axis::x) {
67 return first_column;
68 } else if constexpr (Axis == axis::y) {
69 return first_row;
70 } else {
72 }
73 }
74
75 template<hi::axis Axis>
76 [[nodiscard]] constexpr size_t last() const noexcept
77 {
78 if constexpr (Axis == axis::x) {
79 return last_column;
80 } else if constexpr (Axis == axis::y) {
81 return last_row;
82 } else {
84 }
85 }
86
87 template<hi::axis Axis>
88 [[nodiscard]] constexpr size_t span() const noexcept
89 {
90 hi_axiom(first<Axis>() < last<Axis>());
91 return last<Axis>() - first<Axis>();
92 }
93
94 template<hi::axis Axis>
95 [[nodiscard]] constexpr auto alignment() const noexcept
96 {
97 if constexpr (Axis == axis::x) {
98 return _constraints.alignment.horizontal();
99 } else if constexpr (Axis == axis::y) {
100 return _constraints.alignment.vertical();
101 } else {
103 }
104 }
105
106 template<hi::axis Axis>
107 [[nodiscard]] constexpr int minimum() const noexcept
108 {
109 if constexpr (Axis == axis::x) {
110 return _constraints.minimum.width();
111 } else if constexpr (Axis == axis::y) {
112 return _constraints.minimum.height();
113 } else {
115 }
116 }
117
118 template<hi::axis Axis>
119 [[nodiscard]] constexpr int preferred() const noexcept
120 {
121 if constexpr (Axis == axis::x) {
122 return _constraints.preferred.width();
123 } else if constexpr (Axis == axis::y) {
124 return _constraints.preferred.height();
125 } else {
127 }
128 }
129
130 template<hi::axis Axis>
131 [[nodiscard]] constexpr int maximum() const noexcept
132 {
133 if constexpr (Axis == axis::x) {
134 return _constraints.maximum.width();
135 } else if constexpr (Axis == axis::y) {
136 return _constraints.maximum.height();
137 } else {
139 }
140 }
141
142 template<hi::axis Axis>
143 [[nodiscard]] constexpr int margin_before(bool forward) const noexcept
144 {
145 if constexpr (Axis == axis::x) {
146 if (forward) {
147 return _constraints.margins.left();
148 } else {
149 return _constraints.margins.right();
150 }
151 } else if constexpr (Axis == axis::y) {
152 if (forward) {
153 return _constraints.margins.bottom();
154 } else {
155 return _constraints.margins.top();
156 }
157 } else {
159 }
160 }
161
162 template<hi::axis Axis>
163 [[nodiscard]] constexpr int margin_after(bool forward) const noexcept
164 {
165 if constexpr (Axis == axis::x) {
166 if (forward) {
167 return _constraints.margins.right();
168 } else {
169 return _constraints.margins.left();
170 }
171 } else if constexpr (Axis == axis::y) {
172 if (forward) {
173 return _constraints.margins.top();
174 } else {
175 return _constraints.margins.bottom();
176 }
177 } else {
179 }
180 }
181
182 template<hi::axis Axis>
183 [[nodiscard]] constexpr int padding_before(bool forward) const noexcept
184 {
185 if constexpr (Axis == axis::x) {
186 if (forward) {
187 return _constraints.padding.left();
188 } else {
189 return _constraints.padding.right();
190 }
191 } else if constexpr (Axis == axis::y) {
192 if (forward) {
193 return _constraints.padding.bottom();
194 } else {
195 return _constraints.padding.top();
196 }
197 } else {
199 }
200 }
201
202 template<hi::axis Axis>
203 [[nodiscard]] constexpr int padding_after(bool forward) const noexcept
204 {
205 if constexpr (Axis == axis::x) {
206 if (forward) {
207 return _constraints.padding.right();
208 } else {
209 return _constraints.padding.left();
210 }
211 } else if constexpr (Axis == axis::y) {
212 if (forward) {
213 return _constraints.padding.top();
214 } else {
215 return _constraints.padding.bottom();
216 }
217 } else {
219 }
220 }
221
222private:
223 box_constraints _constraints;
224};
225
226template<hi::axis Axis, typename T>
228public:
229 constexpr static hi::axis axis = Axis;
230
231 using value_type = T;
232 using alignment_type = std::conditional_t<axis == axis::y, vertical_alignment, horizontal_alignment>;
235
239 int minimum = 0;
240
243 int preferred = 0;
244
248
252
256
260
264
267 alignment_type alignment = alignment_type::none;
268
271 bool beyond_maximum = false;
272
277 int position = 0;
278
283 int extent = 0;
284
289 std::optional<int> guideline = 0;
290 };
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;
297
298 constexpr ~grid_layout_axis_constraints() = default;
299 constexpr grid_layout_axis_constraints() noexcept = default;
300 constexpr grid_layout_axis_constraints(grid_layout_axis_constraints const&) noexcept = default;
301 constexpr grid_layout_axis_constraints(grid_layout_axis_constraints&&) noexcept = default;
302 constexpr grid_layout_axis_constraints& operator=(grid_layout_axis_constraints const&) noexcept = default;
303 constexpr grid_layout_axis_constraints& operator=(grid_layout_axis_constraints&&) noexcept = default;
304 [[nodiscard]] constexpr friend bool
305 operator==(grid_layout_axis_constraints const&, grid_layout_axis_constraints const&) noexcept = default;
306
314 constexpr grid_layout_axis_constraints(cell_vector const& cells, size_t num, bool forward) noexcept :
315 _constraints(num), _forward(forward)
316 {
317 for (hilet& cell : cells) {
318 construct_simple_cell(cell);
319 }
320 construct_fixup();
321
322 for (hilet& cell : cells) {
323 construct_span_cell(cell);
324 }
325 construct_fixup();
326 }
327
328 [[nodiscard]] constexpr int margin_before() const noexcept
329 {
330 return empty() ? 0 : _forward ? front().margin_before : back().margin_before;
331 }
332
333 [[nodiscard]] constexpr int margin_after() const noexcept
334 {
335 return empty() ? 0 : _forward ? back().margin_after : front().margin_after;
336 }
337
338 [[nodiscard]] constexpr int padding_before() const noexcept
339 {
340 return empty() ? 0 : _forward ? front().padding_before : back().padding_before;
341 }
342
343 [[nodiscard]] constexpr int padding_after() const noexcept
344 {
345 return empty() ? 0 : _forward ? back().padding_after : front().padding_after;
346 }
347
348 [[nodiscard]] constexpr std::tuple<int, int, int> update_constraints() const noexcept
349 {
350 return constraints(begin(), end());
351 }
352
360 [[nodiscard]] constexpr std::tuple<int, int, int> constraints(cell_type const& cell) const noexcept
361 {
362 return constraints(cell.first<axis>(), cell.last<axis>());
363 }
364
365 [[nodiscard]] constexpr int position(cell_type const& cell) const noexcept
366 {
367 return position(cell.first<axis>(), cell.last<axis>());
368 }
369
370 [[nodiscard]] constexpr int extent(cell_type const& cell) const noexcept
371 {
372 return extent(cell.first<axis>(), cell.last<axis>());
373 }
374
375 [[nodiscard]] constexpr std::optional<int> guideline(cell_type const& cell) const noexcept
376 {
377 if (cell.span<axis>() == 1) {
378 return guideline(cell.first<axis>());
379 } else {
380 return std::nullopt;
381 }
382 }
383
406 constexpr void layout(int new_position, int new_extent, std::optional<int> external_guideline, int guideline_width) noexcept
407 {
408 // Start with the extent of each constraint equal to the preferred extent.
409 for (auto& constraint : _constraints) {
410 constraint.extent = constraint.preferred;
411 }
412
413 // If the total extent is too large, shrink the constraints that allow to be shrunk.
414 auto [total_extent, count] = layout_shrink(begin(), end());
415 while (total_extent > new_extent and count != 0) {
416 // The result may shrink slightly too much, which will be fixed by expanding in the next loop.
417 std::tie(total_extent, count) = layout_shrink(begin(), end(), total_extent - new_extent, count);
418 }
419
420 // If the total extent is too small, expand the constraints that allow to be grown.
421 std::tie(total_extent, count) = layout_expand(begin(), end());
422 while (total_extent < new_extent and count != 0) {
423 // The result may expand slightly too much, we don't care.
424 std::tie(total_extent, count) = layout_expand(begin(), end(), new_extent - total_extent, count);
425 }
426
427 // If the total extent is still too small, expand into the cells that are marked beyond_maximum.
428 if (total_extent < new_extent) {
429 // The result may expand slightly too much, we don't care.
430 count = std::count_if(begin(), end(), [](hilet& item) {
431 return item.beyond_maximum;
432 });
433 if (count) {
434 hilet todo = new_extent - total_extent;
435 hilet per_extent = narrow_cast<int>((todo + count - 1) / count);
436 for (auto& constraint : _constraints) {
437 if (constraint.beyond_maximum) {
438 constraint.extent += per_extent;
439 }
440 }
441 }
442 total_extent = extent(cbegin(), cend());
443 }
444
445 // If the total extent is still too small, expand the first constrain above the maximum size.
446 if (total_extent < new_extent and not empty()) {
447 // The result may expand slightly too much, we don't care.
448 front().extent += new_extent - total_extent;
449 }
450
451 if (_forward) {
452 layout_position(begin(), end(), new_position, guideline_width);
453 } else {
454 layout_position(rbegin(), rend(), new_position, guideline_width);
455 }
456
457 if (external_guideline and size() == 1) {
458 // When there is only 1 cell on this axis, the external guideline is used.
459 // XXX If there are more cell, then the external alignment should be taken into account.
460 front().guideline = *external_guideline;
461 }
462 }
463
466 [[nodiscard]] constexpr size_t size() const noexcept
467 {
468 return _constraints.size();
469 }
470
473 [[nodiscard]] constexpr bool empty() const noexcept
474 {
475 return _constraints.empty();
476 }
477
480 [[nodiscard]] constexpr iterator begin() noexcept
481 {
482 return _constraints.begin();
483 }
484
487 [[nodiscard]] constexpr const_iterator begin() const noexcept
488 {
489 return _constraints.begin();
490 }
491
494 [[nodiscard]] constexpr const_iterator cbegin() const noexcept
495 {
496 return _constraints.cbegin();
497 }
498
501 [[nodiscard]] constexpr iterator end() noexcept
502 {
503 return _constraints.end();
504 }
505
508 [[nodiscard]] constexpr const_iterator end() const noexcept
509 {
510 return _constraints.end();
511 }
512
515 [[nodiscard]] constexpr const_iterator cend() const noexcept
516 {
517 return _constraints.cend();
518 }
519
522 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept
523 {
524 return _constraints.rbegin();
525 }
526
529 [[nodiscard]] constexpr reverse_iterator rend() noexcept
530 {
531 return _constraints.rend();
532 }
533
540 [[nodiscard]] constexpr reference operator[](size_t index) noexcept
541 {
542 hi_axiom(index < size());
543 return _constraints[index];
544 }
545
552 [[nodiscard]] constexpr const_reference operator[](size_t index) const noexcept
553 {
554 hi_axiom(index < size());
555 return _constraints[index];
556 }
557
563 [[nodiscard]] constexpr reference front() noexcept
564 {
565 hi_axiom(not empty());
566 return _constraints.front();
567 }
568
574 [[nodiscard]] constexpr const_reference front() const noexcept
575 {
576 hi_axiom(not empty());
577 return _constraints.front();
578 }
579
585 [[nodiscard]] constexpr reference back() noexcept
586 {
587 hi_axiom(not empty());
588 return _constraints.back();
589 }
590
596 [[nodiscard]] constexpr const_reference back() const noexcept
597 {
598 hi_axiom(not empty());
599 return _constraints.back();
600 }
601
602private:
611 constraint_vector _constraints = {};
612
615 bool _forward = true;
616
633 [[nodiscard]] constexpr std::pair<int, size_t>
634 layout_shrink(const_iterator first, const_iterator last, int extra = 0, size_t count = 1) noexcept
635 {
636 hilet first_ = begin() + std::distance(cbegin(), first);
637 hilet last_ = begin() + std::distance(cbegin(), last);
638
639 hi_axiom(extra >= 0);
640
641 hilet extra_per = narrow_cast<int>((extra + count - 1) / count);
642
643 auto new_extent = 0;
644 auto new_count = 0_uz;
645 for (auto it = first_; it != last_; ++it) {
646 it->extent = it->extent - std::max(extra_per, it->extent - it->minimum);
647
648 if (it != first_) {
649 new_extent += it->margin_before;
650 }
651 new_extent += it->extent;
652
653 if (it->extent > it->minimum) {
654 ++new_count;
655 }
656 }
657
658 return {new_extent, new_count};
659 }
660
677 [[nodiscard]] constexpr std::pair<int, size_t>
678 layout_expand(const_iterator first, const_iterator last, int extra = 0, size_t count = 1) noexcept
679 {
680 hilet first_ = begin() + std::distance(cbegin(), first);
681 hilet last_ = begin() + std::distance(cbegin(), last);
682
683 hi_axiom(extra >= 0);
684
685 hilet extra_per = narrow_cast<int>((extra + count - 1) / count);
686
687 auto new_extent = 0;
688 auto new_count = 0_uz;
689 for (auto it = first_; it != last_; ++it) {
690 it->extent = it->extent + std::min(extra_per, it->maximum - it->extent);
691
692 if (it != first_) {
693 new_extent += it->margin_before;
694 }
695 new_extent += it->extent;
696
697 if (it->extent < it->maximum) {
698 ++new_count;
699 }
700 }
701
702 return {new_extent, new_count};
703 }
704
705 constexpr void layout_position(auto first, auto last, int start_position, int guideline_width) noexcept
706 {
707 auto position = start_position;
708 for (auto it = first; it != last; ++it) {
709 it->position = position;
710 it->guideline = make_guideline(
711 it->alignment, position, position + it->extent, it->padding_before, it->padding_after, guideline_width);
712
713 position += it->extent;
714 position += it->margin_after;
715 }
716 }
717
725 constexpr void construct_simple_cell(cell_type const& cell) noexcept
726 {
727 inplace_max(_constraints[cell.first<axis>()].margin_before, cell.margin_before<axis>(_forward));
728 inplace_max(_constraints[cell.last<axis>() - 1].margin_after, cell.margin_after<axis>(_forward));
729 inplace_max(_constraints[cell.first<axis>()].padding_before, cell.padding_before<axis>(_forward));
730 inplace_max(_constraints[cell.last<axis>() - 1].padding_after, cell.padding_after<axis>(_forward));
731
732 for (auto i = cell.first<axis>(); i != cell.last<axis>(); ++i) {
733 _constraints[i].beyond_maximum |= cell.beyond_maximum;
734 }
735
736 if (cell.span<axis>() == 1) {
737 inplace_max(_constraints[cell.first<axis>()].alignment, cell.alignment<axis>());
738 inplace_max(_constraints[cell.first<axis>()].minimum, cell.minimum<axis>());
739 inplace_max(_constraints[cell.first<axis>()].preferred, cell.preferred<axis>());
740 inplace_min(_constraints[cell.first<axis>()].maximum, cell.maximum<axis>());
741 }
742 }
743
750 constexpr void construct_span_cell(cell_type const& cell) noexcept
751 {
752 auto num_cells = narrow_cast<int>(cell.span<axis>());
753
754 if (cell.span<axis>() > 1) {
755 hilet[span_minimum, span_preferred, span_maximum] = constraints(cell);
756 if (hilet extra = cell.minimum<axis>() - span_minimum; extra > 0) {
757 hilet extra_per_cell = (extra + num_cells - 1) / num_cells;
758 for (auto i = cell.first<axis>(); i != cell.last<axis>(); ++i) {
759 _constraints[i].minimum += extra_per_cell;
760 }
761 }
762
763 if (hilet extra = cell.preferred<axis>() - span_preferred; extra > 0) {
764 hilet extra_per_cell = (extra + num_cells - 1) / num_cells;
765 for (auto i = cell.first<axis>(); i != cell.last<axis>(); ++i) {
766 _constraints[i].preferred += extra_per_cell;
767 }
768 }
769
770 if (hilet extra = cell.maximum<axis>() - span_preferred; extra < 0) {
771 hilet extra_per_cell = (extra + num_cells) / num_cells;
772 for (auto i = cell.first<axis>(); i != cell.last<axis>(); ++i) {
773 // The maximum could become too low here, fixup() will fix this.
774 _constraints[i].maximum += extra_per_cell;
775 }
776 }
777 }
778 }
779
784 constexpr void construct_fixup() noexcept
785 {
786 for (auto it = begin(); it != end(); ++it) {
787 // Fix the margins so that between two constraints they are equal.
788 if (it + 1 != end()) {
789 it->margin_after = (it + 1)->margin_before = std::max(it->margin_after, (it + 1)->margin_before);
790 }
791
792 // Fix the constraints so that minimum <= preferred <= maximum.
793 inplace_max(it->preferred, it->minimum);
794 inplace_max(it->maximum, it->preferred);
795
796 // Fix the padding, so that it doesn't overlap.
797 if (it->padding_before + it->padding_after > it->minimum) {
798 hilet padding_diff = it->padding_after - it->padding_before;
799 hilet middle = std::clamp(it->minimum / 2 + padding_diff, 0, it->minimum);
800 it->padding_after = middle;
801 it->padding_before = it->minimum - middle;
802 }
803 }
804 }
805
814 [[nodiscard]] constexpr std::tuple<int, int, int> constraints(const_iterator first, const_iterator last) const noexcept
815 {
816 auto r_minimum = 0;
817 auto r_preferred = 0;
818 auto r_maximum = 0;
819 auto r_margin = 0;
820
821 if (first != last) {
822 r_minimum = first->minimum;
823 r_preferred = first->preferred;
824 r_maximum = first->maximum;
825 for (auto it = first + 1; it != last; ++it) {
826 r_margin += it->margin_before;
827 r_minimum += it->minimum;
828 r_preferred += it->preferred;
829 r_maximum += it->maximum;
830 }
831 }
832 return {r_minimum + r_margin, r_preferred + r_margin, r_maximum + r_margin};
833 }
834
843 [[nodiscard]] constexpr std::tuple<int, int, int> constraints(size_t first, size_t last) const noexcept
844 {
845 hi_axiom(first <= last);
846 hi_axiom(last <= size());
847 return constraints(begin() + first, begin() + last);
848 }
849
857 [[nodiscard]] constexpr int position(const_iterator first, const_iterator last) const noexcept
858 {
859 hi_axiom(first != last);
860 if (_forward) {
861 return first->position;
862 } else {
863 return (last - 1)->position;
864 }
865 }
866
874 [[nodiscard]] constexpr int position(size_t first, size_t last) const noexcept
875 {
876 hi_axiom(first < last);
877 hi_axiom(last <= size());
878 return position(cbegin() + first, cbegin() + last);
879 }
880
888 [[nodiscard]] constexpr int extent(const_iterator first, const_iterator last) const noexcept
889 {
890 auto r = 0;
891 if (first != last) {
892 r = first->extent;
893 for (auto it = first + 1; it != last; ++it) {
894 r += it->margin_before;
895 r += it->extent;
896 }
897 }
898 return r;
899 }
900
908 [[nodiscard]] constexpr int extent(size_t first, size_t last) const noexcept
909 {
910 hi_axiom(first <= last);
911 hi_axiom(last <= size());
912 return extent(cbegin() + first, cbegin() + last);
913 }
914
915 [[nodiscard]] constexpr std::optional<int> guideline(const_iterator it) const noexcept
916 {
917 return it->guideline;
918 }
919
920 [[nodiscard]] constexpr std::optional<int> guideline(size_t i) const noexcept
921 {
922 return guideline(cbegin() + i);
923 }
924};
925
926} // namespace detail
927
933template<typename T>
935public:
936 using value_type = T;
937
938 using cell_type = detail::grid_layout_cell<value_type>;
939 using cell_vector = std::vector<cell_type>;
940 using iterator = cell_vector::iterator;
941 using const_iterator = cell_vector::const_iterator;
942 using reference = cell_vector::reference;
943 using const_reference = cell_vector::const_reference;
944
945 ~grid_layout() = default;
946 constexpr grid_layout() noexcept = default;
947 constexpr grid_layout(grid_layout const&) noexcept = default;
948 constexpr grid_layout(grid_layout&&) noexcept = default;
949 constexpr grid_layout& operator=(grid_layout const&) noexcept = default;
950 constexpr grid_layout& operator=(grid_layout&&) noexcept = default;
951 [[nodiscard]] constexpr friend bool operator==(grid_layout const&, grid_layout const&) noexcept = default;
952
953 [[nodiscard]] constexpr bool empty() const noexcept
954 {
955 return _cells.empty();
956 }
957
958 [[nodiscard]] constexpr size_t size() const noexcept
959 {
960 return _cells.size();
961 }
962
963 [[nodiscard]] constexpr size_t num_columns() const noexcept
964 {
965 return _num_columns;
966 }
967
968 [[nodiscard]] constexpr size_t num_rows() const noexcept
969 {
970 return _num_rows;
971 }
972
973 [[nodiscard]] constexpr iterator begin() noexcept
974 {
975 return _cells.begin();
976 }
977
978 [[nodiscard]] constexpr iterator end() noexcept
979 {
980 return _cells.end();
981 }
982
983 [[nodiscard]] constexpr const_iterator begin() const noexcept
984 {
985 return _cells.begin();
986 }
987
988 [[nodiscard]] constexpr const_iterator end() const noexcept
989 {
990 return _cells.end();
991 }
992
993 [[nodiscard]] constexpr const_iterator cbegin() const noexcept
994 {
995 return _cells.cbegin();
996 }
997
998 [[nodiscard]] constexpr const_iterator cend() const noexcept
999 {
1000 return _cells.cend();
1001 }
1002
1003 [[nodiscard]] constexpr const_reference operator[](size_t i) const noexcept
1004 {
1005 return _cells[i];
1006 }
1007
1008 [[nodiscard]] constexpr reference operator[](size_t i) noexcept
1009 {
1010 return _cells[i];
1011 }
1012
1021 [[nodiscard]] constexpr bool cell_in_use(size_t first_column, size_t first_row, size_t last_column, size_t last_row) noexcept
1022 {
1023 // At least one cell must be in the range.
1024 hi_axiom(first_column < last_column);
1025 hi_axiom(first_row < last_row);
1026
1027 for (hilet& cell : _cells) {
1028 if (first_column >= cell.last_column) {
1029 continue;
1030 }
1031 if (last_column <= cell.first_column) {
1032 continue;
1033 }
1034 if (first_row >= cell.last_row) {
1035 continue;
1036 }
1037 if (last_row <= cell.first_row) {
1038 continue;
1039 }
1040 return true;
1041 }
1042 return false;
1043 }
1044
1055 template<forward_of<value_type> Value>
1056 constexpr reference add_cell(
1057 size_t first_column,
1058 size_t first_row,
1059 size_t last_column,
1060 size_t last_row,
1061 Value&& value,
1062 bool beyond_maximum = false) noexcept
1063 {
1064 // At least one cell must be in the range.
1065 hi_assert(first_column < last_column);
1066 hi_assert(first_row < last_row);
1067 hi_assert(not cell_in_use(first_column, first_row, last_column, last_row));
1068 auto& r = _cells.emplace_back(first_column, first_row, last_column, last_row, beyond_maximum, std::forward<Value>(value));
1069 update_after_insert_or_delete();
1070 return r;
1071 }
1072
1081 template<forward_of<value_type> Value>
1082 constexpr reference add_cell(size_t column, size_t row, Value&& value, bool beyond_maximum = false) noexcept
1083 {
1084 return add_cell(column, row, column + 1, row + 1, std::forward<Value>(value), beyond_maximum);
1085 }
1086
1087 constexpr void clear() noexcept
1088 {
1089 _cells.clear();
1090 update_after_insert_or_delete();
1091 }
1092
1093 [[nodiscard]] constexpr box_constraints constraints(bool left_to_right) const noexcept
1094 {
1095 // Rows in the grid are laid out from top to bottom which is reverse from the y-axis up.
1096 _row_constraints = {_cells, num_rows(), false};
1097 _column_constraints = {_cells, num_columns(), left_to_right};
1098
1099 auto r = box_constraints{};
1100 std::tie(r.minimum.width(), r.preferred.width(), r.maximum.width()) = _column_constraints.update_constraints();
1101 r.margins.left() = _column_constraints.margin_before();
1102 r.margins.right() = _column_constraints.margin_after();
1103 r.padding.left() = _column_constraints.padding_before();
1104 r.padding.right() = _column_constraints.padding_after();
1105
1106 std::tie(r.minimum.height(), r.preferred.height(), r.maximum.height()) = _row_constraints.update_constraints();
1107 r.margins.bottom() = _row_constraints.margin_after();
1108 r.margins.top() = _row_constraints.margin_before();
1109 r.padding.bottom() = _row_constraints.padding_after();
1110 r.padding.top() = _row_constraints.padding_before();
1111
1112 r.alignment = [&] {
1113 if (num_rows() == 1 and num_columns() == 1) {
1114 return hi::alignment{_column_constraints.front().alignment, _row_constraints.front().alignment};
1115 } else if (num_rows() == 1) {
1116 return hi::alignment{_row_constraints.front().alignment};
1117 } else if (num_columns() == 1) {
1118 return hi::alignment{_column_constraints.front().alignment};
1119 } else {
1120 return hi::alignment{};
1121 }
1122 }();
1123
1124 return r;
1125 }
1126
1132 constexpr void set_layout(box_shape const& shape, int baseline_adjustment) noexcept
1133 {
1134 // Rows in the grid are laid out from top to bottom which is reverse from the y-axis up.
1135 _column_constraints.layout(shape.x(), shape.width(), shape.centerline, 0);
1136 _row_constraints.layout(shape.y(), shape.height(), shape.baseline, baseline_adjustment);
1137
1138 // Assign the shape for each cell.
1139 for (auto& cell : _cells) {
1140 cell.shape.rectangle = {
1141 _column_constraints.position(cell),
1142 _row_constraints.position(cell),
1143 _column_constraints.extent(cell),
1144 _row_constraints.extent(cell)};
1145 cell.shape.centerline = _column_constraints.guideline(cell);
1146 cell.shape.baseline = _row_constraints.guideline(cell);
1147 }
1148 }
1149
1150private:
1151 cell_vector _cells = {};
1152 size_t _num_rows = 0;
1153 size_t _num_columns = 0;
1154 mutable detail::grid_layout_axis_constraints<axis::y, value_type> _row_constraints = {};
1155 mutable detail::grid_layout_axis_constraints<axis::x, value_type> _column_constraints = {};
1156
1161 constexpr void sort_cells() noexcept
1162 {
1163 std::sort(_cells.begin(), _cells.end(), [](cell_type const& lhs, cell_type const& rhs) {
1164 if (lhs.first_row != rhs.first_row) {
1165 return lhs.first_row < rhs.first_row;
1166 } else {
1167 return lhs.first_column < rhs.first_column;
1168 }
1169 });
1170 }
1171
1174 constexpr void update_after_insert_or_delete() noexcept
1175 {
1176 sort_cells();
1177
1178 _num_rows = 0;
1179 _num_columns = 0;
1180 for (hilet& cell : _cells) {
1181 inplace_max(_num_rows, cell.last_row);
1182 inplace_max(_num_columns, cell.last_column);
1183 }
1184 }
1185};
1186
1187}} // namespace hi::v1
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:308
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#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< T > make_guideline(vertical_alignment alignment, T bottom, T top, T padding_bottom, T padding_top, T guideline_width)
Create a guideline between two points.
Definition alignment.hpp:59
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
constexpr value_type & width() noexcept
Access the x-as-width element from the extent.
Definition extent.hpp:166
constexpr value_type & height() noexcept
Access the y-as-height element from the extent.
Definition extent.hpp:177
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:540
constexpr size_t size() const noexcept
Number of cell on this axis.
Definition grid_layout.hpp:466
constexpr void layout(int new_position, int new_extent, std::optional< int > external_guideline, int guideline_width) noexcept
Layout each cell along an axis.
Definition grid_layout.hpp:406
constexpr const_reference back() const noexcept
Get the last element.
Definition grid_layout.hpp:596
constexpr std::tuple< int, int, int > constraints(cell_type const &cell) const noexcept
Get the minimum, preferred, maximum size of the span.
Definition grid_layout.hpp:360
constexpr bool empty() const noexcept
Check if this axis is empty.
Definition grid_layout.hpp:473
constexpr reference front() noexcept
Get the first element.
Definition grid_layout.hpp:563
constexpr reverse_iterator rend() noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:529
constexpr reverse_iterator rbegin() noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:522
constexpr reference back() noexcept
Get the last element.
Definition grid_layout.hpp:585
constexpr const_iterator end() const noexcept
Iterator to beyond the last cell on this axis.
Definition grid_layout.hpp:508
constexpr const_reference operator[](size_t index) const noexcept
Get element.
Definition grid_layout.hpp:552
constexpr const_iterator cbegin() const noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:494
constexpr iterator end() noexcept
Iterator to beyond the last cell on this axis.
Definition grid_layout.hpp:501
constexpr const_iterator begin() const noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:487
constexpr const_iterator cend() const noexcept
Iterator to beyond the last cell on this axis.
Definition grid_layout.hpp:515
constexpr iterator begin() noexcept
Iterator to the first cell on this axis.
Definition grid_layout.hpp:480
constexpr const_reference front() const noexcept
Get the first element.
Definition grid_layout.hpp:574
int maximum
The maximum width/height of the cells.
Definition grid_layout.hpp:247
int margin_after
The right/bottom margin of the cells.
Definition grid_layout.hpp:255
bool beyond_maximum
Allow this cell to be resized beyond the maximum constraint.
Definition grid_layout.hpp:271
int extent
Size of the cell.
Definition grid_layout.hpp:283
int margin_before
The left/top margin of the cells.
Definition grid_layout.hpp:251
int minimum
The minimum width/height of the cells.
Definition grid_layout.hpp:239
int preferred
The preferred width/height of the cells.
Definition grid_layout.hpp:243
int padding_before
The left/top padding of the cells.
Definition grid_layout.hpp:259
alignment_type alignment
The alignment of the cells.
Definition grid_layout.hpp:267
int position
The position of the cell.
Definition grid_layout.hpp:277
std::optional< int > guideline
The before-position within this cell where to align to.
Definition grid_layout.hpp:289
int padding_after
The right/bottom padding of the cells.
Definition grid_layout.hpp:263
Grid layout algorithm.
Definition grid_layout.hpp:934
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:1021
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:1056
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:1082
constexpr void set_layout(box_shape const &shape, int baseline_adjustment) noexcept
Layout the cells based on the width and height.
Definition grid_layout.hpp:1132
T back(T... args)
T begin(T... args)
T clear(T... args)
T count_if(T... args)
T distance(T... args)
T emplace_back(T... args)
T empty(T... args)
T end(T... args)
T front(T... args)
T max(T... args)
T min(T... args)
T rbegin(T... args)
T rend(T... args)
T size(T... args)
T sort(T... args)
T tie(T... args)