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 "../numeric_array.hpp"
23#include "../aarect.hpp"
24#include "../vspan.hpp"
25#include "../text/shaped_text.hpp"
26#include "../color/color.hpp"
27#include <type_traits>
28
29namespace tt {
30
34public:
36 color line_color = color(1.0, 1.0, 1.0, 1.0);
37
39 color fill_color = color(0.0, 0.0, 0.0, 0.0);
40
42 float line_width = 1.0;
43
56 f32x4 corner_shapes = f32x4{0.0, 0.0, 0.0, 0.0};
57
63
69
71 gui_window &window,
72 aarect scissor_rectangle,
73 vspan<pipeline_flat::vertex> &flatVertices,
74 vspan<pipeline_box::vertex> &boxVertices,
75 vspan<pipeline_image::vertex> &imageVertices,
76 vspan<pipeline_SDF::vertex> &sdfVertices) noexcept :
77 _window(&window),
78 _scissor_rectangle(scissor_rectangle),
79 _flat_vertices(&flatVertices),
80 _box_vertices(&boxVertices),
81 _image_vertices(&imageVertices),
82 _sdf_vertices(&sdfVertices),
83 line_color(0.0, 1.0, 0.0, 1.0),
84 fill_color(1.0, 1.0, 0.0, 1.0),
85 line_width(theme::global->borderWidth),
87 clipping_rectangle(static_cast<f32x4>(window.extent))
88 {
89 _flat_vertices->clear();
90 _box_vertices->clear();
91 _image_vertices->clear();
92 _sdf_vertices->clear();
93 }
94
95 draw_context(draw_context const &rhs) noexcept = default;
96 draw_context(draw_context &&rhs) noexcept = default;
97 draw_context &operator=(draw_context const &rhs) noexcept = default;
98 draw_context &operator=(draw_context &&rhs) noexcept = default;
99 ~draw_context() = default;
100
101 gui_window &window() const noexcept
102 {
103 tt_axiom(_window);
104 return *_window;
105 }
106
107 gui_device &device() const noexcept
108 {
109 auto device = window().device();
110 tt_axiom(device);
111 return *device;
112 }
113
121 void draw_filled_quad(f32x4 p1, f32x4 p2, f32x4 p3, f32x4 p4) const noexcept
122 {
123 tt_axiom(_flat_vertices != nullptr);
124 _flat_vertices->emplace_back(transform * p1, clipping_rectangle, fill_color);
125 _flat_vertices->emplace_back(transform * p2, clipping_rectangle, fill_color);
126 _flat_vertices->emplace_back(transform * p3, clipping_rectangle, fill_color);
127 _flat_vertices->emplace_back(transform * p4, clipping_rectangle, fill_color);
128 }
129
137 void draw_filled_quad(aarect r) const noexcept
138 {
139 draw_filled_quad(r.corner<0>(), r.corner<1>(), r.corner<2>(), r.corner<3>());
140 }
141
153 void draw_box(aarect box) const noexcept
154 {
155 tt_axiom(_box_vertices != nullptr);
156
157 pipeline_box::device_shared::placeVertices(
159 }
160
176 void draw_box_with_border_inside(aarect rectangle) const noexcept
177 {
178 tt_axiom(_box_vertices != nullptr);
179
180 ttlet shrink_value = line_width * 0.5f;
181
182 ttlet new_rectangle = shrink(rectangle, shrink_value);
183
184 ttlet new_corner_shapes =
185 f32x4{std::max(0.0f, corner_shapes.x() - shrink_value),
186 std::max(0.0f, corner_shapes.y() - shrink_value),
187 std::max(0.0f, corner_shapes.z() - shrink_value),
188 std::max(0.0f, corner_shapes.w() - shrink_value)};
189
190 pipeline_box::device_shared::placeVertices(
191 *_box_vertices, transform * new_rectangle, fill_color, line_width, line_color, new_corner_shapes, clipping_rectangle);
192 }
193
210 void draw_box_with_border_outside(aarect rectangle) const noexcept
211 {
212 tt_axiom(_box_vertices != nullptr);
213
214 ttlet shrink_value = line_width * 0.5f;
215
216 ttlet new_rectangle = expand(rectangle, shrink_value);
217
218 ttlet new_corner_shapes =
219 f32x4{std::max(0.0f, corner_shapes.x() - shrink_value),
220 std::max(0.0f, corner_shapes.y() - shrink_value),
221 std::max(0.0f, corner_shapes.z() - shrink_value),
222 std::max(0.0f, corner_shapes.w() - shrink_value)};
223
224 pipeline_box::device_shared::placeVertices(
225 *_box_vertices, transform * new_rectangle, fill_color, line_width, line_color, new_corner_shapes, clipping_rectangle);
226 }
227
234 void draw_image(pipeline_image::Image &image) const noexcept
235 {
236 tt_axiom(_image_vertices != nullptr);
237
238 image.placeVertices(*_image_vertices, transform, clipping_rectangle);
239 }
240
251 void draw_text(shaped_text const &text, bool useContextColor = false) const noexcept
252 {
253 tt_axiom(_sdf_vertices != nullptr);
254
255 if (useContextColor) {
256 narrow_cast<gui_device_vulkan &>(device()).SDFPipeline->placeVertices(
257 *_sdf_vertices, text, transform, clipping_rectangle, line_color);
258 } else {
259 narrow_cast<gui_device_vulkan &>(device()).SDFPipeline->placeVertices(
260 *_sdf_vertices, text, transform, clipping_rectangle);
261 }
262 }
263
264 void draw_glyph(font_glyph_ids const &glyph, aarect box) const noexcept
265 {
266 tt_axiom(_sdf_vertices != nullptr);
267
268 narrow_cast<gui_device_vulkan &>(device()).SDFPipeline->placeVertices(
269 *_sdf_vertices, glyph, transform * box, line_color, clipping_rectangle);
270 }
271
272 [[nodiscard]] friend bool overlaps(draw_context const &context, aarect const &rectangle) noexcept
273 {
274 return overlaps(context._scissor_rectangle, rectangle);
275 }
276
277private:
278 gui_window *_window;
279public:
280 aarect _scissor_rectangle;
281private:
282 vspan<pipeline_flat::vertex> *_flat_vertices;
283 vspan<pipeline_box::vertex> *_box_vertices;
284 vspan<pipeline_image::vertex> *_image_vertices;
285 vspan<pipeline_SDF::vertex> *_sdf_vertices;
286};
287
288} // namespace tt
This is a RGBA floating point color.
Definition color.hpp:39
Definition identity.hpp:11
Draw context for drawing using the TTauri shaders.
Definition draw_context.hpp:33
void draw_text(shaped_text const &text, bool useContextColor=false) const noexcept
Draw shaped text.
Definition draw_context.hpp:251
void draw_filled_quad(aarect r) const noexcept
Draw a rectangle of one color.
Definition draw_context.hpp:137
void draw_filled_quad(f32x4 p1, f32x4 p2, f32x4 p3, f32x4 p4) const noexcept
Draw a polygon with four corners of one color.
Definition draw_context.hpp:121
aarect clipping_rectangle
The clipping rectangle when drawing.
Definition draw_context.hpp:62
void draw_box_with_border_inside(aarect rectangle) 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:176
color line_color
Foreground color.
Definition draw_context.hpp:36
matrix3 transform
Transform used on the given coordinates.
Definition draw_context.hpp:68
void draw_box(aarect box) const noexcept
Draw an axis aligned box This function will draw the given box.
Definition draw_context.hpp:153
void draw_box_with_border_outside(aarect rectangle) 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:210
void draw_image(pipeline_image::Image &image) const noexcept
Draw an image This function will draw an image.
Definition draw_context.hpp:234
float line_width
Size of lines.
Definition draw_context.hpp:42
color fill_color
Fill color.
Definition draw_context.hpp:39
f32x4 corner_shapes
Shape of the corners of a box.
Definition draw_context.hpp:56
Definition gui_window.hpp:39
This is a image that is uploaded into the texture atlas.
Definition pipeline_image_image.hpp:30
Definition font_glyph_ids.hpp:78
shaped_text represent a piece of text shaped to be displayed.
Definition shaped_text.hpp:22
Definition vspan.hpp:73
T max(T... args)