HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
grid_layout_widget.hpp
1// Copyright Take Vos 2020.
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 "abstract_container_widget.hpp"
8#include "grid_layout_delegate.hpp"
9#include "../GUI/theme.hpp"
10#include "../cell_address.hpp"
11#include "../flow_layout.hpp"
12#include <memory>
13
14namespace tt {
15
17public:
19
23 std::weak_ptr<grid_layout_delegate> delegate = {}) noexcept :
24 abstract_container_widget(window, parent), _delegate(delegate)
25 {
26 }
27
29 {
30 if (auto delegate_ = _delegate.lock()) {
31 delegate_->deinit(*this);
32 }
33 }
34
35 void init() noexcept override
36 {
37 if (auto delegate_ = _delegate.lock()) {
38 delegate_->init(*this);
39 }
40 }
41
42 [[nodiscard]] bool
43 update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override;
44 [[nodiscard]] void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override;
45
46 /* Add a widget to the grid.
47 */
48 std::shared_ptr<widget> add_widget(cell_address address, std::shared_ptr<widget> childWidget) noexcept;
49
54 template<typename T, typename... Args>
56 {
57 auto tmp = std::make_shared<T>(window, shared_from_this(), std::forward<Args>(args)...);
58 tmp->init();
59 return std::static_pointer_cast<T>(add_widget(address, std::move(tmp)));
60 }
61
66 template<typename T, cell_address CellAddress, typename... Args>
68 {
69 return make_widget_at_address<T>(CellAddress, std::forward<Args>(args)...);
70 }
71
72private:
73 struct cell {
74 cell_address address;
76
77 cell(cell_address address, std::shared_ptr<tt::widget> widget) noexcept : address(address), widget(std::move(widget)) {}
78
79 [[nodiscard]] aarect rectangle(flow_layout const &columns, flow_layout const &rows) const noexcept
80 {
81 ttlet first_column_nr = address.column.begin(columns.nr_items());
82 ttlet last_column_nr = address.column.end(columns.nr_items());
83 ttlet first_row_nr = address.row.begin(rows.nr_items());
84 ttlet last_row_nr = address.row.end(rows.nr_items());
85
86 ttlet[x, width] = columns.get_offset_and_size(first_column_nr, last_column_nr);
87 ttlet[y, height] = rows.get_offset_and_size(first_row_nr, last_row_nr);
88
89 return {x, y, width, height};
90 };
91
92 [[nodiscard]] relative_base_line base_line(flow_layout const &rows) const noexcept
93 {
94 ttlet aligned_row_nr = address.row.aligned_to(rows.nr_items());
95 return rows.get_base_line(aligned_row_nr);
96 }
97 };
98
99 std::vector<cell> _cells;
100 cell_address _current_address = "L0T0"_ca;
101
103
104 flow_layout _rows;
105 flow_layout _columns;
106
107 [[nodiscard]] static std::pair<int, int> calculate_grid_size(std::vector<cell> const &cells) noexcept;
108 [[nodiscard]] static f32x4
109 calculate_cell_min_size(std::vector<cell> const &cells, flow_layout &rows, flow_layout &columns) noexcept;
110};
111
112} // namespace tt
ssize_t end(ssize_t size) const noexcept
Find one beyond the end of the cell.
Definition cell_address.hpp:45
ssize_t begin(ssize_t size) const noexcept
Find the begin of the cell.
Definition cell_address.hpp:29
ssize_t aligned_to(ssize_t size) const noexcept
Find on which cell this aligns to.
Definition cell_address.hpp:61
Definition cell_address.hpp:149
Definition gui_window.hpp:39
Definition abstract_container_widget.hpp:11
Definition grid_layout_widget.hpp:16
std::shared_ptr< T > make_widget(Args &&...args)
Add a widget directly to this widget.
Definition grid_layout_widget.hpp:67
std::shared_ptr< T > make_widget_at_address(cell_address address, Args &&...args)
Add a widget directly to this widget.
Definition grid_layout_widget.hpp:55
void init() noexcept override
Should be called right after allocating and constructing a widget.
Definition grid_layout_widget.hpp:35
bool update_constraints(hires_utc_clock::time_point display_time_point, bool need_reconstrain) noexcept override
Update the constraints of the widget.
void update_layout(hires_utc_clock::time_point display_time_point, bool need_layout) noexcept override
Update the internal layout of the widget.
Definition widget.hpp:96
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:100
float base_line() const noexcept
Get the base-line in local coordinates.
Definition widget.hpp:350
abstract_container_widget const & parent() const noexcept
Get a reference to the parent.
aarect rectangle() const noexcept
Get the rectangle in local coordinates.
Definition widget.hpp:340
T move(T... args)