diff --git a/libControls/ToolTip.cpp b/libControls/ToolTip.cpp index d3e48f701..3f91915fd 100644 --- a/libControls/ToolTip.cpp +++ b/libControls/ToolTip.cpp @@ -13,14 +13,25 @@ namespace constexpr int MarginTight{2}; constexpr auto PaddingSize = NAS2D::Vector{MarginTight, MarginTight}; - void processLines(const std::string& text, const std::function& lineProcessor) + void forEachLine(const std::string& text, const std::function& lineHandler) { std::istringstream stream(text); for (std::string line; std::getline(stream, line);) { - lineProcessor(line); + lineHandler(line); } } + + + // Returns the maximum width of the largest string from the string stream in pixels. + int calculateMaxWidthStringSize(const std::string& text, const NAS2D::Font& font) + { + int maxWidthStringSize = 0; + forEachLine(text, [&font, &maxWidthStringSize](const std::string& lineStr) { + maxWidthStringSize = std::max(maxWidthStringSize, font.size(lineStr).x); + }); + return maxWidthStringSize; + } } @@ -57,7 +68,7 @@ void ToolTip::buildDrawParams(std::pair& item, int mouseX const auto toolTipLineHeight = mFont.height(); const auto numberOfLines = static_cast(std::count(item.second.begin(), item.second.end(), '\n') + 1); const auto toolTipTextHeight = toolTipLineHeight * numberOfLines; - const auto toolTipTextWidth = calculateMaxWidthStringSize(item.second); + const auto toolTipTextWidth = calculateMaxWidthStringSize(item.second, mFont); const auto toolTipSize = NAS2D::Vector{toolTipTextWidth, toolTipTextHeight} + PaddingSize * 2; auto tooltipPosition = item.first->position(); @@ -121,20 +132,9 @@ void ToolTip::draw() const auto linePosition = position() + PaddingSize; - processLines(mFocusedControl->second, [this, &renderer, &linePosition](const std::string& lineStr) { + forEachLine(mFocusedControl->second, [this, &renderer, &linePosition](const std::string& lineStr) { renderer.drawText(mFont, lineStr, linePosition); linePosition.y += mFont.height(); }); } } - - -// Returns the maximum width of the largest string from the string stream in pixels. -int ToolTip::calculateMaxWidthStringSize(const std::string& text) const -{ - int maxWidthStringSize = 0; - processLines(text, [&maxWidthStringSize, this](const std::string& lineStr) { - maxWidthStringSize = std::max(maxWidthStringSize, mFont.size(lineStr).x); - }); - return maxWidthStringSize; -} diff --git a/libControls/ToolTip.h b/libControls/ToolTip.h index 5f1758428..e3618d3ab 100644 --- a/libControls/ToolTip.h +++ b/libControls/ToolTip.h @@ -34,6 +34,4 @@ class ToolTip : public Control std::pair* mFocusedControl{nullptr}; std::vector> mControls; - - int calculateMaxWidthStringSize(const std::string& text) const; };