TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_renderer.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <gui_core/gui_id.h>
5#include <gui_core/gui_base_types.h> // IntRect, int2, float2, color4b, color4f, float3x2
6#include <gui_core/graphics/gui_image.h> // ImageIndex (default args)
8
9#include <stdint.h>
10#include <span>
11
12namespace tachyon
13{
16
17 enum class VisualBoxStyleIndex : uint16_t;
18 enum class VisualColorStyleIndex : uint16_t;
19 enum class ElementState : uint8_t;
20
23 enum class TriangleEdges : uint8_t
24 {
25 None = 0,
26 Edge01 = 1 << 0,
27 Edge12 = 1 << 1,
28 Edge20 = 1 << 2,
29 };
31
32 struct LineStyle
33 {
36 float lineWidth = 0;
37 float dashSize = 0;
38 float dashOffset = 0;
39 float gradientOffset = 0.0f;
40 float gradientScale = 1.0f;
41 };
42
47 {
48 static constexpr int kMaxStyles = 4;
49 std::array<uint32_t, kMaxStyles> styles {};
50 std::array<float, kMaxStyles> weights { 1.0f, 0.0f, 0.0f, 0.0f };
51 uint8_t count = 1;
52
53 static StyleBlend Single(uint32_t style) { StyleBlend b; b.styles[0] = style; return b; }
55 static StyleBlend Between(uint32_t from, uint32_t to, float t)
56 {
57 if (from == to || t >= 1.0f) return Single(to);
58 if (t <= 0.0f) return Single(from);
59 StyleBlend b;
60 b.count = 2;
61 b.styles = t >= 0.5f ? std::array<uint32_t, kMaxStyles>{ to, from } : std::array<uint32_t, kMaxStyles>{ from, to };
62 b.weights = t >= 0.5f ? std::array<float, kMaxStyles>{ t, 1.0f - t } : std::array<float, kMaxStyles>{ 1.0f - t, t };
63 return b;
64 }
65 bool IsBlending() const { return count > 1; }
67 float WeightOf(uint32_t style) const
68 {
69 float w = 0.0f;
70 for (int i = 0; i < count; ++i)
71 if (styles[size_t(i)] == style)
72 w += weights[size_t(i)];
73 return w;
74 }
75 };
76
78 {
79 public:
80 virtual ~GUIRenderer() {}
81
82 virtual void Reset() = 0;
83
84
85 virtual void SetTransform(tachyon::float3x2 transformation) = 0;
88 virtual void SetOpacity(float /*opacity*/) {}
89
93 virtual void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, const StyleBlend& styles,
94 tachyon::VisualColorStyleIndex colorStyleIndex = {},
96 tachyon::IntRect snappedPx = {}) = 0;
100 uint32_t fromStyleIndex, uint32_t toStyleIndex, float blendT,
101 tachyon::VisualColorStyleIndex colorStyleIndex = {},
103 tachyon::IntRect snappedPx = {})
104 {
105 RenderBox(rect, clipRect, StyleBlend::Between(fromStyleIndex, toStyleIndex, blendT), colorStyleIndex, textureIndex, snappedPx);
106 }
107
109 static uint32_t StyleIndex(VisualBoxStyleIndex boxStyleIndex, tachyon::ElementState state);
110
112 tachyon::ElementState state, tachyon::VisualColorStyleIndex colorStyleIndex = {},
114
116 tachyon::VisualColorStyleIndex colorStyleIndex = {},
118
119 virtual void RenderLine(tachyon::int2 p0, tachyon::int2 p1, tachyon::int2 p2, tachyon::int2 p3, tachyon::IntRect clipRect, const LineStyle& lineStyle) = 0;
120 void RenderLine(tachyon::int2 p0, tachyon::int2 p1, tachyon::IntRect clipRect, const LineStyle& lineStyle) { RenderLine(p0, p0, p1, p1, clipRect, lineStyle); }
121
125 virtual void RenderCircle(tachyon::float2 center, float radius, tachyon::IntRect clipRect, const LineStyle& lineStyle) = 0;
126
127 virtual void RenderDot(tachyon::float2 pos, tachyon::IntRect clipRect, float size, tachyon::color4f color) = 0;
129 virtual void RenderDots(std::span<const tachyon::float2> positions, tachyon::IntRect clipRect, float size, tachyon::color4f color)
130 {
131 for (const tachyon::float2& pos : positions)
132 RenderDot(pos, clipRect, size, color);
133 }
134
137
140 virtual void RenderQuad(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius = 0.0f, tachyon::ImageIndex textureIndex = tachyon::ImageIndex::Invalid, const tachyon::UVRect& uv = {}, const tachyon::ImageDisplay& display = {}) = 0;
143 virtual void RenderShadow(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius, int blur) = 0;
147 virtual void RenderOccluder(tachyon::IntRect /*rect*/, tachyon::IntRect /*clipRect*/) {}
148
153 virtual void RenderGlyph(tachyon::float2 pos, tachyon::float2 size, tachyon::float2 uvMin, tachyon::float2 uvMax, float scale, int bpp, int mode, VisualColorStyleIndex textColorIndex, tachyon::IntRect clipRect, tachyon::ImageIndex textureIndex = tachyon::ImageIndex::Invalid, float shadowBlur = -1.0f) = 0;
154
164
165 virtual void RenderGlyphs(std::span<const GlyphInstance> glyphs, tachyon::float2 offset, tachyon::IntRect clipRect)
166 {
167 for (const GlyphInstance& g : glyphs)
168 RenderGlyph(tachyon::float2{ g.pos.x + offset.x, g.pos.y + offset.y }, g.size, g.uvMin, g.uvMax, g.scale,
169 g.bpp, g.mode, g.color, clipRect, g.textureIndex);
170 }
171
173 void RenderFilledPolygon(std::span<const tachyon::float2> points, tachyon::IntRect clipRect,
174 VisualColorStyleIndex colorIndex,
176
178 void RenderPolyline(std::span<const tachyon::int2> points, bool closed,
179 tachyon::IntRect clipRect, const LineStyle& style);
180
185 void RenderArc(tachyon::float2 center, float radius, float width, float startTurns, float sweepTurns,
186 tachyon::IntRect clipRect, VisualColorStyleIndex colorIndex);
187
188 public:
189 virtual bool StartResizing(ID id) = 0;
190 virtual void StopResizing() = 0;
191
192 virtual bool BeginWindow(ID id) = 0;
193 virtual void EndWindow() = 0;
196 virtual bool DrawsChanged(ID /*window*/) const { return true; }
197
198 // Draw caching: a subtree's primitives are captured once and replayed translated.
199 // Called between BeginWindow and EndWindow; the defaults do nothing.
200
203 virtual void BeginScopeCapture(ID /*scopeId*/) {}
205 virtual void EndScopeCapture(ID /*scopeId*/) {}
207 virtual bool ReplayScopeCapture(ID /*scopeId*/, tachyon::int2 /*delta*/) { return false; }
209 virtual bool HasScopeCapture(ID /*scopeId*/) const { return false; }
211 virtual void InvalidateScopeCapture(ID /*scopeId*/) {}
213 virtual void ClearScopeCaptures() {}
214 };
215
217}
Definition gui_renderer.h:78
virtual void StopResizing()=0
virtual void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, const StyleBlend &styles, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})=0
Draws a styled box, styles mixed.
void RenderLine(tachyon::int2 p0, tachyon::int2 p1, tachyon::IntRect clipRect, const LineStyle &lineStyle)
Definition gui_renderer.h:120
void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, VisualBoxStyleIndex boxStyleIndex, tachyon::ElementState state, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})
virtual bool DrawsChanged(ID) const
Whether the window's draws this frame differ from its last frame's (as of its EndWindow); true when t...
Definition gui_renderer.h:196
virtual void InvalidateScopeCapture(ID)
Drops the capture of scopeId.
Definition gui_renderer.h:211
virtual void Reset()=0
virtual bool BeginWindow(ID id)=0
virtual void RenderQuad(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius=0.0f, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, const tachyon::UVRect &uv={}, const tachyon::ImageDisplay &display={})=0
Draws a filled, optionally rounded and textured rect: a box without borders, style blending or elemen...
virtual void RenderGlyph(tachyon::float2 pos, tachyon::float2 size, tachyon::float2 uvMin, tachyon::float2 uvMax, float scale, int bpp, int mode, VisualColorStyleIndex textColorIndex, tachyon::IntRect clipRect, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, float shadowBlur=-1.0f)=0
A glyph from the text atlas.
virtual void RenderGlyphs(std::span< const GlyphInstance > glyphs, tachyon::float2 offset, tachyon::IntRect clipRect)
RenderGlyph for each of glyphs, moved by offset, in order: a text's glyphs at once.
Definition gui_renderer.h:165
virtual void RenderDot(tachyon::float2 pos, tachyon::IntRect clipRect, float size, tachyon::color4f color)=0
virtual void SetOpacity(float)
How opaque what is drawn from now on is, 0 to 1 (1 until set).
Definition gui_renderer.h:88
virtual void RenderLine(tachyon::int2 p0, tachyon::int2 p1, tachyon::int2 p2, tachyon::int2 p3, tachyon::IntRect clipRect, const LineStyle &lineStyle)=0
virtual void EndScopeCapture(ID)
Ends the capture of scopeId and draws it into the current window.
Definition gui_renderer.h:205
void RenderFilledPolygon(std::span< const tachyon::float2 > points, tachyon::IntRect clipRect, VisualColorStyleIndex colorIndex, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid)
Convex polygon, fan-triangulated from points[0]; needs at least 3 points.
virtual void BeginScopeCapture(ID)
Starts recording primitives under scopeId, replacing its previous capture.
Definition gui_renderer.h:203
virtual bool HasScopeCapture(ID) const
True when scopeId has a non-empty capture.
Definition gui_renderer.h:209
virtual void SetTransform(tachyon::float3x2 transformation)=0
virtual ~GUIRenderer()
Definition gui_renderer.h:80
virtual void RenderShadow(tachyon::IntRect rect, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, float cornerRadius, int blur)=0
A box shadow: rect is the shadow's shape, which fades out over blur pixels on either side of its edge...
virtual void RenderDots(std::span< const tachyon::float2 > positions, tachyon::IntRect clipRect, float size, tachyon::color4f color)
RenderDot at each of positions, in order, all alike.
Definition gui_renderer.h:129
virtual void EndWindow()=0
static uint32_t StyleIndex(VisualBoxStyleIndex boxStyleIndex, tachyon::ElementState state)
The flat GPU style index of boxStyleIndex in state, as RenderBox takes it.
void RenderArc(tachyon::float2 center, float radius, float width, float startTurns, float sweepTurns, tachyon::IntRect clipRect, VisualColorStyleIndex colorIndex)
Part of a ring width wide, centred on the circle of radius around center, with round ends.
virtual void RenderCircle(tachyon::float2 center, float radius, tachyon::IntRect clipRect, const LineStyle &lineStyle)=0
A circle outline lineStyle.lineWidth wide, exactly round.
virtual void RenderOccluder(tachyon::IntRect, tachyon::IntRect)
Draws nothing: marks rect as covered by opaque content drawn after this call, so what was drawn befor...
Definition gui_renderer.h:147
virtual bool StartResizing(ID id)=0
virtual void ClearScopeCaptures()
Drops every capture.
Definition gui_renderer.h:213
void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, VisualBoxStyleIndex boxStyleIndex, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})
void RenderBox(tachyon::IntRect rect, tachyon::IntRect clipRect, uint32_t fromStyleIndex, uint32_t toStyleIndex, float blendT, tachyon::VisualColorStyleIndex colorStyleIndex={}, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, tachyon::IntRect snappedPx={})
As above, blended from fromStyleIndex to toStyleIndex (flat GPU style indices) by blendT in [0,...
Definition gui_renderer.h:99
void RenderPolyline(std::span< const tachyon::int2 > points, bool closed, tachyon::IntRect clipRect, const LineStyle &style)
Outlined polyline; closed adds a segment from the last point back to the first.
virtual void RenderTriangle(tachyon::float2 p0, tachyon::float2 p1, tachyon::float2 p2, tachyon::IntRect clipRect, tachyon::VisualColorStyleIndex colorStyleIndex, tachyon::ImageIndex textureIndex=tachyon::ImageIndex::Invalid, TriangleEdges hardEdges=TriangleEdges::None)=0
A filled triangle.
virtual bool ReplayScopeCapture(ID, tachyon::int2)
Draws the capture of scopeId again, moved by delta pixels; false if there is none.
Definition gui_renderer.h:207
@ None
Definition gui_element_flags.h:18
TriangleEdges
A triangle's edges, for the ones drawn without a soft edge: where it meets another triangle of the sa...
Definition gui_renderer.h:24
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:99
@ Edge01
p0 to p1
Definition gui_renderer.h:26
@ Edge20
p2 to p0
Definition gui_renderer.h:28
@ None
Definition gui_renderer.h:25
@ Edge12
p1 to p2
Definition gui_renderer.h:27
@ Invalid
Definition gui_image.h:99
VisualColorStyleIndex
Index into StyleManager's color style pool; Invalid (0) draws nothing (transparent).
Definition gui_style.h:33
VisualBoxStyleIndex
Index into StyleManager's visual box style pool; Invalid (0) = none.
Definition gui_style_ref.h:17
ElementState
Interaction state bits of an element; they pick the Style::states variant it draws with.
Definition gui_style.h:42
#define TACHYON_ENUM_OPERATORS(EnumType)
Definition gui_enum_operators.h:8
Definition gui_logging.h:24
One glyph of RenderGlyphs: RenderGlyph's arguments, its position relative.
Definition gui_renderer.h:157
tachyon::float2 uvMin
Definition gui_renderer.h:158
uint16_t bpp
Definition gui_renderer.h:160
tachyon::ImageIndex textureIndex
Definition gui_renderer.h:161
tachyon::float2 uvMax
Definition gui_renderer.h:158
tachyon::float2 size
Definition gui_renderer.h:158
uint16_t mode
Definition gui_renderer.h:160
float scale
Definition gui_renderer.h:159
VisualColorStyleIndex color
Definition gui_renderer.h:162
tachyon::float2 pos
Definition gui_renderer.h:158
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
How an image is shown, in this order:
Definition gui_image.h:42
Definition gui_base_types.h:406
Definition gui_renderer.h:33
float gradientScale
fraction of gradient to cover
Definition gui_renderer.h:40
float dashOffset
Definition gui_renderer.h:38
float gradientOffset
start of gradient range [0,1]
Definition gui_renderer.h:39
float dashSize
Definition gui_renderer.h:37
float lineWidth
Definition gui_renderer.h:36
VisualColorStyleIndex colorStyleIndex
Defaults to VisualColorStyleIndex::Invalid.
Definition gui_renderer.h:35
A box's look as a mix of up to four of its state styles (flat GPU style indices, as GUIRenderer::Styl...
Definition gui_renderer.h:47
std::array< uint32_t, kMaxStyles > styles
Definition gui_renderer.h:49
static constexpr int kMaxStyles
Definition gui_renderer.h:48
static StyleBlend Between(uint32_t from, uint32_t to, float t)
from mixed with to, t (0 to 1) of the way to to.
Definition gui_renderer.h:55
bool IsBlending() const
Definition gui_renderer.h:65
std::array< float, kMaxStyles > weights
Definition gui_renderer.h:50
uint8_t count
Definition gui_renderer.h:51
static StyleBlend Single(uint32_t style)
Definition gui_renderer.h:53
float WeightOf(uint32_t style) const
The share of style; 0 when it isn't in the mix.
Definition gui_renderer.h:67
The part of an image to draw, in UV: 0,0 is its top-left corner and 1,1 its bottom-right.
Definition gui_image.h:15
Definition gui_base_types.h:394
Definition gui_base_types.h:120
float y
Definition gui_base_types.h:121
float x
Definition gui_base_types.h:121
2D affine transform matrix (2 rows x 3 columns): x' = r0.x*x + r0.y*y + r0.z y' = r1....
Definition gui_base_types.h:218
Integer 2D vector.
Definition gui_base_types.h:18