1
0
Fork 0

Change: Add horizontal scrollbar to Industry Directory window.

This list could be very wide depending on industries and language.
pull/11447/head
Peter Nelson 2023-11-04 14:44:19 +00:00 committed by Peter Nelson
parent 8ff6562b2f
commit badce415ea
2 changed files with 38 additions and 9 deletions

View File

@ -1237,10 +1237,11 @@ static const NWidgetPart _nested_industry_directory_widgets[] = {
NWidget(WWT_PANEL, COLOUR_BROWN, WID_ID_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), SetScrollbar(WID_ID_VSCROLLBAR), NWidget(WWT_PANEL, COLOUR_BROWN, WID_ID_INDUSTRY_LIST), SetDataTip(0x0, STR_INDUSTRY_DIRECTORY_LIST_CAPTION), SetResize(1, 1), SetScrollbar(WID_ID_VSCROLLBAR),
EndContainer(), EndContainer(),
EndContainer(), EndContainer(),
NWidget(NWID_VERTICAL), NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_ID_VSCROLLBAR),
NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_ID_SCROLLBAR), EndContainer(),
NWidget(WWT_RESIZEBOX, COLOUR_BROWN), NWidget(NWID_HORIZONTAL),
EndContainer(), NWidget(NWID_HSCROLLBAR, COLOUR_BROWN, WID_ID_HSCROLLBAR),
NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
EndContainer(), EndContainer(),
}; };
@ -1316,6 +1317,7 @@ protected:
GUIIndustryList industries; GUIIndustryList industries;
Scrollbar *vscroll; Scrollbar *vscroll;
Scrollbar *hscroll;
CargoID cargo_filter[NUM_CARGO + 2]; ///< Available cargo filters; CargoID or CF_ANY or CF_NONE CargoID cargo_filter[NUM_CARGO + 2]; ///< Available cargo filters; CargoID or CF_ANY or CF_NONE
StringID cargo_filter_texts[NUM_CARGO + 3]; ///< Texts for filter_cargo, terminated by INVALID_STRING_ID StringID cargo_filter_texts[NUM_CARGO + 3]; ///< Texts for filter_cargo, terminated by INVALID_STRING_ID
@ -1404,6 +1406,19 @@ protected:
this->industries.SetFilterState(is_filtering_necessary); this->industries.SetFilterState(is_filtering_necessary);
} }
/**
* Get the width needed to draw the longest industry line.
* @return Returns width of the longest industry line, including padding.
*/
uint GetIndustryListWidth() const
{
uint width = 0;
for (const Industry *i : this->industries) {
width = std::max(width, GetStringBoundingBox(this->GetIndustryString(i)).width);
}
return width + WidgetDimensions::scaled.framerect.Horizontal();
}
/** (Re)Build industries list */ /** (Re)Build industries list */
void BuildSortIndustriesList() void BuildSortIndustriesList()
{ {
@ -1429,6 +1444,7 @@ protected:
this->industries.Filter(filter); this->industries.Filter(filter);
this->hscroll->SetCount(this->GetIndustryListWidth()); this->hscroll->SetCount(this->GetIndustryListWidth());
this->vscroll->SetCount(this->industries.size()); // Update scrollbar as well.
} }
IndustryDirectoryWindow::produced_cargo_filter = this->cargo_filter[this->produced_cargo_filter_criteria]; IndustryDirectoryWindow::produced_cargo_filter = this->cargo_filter[this->produced_cargo_filter_criteria];
@ -1620,7 +1636,8 @@ public:
IndustryDirectoryWindow(WindowDesc *desc, WindowNumber) : Window(desc), industry_editbox(MAX_FILTER_LENGTH * MAX_CHAR_LENGTH, MAX_FILTER_LENGTH) IndustryDirectoryWindow(WindowDesc *desc, WindowNumber) : Window(desc), industry_editbox(MAX_FILTER_LENGTH * MAX_CHAR_LENGTH, MAX_FILTER_LENGTH)
{ {
this->CreateNestedTree(); this->CreateNestedTree();
this->vscroll = this->GetScrollbar(WID_ID_SCROLLBAR); this->vscroll = this->GetScrollbar(WID_ID_VSCROLLBAR);
this->hscroll = this->GetScrollbar(WID_ID_HSCROLLBAR);
this->industries.SetListing(this->last_sorting); this->industries.SetListing(this->last_sorting);
this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs); this->industries.SetSortFuncs(IndustryDirectoryWindow::sorter_funcs);
@ -1670,6 +1687,19 @@ public:
case WID_ID_INDUSTRY_LIST: { case WID_ID_INDUSTRY_LIST: {
Rect ir = r.Shrink(WidgetDimensions::scaled.framerect); Rect ir = r.Shrink(WidgetDimensions::scaled.framerect);
/* Setup a clipping rectangle... */
DrawPixelInfo tmp_dpi;
if (!FillDrawPixelInfo(&tmp_dpi, ir.left, ir.top, ir.Width(), ir.Height())) return;
/* ...but keep coordinates relative to the window. */
tmp_dpi.left += ir.left;
tmp_dpi.top += ir.top;
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
ir.left -= this->hscroll->GetPosition();
ir.right += this->hscroll->GetCapacity() - this->hscroll->GetPosition();
if (this->industries.empty()) { if (this->industries.empty()) {
DrawString(ir, STR_INDUSTRY_DIRECTORY_NONE); DrawString(ir, STR_INDUSTRY_DIRECTORY_NONE);
break; break;
@ -1718,9 +1748,6 @@ public:
case WID_ID_INDUSTRY_LIST: { case WID_ID_INDUSTRY_LIST: {
Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE); Dimension d = GetStringBoundingBox(STR_INDUSTRY_DIRECTORY_NONE);
for (uint i = 0; i < this->industries.size(); i++) {
d = maxdim(d, GetStringBoundingBox(this->GetIndustryString(this->industries[i])));
}
resize->height = d.height; resize->height = d.height;
d.height *= 5; d.height *= 5;
d.width += padding.width; d.width += padding.width;
@ -1794,6 +1821,7 @@ public:
void OnResize() override void OnResize() override
{ {
this->vscroll->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST); this->vscroll->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST);
this->hscroll->SetCapacityFromWidget(this, WID_ID_INDUSTRY_LIST);
} }
void OnEditboxChanged(int wid) override void OnEditboxChanged(int wid) override

View File

@ -39,7 +39,8 @@ enum IndustryDirectoryWidgets {
WID_ID_FILTER_BY_PROD_CARGO, ///< Produced cargo filter dropdown list. WID_ID_FILTER_BY_PROD_CARGO, ///< Produced cargo filter dropdown list.
WID_ID_FILTER, ///< Textbox to filter industry name. WID_ID_FILTER, ///< Textbox to filter industry name.
WID_ID_INDUSTRY_LIST, ///< Industry list. WID_ID_INDUSTRY_LIST, ///< Industry list.
WID_ID_SCROLLBAR, ///< Scrollbar of the list. WID_ID_HSCROLLBAR, ///< Horizontal scrollbar of the list.
WID_ID_VSCROLLBAR, ///< Vertical scrollbar of the list.
}; };
/** Widgets of the #IndustryCargoesWindow class */ /** Widgets of the #IndustryCargoesWindow class */