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 "widget.hpp"
12#include "../macros.hpp"
13
14namespace hi { inline namespace v1 {
15
39class overlay_widget final : public widget {
40public:
41 using super = widget;
42
44
50 {
51 if (parent) {
52 // The overlay-widget will reset the semantic_layer as it is the bottom
53 // layer of this virtual-window. However the draw-layer should be above
54 // any other widget drawn.
56 }
57 }
58
59 void set_widget(std::unique_ptr<widget> new_widget) noexcept
60 {
61 _content = std::move(new_widget);
62 ++global_counter<"overlay_widget:set_widget:constrain">;
64 }
65
75 template<typename Widget, typename... Args>
76 Widget& make_widget(Args&&...args) noexcept
77 {
78 hi_axiom(loop::main().on_thread());
79 hi_assert(_content == nullptr);
80
81 auto tmp = std::make_unique<Widget>(this, std::forward<Args>(args)...);
82 auto& ref = *tmp;
83 set_widget(std::move(tmp));
84 return ref;
85 }
86
88 [[nodiscard]] generator<widget_intf &> children(bool include_invisible) noexcept override
89 {
90 co_yield *_content;
91 }
92
93 [[nodiscard]] box_constraints update_constraints() noexcept override
94 {
95 _layout = {};
96 _content_constraints = _content->update_constraints();
97 return _content_constraints;
98 }
99 void set_layout(widget_layout const& context) noexcept override
100 {
101 _layout = context;
102
103 // The clipping rectangle of the overlay matches the rectangle exactly, with a border around it.
104 _layout.clipping_rectangle = context.rectangle() + theme().border_width();
105
106 hilet content_rectangle = context.rectangle();
107 _content_shape = box_shape{_content_constraints, content_rectangle, theme().baseline_adjustment()};
108
109 // The content should not draw in the border of the overlay, so give a tight clipping rectangle.
110 _content->set_layout(_layout.transform(_content_shape, 1.0f, context.rectangle()));
111 }
112 void draw(draw_context const& context) noexcept override
113 {
115 if (overlaps(context, layout())) {
116 draw_background(context);
117 }
118 _content->draw(context);
119 }
120 }
121 [[nodiscard]] color background_color() const noexcept override
122 {
123 return theme().color(semantic_color::fill, semantic_layer + 1);
124 }
125 [[nodiscard]] color foreground_color() const noexcept override
126 {
127 return theme().color(semantic_color::border, semantic_layer + 1);
128 }
129 void scroll_to_show(hi::aarectangle rectangle) noexcept override
130 {
131 // An overlay is in an absolute position on the window,
132 // so do not forward the scroll_to_show message to its parent.
133 }
134 [[nodiscard]] hitbox hitbox_test(point2 position) const noexcept override
135 {
136 hi_axiom(loop::main().on_thread());
137
138 if (*mode >= widget_mode::partial) {
139 return _content->hitbox_test_from_parent(position);
140 } else {
141 return {};
142 }
143 }
145private:
147 box_constraints _content_constraints;
148 box_shape _content_shape;
149
150 void draw_background(draw_context const& context) noexcept
151 {
152 context.draw_box(
153 layout(), layout().rectangle(), background_color(), foreground_color(), theme().border_width(), border_side::outside);
154 }
155};
156
157}} // namespace hi::v1
Defines widget.
@ 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:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
@ outside
The border is drawn outside the edge of a quad.
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:29
widget_intf * parent
Pointer to the parent widget.
Definition widget_intf.hpp:28
2D constraints.
Definition box_constraints.hpp:25
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:49
Widget & make_widget(Args &&...args) noexcept
Add a content widget directly to this overlay widget.
Definition overlay_widget.hpp:76
An interactive graphical object as part of the user-interface.
Definition widget.hpp:37
widget_layout const & layout() const noexcept override
Get the current layout for this widget.
Definition widget.hpp:169
int semantic_layer
The draw layer of the widget.
Definition widget.hpp:66
void scroll_to_show() noexcept
Scroll to show the important part of the widget.
Definition widget_intf.hpp:196
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)