HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
DrawContext.hpp
1
2// Copyright 2020 Pokitec
3// All rights reserved.
4
5#pragma once
6
7#include "TTauri/Foundation/vec.hpp"
8#include "TTauri/Foundation/mat.hpp"
9#include "TTauri/Foundation/aarect.hpp"
10#include "TTauri/Foundation/vspan.hpp"
11#include "TTauri/GUI/GUIDevice.hpp"
12#include "TTauri/GUI/Window.hpp"
13#include "TTauri/GUI/Theme.hpp"
14#include "TTauri/GUI/PipelineImage_Image.hpp"
15#include "TTauri/GUI/PipelineFlat_DeviceShared.hpp"
16#include "TTauri/GUI/PipelineBox_DeviceShared.hpp"
17#include "TTauri/GUI/PipelineImage_DeviceShared.hpp"
18#include "TTauri/GUI/PipelineSDF_DeviceShared.hpp"
19#include "TTauri/GUI/PipelineFlat_Vertex.hpp"
20#include "TTauri/GUI/PipelineBox_Vertex.hpp"
21#include "TTauri/GUI/PipelineImage_Vertex.hpp"
22#include "TTauri/GUI/PipelineSDF_Vertex.hpp"
23#include "TTauri/Text/ShapedText.hpp"
24#include <type_traits>
25
26namespace tt {
27
31private:
32 Window *_window;
33 vspan<PipelineFlat::Vertex> *flatVertices;
34 vspan<PipelineBox::Vertex> *boxVertices;
35 vspan<PipelineImage::Vertex> *imageVertices;
36 vspan<PipelineSDF::Vertex> *sdfVertices;
37
38public:
40 vec color = vec::color(1.0, 1.0, 1.0, 1.0);
41
43 vec fillColor = vec::color(0.0, 0.0, 0.0, 0.0);
44
46 float lineWidth = 1.0;
47
60 vec cornerShapes = vec{0.0, 0.0, 0.0, 0.0};
61
67
73
75 Window &window,
76 vspan<PipelineFlat::Vertex> &flatVertices,
77 vspan<PipelineBox::Vertex> &boxVertices,
78 vspan<PipelineImage::Vertex> &imageVertices,
80 ) noexcept :
81 _window(&window),
82 flatVertices(&flatVertices),
83 boxVertices(&boxVertices),
84 imageVertices(&imageVertices),
85 sdfVertices(&sdfVertices),
86 color(0.0, 1.0, 0.0, 1.0),
87 fillColor(1.0, 1.0, 0.0, 1.0),
90 clippingRectangle(static_cast<vec>(window.currentWindowExtent))
91 {
92 flatVertices.clear();
93 boxVertices.clear();
94 imageVertices.clear();
95 sdfVertices.clear();
96 }
97
98 DrawContext(DrawContext const &rhs) noexcept = default;
99 DrawContext(DrawContext &&rhs) noexcept = default;
100 DrawContext &operator=(DrawContext const &rhs) noexcept = default;
101 DrawContext &operator=(DrawContext &&rhs) noexcept = default;
102 ~DrawContext() = default;
103
104 Window &window() const noexcept {
105 tt_assume(_window);
106 return *_window;
107 }
108
109 GUIDevice &device() const noexcept {
110 auto device = window().device;
111 tt_assume(device);
112 return *device;
113 }
114
122 void drawFilledQuad(vec p1, vec p2, vec p3, vec p4) const noexcept {
123 tt_assume(flatVertices != nullptr);
124 flatVertices->emplace_back(transform * p1, clippingRectangle, fillColor);
125 flatVertices->emplace_back(transform * p2, clippingRectangle, fillColor);
126 flatVertices->emplace_back(transform * p3, clippingRectangle, fillColor);
127 flatVertices->emplace_back(transform * p4, clippingRectangle, fillColor);
128 }
129
137 void drawFilledQuad(aarect r) const noexcept {
138 drawFilledQuad(r.corner<0>(), r.corner<1>(), r.corner<2>(), r.corner<3>());
139 }
140
152 void drawBox(aarect box) const noexcept {
153 tt_assume(boxVertices != nullptr);
154
155 PipelineBox::DeviceShared::placeVertices(
156 *boxVertices,
157 transform * box,
158 fillColor,
159 lineWidth,
160 color,
163 );
164 }
165
182 void drawBoxIncludeBorder(aarect box) const noexcept {
183
184 tt_assume(boxVertices != nullptr);
185
186 ttlet shrink_value = lineWidth * 0.5f;
187
188 ttlet newBox = shrink(box, shrink_value);
189
190 ttlet newCornerShapes = vec{
191 std::max(0.0f, cornerShapes.x() - shrink_value),
192 std::max(0.0f, cornerShapes.y() - shrink_value),
193 std::max(0.0f, cornerShapes.z() - shrink_value),
194 std::max(0.0f, cornerShapes.w() - shrink_value)
195 };
196
197 PipelineBox::DeviceShared::placeVertices(
198 *boxVertices,
199 transform * newBox,
200 fillColor,
201 lineWidth,
202 color,
203 newCornerShapes,
205 );
206 }
207
214 void drawImage(PipelineImage::Image &image) const noexcept {
215 tt_assume(imageVertices != nullptr);
216
217 image.placeVertices(*imageVertices, transform, clippingRectangle);
218 }
219
227 void drawText(ShapedText const &text) const noexcept {
228 tt_assume(sdfVertices != nullptr);
229
230 device().SDFPipeline->placeVertices(
231 *sdfVertices,
232 text,
233 transform,
235 );
236 }
237
245 void drawTextSingleColor(ShapedText const &text) const noexcept {
246 tt_assume(sdfVertices != nullptr);
247
248 device().SDFPipeline->placeVertices(
249 *sdfVertices,
250 text,
251 transform,
253 color
254 );
255 }
256
257 void drawGlyph(FontGlyphIDs const &glyph, aarect box) const noexcept {
258 tt_assume(sdfVertices != nullptr);
259
260 device().SDFPipeline->placeVertices(
261 *sdfVertices,
262 glyph,
263 transform * box,
264 color,
266 );
267 }
268
269};
270
271}
Class which represents an axis-aligned rectangle.
Definition aarect.hpp:13
A 4x4 matrix.
Definition mat.hpp:18
static mat I() noexcept
Create an identity matrix.
Definition mat.hpp:482
A 4D vector.
Definition vec.hpp:37
static tt_force_inline vec color(float r, float g, float b, float a=1.0f) noexcept
Create a color out of 3 to 4 values.
Definition vec.hpp:226
Definition vspan.hpp:72
Draw context for drawing using the TTauri shaders.
Definition DrawContext.hpp:30
float lineWidth
Size of lines.
Definition DrawContext.hpp:46
mat transform
Transform used on the given coordinates.
Definition DrawContext.hpp:72
void drawFilledQuad(vec p1, vec p2, vec p3, vec p4) const noexcept
Draw a polygon with four corners of one color.
Definition DrawContext.hpp:122
vec cornerShapes
Shape of the corners of a box.
Definition DrawContext.hpp:60
void drawBoxIncludeBorder(aarect box) const noexcept
Draw an axis aligned box This function will shrink to include the size of the border within the given...
Definition DrawContext.hpp:182
void drawTextSingleColor(ShapedText const &text) const noexcept
Draw shaped text.
Definition DrawContext.hpp:245
aarect clippingRectangle
The clipping rectangle when drawing.
Definition DrawContext.hpp:66
vec color
Foreground color.
Definition DrawContext.hpp:40
vec fillColor
Fill color.
Definition DrawContext.hpp:43
void drawBox(aarect box) const noexcept
Draw an axis aligned box This function will draw the given box.
Definition DrawContext.hpp:152
void drawImage(PipelineImage::Image &image) const noexcept
Draw an image This function will draw an image.
Definition DrawContext.hpp:214
void drawText(ShapedText const &text) const noexcept
Draw shaped text.
Definition DrawContext.hpp:227
void drawFilledQuad(aarect r) const noexcept
Draw a rectangle of one color.
Definition DrawContext.hpp:137
This is a image that is uploaded into the texture atlas.
Definition PipelineImage_Image.hpp:31
static constexpr float borderWidth
The line-width of a border.
Definition Theme.hpp:39
Definition Window_vulkan_win32.hpp:15
Definition FontGlyphIDs.hpp:77
ShapedText represent a piece of text shaped to be displayed.
Definition ShapedText.hpp:21
T max(T... args)