Added support to OptionListComponent to abbreviate non-multiselect entries.

This commit is contained in:
Leon Styhre 2021-10-09 13:06:04 +02:00
parent 33792baa53
commit 20760c1b43

View file

@ -176,12 +176,13 @@ public:
return selected.at(0);
}
void add(const std::string& name, const T& obj, bool selected)
void add(const std::string& name, const T& obj, bool selected, float maxNameLength = 0.0f)
{
OptionListData e;
e.name = name;
e.object = obj;
e.selected = selected;
e.maxNameLength = maxNameLength;
mEntries.push_back(e);
@ -293,6 +294,7 @@ private:
std::string name;
T object;
bool selected;
float maxNameLength;
};
HelpStyle mHelpStyle;
@ -329,7 +331,37 @@ private:
// Display the selected entry and left/right option arrows.
for (auto it = mEntries.cbegin(); it != mEntries.cend(); it++) {
if (it->selected) {
mText.setText(Utils::String::toUpper(it->name));
if (it->maxNameLength > 0.0f &&
Font::get(FONT_SIZE_MEDIUM)->sizeText(Utils::String::toUpper(it->name)).x >
it->maxNameLength) {
// A maximum length parameter has been passed and the "name" size surpasses
// this value, so abbreviate the string inside the arrows.
auto font = Font::get(FONT_SIZE_MEDIUM);
// Calculate with an extra dot to give some leeway.
float dotsSize = font->sizeText("....").x;
std::string abbreviatedString = font->getTextMaxWidth(
Utils::String::toUpper(it->name), it->maxNameLength);
float sizeDifference = font->sizeText(Utils::String::toUpper(it->name)).x -
font->sizeText(abbreviatedString).x;
if (sizeDifference > 0.0f) {
// It doesn't make sense to abbreviate if the number of pixels removed
// by the abbreviation is less or equal to the size of the three dots
// that would be appended to the string.
if (sizeDifference <= dotsSize) {
abbreviatedString = it->name;
}
else {
if (abbreviatedString.back() == ' ')
abbreviatedString.pop_back();
abbreviatedString += "...";
}
}
mText.setText(Utils::String::toUpper(abbreviatedString));
}
else {
mText.setText(Utils::String::toUpper(it->name));
}
mText.setSize(0.0f, mText.getSize().y);
setSize(mText.getSize().x + mLeftArrow.getSize().x + mRightArrow.getSize().x +
24.0f * Renderer::getScreenWidthModifier(),