HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
grid_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021-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
9#pragma once
10
11#include "widget.hpp"
12#include "grid_layout.hpp"
14#include <memory>
15
16namespace hi { inline namespace v1 {
17
40class grid_widget : public widget {
41public:
42 using super = widget;
43
45
51
62 template<typename Widget, typename... Args>
63 Widget&
64 make_widget(std::size_t column_first, std::size_t row_first, std::size_t column_last, std::size_t row_last, Args&&...args)
65 {
66 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
67 return static_cast<Widget&>(add_widget(column_first, row_first, column_last, row_last, std::move(tmp)));
68 }
69
78 template<typename Widget, typename... Args>
79 Widget& make_widget(std::size_t column, std::size_t row, Args&&...args)
80 {
81 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
82 return static_cast<Widget&>(add_widget(column, row, column + 1, row + 1, std::move(tmp)));
83 }
84
93 template<typename Widget, typename... Args>
94 Widget& make_widget(std::string_view address, Args&&...args)
95 {
96 hilet[column_first, row_first, column_last, row_last] = parse_spreadsheet_range(address);
97 return make_widget<Widget>(column_first, row_first, column_last, row_last, std::forward<Args>(args)...);
98 }
99
101 [[nodiscard]] generator<widget *> children() const noexcept override
102 {
103 for (hilet& cell : _cells) {
104 co_yield cell.widget.get();
105 }
106 }
107
108 widget_constraints const& set_constraints(set_constraints_context const& context) noexcept override;
109 void set_layout(widget_layout const& context) noexcept override;
110 void draw(draw_context const& context) noexcept override;
111 [[nodiscard]] hitbox hitbox_test(point3 position) const noexcept override;
113private:
114 struct cell_type {
115 std::size_t column_first;
116 std::size_t row_first;
117 std::size_t column_last;
118 std::size_t row_last;
120
121 cell_type(
122 std::size_t column_first,
123 std::size_t row_first,
124 std::size_t column_last,
125 std::size_t row_last,
126 std::unique_ptr<hi::widget> widget) noexcept :
127 column_first(column_first),
128 row_first(row_first),
129 column_last(column_last),
130 row_last(row_last),
131 widget(std::move(widget))
132 {
133 }
134
135 [[nodiscard]] aarectangle
136 rectangle(grid_layout const& columns, grid_layout const& rows, extent2 container_size, bool ltor) const noexcept
137 {
138 hilet[x_min, x_max] = columns.get_positions(column_first, column_last);
139 hilet[y_min, y_max] = rows.get_positions(row_first, row_last);
140
141 hilet x0 = ltor ? x_min : container_size.width() - x_max;
142 hilet x3 = ltor ? x_max : container_size.width() - x_min;
143 hilet y0 = container_size.height() - y_max;
144 hilet y3 = container_size.height() - y_min;
145
146 return {point2{x0, y0}, point2{x3, y3}};
147 }
148
149 [[nodiscard]] widget_baseline baseline(grid_layout const& rows) const noexcept
150 {
151 return rows.get_baseline(row_first, row_last);
152 }
153 };
154
156
157 grid_layout _rows;
158 grid_layout _columns;
159
160 [[nodiscard]] bool
161 address_in_use(std::size_t column_first, std::size_t row_first, std::size_t column_last, std::size_t row_last) const noexcept;
162
163 /* Add a widget to the grid.
164 */
165 widget& add_widget(
166 std::size_t column_first,
167 std::size_t row_first,
168 std::size_t column_last,
169 std::size_t row_last,
170 std::unique_ptr<widget> child_widget) noexcept;
171};
172
173}} // namespace hi::v1
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
The functions in this file are for handling spreadsheet addresses.
Defines widget.
@ rectangle
The gui_event has rectangle data.
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
A GUI widget that lays out child-widgets in a grid with variable sized cells.
Definition grid_widget.hpp:40
Widget & make_widget(std::size_t column, std::size_t row, Args &&...args)
Add a widget directly to this grid-widget.
Definition grid_widget.hpp:79
Widget & make_widget(std::size_t column_first, std::size_t row_first, std::size_t column_last, std::size_t row_last, Args &&...args)
Add a widget directly to this grid-widget.
Definition grid_widget.hpp:64
Widget & make_widget(std::string_view address, Args &&...args)
Add a widget directly to this grid-widget.
Definition grid_widget.hpp:94
grid_widget(widget *parent) noexcept
Constructs an empty grid widget.
An interactive graphical object as part of the user-interface.
Definition widget.hpp:45
widget(widget *parent) noexcept
widget * parent
Pointer to the parent widget.
Definition widget.hpp:50
T move(T... args)