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 "../layout/module.hpp"
13#include "../macros.hpp"
14#include <memory>
15
16namespace hi { inline namespace v1 {
17
40class grid_widget : public widget {
41public:
42 using super = widget;
43
44 ~grid_widget() {}
45
51 {
52 hi_axiom(loop::main().on_thread());
53
54 if (parent) {
55 semantic_layer = parent->semantic_layer;
56 }
57 }
58
69 template<typename Widget, typename... Args>
70 Widget&
71 make_widget(std::size_t first_column, std::size_t first_row, std::size_t last_column, std::size_t last_row, Args&&...args)
72 {
73 hi_axiom(first_column < last_column);
74 hi_axiom(first_row < last_row);
75 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
76 return static_cast<Widget&>(add_widget(first_column, first_row, last_column, last_row, std::move(tmp)));
77 }
78
87 template<typename Widget, typename... Args>
89 {
90 return make_widget<Widget>(column, row, column + 1, row + 1, std::forward<Args>(args)...);
91 }
92
101 template<typename Widget, typename... Args>
102 Widget& make_widget(std::string_view address, Args&&...args)
103 {
104 hilet[column_first, row_first, column_last, row_last] = parse_spreadsheet_range(address);
105 return make_widget<Widget>(column_first, row_first, column_last, row_last, std::forward<Args>(args)...);
106 }
107
109 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
110 {
111 for (hilet& cell : _grid) {
112 co_yield *cell.value;
113 }
114 }
115
116 [[nodiscard]] box_constraints update_constraints() noexcept override
117 {
118 _layout = {};
119
120 for (auto& cell : _grid) {
121 cell.set_constraints(cell.value->update_constraints());
122 }
123
124 return _grid.constraints(os_settings::left_to_right());
125 }
126 void set_layout(widget_layout const& context) noexcept override
127 {
128 if (compare_store(_layout, context)) {
129 _grid.set_layout(context.shape, theme().baseline_adjustment());
130 }
131
132 for (hilet& cell : _grid) {
133 cell.value->set_layout(context.transform(cell.shape, 0.0f));
134 }
135 }
136 void draw(draw_context const& context) noexcept override
137 {
139 for (hilet& cell : _grid) {
140 cell.value->draw(context);
141 }
142 }
143 }
144 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
145 {
146 hi_axiom(loop::main().on_thread());
147
148 if (*mode >= widget_mode::partial) {
149 auto r = hitbox{};
150 for (hilet& cell : _grid) {
151 r = cell.value->hitbox_test_from_parent(position, r);
152 }
153 return r;
154 } else {
155 return {};
156 }
157 }
159private:
161
162 /* Add a widget to the grid.
163 */
164 widget& add_widget(
165 std::size_t first_column,
166 std::size_t first_row,
167 std::size_t last_column,
168 std::size_t last_row,
170 {
171 hi_axiom(loop::main().on_thread());
172 hi_axiom(first_column < last_column);
173 hi_axiom(first_row < last_row);
174
175 if (_grid.cell_in_use(first_column, first_row, last_column, last_row)) {
176 hi_log_fatal("cell ({},{}) of grid_widget is already in use", first_column, first_row);
177 }
178
179 auto& ref = *widget;
180 _grid.add_cell(first_column, first_row, last_column, last_row, std::move(widget));
181 hi_log_info("grid_widget::add_widget({}, {}, {}, {})", first_column, first_row, last_column, last_row);
182
183 ++global_counter<"grid_widget:add_widget:constrain">;
185 return ref;
186 }
187};
188
189}} // namespace hi::v1
Defines widget.
@ window_reconstrain
Request that widget get constraint on the next frame.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition misc.hpp:56
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
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 first_column, std::size_t first_row, std::size_t last_column, std::size_t last_row, Args &&...args)
Add a widget directly to this grid-widget.
Definition grid_widget.hpp:71
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:88
Widget & make_widget(std::string_view address, Args &&...args)
Add a widget directly to this grid-widget.
Definition grid_widget.hpp:102
grid_widget(widget *parent) noexcept
Constructs an empty grid widget.
Definition grid_widget.hpp:50
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:66
widget(widget *parent) noexcept
Definition widget.hpp:87
bool process_event(gui_event const &event) const noexcept override
Send a event to the window.
Definition widget.hpp:178
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:42
T move(T... args)
T ref(T... args)