diff --git a/text/texthandler.cpp b/text/texthandler.cpp index 247c6f1..7711961 100644 --- a/text/texthandler.cpp +++ b/text/texthandler.cpp @@ -90,3 +90,27 @@ TextureAsset Text::_drawTextToTexture(Font font, std::string textToDraw, Color c return tex; } + +void Text::_drawWrappedText(Font font, vf2d pos, std::string textToDraw, float maxWidth, Color color) { + + std::stringstream ss(textToDraw); + std::string word; + std::string currentLine; + + while (ss >> word) { + std::string testLine = currentLine.empty() ? word : currentLine + " " + word; + float testLineWidth = MeasureText(font, testLine); + + if (testLineWidth <= maxWidth) { + currentLine = testLine; + } else { + DrawText(font, pos, currentLine, color); + pos.y += GetRenderedTextSize(font, currentLine).y; + currentLine = word; + } + } + + if (!currentLine.empty()) { + DrawText(font, pos, currentLine, color); + } +} diff --git a/text/texthandler.h b/text/texthandler.h index 564b3ef..33300d8 100644 --- a/text/texthandler.h +++ b/text/texthandler.h @@ -29,6 +29,21 @@ class Text { get()._drawText(font, pos, textToDraw, color); } + /** + * @brief Renders text that is wider than the given width by splitting it at space boundaries. + * The text will be drawn across multiple lines to ensure it fits within the maximum width. + * + * @param font The font to use for rendering the text. + * @param pos The starting position (top-left) to begin rendering the text. + * @param textToDraw The full text string to render, potentially split across multiple lines. + * @param maxWidth The maximum width allowed for each line of text. If a line exceeds this width, it is wrapped. + * @param color The color to use for rendering the text. + */ + static void DrawWrappedText(Font font, vf2d pos, std::string textToDraw, float maxWidth, Color color) { + get()._drawWrappedText(font, pos, textToDraw, maxWidth, color); + } + + /** * @brief Measures the width of the specified text when rendered with the given font. * @@ -68,6 +83,8 @@ class Text { void _drawText(Font font, vf2d pos, std::string textToDraw, Color color); + void _drawWrappedText(Font font, vf2d pos, std::string textToDraw, float maxWidth, Color color); + int _measureText(Font font, std::string text); vf2d _getRenderedTextSize(Font font, std::string textToDraw);