7#include "gfx_pipeline_box_vulkan_intf.hpp"
8#include "gfx_pipeline_image_vulkan_intf.hpp"
9#include "gfx_pipeline_SDF_vulkan_intf.hpp"
10#include "gfx_pipeline_override_vulkan_intf.hpp"
11#include "../settings/settings.hpp"
12#include "../geometry/geometry.hpp"
13#include "../unicode/unicode.hpp"
14#include "../text/text.hpp"
16#include "../container/container.hpp"
17#include "../utility/utility.hpp"
18#include "../macros.hpp"
20hi_export_module(hikogui.GFX : draw_context_intf);
22hi_export
namespace hi {
inline namespace v1 {
42template<
typename Context>
43concept draw_attribute = std::same_as<Context, quad_color> or std::same_as<Context, color> or
44 std::same_as<Context, border_side> or std::same_as<Context, line_end_cap> or std::same_as<Context, corner_radii> or
45 std::same_as<Context, aarectangle> or std::same_as<Context, float> or std::same_as<Context, int>;
50 unsigned char num_colors = 0;
51 unsigned char num_line_caps = 0;
93 constexpr draw_attributes(draw_attributes
const&)
noexcept =
default;
94 constexpr draw_attributes(draw_attributes&&) noexcept = default;
95 constexpr draw_attributes& operator=(draw_attributes const&) noexcept = default;
96 constexpr draw_attributes& operator=(draw_attributes&&) noexcept = default;
97 constexpr draw_attributes() noexcept = default;
120 constexpr draw_attributes(Args const&...args) noexcept
125 constexpr void add() noexcept {}
127 template<draw_attribute T>
128 constexpr void add(T
const& attribute)
noexcept
130 if constexpr (std::is_same_v<T, quad_color>) {
131 if (num_colors++ == 0) {
136 hi_axiom(num_colors <= 2);
138 }
else if constexpr (std::is_same_v<T, color>) {
139 if (num_colors++ == 0) {
144 hi_axiom(num_colors <= 2);
146 }
else if constexpr (std::is_same_v<T, line_end_cap>) {
147 if (num_line_caps++ == 0) {
153 hi_axiom(num_line_caps <= 2);
155 }
else if constexpr (std::is_same_v<T, hi::border_side>) {
158 hi_assert(not _has_border_side);
159 _has_border_side =
true;
162 }
else if constexpr (std::is_same_v<T, corner_radii>) {
165 hi_assert(not _has_corner_radii);
166 _has_corner_radii =
true;
169 }
else if constexpr (std::is_same_v<T, aarectangle>) {
172 hi_assert(not _has_clipping_rectangle);
173 _has_clipping_rectangle =
true;
176 }
else if constexpr (std::is_same_v<T, float> or std::is_same_v<T, int>) {
179 hi_assert(not _has_line_width);
180 _has_line_width =
true;
183 hi_static_no_default();
187 template<draw_attribute First, draw_attribute Second, draw_attribute... Rest>
188 constexpr void add(First
const& first, Second
const& second, Rest
const&...rest)
noexcept
191 add(second, rest...);
196 bool _has_border_side =
false;
197 bool _has_corner_radii =
false;
198 bool _has_clipping_rectangle =
false;
199 bool _has_line_width =
false;
203template<
typename Context>
204concept draw_quad_shape = std::same_as<Context, quad> or std::same_as<Context, rectangle> or std::same_as<Context, aarectangle> or
205 std::same_as<Context, aarectangle>;
233 draw_context(draw_context
const& rhs)
noexcept =
default;
234 draw_context(draw_context&& rhs)
noexcept =
default;
235 draw_context& operator=(draw_context
const& rhs)
noexcept =
default;
236 draw_context& operator=(draw_context&& rhs)
noexcept =
default;
237 ~draw_context() =
default;
241 vector_span<gfx_pipeline_box::vertex>& box_vertices,
242 vector_span<gfx_pipeline_image::vertex>& image_vertices,
243 vector_span<gfx_pipeline_SDF::vertex>& sdf_vertices,
244 vector_span<gfx_pipeline_override::vertex>& override_vertices)
noexcept;
248 operator bool() const noexcept
259 template<std::same_as<w
idget_layout> W
idgetLayout>
263 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
layout.to_window3() * box, attributes);
272 template<std::same_as<w
idget_layout> W
idgetLayout, draw_quad_shape Shape, draw_attribute... Attributes>
273 void draw_box(WidgetLayout
const&
layout, Shape
const& shape, Attributes
const&...attributes)
const noexcept
284 template<std::same_as<w
idget_layout> W
idgetLayout>
287 auto const box = make_rectangle(line, attributes.line_width, attributes.begin_line_cap, attributes.end_line_cap);
289 auto box_attributes = attributes;
290 box_attributes.line_width = 0.0f;
291 box_attributes.corner_radius =
292 make_corner_radii(attributes.line_width, attributes.begin_line_cap, attributes.end_line_cap);
302 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
314 template<std::same_as<w
idget_layout> W
idgetLayout>
317 auto box_attributes = attributes;
318 box_attributes.corner_radius = make_corner_radii(
circle);
328 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
343 template<std::same_as<w
idget_layout> W
idgetLayout>
347 return _draw_image(
layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
layout.to_window3() * box, image);
359 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
374 template<std::same_as<w
idget_layout> W
idgetLayout>
376 WidgetLayout
const&
layout,
383 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
layout.to_window3() * box,
font, glyph, attributes);
393 template<std::same_as<w
idget_layout> W
idgetLayout, draw_quad_shape Shape, draw_attribute... Attributes>
395 WidgetLayout
const&
layout,
399 Attributes
const&...attributes)
const noexcept
411 template<std::same_as<w
idget_layout> W
idgetLayout>
413 WidgetLayout
const&
layout,
420 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
layout.to_window3() * box, glyphs.font,
glyph_id, attributes);
431 template<std::same_as<w
idget_layout> W
idgetLayout, draw_quad_shape Shape, draw_attribute... Attributes>
433 WidgetLayout
const&
layout,
436 Attributes
const&...attributes)
const noexcept
450 template<std::same_as<w
idget_layout> W
idgetLayout>
456 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
457 layout.to_window3() * transform,
469 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
470 void draw_text(WidgetLayout
const&
layout,
matrix3 const& transform, text_shaper
const& text, Attributes
const&...attributes)
482 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
483 void draw_text(WidgetLayout
const&
layout, text_shaper
const& text, Attributes
const&...attributes)
const noexcept
495 template<std::same_as<w
idget_layout> W
idgetLayout>
497 WidgetLayout
const&
layout,
498 text_shaper
const& text,
499 text_selection
const& selection,
502 return _draw_text_selection(
503 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
layout.to_window3(), text, selection, attributes);
513 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
515 WidgetLayout
const&
layout,
516 text_shaper
const& text,
517 text_selection
const& selection,
518 Attributes
const&...attributes)
const noexcept
532 template<std::same_as<w
idget_layout> W
idgetLayout>
534 WidgetLayout
const&
layout,
535 text_shaper
const& text,
538 bool dead_character_mode,
541 return _draw_text_cursors(
542 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
560 template<std::same_as<w
idget_layout> W
idgetLayout, draw_attribute... Attributes>
562 WidgetLayout
const&
layout,
563 text_shaper
const& text,
566 bool dead_character_mode,
567 Attributes
const&...attributes)
const noexcept
581 template<std::same_as<w
idget_layout> W
idgetLayout>
585 attributes.fill_color =
color{0.0f, 0.0f, 0.0f, 0.0f};
586 attributes.line_color =
color{0.0f, 0.0f, 0.0f, 1.0f};
587 return _draw_override(
588 layout.clipping_rectangle_on_window(attributes.clipping_rectangle),
layout.to_window3() * box, attributes);
600 template<std::same_as<w
idget_layout> W
idgetLayout, draw_quad_shape Shape, draw_attribute... Attributes>
601 void draw_hole(WidgetLayout
const&
layout, Shape
const& box, Attributes
const&...attributes)
const noexcept
613 template<std::same_as<w
idget_layout> W
idgetLayout>
614 [[nodiscard]]
friend bool overlaps(draw_context
const& context, WidgetLayout
const&
layout)
noexcept
616 return overlaps(context.scissor_rectangle,
layout.clipping_rectangle_on_window());
620 vector_span<gfx_pipeline_box::vertex> *_box_vertices;
621 vector_span<gfx_pipeline_image::vertex> *_image_vertices;
622 vector_span<gfx_pipeline_SDF::vertex> *_sdf_vertices;
623 vector_span<gfx_pipeline_override::vertex> *_override_vertices;
625 template<draw_quad_shape Shape>
626 [[nodiscard]]
constexpr static quad make_quad(Shape
const& shape)
noexcept
628 if constexpr (std::is_same_v<Shape, quad>) {
638 auto right = line.direction();
640 auto const radius = width * 0.5f;
641 auto const n = normal(
right, 0.0f);
642 auto const up = n * width;
643 auto const t = normalize(
right);
645 auto origin = line.origin() - n * radius;
648 auto const radius_offset = t * radius;
650 origin -= radius_offset;
651 right += radius_offset;
654 right += radius_offset;
660 [[nodiscard]]
constexpr static rectangle make_rectangle(hi::circle
const& circle)
noexcept
662 auto const circle_ = f32x4{circle};
663 auto const origin = point3{circle_.xyz1() - circle_.ww00()};
664 auto const right = vector3{circle_.w000() * f32x4::broadcast(2.0f)};
665 auto const up = vector3{circle_._0w00() * f32x4::broadcast(2.0f)};
669 [[nodiscard]]
constexpr static corner_radii make_corner_radii(
float width,
line_end_cap c1,
line_end_cap c2)
noexcept
671 auto r = f32x4::broadcast(width * 0.5f);
674 r = blend<0b0101>(r, f32x4{});
677 r = blend<0b1010>(r, f32x4{});
680 return corner_radii{r};
683 [[nodiscard]]
constexpr static corner_radii make_corner_radii(hi::circle
const& circle)
noexcept
685 return corner_radii{f32x4{circle}.wwww()};
688 void _draw_override(aarectangle
const& clipping_rectangle, quad box, draw_attributes
const& attributes)
const noexcept;
690 void _draw_box(aarectangle
const& clipping_rectangle, quad box, draw_attributes
const& attributes)
const noexcept;
693 aarectangle
const& clipping_rectangle,
694 matrix3
const& transform,
695 text_shaper
const& text,
696 draw_attributes
const& attributes)
const noexcept;
698 void _draw_text_selection(
699 aarectangle
const& clipping_rectangle,
700 matrix3
const& transform,
701 text_shaper
const& text,
702 text_selection
const& selection,
703 draw_attributes
const& attributes)
const noexcept;
705 void _draw_text_insertion_cursor_empty(
706 aarectangle
const& clipping_rectangle,
707 matrix3
const& transform,
708 text_shaper
const& text,
709 draw_attributes
const& attributes)
const noexcept;
711 void _draw_text_insertion_cursor(
712 aarectangle
const& clipping_rectangle,
713 matrix3
const& transform,
714 text_shaper
const& text,
717 draw_attributes
const& attributes)
const noexcept;
719 void _draw_text_overwrite_cursor(
720 aarectangle
const& clipping_rectangle,
721 matrix3
const& transform,
722 text_shaper::char_const_iterator it,
723 draw_attributes
const& attributes)
const noexcept;
725 void _draw_text_cursors(
726 aarectangle
const& clipping_rectangle,
727 matrix3
const& transform,
728 text_shaper
const& text,
731 bool dead_character_mode,
732 draw_attributes
const& attributes)
const noexcept;
735 aarectangle
const& clipping_rectangle,
739 draw_attributes
const& attributes)
const noexcept;
742 _draw_image(aarectangle
const& clipping_rectangle, quad
const& box, gfx_pipeline_image::paged_image
const& image)
const noexcept;
line_end_cap
The way two lines should be joined.
Definition line_end_cap.hpp:21
@ right
Align the text to the right side.
Definition alignment.hpp:136
@ flat
The end cap of the line is flat.
Definition line_end_cap.hpp:24
@ round
The end cap of the line is round.
Definition line_end_cap.hpp:28
@ rectangle
The gui_event has rectangle data.
Definition gui_event_variant.hpp:44
The HikoGUI namespace.
Definition array_generic.hpp:21
The HikoGUI API version 1.
Definition array_generic.hpp:22
@ font
A font or font-size has changed.
Definition style_modify_mask.hpp:47
@ layout
A layout (size, alignment) value was modified.
Definition style_modify_mask.hpp:55
border_side
The side where the border is drawn.
Definition draw_context_intf.hpp:28
@ inside
The border is drawn inside the edge of a quad.
Definition draw_context_intf.hpp:35
@ outside
The border is drawn outside the edge of a quad.
Definition draw_context_intf.hpp:39
@ on
The border is drawn on the edge of a quad.
Definition draw_context_intf.hpp:31
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
This is a RGBA floating point color.
Definition color_intf.hpp:49
A color for each corner of a quad.
Definition quad_color.hpp:22
Definition font_glyph_ids.hpp:17
An identifier for a font-family that was registered with HikoGUI.
Definition font_id.hpp:23
The identifier of a glyph in a font-file.
Definition glyph_id.hpp:22
Class which represents an axis-aligned rectangle.
Definition aarectangle.hpp:33
static constexpr aarectangle large() noexcept
Create a large axis aligned rectangle.
Definition aarectangle.hpp:46
A type defining a 2D circle.
Definition circle.hpp:23
The 4 radii of the corners of a quad or rectangle.
Definition corner_radii.hpp:26
Line segment.
Definition line_segment.hpp:26
A 2D or 3D homogenius matrix for transforming homogenious vectors and points.
Definition matrix3.hpp:36
The draw attributes used to draw shaped into the draw context.
Definition draw_context_intf.hpp:49
aarectangle clipping_rectangle
The rectangle used the clip the shape when drawing.
Definition draw_context_intf.hpp:83
hi::border_side border_side
The side on which side of the edge of a shape the border should be drawn.
Definition draw_context_intf.hpp:72
float line_width
The width of a line, or the width of a border.
Definition draw_context_intf.hpp:68
line_end_cap end_line_cap
The shape of the beginning of a line.
Definition draw_context_intf.hpp:91
quad_color fill_color
The fill color used for the color of a box inside the border.
Definition draw_context_intf.hpp:58
quad_color line_color
The line color used for the color of the border of the box.
Definition draw_context_intf.hpp:64
line_end_cap begin_line_cap
The shape of the beginning of a line.
Definition draw_context_intf.hpp:87
hi::corner_radii corner_radius
The radii of each corner of a quad.
Definition draw_context_intf.hpp:76
Draw context for drawing using the HikoGUI shaders.
Definition draw_context_intf.hpp:209
void draw_text(WidgetLayout const &layout, text_shaper const &text, Attributes const &...attributes) const noexcept
Draw shaped text.
Definition draw_context_intf.hpp:483
void draw_circle(WidgetLayout const &layout, hi::circle const &circle, Attributes const &...attributes) const noexcept
Draw a circle.
Definition draw_context_intf.hpp:329
void draw_text_cursors(WidgetLayout const &layout, text_shaper const &text, text_cursor cursor, bool overwrite_mode, bool dead_character_mode, Attributes const &...attributes) const noexcept
Draw text cursors of shaped text.
Definition draw_context_intf.hpp:561
hi::subpixel_orientation subpixel_orientation
The subpixel orientation for rendering glyphs.
Definition draw_context_intf.hpp:223
friend bool overlaps(draw_context const &context, WidgetLayout const &layout) noexcept
Checks if a widget's layout overlaps with the part of the window that is being drawn.
Definition draw_context_intf.hpp:614
void draw_box(WidgetLayout const &layout, Shape const &shape, Attributes const &...attributes) const noexcept
Draw a box.
Definition draw_context_intf.hpp:273
bool draw_image(WidgetLayout const &layout, draw_quad_shape auto const &box, gfx_pipeline_image::paged_image &image, Attributes const &...attributes) const noexcept
Draw an image.
Definition draw_context_intf.hpp:361
void draw_hole(WidgetLayout const &layout, quad const &box, draw_attributes attributes) const noexcept
Make a hole in the user interface.
Definition draw_context_intf.hpp:582
void draw_circle(WidgetLayout const &layout, hi::circle const &circle, draw_attributes const &attributes) const noexcept
Draw a circle.
Definition draw_context_intf.hpp:315
float saturation
The tone-mapper's saturation.
Definition draw_context_intf.hpp:227
void draw_text(WidgetLayout const &layout, matrix3 const &transform, text_shaper const &text, Attributes const &...attributes) const noexcept
Draw shaped text.
Definition draw_context_intf.hpp:470
void draw_glyph(WidgetLayout const &layout, quad const &box, font_glyph_ids const &glyphs, draw_attributes const &attributes) const noexcept
Draw a glyph.
Definition draw_context_intf.hpp:412
void draw_glyph(WidgetLayout const &layout, quad const &box, hi::font_id font, glyph_id glyph, draw_attributes const &attributes) const noexcept
Draw a glyph.
Definition draw_context_intf.hpp:375
void draw_glyph(WidgetLayout const &layout, Shape const &box, font_glyph_ids const &glyphs, Attributes const &...attributes) const noexcept
Draw a glyph.
Definition draw_context_intf.hpp:432
void draw_text_selection(WidgetLayout const &layout, text_shaper const &text, text_selection const &selection, draw_attributes const &attributes) const noexcept
Draw text-selection of shaped text.
Definition draw_context_intf.hpp:496
void draw_glyph(WidgetLayout const &layout, Shape const &box, hi::font_id font, hi::glyph_id glyph_id, Attributes const &...attributes) const noexcept
Draw a glyph.
Definition draw_context_intf.hpp:394
aarectangle scissor_rectangle
This is the rectangle of the window that is being redrawn.
Definition draw_context_intf.hpp:219
void draw_line(WidgetLayout const &layout, line_segment const &line, Attributes const &...attributes) const noexcept
Draw a line.
Definition draw_context_intf.hpp:303
std::size_t frame_buffer_index
The frame buffer index of the image we are currently rendering.
Definition draw_context_intf.hpp:215
void draw_hole(WidgetLayout const &layout, Shape const &box, Attributes const &...attributes) const noexcept
Make a hole in the user interface.
Definition draw_context_intf.hpp:601
void draw_text_cursors(WidgetLayout const &layout, text_shaper const &text, text_cursor cursor, bool overwrite_mode, bool dead_character_mode, draw_attributes const &attributes) const noexcept
Draw text cursors of shaped text.
Definition draw_context_intf.hpp:533
void draw_line(WidgetLayout const &layout, line_segment const &line, draw_attributes const &attributes) const noexcept
Draw a line.
Definition draw_context_intf.hpp:285
bool draw_image(WidgetLayout const &layout, quad const &box, gfx_pipeline_image::paged_image &image, draw_attributes const &attributes) const noexcept
Draw an image.
Definition draw_context_intf.hpp:345
void draw_text_selection(WidgetLayout const &layout, text_shaper const &text, text_selection const &selection, Attributes const &...attributes) const noexcept
Draw text-selection of shaped text.
Definition draw_context_intf.hpp:514
void draw_box(WidgetLayout const &layout, quad const &box, draw_attributes const &attributes) const noexcept
Draw a box.
Definition draw_context_intf.hpp:260
utc_nanoseconds display_time_point
The time when the drawing will appear on the screen.
Definition draw_context_intf.hpp:231
void draw_text(WidgetLayout const &layout, matrix3 const &transform, text_shaper const &text, draw_attributes const &attributes) const noexcept
Draw shaped text.
Definition draw_context_intf.hpp:452
This is a image that is uploaded into the texture atlas.
Definition gfx_pipeline_image_vulkan_intf.hpp:83
The layout of a widget.
Definition widget_layout.hpp:56
Definition draw_context_intf.hpp:43
Definition draw_context_intf.hpp:204