diff --git a/include/Util/Text.hpp b/include/Util/Text.hpp index 44f88e5a..1707d860 100644 --- a/include/Util/Text.hpp +++ b/include/Util/Text.hpp @@ -23,8 +23,26 @@ namespace Util { */ class Text : public Core::Drawable { public: + enum class Alignment { + LEFT = TTF_WRAPPED_ALIGN_LEFT, + CENTER = TTF_WRAPPED_ALIGN_CENTER, + RIGHT = TTF_WRAPPED_ALIGN_RIGHT, + }; + + /** + * @brief Constructor for the Text class. + * + * @param font The font to use for the text. + * @param size The size of the text. + * @param text The actual text content. + * @param color The color of the text (default is gray). + * @param alignment The alignment of the text (default is right-aligned). + * @param wrapLength The maximum length at which text should wrap (default + * is 0, no wrapping). + */ Text(const std::string &font, int size, const std::string &text, - const Util::Color &color = Color(127, 127, 127)); + const Util::Color &color = Color(127, 127, 127), + Alignment alignment = Alignment::LEFT, int wrapLength = 0); glm::vec2 GetSize() const override { return m_Size; }; @@ -48,6 +66,25 @@ class Text : public Core::Drawable { ApplyTexture(); }; + /** + * @brief Set the maximum length at which text should wrap. + * + * The text will only wrap on `\n` when set to 0 + * + * @param wrapLength The maximum length at which text should wrap. + */ + void SetWrapLength(int wrapLength) { + m_WrapLength = wrapLength; + ApplyTexture(); + } + + /** + * @brief Set the alignment of the text. + * + * @param alignment The alignment to set. + */ + void SetAlignment(Alignment alignment); + /** * @brief Draws the text with a given transform and z-index. * @@ -81,6 +118,7 @@ class Text : public Core::Drawable { std::string m_Text; Util::Color m_Color; glm::vec2 m_Size; + int m_WrapLength; }; } // namespace Util diff --git a/src/Util/Text.cpp b/src/Util/Text.cpp index 1bbec036..7cab5846 100644 --- a/src/Util/Text.cpp +++ b/src/Util/Text.cpp @@ -10,9 +10,10 @@ namespace Util { Text::Text(const std::string &font, int fontSize, const std::string &text, - const Util::Color &color) + const Util::Color &color, Alignment alignment, int wrapLength) : m_Text(text), - m_Color(color) { + m_Color(color), + m_WrapLength(wrapLength) { if (s_Program == nullptr) { InitProgram(); } @@ -30,10 +31,12 @@ Text::Text(const std::string &font, int fontSize, const std::string &text, LOG_ERROR("{}", TTF_GetError()); } + TTF_SetFontWrappedAlign(m_Font.get(), static_cast(alignment)); + auto surface = std::unique_ptr>{ TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(), - m_Color.ToSdlColor(), 0), + m_Color.ToSdlColor(), m_WrapLength), SDL_FreeSurface, }; if (surface == nullptr) { @@ -48,6 +51,11 @@ Text::Text(const std::string &font, int fontSize, const std::string &text, m_Size = {surface->pitch / surface->format->BytesPerPixel, surface->h}; } +void Text::SetAlignment(Alignment alignment) { + TTF_SetFontWrappedAlign(m_Font.get(), static_cast(alignment)); + ApplyTexture(); +} + void Text::Draw(const Util::Transform &transform, const float zIndex) { auto data = Util::ConvertToUniformBufferData(transform, m_Size, zIndex); s_UniformBuffer->SetData(0, data); @@ -115,7 +123,7 @@ void Text::ApplyTexture() { auto surface = std::unique_ptr>{ TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(), - m_Color.ToSdlColor(), 0), + m_Color.ToSdlColor(), m_WrapLength), SDL_FreeSurface, }; if (surface == nullptr) {