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 grid_widget(gui_window& window, widget *parent) noexcept;
52
63 template<typename Widget, typename... Args>
64 Widget&
65 make_widget(std::size_t column_first, std::size_t row_first, std::size_t column_last, std::size_t row_last, Args&&...args)
66 {
67 auto tmp = std::make_unique<Widget>(window, this, std::forward<Args>(args)...);
68 return static_cast<Widget&>(add_widget(column_first, row_first, column_last, row_last, std::move(tmp)));
69 }
70
79 template<typename Widget, typename... Args>
80 Widget& make_widget(std::size_t column, std::size_t row, Args&&...args)
81 {
82 auto tmp = std::make_unique<Widget>(window, this, std::forward<Args>(args)...);
83 return static_cast<Widget&>(add_widget(column, row, column + 1, row + 1, std::move(tmp)));
84 }
85
94 template<typename Widget, typename... Args>
95 Widget& make_widget(std::string_view address, Args&&...args)
96 {
97 hilet[column_first, row_first, column_last, row_last] = parse_spreadsheet_range(address);
98 return make_widget<Widget>(column_first, row_first, column_last, row_last, std::forward<Args>(args)...);
99 }
100
102 [[nodiscard]] generator<widget *> children() const noexcept override
103 {
104 for (hilet& cell : _cells) {
105 co_yield cell.widget.get();
106 }
107 }
108
109 widget_constraints const& set_constraints() noexcept override;
110 void set_layout(widget_layout const& layout) noexcept override;
111 void draw(draw_context const& context) noexcept override;
112 [[nodiscard]] hitbox hitbox_test(point3 position) const noexcept override;
114private:
115 struct cell_type {
116 std::size_t column_first;
117 std::size_t row_first;
118 std::size_t column_last;
119 std::size_t row_last;
121
122 cell_type(
123 std::size_t column_first,
124 std::size_t row_first,
125 std::size_t column_last,
126 std::size_t row_last,
128 column_first(column_first),
129 row_first(row_first),
130 column_last(column_last),
131 row_last(row_last),
133 {
134 }
135
136 [[nodiscard]] aarectangle
137 rectangle(grid_layout const& columns, grid_layout const& rows, float container_height) const noexcept
138 {
139 hilet[x0, x3] = columns.get_positions(column_first, column_last);
140 hilet[y0, y3] = rows.get_positions(row_first, row_last);
141
142 return {point2{x0, container_height - y3}, point2{x3, container_height - y0}};
143 }
144
145 [[nodiscard]] widget_baseline baseline(grid_layout const& rows) const noexcept
146 {
147 return rows.get_baseline(row_first, row_last);
148 }
149 };
150
152
153 grid_layout _rows;
154 grid_layout _columns;
155
156 [[nodiscard]] bool
157 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;
158
159 /* Add a widget to the grid.
160 */
161 widget& add_widget(
162 std::size_t column_first,
163 std::size_t row_first,
164 std::size_t column_last,
165 std::size_t row_last,
166 std::unique_ptr<widget> child_widget) noexcept;
167};
168
169}} // 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.
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:80
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:65
Widget & make_widget(std::string_view address, Args &&...args)
Add a widget directly to this grid-widget.
Definition grid_widget.hpp:95
grid_widget(gui_window &window, widget *parent) noexcept
Constructs an empty grid widget.
An interactive graphical object as part of the user-interface.
Definition widget.hpp:44
widget_layout const & layout() const noexcept
Get the current layout for this widget.
Definition widget.hpp:198
widget *const parent
Pointer to the parent widget.
Definition widget.hpp:53
widget(gui_window &window, widget *parent) noexcept
gui_window & window
Convenient reference to the Window.
Definition widget.hpp:48
T move(T... args)