class EE::Graphics::RichText

Overview

A drawable class that renders rich text with multiple styles and spans. More…

#include <richtext.hpp>

class RichText: public EE::Graphics::Drawable {
public:
    // typedefs

    typedef std::variant<std::shared_ptr<Text>, std::shared_ptr<Drawable>, Sizef> Block;

    // enums

    enum BlockType;

    // structs

    struct RenderParagraph;
    struct RenderSpan;

    // construction

    ~RichText();

    // methods

    static RichText* New();
    void addSpan(const String& text, const FontStyleConfig& style);
    void addSpan(const String& text, Font* font = nullptr, Uint32 characterSize = 0, Color color = Color::White, Uint32 style = Text::Regular, Color backgroundColor = Color::Transparent);
    void clear();
    void setFontStyleConfig(const FontStyleConfig& styleConfig);
    FontStyleConfig& getFontStyleConfig();
    const FontStyleConfig& getFontStyleConfig() const;
    void setAlign(Uint32 align);
    Uint32 getAlign() const;
    void setMaxWidth(Float width);
    Float getMaxWidth() const;
    void addDrawable(std::shared_ptr<Drawable> drawable);
    void addCustomSize(const Sizef& size);
    const std::vector<Block>& getBlocks();

    virtual void draw(
        const Float& X,
        const Float& Y,
        const Vector2f& scale = Vector2f::One,
        const Float& rotation = 0,
        BlendMode effect = BlendMode::Alpha(),
        const OriginPoint& rotationCenter = OriginPoint::OriginCenter,
        const OriginPoint& scaleCenter = OriginPoint::OriginCenter
    );

    virtual void draw();
    virtual void draw(const Vector2f& position);
    virtual void draw(const Vector2f& position, const Sizef& size);
    virtual bool isStateful();
    virtual Sizef getSize();
    virtual Sizef getPixelsSize();
    void invalidate();
    const std::vector<RenderParagraph>& getLines() const;
    void setSelection(TextSelectionRange range);
    TextSelectionRange getSelection() const;
    void setSelectionColor(const Color& color);
    const Color& getSelectionColor() const;
    void setSelectionBackColor(const Color& color);
    const Color& getSelectionBackColor() const;
    Int64 getCharacterCount() const;
    Int64 findCharacterFromPos(const Vector2i& pos) const;
    Vector2f findCharacterPos(Int64 index) const;
    std::vector<Rectf> getSelectionRects() const;
    String getSelectionString() const;
    void updateLayout();
};

Inherited Members

public:
    // enums

    enum Type;

    // methods

    virtual Sizef getSize() = 0;
    virtual Sizef getPixelsSize() = 0;
    virtual void draw() = 0;
    virtual void draw(const Vector2f& position) = 0;
    virtual void draw(const Vector2f& position, const Sizef& size) = 0;
    virtual bool isStateful() = 0;
    void setAlpha(Uint8 alpha);
    const Uint8& getAlpha();
    void setColor(const Color& color);
    const Color& getColor() const;
    void setColorFilter(const Color& color);
    RGB getColorFilter();
    void clearColor();
    void clearColorFilter();
    void resetAlpha();
    Type getDrawableType() const;
    const Vector2f& getPosition() const;
    void setPosition(const Vector2f& position);
    virtual bool isDrawableResource() const;

Detailed Documentation

A drawable class that renders rich text with multiple styles and spans.

RichText allows rendering text with different fonts, sizes, colors, and styles mixed together. It supports word wrapping and alignment.

Construction

~RichText()

Destructor.

Methods

static RichText* New()

Returns:

A new instance of RichText.

void addSpan(const String& text, const FontStyleConfig& style)

Adds a text span with a specific style configuration.

Parameters:

text

The text content.

style

The font style configuration to apply.

void addSpan(const String& text, Font* font = nullptr, Uint32 characterSize = 0, Color color = Color::White, Uint32 style = Text::Regular, Color backgroundColor = Color::Transparent)

Adds a text span with individual style parameters.

Parameters:

text

The text content.

font

The font to use (optional, uses default if null).

characterSize

The character size (optional, uses default if 0).

color

The text color (optional, uses default if White).

style

The text style (optional, uses default if Regular).

void clear()

Clears all text spans.

void setFontStyleConfig(const FontStyleConfig& styleConfig)

Sets the default font style configuration used for new spans if not specified.

FontStyleConfig& getFontStyleConfig()

Returns:

The default font style configuration.

const FontStyleConfig& getFontStyleConfig() const

Returns:

The default font style configuration.

void setAlign(Uint32 align)

Sets the text alignment (Left, Center, Right).

Uint32 getAlign() const

Returns:

The text alignment.

void setMaxWidth(Float width)

Sets the maximum width for wrapping. If 0, wrapping is disabled.

Float getMaxWidth() const

Returns:

The maximum width for wrapping.

void addDrawable(std::shared_ptr<Drawable> drawable)

Adds a drawable (e.g., an image) into the text flow.

Parameters:

drawable

The drawable to add.

void addCustomSize(const Sizef& size)

Adds a custom size spacer into the text flow.

Parameters:

size

The physical dimensions of the spacer.

const std::vector<Block>& getBlocks()

Returns:

The list of blocks.

void invalidate()

Invalidates the layout, forcing a recalculation on the next update.

const std::vector<RenderParagraph>& getLines() const

Returns:

The list of rendered lines.

void setSelection(TextSelectionRange range)

Sets the text selection range.

TextSelectionRange getSelection() const

Returns:

The current text selection range.

void setSelectionColor(const Color& color)

Sets the selection text color.

const Color& getSelectionColor() const

Returns:

The selection text color.

void setSelectionBackColor(const Color& color)

Sets the selection background color.

const Color& getSelectionBackColor() const

Returns:

The selection background color.

Int64 getCharacterCount() const

Returns:

The total number of characters in the RichText.

Int64 findCharacterFromPos(const Vector2i& pos) const

Returns:

The character index at the given position.

Vector2f findCharacterPos(Int64 index) const

Returns:

The position of the character at the given index.

std::vector<Rectf> getSelectionRects() const

Returns:

A list of rectangles that cover the selection.

String getSelectionString() const

Returns:

The current selection as a string.