TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_custom_draw_context.h
Go to the documentation of this file.
1#pragma once
2
3// The contexts passed to callbacks registered with ElementStorage: CustomDrawContext for
4// drawing, PostLayoutContext for after layout.
5
7#include <gui_core/elements/gui_element.h> // ElementRef (member by value)
8#include <gui_core/graphics/gui_image.h> // ImageIndex (default args)
9#include <gui_core/graphics/gui_renderer.h> // TriangleEdges (default args)
10
11#include <span>
12
13namespace tachyon
14{
17
18 class GUIRenderer;
19 class ElementStorage;
20 class ITextSystem;
21 enum class VisualBoxStyleIndex : uint16_t;
22 enum class VisualColorStyleIndex : uint16_t;
23 enum class ElementState : uint8_t;
24 enum class TextID : uint32_t;
25 struct LineStyle;
26
28 enum class DrawSpace : uint8_t
29 {
34 };
35
43 {
44 public:
47 ElementRef elementRef,
48 IntRect elementRectLayout, IntRect innerRectLayout,
49 IntRect clipRectScreen,
50 float3x2 renderTransform, int2 windowOffset,
51 ITextSystem* textSystem = nullptr,
52 float4 ownerPixelRect = {}, IntRect ownerSnappedRect = {}) noexcept
53 : m_renderer(&renderer), m_storage(&storage),
54 m_elementRef(elementRef),
55 m_elementRect(elementRectLayout), m_innerRect(innerRectLayout),
56 m_clipRect(clipRectScreen),
57 m_renderTransform(renderTransform),
58 m_currentTransform(renderTransform),
59 m_windowOffset(windowOffset),
60 m_textSystem(textSystem),
61 m_ownerPixelRect(ownerPixelRect), m_ownerSnappedRect(ownerSnappedRect) {}
62
64 inline ElementRef Ref() const noexcept { return m_elementRef; }
65
67 inline IntRect ElementRect() const noexcept { return ToCurrentSpace(m_elementRect); }
68
70 inline IntRect ElementInnerRect() const noexcept { return ToCurrentSpace(m_innerRect); }
71
73 inline IntRect ClipRect() const noexcept { return m_clipRect; }
74
78 void SetSpace(DrawSpace space);
79
81 void SetSpaceRaw(float3x2 spaceToWindowLocal);
82
84 inline float3x2 CurrentTransform() const noexcept { return m_currentTransform; }
85
88 int2 ScreenToCurrent(int2 screenPoint) const;
89
91 IntRect GetElementRect(const ElementRef& ref) const noexcept;
93 IntRect GetInnerRect(const ElementRef& ref) const noexcept;
95 ElementRef GetParentRef(const ElementRef& ref) const noexcept;
96 ElementRef GetNextSiblingRef(const ElementRef& ref) const noexcept;
98 int2 GetContentSize(const ElementRef& ref) const noexcept;
100 int2 GetInnerSize(const ElementRef& ref) const noexcept;
101
102 // Drawing primitives. Input coords are in the current space; clipped to ClipRect().
103
107 void RenderBox(IntRect rect, VisualBoxStyleIndex styleIndex, ElementState state,
108 VisualColorStyleIndex colorStyleIndex = {},
109 ImageIndex textureIndex = ImageIndex::Invalid) const;
110
111 void RenderBox(IntRect rect, VisualBoxStyleIndex styleIndex,
112 VisualColorStyleIndex colorStyleIndex = {},
113 ImageIndex textureIndex = ImageIndex::Invalid) const;
114
117 void RenderBox(IntRect rect, VisualBoxStyleIndex styleIndex,
118 ElementState fromState, ElementState toState, float t) const;
119
122 void RenderBox(IntRect rect, IntRect subClip,
123 VisualBoxStyleIndex styleIndex, ElementState state,
124 VisualColorStyleIndex colorStyleIndex = {},
125 ImageIndex textureIndex = ImageIndex::Invalid) const;
126
127 void RenderBox(IntRect rect, IntRect subClip,
128 VisualBoxStyleIndex styleIndex,
129 VisualColorStyleIndex colorStyleIndex = {},
130 ImageIndex textureIndex = ImageIndex::Invalid) const;
131
133 void RenderQuad(IntRect rect, VisualColorStyleIndex colorStyleIndex,
134 float cornerRadius = 0.0f,
135 ImageIndex textureIndex = ImageIndex::Invalid) const;
136
138 void RenderQuad(IntRect rect, IntRect subClip,
139 VisualColorStyleIndex colorStyleIndex,
140 float cornerRadius = 0.0f,
141 ImageIndex textureIndex = ImageIndex::Invalid) const;
142
144 void RenderLine(int2 p0, int2 p1, const LineStyle& style) const;
146 void RenderLine(int2 p0, int2 p1, int2 p2, int2 p3, const LineStyle& style) const;
147
149 void RenderDot(float2 pos, float size, color4f color) const;
150
153 VisualColorStyleIndex colorStyleIndex,
154 ImageIndex textureIndex = ImageIndex::Invalid,
155 TriangleEdges hardEdges = TriangleEdges::None) const;
156
158 void RenderCircle(float2 center, float radius, const LineStyle& style) const;
159 void RenderCircle(int2 center, float radius, const LineStyle& style) const;
161 void RenderDisc(int2 center, float radius, VisualColorStyleIndex colorStyleIndex) const;
162
164 void RenderFilledPolygon(std::span<const float2> points,
165 VisualColorStyleIndex colorStyleIndex,
166 ImageIndex textureIndex = ImageIndex::Invalid) const;
167
170 void RenderPolyline(std::span<const int2> points, bool closed,
171 const LineStyle& style, float cornerRadius = 0.0f) const;
172
177 void RenderText(TextID textId, IntRect rect, float extraScale = 1.0f) const;
178
179 private:
183 inline IntRect ToCurrentSpace(IntRect rectAbs) const noexcept
184 {
185 return m_currentSpaceIsLocal ? (rectAbs - m_innerRect.position) : rectAbs;
186 }
187
188 GUIRenderer* m_renderer;
189 ElementStorage* m_storage;
190 ElementRef m_elementRef;
191 IntRect m_elementRect;
192 IntRect m_innerRect;
193 IntRect m_clipRect;
194 float3x2 m_renderTransform;
195 float3x2 m_currentTransform;
196 int2 m_windowOffset{};
197 ITextSystem* m_textSystem = nullptr;
200 float4 m_ownerPixelRect {};
201 IntRect m_ownerSnappedRect {};
202 IntRect SnappedPixelRect(IntRect rect) const;
203 bool m_currentSpaceIsLocal = true;
204 };
205
219 {
220 public:
223 IntRect elementRectLayout, IntRect innerRectLayout,
224 float3x2 contentTransform) noexcept
225 : m_storage(&storage), m_elementRef(ref),
226 m_elementRect(elementRectLayout), m_innerRect(innerRectLayout),
227 m_contentTransform(contentTransform) {}
228
230 inline ElementRef Ref() const noexcept { return m_elementRef; }
231 inline IntRect ElementRect() const noexcept { return m_elementRect; }
232 inline IntRect ElementInnerRect() const noexcept { return m_innerRect; }
233 inline float3x2 ContentTransform() const noexcept { return m_contentTransform; }
234
236 IntRect GetElementRect(const ElementRef& ref) const noexcept;
237 IntRect GetInnerRect(const ElementRef& ref) const noexcept;
238 ElementRef GetParentRef(const ElementRef& ref) const noexcept;
239 ElementRef GetNextSiblingRef(const ElementRef& ref) const noexcept;
240 int2 GetContentSize(const ElementRef& ref) const noexcept;
241 int2 GetInnerSize(const ElementRef& ref) const noexcept;
242
243 private:
244 ElementStorage* m_storage;
245 ElementRef m_elementRef;
246 IntRect m_elementRect;
247 IntRect m_innerRect;
248 float3x2 m_contentTransform;
249 };
250
252} // namespace tachyon
void RenderBox(IntRect rect, IntRect subClip, VisualBoxStyleIndex styleIndex, VisualColorStyleIndex colorStyleIndex={}, ImageIndex textureIndex=ImageIndex::Invalid) const
void RenderBox(IntRect rect, VisualBoxStyleIndex styleIndex, ElementState fromState, ElementState toState, float t) const
A styled box part way from its fromState variant to its toState one: t = 0 is all fromState,...
CustomDrawContext(GUIRenderer &renderer, ElementStorage &storage, ElementRef elementRef, IntRect elementRectLayout, IntRect innerRectLayout, IntRect clipRectScreen, float3x2 renderTransform, int2 windowOffset, ITextSystem *textSystem=nullptr, float4 ownerPixelRect={}, IntRect ownerSnappedRect={}) noexcept
Built by the renderer; callbacks receive it.
Definition gui_custom_draw_context.h:46
void RenderCircle(float2 center, float radius, const LineStyle &style) const
A circle outline, stroked with style; see GUIRenderer::RenderCircle.
int2 ScreenToCurrent(int2 screenPoint) const
Convert a SCREEN-space input (e.g.
ElementRef GetParentRef(const ElementRef &ref) const noexcept
Tree navigation for other elements.
void RenderDisc(int2 center, float radius, VisualColorStyleIndex colorStyleIndex) const
A filled, anti-aliased disc (a quad rounded to a circle).
void RenderText(TextID textId, IntRect rect, float extraScale=1.0f) const
Draw a previously-registered text (textSystem->RegisterText) within rect, expressed in the CURRENT sp...
int2 GetInnerSize(const ElementRef &ref) const noexcept
Size of an element's padding-box, in pixels.
void RenderFilledPolygon(std::span< const float2 > points, VisualColorStyleIndex colorStyleIndex, ImageIndex textureIndex=ImageIndex::Invalid) const
Filled convex polygon via fan triangulation.
int2 GetContentSize(const ElementRef &ref) const noexcept
Size of the bounds of an element's children (can exceed its inner size when it scrolls),...
void RenderBox(IntRect rect, IntRect subClip, VisualBoxStyleIndex styleIndex, ElementState state, VisualColorStyleIndex colorStyleIndex={}, ImageIndex textureIndex=ImageIndex::Invalid) const
RenderBox variants with a sub-clip.
void RenderBox(IntRect rect, VisualBoxStyleIndex styleIndex, ElementState state, VisualColorStyleIndex colorStyleIndex={}, ImageIndex textureIndex=ImageIndex::Invalid) const
A styled box (background, border, radius) like an element's.
void RenderDot(float2 pos, float size, color4f color) const
A filled, anti-aliased dot centered on pos; size is its diameter.
void RenderBox(IntRect rect, VisualBoxStyleIndex styleIndex, VisualColorStyleIndex colorStyleIndex={}, ImageIndex textureIndex=ImageIndex::Invalid) const
IntRect ClipRect() const noexcept
Scissor region in SCREEN space. Applied implicitly by every Render* method.
Definition gui_custom_draw_context.h:73
void RenderLine(int2 p0, int2 p1, int2 p2, int2 p3, const LineStyle &style) const
A curve from p0 to p3 shaped by the control points p1 and p2 (Bezier-like).
ElementRef Ref() const noexcept
ElementRef this callback was registered on.
Definition gui_custom_draw_context.h:64
void RenderPolyline(std::span< const int2 > points, bool closed, const LineStyle &style, float cornerRadius=0.0f) const
Outlined polyline.
float3x2 CurrentTransform() const noexcept
The current space -> window-local transform.
Definition gui_custom_draw_context.h:84
void RenderTriangle(float2 p0, float2 p1, float2 p2, VisualColorStyleIndex colorStyleIndex, ImageIndex textureIndex=ImageIndex::Invalid, TriangleEdges hardEdges=TriangleEdges::None) const
A filled triangle; see GUIRenderer::RenderTriangle for hardEdges.
IntRect GetInnerRect(const ElementRef &ref) const noexcept
Padding-box of another element, in the CURRENT space; empty when not found.
IntRect ElementRect() const noexcept
Border-box of the registered element, in the CURRENT space.
Definition gui_custom_draw_context.h:67
IntRect ElementInnerRect() const noexcept
Padding-box (inner content area) of the registered element, in the CURRENT space.
Definition gui_custom_draw_context.h:70
void RenderLine(int2 p0, int2 p1, const LineStyle &style) const
A straight line from p0 to p1.
void RenderQuad(IntRect rect, VisualColorStyleIndex colorStyleIndex, float cornerRadius=0.0f, ImageIndex textureIndex=ImageIndex::Invalid) const
A plain filled rect (no border or element state), optionally rounded or textured.
void SetSpace(DrawSpace space)
Set the current coordinate space.
void SetSpaceRaw(float3x2 spaceToWindowLocal)
Set an arbitrary current space; spaceToWindowLocal maps current-space -> window-local.
ElementRef GetNextSiblingRef(const ElementRef &ref) const noexcept
IntRect GetElementRect(const ElementRef &ref) const noexcept
Lookups for OTHER elements (returns rects in the CURRENT space).
void RenderQuad(IntRect rect, IntRect subClip, VisualColorStyleIndex colorStyleIndex, float cornerRadius=0.0f, ImageIndex textureIndex=ImageIndex::Invalid) const
RenderQuad with a sub-clip in the same space as rect.
void RenderCircle(int2 center, float radius, const LineStyle &style) const
see above
The frame's element tree.
Definition gui_element_storage.h:100
Definition gui_renderer.h:43
Text and icon registration, measurement, rendering and editing.
Definition gui_text_system.h:178
ElementRef GetNextSiblingRef(const ElementRef &ref) const noexcept
IntRect GetElementRect(const ElementRef &ref) const noexcept
Lookups for other elements, as on CustomDrawContext but with rects in layout space.
ElementRef GetParentRef(const ElementRef &ref) const noexcept
int2 GetContentSize(const ElementRef &ref) const noexcept
IntRect ElementRect() const noexcept
Definition gui_custom_draw_context.h:231
IntRect GetInnerRect(const ElementRef &ref) const noexcept
int2 GetInnerSize(const ElementRef &ref) const noexcept
IntRect ElementInnerRect() const noexcept
Definition gui_custom_draw_context.h:232
PostLayoutContext(ElementStorage &storage, ElementRef ref, IntRect elementRectLayout, IntRect innerRectLayout, float3x2 contentTransform) noexcept
Built by the layout pass; callbacks receive it.
Definition gui_custom_draw_context.h:222
ElementRef Ref() const noexcept
The registered element, its border-box and padding-box, and its contentTransform.
Definition gui_custom_draw_context.h:230
float3x2 ContentTransform() const noexcept
Definition gui_custom_draw_context.h:233
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:23
ImageIndex
Index of an image in the graphics context's image set, as the shaders sample it.
Definition gui_image.h:37
DrawSpace
The coordinate space a CustomDrawContext draws in.
Definition gui_custom_draw_context.h:29
@ None
Definition gui_renderer.h:24
@ Invalid
Definition gui_image.h:37
@ Screen
Absolute layout pixels: the space of stored element rects and event.state.screenPos.
Definition gui_custom_draw_context.h:33
@ Local
The default: the registered element's content space, (0,0) at its padding box's top-left.
Definition gui_custom_draw_context.h:30
VisualColorStyleIndex
Index into StyleManager's color style pool; Invalid (0) draws nothing (transparent).
Definition gui_style.h:33
TextID
Handle for text registered with the text system (ITextSystem::RegisterText and friends).
Definition gui_style_text.h:13
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
Definition gui_logging.h:24
An element's slot in ElementStorage: its depth and index within that depth.
Definition gui_element.h:127
Definition gui_base_types.h:407
Definition gui_renderer.h:32
Definition gui_base_types.h:395
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:219
Definition gui_base_types.h:174
Integer 2D vector.
Definition gui_base_types.h:19