HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
overlay_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2021.
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 "../GUI/module.hpp"
12
13namespace hi { inline namespace v1 {
14
38template<fixed_string Name = "">
39class overlay_widget final : public widget {
40public:
41 using super = widget;
42 constexpr static auto prefix = Name / "overlay";
43
45
51 {
52 if (parent) {
53 // The overlay-widget will reset the semantic_layer as it is the bottom
54 // layer of this virtual-window. However the draw-layer should be above
55 // any other widget drawn.
57 }
58 }
59
60 void set_widget(std::unique_ptr<widget> new_widget) noexcept
61 {
62 _content = std::move(new_widget);
63 ++global_counter<"overlay_widget:set_widget:constrain">;
65 }
66
76 template<typename Widget, typename... Args>
77 Widget& make_widget(Args&&...args) noexcept
78 {
79 hi_axiom(loop::main().on_thread());
80 hi_assert(_content == nullptr);
81
82 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
83 auto& ref = *tmp;
84 set_widget(std::move(tmp));
85 return ref;
86 }
87
89 [[nodiscard]] generator<widget const&> children(bool include_invisible) const noexcept override
90 {
91 co_yield *_content;
92 }
93
94 [[nodiscard]] box_constraints update_constraints() noexcept override
95 {
96 _content_constraints = _content->update_constraints();
97 return _content_constraints;
98 }
99
100 void set_layout(widget_layout const& context) noexcept override
101 {
102 layout = context;
103
104 // The clipping rectangle of the overlay matches the rectangle exactly, with a border around it.
105 layout.clipping_rectangle = context.rectangle() + theme<prefix>.border_width(this);
106
107 hilet content_rectangle = context.rectangle();
108 _content_shape = box_shape{_content_constraints, content_rectangle, theme<prefix>.cap_height(this)};
109
110 // The content should not draw in the border of the overlay, so give a tight clipping rectangle.
111 _content->set_layout(layout.transform(_content_shape, 1.0f, context.rectangle()));
112 }
113
114 void draw(widget_draw_context& context) noexcept override
115 {
117 if (overlaps(context, layout)) {
118 draw_background(context);
119 }
120 _content->draw(context);
121 }
122 }
123
124 void scroll_to_show(hi::aarectanglei rectangle) noexcept override
125 {
126 // An overlay is in an absolute position on the window,
127 // so do not forward the scroll_to_show message to its parent.
128 }
129
130 [[nodiscard]] hitbox hitbox_test(point2i position) const noexcept override
131 {
132 hi_axiom(loop::main().on_thread());
133
134 if (*mode >= widget_mode::partial) {
135 return _content->hitbox_test_from_parent(position);
136 } else {
137 return {};
138 }
139 }
141private:
143 box_constraints _content_constraints;
144 box_shape _content_shape;
145
146 void draw_background(widget_draw_context& context) noexcept
147 {
148 context.draw_box(
149 layout,
150 layout.rectangle(),
151 theme<prefix>.background_color(this),
152 theme<prefix>.border_color(this),
153 theme<prefix>.border_width(this),
155 }
156};
157
158}} // namespace hi::v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ window_reconstrain
Request that widget get constraint on the next frame.
@ rectangle
The gui_event has rectangle data.
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
@ outside
The border is drawn outside the edge of a quad.
Definition widget.hpp:26
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
size_t semantic_layer
The draw layer of the widget.
Definition widget.hpp:81
constexpr widget_layout transform(box_shape const &child_shape, float child_elevation, aarectanglei new_clipping_rectangle) const noexcept
Create a new widget_layout for the child widget.
Definition widget_layout.hpp:203
aarectanglei clipping_rectangle
The clipping rectangle.
Definition widget_layout.hpp:86
2D constraints.
Definition box_constraints.hpp:22
A GUI widget which may exist anywhere on a window overlaid above any other widget.
Definition overlay_widget.hpp:39
overlay_widget(widget *parent) noexcept
Constructs an empty overlay widget.
Definition overlay_widget.hpp:50
Widget & make_widget(Args &&...args) noexcept
Add a content widget directly to this overlay widget.
Definition overlay_widget.hpp:77
T move(T... args)