HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
widget_draw_context.hpp
1// Copyright Take Vos 2020-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
5#pragma once
6
7#include "widget_layout.hpp"
8#include "../GFX/module.hpp"
9#include "../geometry/module.hpp"
10#include "../unicode/module.hpp"
11#include "../text/module.hpp"
12#include "../font/module.hpp"
13#include "../color/module.hpp"
14#include "../utility/module.hpp"
15#include "../vector_span.hpp"
16
17namespace hi { inline namespace v1 {
18struct paged_image;
19
22enum class border_side {
25 on,
26
29 inside,
30
34};
35
36template<typename Context>
38 std::same_as<Context, quad_color> or std::same_as<Context, color> or std::same_as<Context, border_side> or
39 std::same_as<Context, line_end_cap> or std::same_as<Context, corner_radii> or
40 std::same_as<Context, aarectangle> or std::same_as<Context, float>;
41
45 unsigned char num_colors = 0;
46 unsigned char num_line_caps = 0;
47
54
60
63 float line_width = 0.0f;
64
67 hi::border_side border_side = hi::border_side::on;
68
72
79
83
87
88 constexpr draw_attributes(draw_attributes const&) noexcept = default;
89 constexpr draw_attributes(draw_attributes&&) noexcept = default;
90 constexpr draw_attributes& operator=(draw_attributes const&) noexcept = default;
91 constexpr draw_attributes& operator=(draw_attributes&&) noexcept = default;
92 constexpr draw_attributes() noexcept = default;
93
114 template<draw_attribute... Args>
115 constexpr draw_attributes(Args const&...args) noexcept
116 {
117 add(args...);
118 }
119
120 constexpr void add() noexcept {}
121
122 template<draw_attribute T>
123 constexpr void add(T const& attribute) noexcept
124 {
125 if constexpr (std::is_same_v<T, quad_color>) {
126 if (num_colors++ == 0) {
127 fill_color = attribute;
128 } else {
129 line_color = attribute;
130 }
131 hi_axiom(num_colors <= 2);
132
133 } else if constexpr (std::is_same_v<T, color>) {
134 if (num_colors++ == 0) {
135 fill_color = quad_color{attribute};
136 } else {
137 line_color = quad_color{attribute};
138 }
139 // XXX Handle 4 colors.
140 // hi_axiom(num_colors <= 2);
141
142 } else if constexpr (std::is_same_v<T, line_end_cap>) {
143 if (num_line_caps++ == 0) {
144 begin_line_cap = attribute;
145 end_line_cap = attribute;
146 } else {
147 end_line_cap = attribute;
148 }
149 hi_axiom(num_line_caps <= 2);
150
151 } else if constexpr (std::is_same_v<T, hi::border_side>) {
152 border_side = attribute;
153#ifndef NDEBUG
154 hi_assert(not _has_border_side);
155 _has_border_side = true;
156#endif
157
158 } else if constexpr (std::is_same_v<T, corner_radii>) {
159 border_radius = attribute;
160#ifndef NDEBUG
161 hi_assert(not _has_corner_radii);
162 _has_corner_radii = true;
163#endif
164
165 } else if constexpr (std::is_same_v<T, aarectangle>) {
166 clipping_rectangle = attribute;
167#ifndef NDEBUG
168 hi_assert(not _has_clipping_rectangle);
169 _has_clipping_rectangle = true;
170#endif
171
172 } else if constexpr (std::is_same_v<T, float>) {
173 line_width = attribute;
174#ifndef NDEBUG
175 hi_assert(not _has_line_width);
176 _has_line_width = true;
177#endif
178 } else {
180 }
181 }
182
183 template<draw_attribute First, draw_attribute Second, draw_attribute... Rest>
184 constexpr void add(First const& first, Second const& second, Rest const&...rest) noexcept
185 {
186 add(first);
187 add(second, rest...);
188 }
189
190private:
191#ifndef NDEBUG
192 bool _has_border_side = false;
193 bool _has_corner_radii = false;
194 bool _has_clipping_rectangle = false;
195 bool _has_line_width = false;
196#endif
197};
198
199template<typename Context>
200concept draw_quad_shape = std::same_as<Context, quad> or std::same_as<Context, rectangle> or std::same_as<Context, aarectangle>;
201
205public:
206 gfx_draw_context gfx_context;
207
210 utc_nanoseconds display_time_point;
211
212 widget_draw_context(widget_draw_context& rhs) noexcept = delete;
213 widget_draw_context(widget_draw_context&& rhs) noexcept = default;
214 widget_draw_context& operator=(widget_draw_context& rhs) noexcept = delete;
215 widget_draw_context& operator=(widget_draw_context&& rhs) noexcept = default;
216 ~widget_draw_context() = default;
217
218 widget_draw_context(gfx_draw_context&& gfx_context) noexcept : gfx_context(std::move(gfx_context)) {}
219
226 void draw_box(widget_layout const& layout, quad const& box, draw_attributes const& attributes) noexcept
227 {
228 return _draw_box(
229 layout.clipping_rectangle_on_window(attributes.clipping_rectangle), layout.to_window3() * box, attributes);
230 }
231
238 template<draw_quad_shape Shape, draw_attribute... Attributes>
239 void draw_box(widget_layout const& layout, Shape const& shape, Attributes const&...attributes) noexcept
240 {
241 return draw_box(layout, make_quad(shape), draw_attributes{attributes...});
242 }
243
250 void draw_line(widget_layout const& layout, line_segment const& line, draw_attributes const& attributes) noexcept
251 {
252 hilet box = make_rectangle(line, attributes.line_width, attributes.begin_line_cap, attributes.end_line_cap);
253
254 auto box_attributes = attributes;
255 box_attributes.line_width = 0.0f;
256 box_attributes.border_radius =
257 make_corner_radii(attributes.line_width, attributes.begin_line_cap, attributes.end_line_cap);
258 return draw_box(layout, box, box_attributes);
259 }
260
267 template<draw_attribute... Attributes>
268 void draw_line(widget_layout const& layout, line_segment const& line, Attributes const&...attributes) noexcept
269 {
270 return draw_line(layout, line, draw_attributes{attributes...});
271 }
272
279 void draw_circle(widget_layout const& layout, hi::circle const& circle, draw_attributes const& attributes) noexcept
280 {
281 auto box_attributes = attributes;
282 box_attributes.border_radius = make_corner_radii(circle);
283 return draw_box(layout, make_rectangle(circle), box_attributes);
284 }
285
292 template<draw_attribute... Attributes>
293 void draw_circle(widget_layout const& layout, hi::circle const& circle, Attributes const&...attributes) noexcept
294 {
295 return draw_circle(layout, circle, draw_attributes{attributes...});
296 }
297
307 [[nodiscard]] bool
308 draw_image(widget_layout const& layout, quad const& box, paged_image& image, draw_attributes const& attributes) noexcept
309 {
310 return _draw_image(layout.clipping_rectangle_on_window(attributes.clipping_rectangle), layout.to_window3() * box, image);
311 }
312
322 template<draw_attribute... Attributes>
323 [[nodiscard]] bool
324 draw_image(widget_layout const& layout, draw_quad_shape auto const& box, paged_image& image, Attributes const&...attributes)
325 noexcept
326 {
327 return draw_image(layout, make_quad(box), image, draw_attributes{attributes...});
328 }
329
338 widget_layout const& layout,
339 quad const& box,
340 hi::font const& font,
341 glyph_id glyph,
342 draw_attributes const& attributes) noexcept
343 {
344 return _draw_glyph(
345 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
346 layout.to_window3() * box,
347 font,
348 glyph,
349 attributes);
350 }
351
359 template<draw_quad_shape Shape, draw_attribute... Attributes>
361 widget_layout const& layout,
362 Shape const& box,
363 hi::font const& font,
364 glyph_id glyph,
365 Attributes const&...attributes) noexcept
366 {
367 return draw_glyph(layout, make_quad(box), font, glyph, draw_attributes{attributes...});
368 }
369
378 widget_layout const& layout,
379 quad const& box,
380 font_book::font_glyph_type font_glyph,
381 draw_attributes const& attributes) noexcept
382 {
383 return _draw_glyph(
384 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
385 layout.to_window3() * box,
386 *font_glyph.font,
387 font_glyph.glyph,
388 attributes);
389 }
390
398 template<draw_quad_shape Shape, draw_attribute... Attributes>
400 widget_layout const& layout,
401 Shape const& box,
402 font_book::font_glyph_type font_glyph,
403 Attributes const&...attributes) noexcept
404 {
405 return draw_glyph(layout, make_quad(box), font_glyph, draw_attributes{attributes...});
406 }
407
415 void
416 draw_text(widget_layout const& layout, matrix3 const& transform, text_shaper const& text, draw_attributes const& attributes)
417 noexcept
418 {
419 return _draw_text(
420 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
421 layout.to_window3() * transform,
422 text,
423 attributes);
424 }
425
433 template<draw_attribute... Attributes>
434 void draw_text(widget_layout const& layout, matrix3 const& transform, text_shaper const& text, Attributes const&...attributes)
435 noexcept
436 {
437 return draw_text(layout, transform, text, draw_attributes{attributes...});
438 }
439
446 template<draw_attribute... Attributes>
447 void draw_text(widget_layout const& layout, text_shaper const& text, Attributes const&...attributes) noexcept
448 {
449 return draw_text(layout, geo::identity{}, text, draw_attributes{attributes...});
450 }
451
460 widget_layout const& layout,
461 text_shaper const& text,
462 text_selection const& selection,
463 draw_attributes const& attributes) noexcept
464 {
465 return _draw_text_selection(
466 layout.clipping_rectangle_on_window(attributes.clipping_rectangle), layout.to_window3(), text, selection, attributes);
467 }
468
476 template<draw_attribute... Attributes>
478 widget_layout const& layout,
479 text_shaper const& text,
480 text_selection const& selection,
481 Attributes const&...attributes) noexcept
482 {
483 return draw_text_selection(layout, text, selection, draw_attributes{attributes...});
484 }
485
496 widget_layout const& layout,
497 text_shaper const& text,
498 text_cursor cursor,
499 bool overwrite_mode,
500 bool dead_character_mode,
501 draw_attributes const& attributes) noexcept
502 {
503 return _draw_text_cursors(
504 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
505 layout.to_window3(),
506 text,
507 cursor,
508 overwrite_mode,
509 dead_character_mode,
510 attributes);
511 }
512
522 template<draw_attribute... Attributes>
524 widget_layout const& layout,
525 text_shaper const& text,
526 text_cursor cursor,
527 bool overwrite_mode,
528 bool dead_character_mode,
529 Attributes const&...attributes) noexcept
530 {
531 return draw_text_cursors(layout, text, cursor, overwrite_mode, dead_character_mode, draw_attributes{attributes...});
532 }
533
543 void draw_hole(widget_layout const& layout, quad const& box, draw_attributes const& attributes) noexcept
544 {
545 return _override_alpha(
546 layout.clipping_rectangle_on_window(attributes.clipping_rectangle), layout.to_window3() * box, attributes);
547 }
548
558 template<draw_quad_shape Shape, draw_attribute... Attributes>
559 void draw_hole(widget_layout const& layout, Shape const& box, Attributes const&...attributes) noexcept
560 {
561 return draw_hole(layout, make_quad(box), draw_attributes{attributes...});
562 }
563
571 [[nodiscard]] friend bool overlaps(widget_draw_context const& context, widget_layout const& layout) noexcept
572 {
573 return overlaps(context.gfx_context.scissor_rectangle, layout.clipping_rectangle_on_window());
574 }
575
576private:
577 template<draw_quad_shape Shape>
578 [[nodiscard]] constexpr static quad make_quad(Shape const& shape) noexcept
579 {
580 if constexpr (std::is_same_v<Shape, aarectangle>) {
581 return narrow_cast<aarectangle>(shape);
582 } else {
583 return shape;
584 }
585 }
586
587 [[nodiscard]] constexpr static rectangle
588 make_rectangle(line_segment const& line, float width, line_end_cap c1, line_end_cap c2) noexcept
589 {
590 auto right = line.direction();
591
592 hilet radius = width * 0.5f;
593 hilet n = normal(right, 0.0f);
594 hilet up = n * width;
595 hilet t = normalize(right);
596
597 auto origin = line.origin() - n * radius;
598
599 // Extend the line by the radius for rounded end-caps.
600 hilet radius_offset = t * radius;
601 if (c1 == line_end_cap::round) {
602 origin -= radius_offset;
603 right += radius_offset;
604 }
605 if (c2 == line_end_cap::round) {
606 right += radius_offset;
607 }
608
609 return rectangle{origin, right, up};
610 }
611
612 [[nodiscard]] constexpr static rectangle make_rectangle(hi::circle const& circle) noexcept
613 {
614 hilet circle_ = f32x4{circle};
615 hilet origin = point3{circle_.xyz1() - circle_.ww00()};
616 hilet right = vector3{circle_.w000() * 2.0f};
617 hilet up = vector3{circle_._0w00() * 2.0f};
618 return rectangle{origin, right, up};
619 }
620
621 [[nodiscard]] constexpr static corner_radii make_corner_radii(float width, line_end_cap c1, line_end_cap c2) noexcept
622 {
623 auto r = f32x4::broadcast(width * 0.5f);
624
625 if (c1 == line_end_cap::flat) {
626 r = set_zero<0b0101>(r);
627 }
628 if (c2 == line_end_cap::flat) {
629 r = set_zero<0b1010>(r);
630 }
631
632 return corner_radii{r};
633 }
634
635 [[nodiscard]] constexpr static corner_radii make_corner_radii(hi::circle const& circle) noexcept
636 {
637 return corner_radii{f32x4{circle}.wwww()};
638 }
639
640 void _override_alpha(aarectangle const& clipping_rectangle, quad box, draw_attributes const& attributes) noexcept;
641
642 void _draw_box(aarectangle const& clipping_rectangle, quad box, draw_attributes const& attributes) noexcept;
643
644 void _draw_text(
645 aarectangle const& clipping_rectangle,
646 matrix3 const& transform,
647 text_shaper const& text,
648 draw_attributes const& attributes) noexcept;
649
650 void _draw_text_selection(
651 aarectangle const& clipping_rectangle,
652 matrix3 const& transform,
653 text_shaper const& text,
654 text_selection const& selection,
655 draw_attributes const& attributes) noexcept;
656
657 void _draw_text_insertion_cursor_empty(
658 aarectangle const& clipping_rectangle,
659 matrix3 const& transform,
660 text_shaper const& text,
661 draw_attributes const& attributes) noexcept;
662
663 void _draw_text_insertion_cursor(
664 aarectangle const& clipping_rectangle,
665 matrix3 const& transform,
666 text_shaper const& text,
667 text_cursor cursor,
668 bool show_flag,
669 draw_attributes const& attributes) noexcept;
670
671 void _draw_text_overwrite_cursor(
672 aarectangle const& clipping_rectangle,
673 matrix3 const& transform,
674 text_shaper::char_const_iterator it,
675 draw_attributes const& attributes) noexcept;
676
677 void _draw_text_cursors(
678 aarectangle const& clipping_rectangle,
679 matrix3 const& transform,
680 text_shaper const& text,
681 text_cursor cursor,
682 bool overwrite_mode,
683 bool dead_character_mode,
684 draw_attributes const& attributes) noexcept;
685
686 void _draw_glyph(
687 aarectangle const& clipping_rectangle,
688 quad const& box,
689 hi::font const& font,
690 glyph_id const& glyph,
691 draw_attributes const& attributes) noexcept;
692
693 [[nodiscard]] bool
694 _draw_image(aarectangle const& clipping_rectangle, quad const& box, paged_image const& image) noexcept;
695};
696
697}} // namespace hi::v1
#define hi_static_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:323
#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
line_end_cap
The way two lines should be joined.
Definition line_end_cap.hpp:17
@ flat
The end cap of the line is flat.
@ round
The end cap of the line is round.
@ rectangle
The gui_event has rectangle data.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
border_side
The side where the border is drawn.
Definition widget_draw_context.hpp:22
@ inside
The border is drawn inside the edge of a quad.
@ outside
The border is drawn outside the edge of a quad.
@ on
The border is drawn on the edge of a quad.
geo::matrix< 3 > matrix3
A 3D homogenious transformation matrix.
Definition matrix.hpp:614
@ normal
A font that is normal, non-italic.
A color for each corner of a quad.
Definition quad_color.hpp:19
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:27
static constexpr axis_aligned_rectangle large() noexcept
Create a large axis aligned rectangle.
Definition axis_aligned_rectangle.hpp:40
A type defining a 2D circle.
Definition circle.hpp:18
Identity transform.
Definition identity.hpp:20
Line segment.
Definition line_segment.hpp:19
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix.hpp:33
Definition gfx_draw_context.hpp:17
The draw attributes used to draw shaped into the draw context.
Definition widget_draw_context.hpp:44
aarectangle clipping_rectangle
The rectangle used the clip the shape when drawing.
Definition widget_draw_context.hpp:78
hi::border_side border_side
The side on which side of the edge of a shape the border should be drawn.
Definition widget_draw_context.hpp:67
float line_width
The width of a line, or the width of a border.
Definition widget_draw_context.hpp:63
line_end_cap end_line_cap
The shape of the beginning of a line.
Definition widget_draw_context.hpp:86
quad_color fill_color
The fill color used for the color of a box inside the border.
Definition widget_draw_context.hpp:53
quad_color line_color
The line color used for the color of the border of the box.
Definition widget_draw_context.hpp:59
line_end_cap begin_line_cap
The shape of the beginning of a line.
Definition widget_draw_context.hpp:82
hi::corner_radii border_radius
The radii of each corner of a quad.
Definition widget_draw_context.hpp:71
Draw context for drawing using the HikoGUI shaders.
Definition widget_draw_context.hpp:204
void draw_text_selection(widget_layout const &layout, text_shaper const &text, text_selection const &selection, draw_attributes const &attributes) noexcept
Draw text-selection of shaped text.
Definition widget_draw_context.hpp:459
void draw_hole(widget_layout const &layout, Shape const &box, Attributes const &...attributes) noexcept
Make a hole in the user interface.
Definition widget_draw_context.hpp:559
bool draw_image(widget_layout const &layout, draw_quad_shape auto const &box, paged_image &image, Attributes const &...attributes) noexcept
Draw an image.
Definition widget_draw_context.hpp:324
void draw_circle(widget_layout const &layout, hi::circle const &circle, draw_attributes const &attributes) noexcept
Draw a circle.
Definition widget_draw_context.hpp:279
void draw_circle(widget_layout const &layout, hi::circle const &circle, Attributes const &...attributes) noexcept
Draw a circle.
Definition widget_draw_context.hpp:293
void draw_line(widget_layout const &layout, line_segment const &line, Attributes const &...attributes) noexcept
Draw a line.
Definition widget_draw_context.hpp:268
void draw_text(widget_layout const &layout, text_shaper const &text, Attributes const &...attributes) noexcept
Draw shaped text.
Definition widget_draw_context.hpp:447
void draw_glyph(widget_layout const &layout, quad const &box, hi::font const &font, glyph_id glyph, draw_attributes const &attributes) noexcept
Draw a glyph.
Definition widget_draw_context.hpp:337
friend bool overlaps(widget_draw_context const &context, widget_layout const &layout) noexcept
Checks if a widget's layout overlaps with the part of the window that is being drawn.
Definition widget_draw_context.hpp:571
void draw_text(widget_layout const &layout, matrix3 const &transform, text_shaper const &text, Attributes const &...attributes) noexcept
Draw shaped text.
Definition widget_draw_context.hpp:434
void draw_text(widget_layout const &layout, matrix3 const &transform, text_shaper const &text, draw_attributes const &attributes) noexcept
Draw shaped text.
Definition widget_draw_context.hpp:416
void draw_text_cursors(widget_layout const &layout, text_shaper const &text, text_cursor cursor, bool overwrite_mode, bool dead_character_mode, draw_attributes const &attributes) noexcept
Draw text cursors of shaped text.
Definition widget_draw_context.hpp:495
void draw_box(widget_layout const &layout, Shape const &shape, Attributes const &...attributes) noexcept
Draw a box.
Definition widget_draw_context.hpp:239
bool draw_image(widget_layout const &layout, quad const &box, paged_image &image, draw_attributes const &attributes) noexcept
Draw an image.
Definition widget_draw_context.hpp:308
void draw_text_selection(widget_layout const &layout, text_shaper const &text, text_selection const &selection, Attributes const &...attributes) noexcept
Draw text-selection of shaped text.
Definition widget_draw_context.hpp:477
void draw_glyph(widget_layout const &layout, quad const &box, font_book::font_glyph_type font_glyph, draw_attributes const &attributes) noexcept
Draw a glyph.
Definition widget_draw_context.hpp:377
void draw_box(widget_layout const &layout, quad const &box, draw_attributes const &attributes) noexcept
Draw a box.
Definition widget_draw_context.hpp:226
void draw_glyph(widget_layout const &layout, Shape const &box, hi::font const &font, glyph_id glyph, Attributes const &...attributes) noexcept
Draw a glyph.
Definition widget_draw_context.hpp:360
void draw_text_cursors(widget_layout const &layout, text_shaper const &text, text_cursor cursor, bool overwrite_mode, bool dead_character_mode, Attributes const &...attributes) noexcept
Draw text cursors of shaped text.
Definition widget_draw_context.hpp:523
void draw_line(widget_layout const &layout, line_segment const &line, draw_attributes const &attributes) noexcept
Draw a line.
Definition widget_draw_context.hpp:250
void draw_hole(widget_layout const &layout, quad const &box, draw_attributes const &attributes) noexcept
Make a hole in the user interface.
Definition widget_draw_context.hpp:543
utc_nanoseconds display_time_point
The time when the drawing will appear on the screen.
Definition widget_draw_context.hpp:210
void draw_glyph(widget_layout const &layout, Shape const &box, font_book::font_glyph_type font_glyph, Attributes const &...attributes) noexcept
Draw a glyph.
Definition widget_draw_context.hpp:399
The layout of a widget.
Definition widget_layout.hpp:37
Definition widget_draw_context.hpp:37
Definition widget_draw_context.hpp:200
T right(T... args)
T move(T... args)