Browse Source

Fix `RichTextLabel` column size growing beyond its calculated `max_width`

When `RichTextLabel` calculated the actual width of columns, it let
them grow to sizes greater than its calculated `max_width`. Now this
PR ensures no columns grows beyond `max_width`, and the excess width
is distributed among the columns which can still grow.

It should fix #17731.
robfram 7 years ago
parent
commit
ef2b7b090c
1 changed files with 33 additions and 0 deletions
  1. 33 0
      scene/gui/rich_text_label.cpp

+ 33 - 0
scene/gui/rich_text_label.cpp

@@ -516,6 +516,39 @@ int RichTextLabel::_process_line(ItemFrame *p_frame, const Vector2 &p_ofs, int &
 						table->total_width += table->columns[i].width + hseparation;
 						table->total_width += table->columns[i].width + hseparation;
 					}
 					}
 
 
+					//resize to max_width if needed and distribute the remaining space
+					bool table_need_fit = true;
+					while (table_need_fit) {
+						table_need_fit = false;
+						//fit slim
+						for (int i = 0; i < table->columns.size(); i++) {
+							if (!table->columns[i].expand)
+								continue;
+							int dif = table->columns[i].width - table->columns[i].max_width;
+							if (dif > 0) {
+								table_need_fit = true;
+								table->columns[i].width = table->columns[i].max_width;
+								table->total_width -= dif;
+								total_ratio -= table->columns[i].expand_ratio;
+							}
+						}
+						//grow
+						remaining_width = available_width - table->total_width;
+						if (remaining_width > 0 && total_ratio > 0) {
+							for (int i = 0; i < table->columns.size(); i++) {
+								if (table->columns[i].expand) {
+									int dif = table->columns[i].max_width - table->columns[i].width;
+									if (dif > 0) {
+										int slice = table->columns[i].expand_ratio * remaining_width / total_ratio;
+										int incr = MIN(dif, slice);
+										table->columns[i].width += incr;
+										table->total_width += incr;
+									}
+								}
+							}
+						}
+					}
+
 					//compute caches properly again with the right width
 					//compute caches properly again with the right width
 					idx = 0;
 					idx = 0;
 					for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {
 					for (List<Item *>::Element *E = table->subitems.front(); E; E = E->next()) {