Pārlūkot izejas kodu

Fix text trimming in object window

- Do not attempt to preserve object count in string when string does not
contains object count
- Correctly look up last bracket, to correctly handle object names that
contain brackets
Ivan Savenko 1 mēnesi atpakaļ
vecāks
revīzija
483e170f6b
2 mainītis faili ar 17 papildinājumiem un 9 dzēšanām
  1. 16 8
      client/windows/GUIClasses.cpp
  2. 1 1
      client/windows/GUIClasses.h

+ 16 - 8
client/windows/GUIClasses.cpp

@@ -1596,7 +1596,7 @@ CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::share
 	for(int id : _items)
 	{
 		std::string objectName = GAME->interface()->cb->getObjInstance(ObjectInstanceID(id))->getObjectName();
-		trimTextIfTooWide(objectName);
+		trimTextIfTooWide(objectName, false);
 		items.emplace_back(id, objectName);
 	}
 	itemsVisible = items;
@@ -1620,7 +1620,7 @@ CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, st
 	for(size_t i = 0; i < _items.size(); i++)
 	{
 		std::string objectName = _items[i];
-		trimTextIfTooWide(objectName);
+		trimTextIfTooWide(objectName, true);
 		items.emplace_back(static_cast<int>(i), objectName);
 	}
 	itemsVisible = items;
@@ -1664,16 +1664,24 @@ void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::stri
 	searchBox->setCallback(std::bind(&CObjectListWindow::itemsSearchCallback, this, std::placeholders::_1));
 }
 
-void CObjectListWindow::trimTextIfTooWide(std::string & text) const
+void CObjectListWindow::trimTextIfTooWide(std::string & text, bool preserveCountSuffix) const
 {
+	std::string suffix = "...";
 	int maxWidth = pos.w - 60;	// 60 px for scrollbar and borders
-	auto posBrace = text.find('(');
-	auto posClosing = text.find(')');
-	std::string objCount = text.substr(posBrace, posClosing - posBrace) + ')';
+
 	if(text[0] == '{')
-		objCount = '}' + objCount;
+		suffix += "}";
+
+	if (preserveCountSuffix)
+	{
+		auto posBrace = text.find_last_of("(");
+		auto posClosing = text.find_last_of(")");
+		std::string objCount = text.substr(posBrace, posClosing - posBrace) + ')';
+		suffix += " ";
+		suffix += objCount;
+	}
+
 	const auto & font = ENGINE->renderHandler().loadFont(FONT_SMALL);
-	std::string suffix = "... " + objCount;
 
 	if(font->getStringWidth(text) >= maxWidth)
 	{

+ 1 - 1
client/windows/GUIClasses.h

@@ -206,7 +206,7 @@ class CObjectListWindow : public CWindowObject
 	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 trimTextIfTooWide(std::string & text) const; // trim item's text to fit within window's width
+	void trimTextIfTooWide(std::string & text, bool preserveCountSuffix) const; // trim item's text to fit within window's width
 	void itemsSearchCallback(const std::string & text);
 	void exitPressed();
 public: