Changeset - r26698:959ec0cf0684
[Not reviewed]
master
0 4 0
PeterN - 22 months ago 2022-12-27 18:39:37
peter1138@openttd.org
Change: Make vehicle list dropdown buttons resize to fit strings. (#10286)
4 files changed with 76 insertions and 20 deletions:
0 comments (0 inline, 0 general)
src/gfx.cpp
Show inline comments
 
@@ -920,12 +920,27 @@ Dimension GetStringBoundingBox(StringID 
 

	
 
	GetString(buffer, strid, lastof(buffer));
 
	return GetStringBoundingBox(buffer, start_fontsize);
 
}
 

	
 
/**
 
 * Get maximum width of a list of strings.
 
 * @param list List of strings, terminated with INVALID_STRING_ID.
 
 * @param fontsize Font size to use.
 
 * @return Width of longest string within the list.
 
 */
 
uint GetStringListWidth(const StringID *list, FontSize fontsize)
 
{
 
	uint width = 0;
 
	for (const StringID *str = list; *str != INVALID_STRING_ID; str++) {
 
		width = std::max(width, GetStringBoundingBox(*str, fontsize).width);
 
	}
 
	return width;
 
}
 

	
 
/**
 
 * Get the leading corner of a character in a single-line string relative
 
 * to the start of the string.
 
 * @param str String containing the character.
 
 * @param ch Pointer to the character in the string.
 
 * @param start_fontsize Font size to start the text with.
 
 * @return Upper left corner of the glyph associated with the character.
src/gfx_func.h
Show inline comments
 
@@ -145,12 +145,13 @@ static inline void GfxFillRect(const Rec
 
	GfxFillRect(r.left, r.top, r.right, r.bottom, colour, mode);
 
}
 

	
 
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize = FS_NORMAL);
 
Dimension GetStringBoundingBox(const std::string &str, FontSize start_fontsize = FS_NORMAL);
 
Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize = FS_NORMAL);
 
uint GetStringListWidth(const StringID *list, FontSize fontsize = FS_NORMAL);
 
int GetStringHeight(const char *str, int maxw, FontSize fontsize = FS_NORMAL);
 
int GetStringHeight(StringID str, int maxw);
 
int GetStringLineCount(StringID str, int maxw);
 
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
 
Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion);
 
void LoadStringWidthTable(bool monospace = false);
src/group_gui.cpp
Show inline comments
 
@@ -24,12 +24,13 @@
 
#include "core/geometry_func.hpp"
 
#include "company_base.h"
 
#include "company_gui.h"
 
#include "gui.h"
 
#include "group_cmd.h"
 
#include "vehicle_cmd.h"
 
#include "gfx_func.h"
 

	
 
#include "widgets/group_widget.h"
 

	
 
#include "table/sprites.h"
 

	
 
#include "safeguards.h"
 
@@ -69,21 +70,27 @@ static const NWidgetPart _nested_group_w
 
						SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN, STR_GROUP_REPLACE_PROTECTION_TOOLTIP),
 
			EndContainer(),
 
		EndContainer(),
 
		/* right part */
 
		NWidget(NWID_VERTICAL),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GL_GROUP_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_STATION_VIEW_GROUP, STR_TOOLTIP_GROUP_ORDER),
 
				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_GROUP_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
 
				NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
 
			EndContainer(),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_SORT_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
 
				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_SORT_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
 
				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_FILTER_BY_CARGO), SetMinimalSize(167, 12), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_FILTER_CRITERIA),
 
				NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
 
				NWidget(NWID_VERTICAL),
 
					NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GL_GROUP_BY_ORDER), SetFill(1, 0), SetMinimalSize(0, 12), SetDataTip(STR_STATION_VIEW_GROUP, STR_TOOLTIP_GROUP_ORDER),
 
					NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_SORT_BY_ORDER), SetFill(1, 0), SetMinimalSize(0, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
 
				EndContainer(),
 
				NWidget(NWID_VERTICAL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_GROUP_BY_DROPDOWN), SetFill(1, 0), SetMinimalSize(0, 12), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_SORT_BY_DROPDOWN), SetFill(1, 0), SetMinimalSize(0, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
 
				EndContainer(),
 
				NWidget(NWID_VERTICAL),
 
					NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetResize(1, 0), EndContainer(),
 
					NWidget(NWID_HORIZONTAL),
 
						NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_FILTER_BY_CARGO), SetMinimalSize(0, 12), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_FILTER_CRITERIA),
 
						NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetResize(1, 0), EndContainer(),
 
					EndContainer(),
 
				EndContainer(),
 
			EndContainer(),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(WWT_MATRIX, COLOUR_GREY, WID_GL_LIST_VEHICLE), SetMinimalSize(248, 0), SetMatrixDataTip(1, 0, STR_NULL), SetResize(1, 1), SetFill(1, 0), SetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR),
 
				NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_VEHICLE_SCROLLBAR),
 
			EndContainer(),
 
			NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
 
@@ -403,12 +410,26 @@ public:
 
			case WID_GL_LIST_VEHICLE:
 
				this->ComputeGroupInfoSize();
 
				resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
 
				size->height = 4 * resize->height;
 
				break;
 

	
 
			case WID_GL_GROUP_BY_DROPDOWN:
 
				size->width = GetStringListWidth(this->vehicle_group_by_names) + padding.width;
 
				break;
 

	
 
			case WID_GL_SORT_BY_DROPDOWN:
 
				size->width = GetStringListWidth(this->vehicle_group_none_sorter_names);
 
				size->width = std::max(size->width, GetStringListWidth(this->vehicle_group_shared_orders_sorter_names));
 
				size->width += padding.width;
 
				break;
 

	
 
			case WID_GL_FILTER_BY_CARGO:
 
				size->width = GetStringListWidth(this->cargo_filter_texts) + padding.width;
 
				break;
 

	
 
			case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
 
				Dimension d = this->GetActionDropdownSize(true, true);
 
				d.height += padding.height;
 
				d.width  += padding.width;
 
				*size = maxdim(*size, d);
 
				break;
src/vehicle_gui.cpp
Show inline comments
 
@@ -1501,24 +1501,29 @@ static const NWidgetPart _nested_vehicle
 
		NWidget(WWT_SHADEBOX, COLOUR_GREY),
 
		NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
 
		NWidget(WWT_STICKYBOX, COLOUR_GREY),
 
	EndContainer(),
 

	
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_VL_GROUP_ORDER), SetMinimalSize(81, 12), SetFill(0, 1), SetDataTip(STR_STATION_VIEW_GROUP, STR_TOOLTIP_GROUP_ORDER),
 
		NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VL_GROUP_BY_PULLDOWN), SetMinimalSize(167, 12), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
 
		NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
 
	EndContainer(),
 

	
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VL_SORT_ORDER), SetMinimalSize(81, 12), SetFill(0, 1), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
 
		NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VL_SORT_BY_PULLDOWN), SetMinimalSize(167, 12), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
 
		NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VL_FILTER_BY_CARGO_SEL),
 
			NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VL_FILTER_BY_CARGO), SetMinimalSize(167, 12), SetFill(0, 1), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_FILTER_CRITERIA),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_VL_GROUP_ORDER), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_STATION_VIEW_GROUP, STR_TOOLTIP_GROUP_ORDER),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VL_SORT_ORDER), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
 
		EndContainer(),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VL_GROUP_BY_PULLDOWN), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
 
			NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VL_SORT_BY_PULLDOWN), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
 
		EndContainer(),
 
		NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VL_FILTER_BY_CARGO_SEL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VL_FILTER_BY_CARGO), SetMinimalSize(0, 12), SetFill(0, 0), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_FILTER_CRITERIA),
 
				EndContainer(),
 
				NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
 
			EndContainer(),
 
		EndContainer(),
 
	EndContainer(),
 

	
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_MATRIX, COLOUR_GREY, WID_VL_LIST), SetMinimalSize(248, 0), SetFill(1, 0), SetResize(1, 1), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_VL_SCROLLBAR),
 
		NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_VL_SCROLLBAR),
 
	EndContainer(),
 
@@ -1865,12 +1870,26 @@ public:
 
				d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
 
				d.height += padding.height;
 
				*size = maxdim(*size, d);
 
				break;
 
			}
 

	
 
			case WID_VL_GROUP_BY_PULLDOWN:
 
				size->width = GetStringListWidth(this->vehicle_group_by_names) + padding.width;
 
				break;
 

	
 
			case WID_VL_SORT_BY_PULLDOWN:
 
				size->width = GetStringListWidth(this->vehicle_group_none_sorter_names);
 
				size->width = std::max(size->width, GetStringListWidth(this->vehicle_group_shared_orders_sorter_names));
 
				size->width += padding.width;
 
				break;
 

	
 
			case WID_VL_FILTER_BY_CARGO:
 
				size->width = GetStringListWidth(this->cargo_filter_texts) + padding.width;
 
				break;
 

	
 
			case WID_VL_MANAGE_VEHICLES_DROPDOWN: {
 
				Dimension d = this->GetActionDropdownSize(this->vli.type == VL_STANDARD, false);
 
				d.height += padding.height;
 
				d.width  += padding.width;
 
				*size = maxdim(*size, d);
 
				break;
0 comments (0 inline, 0 general)