Skip to content

Commit

Permalink
Optional Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar139 committed Dec 27, 2024
1 parent edff544 commit 24bbc01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
30 changes: 15 additions & 15 deletions libControls/ToolTip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(const std::string&)>& lineProcessor)
void forEachLine(const std::string& text, const std::function<void(const std::string&)>& 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;
}
}


Expand Down Expand Up @@ -57,7 +68,7 @@ void ToolTip::buildDrawParams(std::pair<Control*, std::string>& item, int mouseX
const auto toolTipLineHeight = mFont.height();
const auto numberOfLines = static_cast<int>(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();
Expand Down Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions libControls/ToolTip.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ class ToolTip : public Control
std::pair<Control*, std::string>* mFocusedControl{nullptr};

std::vector<std::pair<Control*, std::string>> mControls;

int calculateMaxWidthStringSize(const std::string& text) const;
};

0 comments on commit 24bbc01

Please sign in to comment.