AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
IRenderer.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <glm/glm.hpp>
15#include <AUI/Reflect/AEnumerate.h>
16#include <AUI/Common/ASide.h>
17#include <AUI/Common/AColor.h>
18#include <AUI/Render/ABrush.h>
19#include <AUI/Util/APool.h>
20#include <AUI/Util/AArrayView.h>
21#include "AUI/Font/AFontStyle.h"
22#include "AUI/Render/ABorderStyle.h"
23#include "AUI/ASS/Property/Backdrop.h"
24#include "AUI/Util/AMetric.h"
25#include "ITexture.h"
26#include "ATextLayoutHelper.h"
27#include "IRenderViewToTexture.h"
28
29class AColor;
30class AWindowBase;
31
32
33
69enum class Blending {
85 NORMAL,
86
87
103 ADDITIVE,
104
120 INVERSE_DST,
121
137 INVERSE_SRC,
138};
139
150public:
152 public:
153 virtual void draw() = 0;
154 virtual ~IPrerenderedString() = default;
155 virtual int getWidth() = 0;
156 virtual int getHeight() = 0;
157 };
159 private:
161
162 protected:
163
171 void notifySymbolAdded(const ATextLayoutHelper::Boundary& symbol) noexcept {
172 if (mSymbols) mSymbols->last().push_back(symbol);
173 }
174
175 public:
176 virtual ~IMultiStringCanvas() = default;
177
182 mSymbols = ATextLayoutHelper::Symbols{};
183 nextLine();
184 }
185
189 void nextLine() noexcept {
190 if (mSymbols) mSymbols->push_back({});
191 }
192
198 virtual void addString(const glm::ivec2& position, const AString& text) noexcept = 0;
199
206
217 AUI_ASSERTX(bool(mSymbols), "call enableCachingForTextLayoutHelper() before using getTextLayoutHelper");
218 return ATextLayoutHelper(std::move(*mSymbols));
219 }
220 };
221
222public:
223 IRenderer(): mTexturePool([this] { return createNewTexture(); }) {}
224 virtual ~IRenderer() = default;
225
230 return mTexturePool.get();
231 }
232
238
245 virtual void rectangle(const ABrush& brush,
246 glm::vec2 position,
247 glm::vec2 size) = 0;
248
249
257 virtual void roundedRectangle(const ABrush& brush,
258 glm::vec2 position,
259 glm::vec2 size,
260 float radius) = 0;
261
269 virtual void rectangleBorder(const ABrush& brush,
270 glm::vec2 position,
271 glm::vec2 size,
272 float lineWidth = 1.f) = 0;
281 virtual void roundedRectangleBorder(const ABrush& brush,
282 glm::vec2 position,
283 glm::vec2 size,
284 float radius,
285 int borderWidth) = 0;
286
287
295 virtual void boxShadow(glm::vec2 position,
296 glm::vec2 size,
297 float blurRadius,
298 const AColor& color) = 0;
299
311 virtual void boxShadowInner(glm::vec2 position,
312 glm::vec2 size,
313 float blurRadius,
314 float spreadRadius,
315 float borderRadius,
316 const AColor& color,
317 glm::vec2 offset) = 0;
318
330 virtual void string(glm::vec2 position,
331 const AString& string,
332 const AFontStyle& fs = {}) = 0;
333
342 virtual _<IPrerenderedString> prerenderString(glm::vec2 position, const AString& text, const AFontStyle& fs) = 0;
343
351 void line(const ABrush& brush, glm::vec2 p1, glm::vec2 p2, const ABorderStyle& style = ABorderStyle::Solid{}, AMetric width = 1_dp) {
352 glm::vec2 points[] = { p1, p2 };
353 lines(brush, points, style, width);
354 }
355
363 virtual void lines(const ABrush& brush, AArrayView<glm::vec2> points, const ABorderStyle& style, AMetric width) = 0;
364
371 void lines(const ABrush& brush, AArrayView<glm::vec2> points, const ABorderStyle& style = ABorderStyle::Solid{}) {
372 lines(brush, points, style, 1_dp);
373 }
374
381 virtual void points(const ABrush& brush, AArrayView<glm::vec2> points, AMetric size) = 0;
382
390 virtual void lines(const ABrush& brush, AArrayView<std::pair<glm::vec2, glm::vec2>> points, const ABorderStyle& style, AMetric width) = 0;
391
398 void lines(const ABrush& brush, AArrayView<std::pair<glm::vec2, glm::vec2>> points, const ABorderStyle& style = ABorderStyle::Solid{}) {
399 lines(brush, points, style, 1_dp);
400 }
401
410 virtual void squareSector(const ABrush& brush,
411 const glm::vec2& position,
412 const glm::vec2& size,
413 AAngleRadians begin,
414 AAngleRadians end) = 0;
415
420 void setColorForced(const AColor& color)
421 {
422 mColor = color;
423 }
424
430 void setColor(const AColor& color)
431 {
432 setColorForced(mColor * color);
433 }
434
435 const AColor& getColor() const
436 {
437 return mColor;
438 }
439
445 void setTransform(const glm::mat4& transform)
446 {
447 mTransform *= transform;
448 }
449
454 void setTransformForced(const glm::mat4& transform)
455 {
456 mTransform = transform;
457 }
458
465 virtual void pushMaskBefore() = 0;
466
473 virtual void pushMaskAfter() = 0;
474
481 virtual void popMaskBefore() = 0;
482
489 virtual void popMaskAfter() = 0;
490
495 virtual void setBlending(Blending blending) = 0;
496
497
502 [[nodiscard]]
503 virtual _unique<IRenderViewToTexture> newRenderViewToTexture() noexcept = 0;
504
509 virtual void setWindow(AWindowBase* window)
510 {
511 mWindow = window;
512 setColorForced(1.f);
513 setTransformForced(getProjectionMatrix());
514 mStencilDepth = 0;
515 }
516
517 [[nodiscard]]
518 AWindowBase* getWindow() const noexcept {
519 return mWindow;
520 }
521
522 virtual glm::mat4 getProjectionMatrix() const = 0;
523
524 const glm::mat4& getTransform()
525 {
526 return mTransform;
527 }
528
529 [[nodiscard]]
530 std::uint8_t getStencilDepth() const noexcept {
531 return mStencilDepth;
532 }
533
534 void setStencilDepth(uint8_t stencilDepth) {
535 mStencilDepth = stencilDepth;
536 }
537
538
543 void translate(const glm::vec2& offset) {
544 setTransformForced(glm::translate(getTransform(), glm::vec3(offset, 0.f)));
545 }
546
552 void rotate(const glm::vec3& axis, AAngleRadians angle) {
553 setTransformForced(glm::rotate(getTransform(), angle.radians(), axis));
554 }
555
560 void rotate(AAngleRadians angle) {
561 rotate({0.f, 0.f, 1.f}, angle);
562 }
563
564 void setAllowRenderToTexture(bool allowRenderToTexture) {
565 mAllowRenderToTexture = allowRenderToTexture;
566 }
567
568 [[nodiscard]]
569 bool allowRenderToTexture() const noexcept {
570 return mAllowRenderToTexture;
571 }
572
581 void backdrops(glm::ivec2 position, glm::ivec2 size, std::span<ass::Backdrop::Any> backdrops);
582
583protected:
584 AColor mColor;
585 glm::mat4 mTransform;
586 AWindowBase* mWindow = nullptr;
587 APool<ITexture> mTexturePool;
588 uint8_t mStencilDepth = 0;
589
590 virtual _unique<ITexture> createNewTexture() = 0;
591
597 void stub(glm::vec2 position, glm::vec2 size);
598
599 virtual void backdrops(glm::ivec2 position, glm::ivec2 size, std::span<ass::Backdrop::Preprocessed> backdrops);
600
601private:
602 bool mAllowRenderToTexture = false;
603
604};
605
606
Strong type used to store angle in radians.
Definition: AAngleRadians.h:42
Definition: AArrayView.h:25
Describes border style, like CSS border-style.
Definition: ABorderStyle.h:28
Represents a 4-component floating point color.
Definition: AColor.h:27
Stores dimensions in scalable units (dp, pt, etc...).
Definition: AMetric.h:75
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Definition: APool.h:19
Represents a Unicode character string.
Definition: AString.h:37
Helps mapping prerendered string with positions.
Definition: ATextLayoutHelper.h:25
Definition: AWindowBase.h:33
Definition: IRenderer.h:158
void nextLine() noexcept
When caching for text layout helper is enabled, a new line added.
Definition: IRenderer.h:189
virtual void addString(const glm::ivec2 &position, const AString &text) noexcept=0
Bakes a string with some position.
void enableCachingForTextLayoutHelper() noexcept
Notifies IMultiStringCanvas that getTextLayoutHelper() will be used.
Definition: IRenderer.h:181
void notifySymbolAdded(const ATextLayoutHelper::Boundary &symbol) noexcept
Notifies IMultiStringCanvas than a symbol was added used to construct a ATextLayoutHelper.
Definition: IRenderer.h:171
virtual _< IRenderer::IPrerenderedString > finalize() noexcept=0
Bakes multi string canvas to IPrerenderedString which can be used for drawing text.
ATextLayoutHelper getTextLayoutHelper() noexcept
Returns text layout helper.
Definition: IRenderer.h:216
Definition: IRenderer.h:151
Base class for rendering.
Definition: IRenderer.h:149
virtual void popMaskAfter()=0
Switches drawing to the color buffer back from the stencil. Decreases stencil depth.
virtual _< IMultiStringCanvas > newMultiStringCanvas(const AFontStyle &style)=0
Creates new canvas for batching multiple prerender string calls.
virtual void squareSector(const ABrush &brush, const glm::vec2 &position, const glm::vec2 &size, AAngleRadians begin, AAngleRadians end)=0
Draws sector in rectangle shape. The sector is drawn clockwise from begin to end angles.
void setColor(const AColor &color)
Definition: IRenderer.h:430
virtual void lines(const ABrush &brush, AArrayView< glm::vec2 > points, const ABorderStyle &style, AMetric width)=0
Draws polyline (non-loop line strip).
virtual void points(const ABrush &brush, AArrayView< glm::vec2 > points, AMetric size)=0
Draws points list.
void lines(const ABrush &brush, AArrayView< std::pair< glm::vec2, glm::vec2 > > points, const ABorderStyle &style=ABorderStyle::Solid{})
Draws multiple individual lines in a batch.
Definition: IRenderer.h:398
virtual void string(glm::vec2 position, const AString &string, const AFontStyle &fs={})=0
Draws string.
void setColorForced(const AColor &color)
Sets the color which is multiplied with any brush.
Definition: IRenderer.h:420
void line(const ABrush &brush, glm::vec2 p1, glm::vec2 p2, const ABorderStyle &style=ABorderStyle::Solid{}, AMetric width=1_dp)
Definition: IRenderer.h:351
virtual void setBlending(Blending blending)=0
Sets blending mode.
_< ITexture > getNewTexture()
Creates new texture (image representation optimized for GPU rendering).
Definition: IRenderer.h:229
void setTransform(const glm::mat4 &transform)
Sets the transform matrix which is applicable for any figure. Unlike setTransformForced,...
Definition: IRenderer.h:445
virtual void roundedRectangleBorder(const ABrush &brush, glm::vec2 position, glm::vec2 size, float radius, int borderWidth)=0
Draws rounded rectangle's border.
void rotate(const glm::vec3 &axis, AAngleRadians angle)
wrapper for setTransform applying matrix rotation along the specified axis.
Definition: IRenderer.h:552
void stub(glm::vec2 position, glm::vec2 size)
Draws stub (i.e., gray rectangle)
Definition: IRenderer.cpp:15
virtual void roundedRectangle(const ABrush &brush, glm::vec2 position, glm::vec2 size, float radius)=0
Draws rounded rect (with antialiasing, if msaa enabled).
virtual void popMaskBefore()=0
Switches drawing to the stencil buffer instead of color buffer.
void rotate(AAngleRadians angle)
wrapper for setTransform applying matrix rotation along z axis.
Definition: IRenderer.h:560
void lines(const ABrush &brush, AArrayView< glm::vec2 > points, const ABorderStyle &style=ABorderStyle::Solid{})
Draws polyline (non-loop line strip).
Definition: IRenderer.h:371
virtual void setWindow(AWindowBase *window)
Sets the window to render on.
Definition: IRenderer.h:509
void setTransformForced(const glm::mat4 &transform)
Sets the transform matrix which is applicable for any figure.
Definition: IRenderer.h:454
virtual void rectangleBorder(const ABrush &brush, glm::vec2 position, glm::vec2 size, float lineWidth=1.f)=0
Draws rectangle's border.
void translate(const glm::vec2 &offset)
Wrapper for setTransform applying matrix translate transformation.
Definition: IRenderer.h:543
virtual void lines(const ABrush &brush, AArrayView< std::pair< glm::vec2, glm::vec2 > > points, const ABorderStyle &style, AMetric width)=0
Draws multiple individual lines in a batch.
virtual void pushMaskAfter()=0
Switches drawing to the color buffer back from the stencil. Increases stencil depth.
virtual _unique< IRenderViewToTexture > newRenderViewToTexture() noexcept=0
Returns a new instance of IRenderViewToTexture interface associated with this renderer.
virtual void boxShadowInner(glm::vec2 position, glm::vec2 size, float blurRadius, float spreadRadius, float borderRadius, const AColor &color, glm::vec2 offset)=0
Draws inner (inset) rectangle-shaped shadow.
virtual void boxShadow(glm::vec2 position, glm::vec2 size, float blurRadius, const AColor &color)=0
Draws a rectangle-shaped shadow.
virtual _< IPrerenderedString > prerenderString(glm::vec2 position, const AString &text, const AFontStyle &fs)=0
Analyzes string and creates an instance of IRenderer::IPrerenderedString which helps IRenderer to eff...
virtual void pushMaskBefore()=0
witches drawing to the stencil buffer instead of color buffer.
void backdrops(glm::ivec2 position, glm::ivec2 size, std::span< ass::Backdrop::Any > backdrops)
Draws rectangular backdrop effects.
Definition: IRenderer.cpp:20
virtual void rectangle(const ABrush &brush, glm::vec2 position, glm::vec2 size)=0
Draws simple rectangle.
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition: Assert.h:74
Definition: ABorderStyle.h:30
Definition: AFontStyle.h:24
Definition: ATextLayoutHelper.h:27
Definition: Size.h:22
Definition: Text.h:21
Forbids copy of your class.
Definition: values.h:40