Loading...
Searching...
No Matches
RichTextRenderer.h
Go to the documentation of this file.
1//
2// RichTextRenderer.h
3//
4
5#ifndef MALENA_RICHTEXTRENDERER_H
6#define MALENA_RICHTEXTRENDERER_H
7
8#pragma once
9
15#include <vector>
16
17namespace ml
18{
19 // ── RenderedSegment ───────────────────────────────────────────────────────
20
31 {
33 std::size_t bufStart = 0;
34 std::size_t bufEnd = 0;
36
38 std::size_t start,
39 std::size_t end,
40 sf::Vector2f pos)
41 : sfText(std::move(text)),
42 bufStart(start),
43 bufEnd(end),
44 position(pos)
45 {}
46 };
47
48 // ── RenderedLine ──────────────────────────────────────────────────────────
49
54 {
55 std::vector<RenderedSegment> segments;
56 float y = 0.f;
57 float height = 0.f;
58 float rowHeight = 0.f;
59 std::size_t charStart = 0;
60 std::size_t charEnd = 0;
61 };
62
63 // ── RichTextRenderer ─────────────────────────────────────────────────────
64
80 {
81 public:
83 const sf::Font& defaultFont,
84 unsigned int defaultSize,
85 sf::Color defaultColor);
86
87 // ── Layout control ────────────────────────────────────────────────────
88
95 void setOrigin(const sf::Vector2f& origin);
96
98 void setMaxWidth(float width);
99
105 void rebuild();
106
112 void reflow();
113
114 // ── Drawing ───────────────────────────────────────────────────────────
115
116 void draw(sf::RenderTarget& target, const sf::RenderStates& states) const;
117
119 const sf::RenderStates& states,
120 std::size_t selStart,
121 std::size_t selEnd,
122 const sf::Color& color) const;
123
125 const sf::RenderStates& states,
126 std::size_t charIndex,
127 const sf::Color& color,
128 float width = 2.f) const;
129
130 // ── Coordinate mapping ────────────────────────────────────────────────
131
132 [[nodiscard]] sf::Vector2f charIndexToPosition(std::size_t index) const;
133 [[nodiscard]] std::size_t positionToCharIndex(const sf::Vector2f& pos) const;
134 [[nodiscard]] std::size_t lineIndexForChar(std::size_t charIndex) const;
135 [[nodiscard]] std::size_t charIndexAbove(std::size_t cursorIndex) const;
136 [[nodiscard]] std::size_t charIndexBelow(std::size_t cursorIndex) const;
137
138 // ── Layout info ───────────────────────────────────────────────────────
139
140 [[nodiscard]] float getTotalHeight() const;
141 [[nodiscard]] const std::vector<RenderedLine>& getLines() const;
142
143 private:
144 const RichTextBuffer& _buffer;
145 const sf::Font* _defaultFont;
146 unsigned int _defaultSize;
147 sf::Color _defaultColor;
148
149 sf::Vector2f _origin;
150 float _maxWidth = 0.f;
151 std::vector<RenderedLine> _lines;
152
153 void buildSegments();
154 void layoutLines();
155 sf::Text makeText(const std::string& str, const TextAttribute& attr) const;
156 };
157
158} // namespace ml
159
160#endif // MALENA_RICHTEXTRENDERER_H
Pure data layer for rich text editing.
float getTotalHeight() const
sf::Vector2f charIndexToPosition(std::size_t index) const
void drawCursor(sf::RenderTarget &target, const sf::RenderStates &states, std::size_t charIndex, const sf::Color &color, float width=2.f) const
std::size_t charIndexAbove(std::size_t cursorIndex) const
std::size_t charIndexBelow(std::size_t cursorIndex) const
void draw(sf::RenderTarget &target, const sf::RenderStates &states) const
const std::vector< RenderedLine > & getLines() const
void setMaxWidth(float width)
Set max line width for wrapping. 0 = no wrap.
std::size_t lineIndexForChar(std::size_t charIndex) const
void reflow()
Reposition existing sf::Text objects to the current origin.
std::size_t positionToCharIndex(const sf::Vector2f &pos) const
void rebuild()
Rebuild sf::Text objects from the current buffer content.
void setOrigin(const sf::Vector2f &origin)
Set the top-left origin and reposition segments.
RichTextRenderer(const RichTextBuffer &buffer, const sf::Font &defaultFont, unsigned int defaultSize, sf::Color defaultColor)
void drawSelection(sf::RenderTarget &target, const sf::RenderStates &states, std::size_t selStart, std::size_t selEnd, const sf::Color &color) const
#define MALENA_API
Definition Component.h:22
Vector2< float > Vector2f
One visual line of text made up of RenderedSegment objects.
std::size_t charStart
float height
Total height including word-wrap rows.
std::vector< RenderedSegment > segments
float rowHeight
Single visual row height (for cursor/selection).
sf::Text sfText
SFML text object, positioned and styled.
sf::Vector2f position
Top-left position.
std::size_t bufEnd
Last char index in the buffer (exclusive).
std::size_t bufStart
First char index in the buffer (inclusive).
RenderedSegment(sf::Text &&text, std::size_t start, std::size_t end, sf::Vector2f pos)
A styled attribute range applied to a span of characters.