From 02dfa9047e7928d3e587beda65efc72fd19d4c91 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 20 Dec 2024 12:44:20 -0500 Subject: [PATCH] Update ToolTip::buildDrawParams and ToolTip::draw to handle new line characters. --- libControls/ToolTip.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/libControls/ToolTip.cpp b/libControls/ToolTip.cpp index 6680ccc67..fdccc6efe 100644 --- a/libControls/ToolTip.cpp +++ b/libControls/ToolTip.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace { @@ -42,7 +43,20 @@ void ToolTip::add(Control& c, const std::string& str) void ToolTip::buildDrawParams(std::pair& item, int mouseX) { - const auto toolTipSize = mFont.size(item.second) + PaddingSize * 2; + const auto toolTipLineHeight = mFont.height(); + const auto numberOfLines = static_cast(std::count(item.second.begin(), item.second.end(), '\n') + 1); + const auto toolTipHeight = toolTipLineHeight * numberOfLines + PaddingSize.y * 2; + + std::istringstream stream(item.second); + std::string line; + int maxWidth = 0; + while (std::getline(stream, line, '\n')) + { + maxWidth = std::max(maxWidth, mFont.size(line).x); + } + const auto toolTipWidth = maxWidth + PaddingSize.x * 2; + + const auto toolTipSize = NAS2D::Vector{toolTipWidth, toolTipHeight}; auto tooltipPosition = item.first->position(); @@ -102,6 +116,14 @@ void ToolTip::draw() const auto& renderer = NAS2D::Utility::get(); renderer.drawBoxFilled(rect(), NAS2D::Color::DarkGray); renderer.drawBox(rect(), NAS2D::Color::Black); - renderer.drawText(mFont, mFocusedControl->second, position() + PaddingSize); + + std::istringstream stream(mFocusedControl->second); + std::string line; + auto linePosition = position() + PaddingSize; + while (std::getline(stream, line, '\n')) + { + renderer.drawText(mFont, line, linePosition); + linePosition.y += mFont.height(); + } } }