diff --git a/libControls/ToolTip.cpp b/libControls/ToolTip.cpp index 6680ccc67..2a678031c 100644 --- a/libControls/ToolTip.cpp +++ b/libControls/ToolTip.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace { @@ -42,7 +43,21 @@ 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; + + auto calculateMaxWidth = [this](const std::string& text) { + int maxWidth = 0; + processLines(text, [this, &maxWidth](const std::string& line) { + maxWidth = std::max(maxWidth, mFont.size(line).x); + }); + return maxWidth; + }; + + const auto toolTipWidth = calculateMaxWidth(item.second) +PaddingSize.x * 2; + + const auto toolTipSize = NAS2D::Vector{toolTipWidth, toolTipHeight}; auto tooltipPosition = item.first->position(); @@ -102,6 +117,21 @@ 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); + + auto linePosition = position() + PaddingSize; + processLines(mFocusedControl->second, [&renderer, this, &linePosition](const std::string& line) { + renderer.drawText(mFont, line, linePosition); + linePosition.y += mFont.height(); + }); + } +} + + +void ToolTip::processLines(const std::string& text, const std::function& lineProcessor) const +{ + std::istringstream stream(text); + for (std::string line; std::getline(stream, line);) + { + lineProcessor(line); } } diff --git a/libControls/ToolTip.h b/libControls/ToolTip.h index f4da17740..679d546ac 100644 --- a/libControls/ToolTip.h +++ b/libControls/ToolTip.h @@ -9,6 +9,7 @@ #include #include +#include class ToolTip : public Control @@ -34,4 +35,6 @@ class ToolTip : public Control std::pair* mFocusedControl{nullptr}; std::vector> mControls; + + void processLines(const std::string& text, const std::function& lineProcessor) const; };