AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
IRenderer.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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
  149class IRenderer: public aui::noncopyable {
  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:
  160        AOptional<ATextLayoutHelper::Symbols> mSymbols;
  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
  181        void enableCachingForTextLayoutHelper() noexcept {
  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
  205        virtual _<IRenderer::IPrerenderedString> finalize() noexcept = 0;
  206
  216        ATextLayoutHelper getTextLayoutHelper() noexcept {
  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
  412    virtual void squareSector(const ABrush& brush,
  413                              const glm::vec2& position,
  414                              const glm::vec2& size,
  415                              AAngleRadians begin,
  416                              AAngleRadians end) = 0;
  417
  422    void setColorForced(const AColor& color)
  423    {
  424        mColor = color;
  425    }
  426
  432    void setColor(const AColor& color)
  433    {
  434        setColorForced(mColor * color);
  435    }
  436
  437    const AColor& getColor() const
  438    {
  439        return mColor;
  440    }
  441
  447    void setTransform(const glm::mat4& transform)
  448    {
  449        mTransform *= transform;
  450    }
  451
  456    void setTransformForced(const glm::mat4& transform)
  457    {
  458        mTransform = transform;
  459    }
  460
  467    virtual void pushMaskBefore() = 0;
  468
  475    virtual void pushMaskAfter() = 0;
  476
  483    virtual void popMaskBefore() = 0;
  484
  491    virtual void popMaskAfter() = 0;
  492
  497    virtual void setBlending(Blending blending) = 0;
  498
  499
  504    [[nodiscard]]
  505    virtual _unique<IRenderViewToTexture> newRenderViewToTexture() noexcept = 0;
  506
  511    virtual void setWindow(AWindowBase* window)
  512    {
  513        mWindow = window;
  514        setColorForced(1.f);
  515        setTransformForced(getProjectionMatrix());
  516        mStencilDepth = 0;
  517    }
  518
  519    [[nodiscard]]
  520    AWindowBase* getWindow() const noexcept {
  521        return mWindow;
  522    }
  523
  524    virtual glm::mat4 getProjectionMatrix() const = 0;
  525
  526    const glm::mat4& getTransform()
  527    {
  528        return mTransform;
  529    }
  530
  531    [[nodiscard]]
  532    std::uint8_t getStencilDepth() const noexcept {
  533        return mStencilDepth;
  534    }
  535
  536    void setStencilDepth(uint8_t stencilDepth) {
  537        mStencilDepth = stencilDepth;
  538    }
  539
  540
  545    void translate(const glm::vec2& offset) {
  546        setTransformForced(glm::translate(getTransform(), glm::vec3(offset, 0.f)));
  547    }
  548
  554    void rotate(const glm::vec3& axis, AAngleRadians angle) {
  555        setTransformForced(glm::rotate(getTransform(), angle.radians(), axis));
  556    }
  557
  562    void rotate(AAngleRadians angle) {
  563        rotate({0.f, 0.f, 1.f}, angle);
  564    }
  565
  566    void setAllowRenderToTexture(bool allowRenderToTexture) {
  567        mAllowRenderToTexture = allowRenderToTexture;
  568    }
  569
  570    [[nodiscard]]
  571    bool allowRenderToTexture() const noexcept {
  572        return mAllowRenderToTexture;
  573    }
  574
  583    void backdrops(glm::ivec2 position, glm::ivec2 size, std::span<ass::Backdrop::Any> backdrops);
  584
  585protected:
  586    AColor mColor;
  587    glm::mat4 mTransform;
  588    AWindowBase* mWindow = nullptr;
  589    APool<ITexture> mTexturePool;
  590    uint8_t mStencilDepth = 0;
  591
  592    virtual _unique<ITexture> createNewTexture() = 0;
  593
  599    void stub(glm::vec2 position, glm::vec2 size);
  600
  601    virtual void backdrops(glm::ivec2 position, glm::ivec2 size, std::span<ass::Backdrop::Preprocessed> backdrops);
  602
  603private:
  604    bool mAllowRenderToTexture = false;
  605
  606};
  607
  608
Strong type used to store angle in radians.
Definition AAngleRadians.h:42
Definition AArrayView.h:32
Describes border style, like CSS border-style.
Definition ABorderStyle.h:28
Represents a 4-component floating point color (RGBA).
Definition AColor.h:26
Stores dimensions in scalable units (dp, pt, etc...).
Definition AMetric.h:75
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
Definition APool.h:19
Represents a Unicode character string.
Definition AString.h:38
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)
Sets the color which is multiplied with any brush. Unlike setColorForced, the new color is multiplied...
Definition IRenderer.h:432
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:422
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:447
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:554
void stub(glm::vec2 position, glm::vec2 size)
Draws stub (i.e., gray rectangle)
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:562
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:511
void setTransformForced(const glm::mat4 &transform)
Sets the transform matrix which is applicable for any figure.
Definition IRenderer.h:456
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:545
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.
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:179
#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
Forbids copy of your class.
Definition values.h:45