1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

Fix trimTextIfTooWide function

This commit is contained in:
Michał Zaremba
2025-10-17 20:46:00 +02:00
parent e81d5bc973
commit 30c58f0ffa
2 changed files with 11 additions and 9 deletions

View File

@@ -1596,7 +1596,7 @@ CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::share
for(int id : _items) for(int id : _items)
{ {
std::string objectName = GAME->interface()->cb->getObjInstance(ObjectInstanceID(id))->getObjectName(); std::string objectName = GAME->interface()->cb->getObjInstance(ObjectInstanceID(id))->getObjectName();
trimTextIfTooWide(objectName, id); trimTextIfTooWide(objectName);
items.emplace_back(id, objectName); items.emplace_back(id, objectName);
} }
itemsVisible = items; itemsVisible = items;
@@ -1620,7 +1620,7 @@ CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, st
for(size_t i = 0; i < _items.size(); i++) for(size_t i = 0; i < _items.size(); i++)
{ {
std::string objectName = _items[i]; std::string objectName = _items[i];
trimTextIfTooWide(objectName, static_cast<int>(i)); trimTextIfTooWide(objectName);
items.emplace_back(static_cast<int>(i), objectName); items.emplace_back(static_cast<int>(i), objectName);
} }
itemsVisible = items; itemsVisible = items;
@@ -1664,19 +1664,21 @@ void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::stri
searchBox->setCallback(std::bind(&CObjectListWindow::itemsSearchCallback, this, std::placeholders::_1)); searchBox->setCallback(std::bind(&CObjectListWindow::itemsSearchCallback, this, std::placeholders::_1));
} }
void CObjectListWindow::trimTextIfTooWide(std::string & text, int id) const void CObjectListWindow::trimTextIfTooWide(std::string & text) const
{ {
int maxWidth = pos.w - 60; // 60 px for scrollbar and borders int maxWidth = pos.w - 60; // 60 px for scrollbar and borders
std::string idStr = '(' + std::to_string(id) + ')'; auto posBrace = text.find('(');
auto posClosing = text.find(')');
std::string objCount = text.substr(posBrace, posClosing - posBrace) + ')';
if(text[0] == '{') if(text[0] == '{')
idStr = '}' + idStr; objCount = '}' + objCount;
const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL); const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL);
std::string suffix = " ... " + idStr; std::string suffix = "... " + objCount;
if(font->getStringWidth(text) >= maxWidth) if(font->getStringWidth(text) >= maxWidth)
{ {
logGlobal->warn("Mapobject name '%s' is too long and probably needs to be fixed! Trimming...", logGlobal->trace("Mapobject name '%s' is too long and probably needs to be fixed! "
text.substr(0, text.size() - idStr.size() + 1)); "Trimming it to fit into CObjectListWindow...", text);
// Trim text until it fits // Trim text until it fits
while(!text.empty()) while(!text.empty())

View File

@@ -206,7 +206,7 @@ class CObjectListWindow : public CWindowObject
std::vector< std::pair<int, std::string> > itemsVisible; //visible items present in list std::vector< std::pair<int, std::string> > itemsVisible; //visible items present in list
void init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, bool searchBoxEnabled); void init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, bool searchBoxEnabled);
void trimTextIfTooWide(std::string & text, int id) const; // trim item's text to fit within window's width void trimTextIfTooWide(std::string & text) const; // trim item's text to fit within window's width
void itemsSearchCallback(const std::string & text); void itemsSearchCallback(const std::string & text);
void exitPressed(); void exitPressed();
public: public: