From db41807aaf9567ca5b0e31b05f9240930e29afea Mon Sep 17 00:00:00 2001 From: Skyrpex Date: Sun, 2 Mar 2014 03:18:24 +0100 Subject: [PATCH] Used C++ static_cast in favor of C-style cast Refactored text geometry update into a single function --- RichText.cpp | 26 +++++++++++++++----------- RichText.hpp | 5 +++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/RichText.cpp b/RichText.cpp index 8a6a08f..a4a13f1 100644 --- a/RichText.cpp +++ b/RichText.cpp @@ -43,11 +43,7 @@ const std::vector &RichText::Line::getTexts() const void RichText::Line::appendText(sf::Text text) { // Set text offset - text.setPosition(m_bounds.width, 0.f); - - // Update bounds - m_bounds.height = std::max(m_bounds.height, text.getGlobalBounds().height); - m_bounds.width += text.getGlobalBounds().width; + updateTextAndGeometry(text); // Push back m_texts.push_back(std::move(text)); @@ -83,13 +79,21 @@ void RichText::Line::updateGeometry() const { m_bounds = sf::FloatRect(); - for (sf::Text &text : m_texts) { - text.setPosition(m_bounds.width, 0.f); + for (sf::Text &text : m_texts) + updateTextAndGeometry(text); +} - m_bounds.height = std::max(m_bounds.height, - float(text.getFont()->getLineSpacing(text.getCharacterSize()))); - m_bounds.width += text.getGlobalBounds().width; - } + +//////////////////////////////////////////////////////////////////////////////// +void RichText::Line::updateTextAndGeometry(sf::Text &text) const +{ + // Set text offset + text.setPosition(m_bounds.width, 0.f); + + // Update bounds + int lineSpacing = text.getFont()->getLineSpacing(text.getCharacterSize()); + m_bounds.height = std::max(m_bounds.height, static_cast(lineSpacing)); + m_bounds.width += text.getGlobalBounds().width; } diff --git a/RichText.hpp b/RichText.hpp index d355eb1..51d4103 100644 --- a/RichText.hpp +++ b/RichText.hpp @@ -74,6 +74,11 @@ public: ////////////////////////////////////////////////////////////////////// void updateGeometry() const; + ////////////////////////////////////////////////////////////////////// + // Update geometry for a given text + ////////////////////////////////////////////////////////////////////// + void updateTextAndGeometry(sf::Text &text) const; + ////////////////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////////////////