HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
draw_context.hpp
1// Copyright Take Vos 2020.
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// Copyright 2020 Pokitec
6// All rights reserved.
7
8#pragma once
9
10#include "gui_device_vulkan.hpp"
11#include "gui_window.hpp"
12#include "theme.hpp"
13#include "pipeline_image_image.hpp"
14#include "pipeline_flat_device_shared.hpp"
15#include "pipeline_box_device_shared.hpp"
16#include "pipeline_image_device_shared.hpp"
17#include "pipeline_SDF_device_shared.hpp"
18#include "pipeline_flat_vertex.hpp"
19#include "pipeline_box_vertex.hpp"
20#include "pipeline_image_vertex.hpp"
21#include "pipeline_SDF_vertex.hpp"
22#include "../geometry/axis_aligned_rectangle.hpp"
23#include "../vspan.hpp"
24#include "../text/shaped_text.hpp"
25#include "../color/color.hpp"
26#include "../geometry/corner_shapes.hpp"
27#include <type_traits>
28
29namespace tt {
30
34public:
35 draw_context(draw_context const &rhs) noexcept = default;
36 draw_context(draw_context &&rhs) noexcept = default;
37 draw_context &operator=(draw_context const &rhs) noexcept = default;
38 draw_context &operator=(draw_context &&rhs) noexcept = default;
39 ~draw_context() = default;
40
42 gui_window &window,
43 aarectangle scissor_rectangle,
44 vspan<pipeline_flat::vertex> &flatVertices,
45 vspan<pipeline_box::vertex> &boxVertices,
46 vspan<pipeline_image::vertex> &imageVertices,
47 vspan<pipeline_SDF::vertex> &sdfVertices) noexcept :
48 _window(&window),
49 _scissor_rectangle(scissor_rectangle),
50 _flat_vertices(&flatVertices),
51 _box_vertices(&boxVertices),
52 _image_vertices(&imageVertices),
53 _sdf_vertices(&sdfVertices),
54 _clipping_rectangle(window.extent)
55 {
56 _flat_vertices->clear();
57 _box_vertices->clear();
58 _image_vertices->clear();
59 _sdf_vertices->clear();
60 }
61
62 [[nodiscard]] draw_context
63 make_child_context(matrix3 parent_to_local, matrix3 local_to_window, aarectangle clipping_rectangle) const noexcept
64 {
65 auto new_context = *this;
66 new_context._scissor_rectangle = aarectangle{parent_to_local * this->_scissor_rectangle};
67 new_context._clipping_rectangle = clipping_rectangle;
68 new_context._transform = local_to_window;
69 return new_context;
70 }
71
72 [[nodiscard]] aarectangle clipping_rectangle() const noexcept
73 {
74 return _clipping_rectangle;
75 }
76
77 void set_clipping_rectangle(aarectangle clipping_rectangle) noexcept
78 {
79 _clipping_rectangle = clipping_rectangle;
80 }
81
82 [[nodiscard]] matrix3 transform() const noexcept
83 {
84 return _transform;
85 }
86
87 gui_window &window() const noexcept
88 {
89 tt_axiom(_window);
90 return *_window;
91 }
92
93 gui_device &device() const noexcept
94 {
95 auto device = window().device();
96 tt_axiom(device);
97 return *device;
98 }
99
107 void draw_filled_quad(point3 p1, point3 p2, point3 p3, point3 p4, color fill_color) const noexcept
108 {
109 tt_axiom(_flat_vertices != nullptr);
110 _flat_vertices->emplace_back(aarectangle{_transform * _clipping_rectangle}, _transform * p1, fill_color);
111 _flat_vertices->emplace_back(aarectangle{_transform * _clipping_rectangle}, _transform * p2, fill_color);
112 _flat_vertices->emplace_back(aarectangle{_transform * _clipping_rectangle}, _transform * p3, fill_color);
113 _flat_vertices->emplace_back(aarectangle{_transform * _clipping_rectangle}, _transform * p4, fill_color);
114 }
115
123 void draw_filled_quad(rectangle r, color fill_color) const noexcept
124 {
125 draw_filled_quad(get<0>(r), get<1>(r), get<2>(r), get<3>(r), fill_color);
126 }
127
140 rectangle box,
141 color fill_color,
142 color line_color,
143 float line_width = 1.0,
145 {
146 tt_axiom(_box_vertices != nullptr);
147
148 pipeline_box::device_shared::place_vertices(
149 *_box_vertices,
150 aarectangle{_transform * _clipping_rectangle},
151 _transform * box,
152 fill_color,
153 line_color,
154 line_width,
155 corner_shapes);
156 }
157
158 void draw_box(rectangle box, color fill_color, color line_color, tt::corner_shapes corner_shapes) const noexcept
159 {
160 draw_box(box, fill_color, line_color, 1.0, corner_shapes);
161 }
162
163 void draw_box(rectangle box, color fill_color, tt::corner_shapes corner_shapes) const noexcept
164 {
165 draw_box(box, fill_color, fill_color, 0.0, corner_shapes);
166 }
167
185 color fill_color,
186 color line_color,
187 float line_width = 1.0,
189 {
190 tt_axiom(_box_vertices != nullptr);
191
192 ttlet shrink_value = line_width * 0.5f;
193
194 ttlet new_rectangle = shrink(rectangle, shrink_value);
195
196 ttlet new_corner_shapes = corner_shapes - shrink_value;
197
198 pipeline_box::device_shared::place_vertices(
199 *_box_vertices,
200 aarectangle{_transform * _clipping_rectangle},
201 _transform * new_rectangle,
202 fill_color,
203 line_color,
204 line_width,
205 new_corner_shapes);
206 }
207
208
209
210 void draw_box_with_border_inside(rectangle rectangle, color fill_color, color line_color, tt::corner_shapes corner_shapes)
211 const noexcept
212 {
213 draw_box_with_border_inside(rectangle, fill_color, line_color, 1.0, corner_shapes);
214 }
215
234 color fill_color,
235 color line_color,
236 float line_width = 1.0,
238 {
239 tt_axiom(_box_vertices != nullptr);
240
241 ttlet expand_value = line_width * 0.5f;
242
243 ttlet new_rectangle = expand(rectangle, expand_value);
244
245 ttlet new_corner_shapes = corner_shapes + expand_value;
246
247 pipeline_box::device_shared::place_vertices(
248 *_box_vertices,
249 aarectangle{_transform * _clipping_rectangle},
250 _transform * new_rectangle,
251 fill_color,
252 line_color,
253 line_width,
254 new_corner_shapes);
255 }
256
257 void draw_box_with_border_outside(rectangle rectangle, color fill_color, color line_color, tt::corner_shapes corner_shapes)
258 const noexcept
259 {
260 draw_box_with_border_outside(rectangle, fill_color, line_color, 1.0, corner_shapes);
261 }
262
269 void draw_image(pipeline_image::Image &image, matrix3 image_transform) const noexcept
270 {
271 tt_axiom(_image_vertices != nullptr);
272
273 image.place_vertices(*_image_vertices, aarectangle{_transform * _clipping_rectangle}, _transform * image_transform);
274 }
275
286 void draw_text(shaped_text const &text, std::optional<color> text_color = {}, matrix3 transform = geo::identity{}) const noexcept
287 {
288 tt_axiom(_sdf_vertices != nullptr);
289
290 if (text_color) {
291 narrow_cast<gui_device_vulkan &>(device()).SDFPipeline->place_vertices(
292 *_sdf_vertices, aarectangle{_transform * _clipping_rectangle}, _transform * transform, text, *text_color);
293 } else {
294 narrow_cast<gui_device_vulkan &>(device()).SDFPipeline->place_vertices(
295 *_sdf_vertices, aarectangle{_transform * _clipping_rectangle}, _transform * transform, text);
296 }
297 }
298
299 void draw_glyph(font_glyph_ids const &glyph, rectangle box, color text_color) const noexcept
300 {
301 tt_axiom(_sdf_vertices != nullptr);
302
303 narrow_cast<gui_device_vulkan &>(device()).SDFPipeline->place_vertices(
304 *_sdf_vertices, aarectangle{_transform * _clipping_rectangle}, _transform * box, glyph, text_color);
305 }
306
307 [[nodiscard]] friend bool overlaps(draw_context const &context, aarectangle const &rectangle) noexcept
308 {
309 return overlaps(context._scissor_rectangle, rectangle);
310 }
311
312private:
313 gui_window *_window;
314
315 vspan<pipeline_flat::vertex> *_flat_vertices;
316 vspan<pipeline_box::vertex> *_box_vertices;
317 vspan<pipeline_image::vertex> *_image_vertices;
318 vspan<pipeline_SDF::vertex> *_sdf_vertices;
319
323 aarectangle _scissor_rectangle;
324
328 aarectangle _clipping_rectangle;
329
334 matrix3 _transform = geo::identity{};
335};
336
337} // namespace tt
This is a RGBA floating point color.
Definition color.hpp:39
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:18
Definition corner_shapes.hpp:9
Definition identity.hpp:11
Class which represents an rectangle.
Definition rectangle.hpp:16
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
void draw_filled_quad(rectangle r, color fill_color) const noexcept
Draw a rectangle of one color.
Definition draw_context.hpp:123
void draw_filled_quad(point3 p1, point3 p2, point3 p3, point3 p4, color fill_color) const noexcept
Draw a polygon with four corners of one color.
Definition draw_context.hpp:107
void draw_box_with_border_inside(rectangle rectangle, color fill_color, color line_color, float line_width=1.0, tt::corner_shapes corner_shapes=tt::corner_shapes{}) const noexcept
Draw an axis aligned box This function will shrink to include the size of the border inside the given...
Definition draw_context.hpp:183
void draw_text(shaped_text const &text, std::optional< color > text_color={}, matrix3 transform=geo::identity{}) const noexcept
Draw shaped text.
Definition draw_context.hpp:286
void draw_box_with_border_outside(rectangle rectangle, color fill_color, color line_color, float line_width=1.0, tt::corner_shapes corner_shapes=tt::corner_shapes{}) const noexcept
Draw an axis aligned box This function will expand to include the size of the border outside the give...
Definition draw_context.hpp:232
void draw_image(pipeline_image::Image &image, matrix3 image_transform) const noexcept
Draw an image This function will draw an image.
Definition draw_context.hpp:269
void draw_box(rectangle box, color fill_color, color line_color, float line_width=1.0, tt::corner_shapes corner_shapes=tt::corner_shapes{}) const noexcept
Draw an axis aligned box This function will draw the given box.
Definition draw_context.hpp:139
Definition gui_device.hpp:22
Definition gui_window.hpp:37
extent2 extent
The current window extent as set by the GPU library.
Definition gui_window.hpp:76
This is a image that is uploaded into the texture atlas.
Definition pipeline_image_image.hpp:28
shaped_text represent a piece of text shaped to be displayed.
Definition shaped_text.hpp:23
Definition vspan.hpp:73
T transform(T... args)