Skip to content

Commit

Permalink
Update ToolTip::buildDrawParams and ToolTip::draw to handle new line …
Browse files Browse the repository at this point in the history
…characters.
  • Loading branch information
oscar139 committed Dec 20, 2024
1 parent 7cb0c25 commit 02dfa90
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions libControls/ToolTip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <NAS2D/Utility.h>
#include <NAS2D/Renderer/Renderer.h>

#include <sstream>

namespace
{
Expand Down Expand Up @@ -42,7 +43,20 @@ void ToolTip::add(Control& c, const std::string& str)

void ToolTip::buildDrawParams(std::pair<Control*, std::string>& item, int mouseX)
{
const auto toolTipSize = mFont.size(item.second) + PaddingSize * 2;
const auto toolTipLineHeight = mFont.height();
const auto numberOfLines = static_cast<int>(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<int>{toolTipWidth, toolTipHeight};

auto tooltipPosition = item.first->position();

Expand Down Expand Up @@ -102,6 +116,14 @@ void ToolTip::draw() const
auto& renderer = NAS2D::Utility<NAS2D::Renderer>::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();
}
}
}

0 comments on commit 02dfa90

Please sign in to comment.