Change: Cargo flow legend only shows defined cargo. (#10872)

This removes the large mostly-empty grid of cargo buttons when not using a complex industry set.
This commit is contained in:
PeterN 2023-05-29 06:48:43 +01:00 committed by GitHub
parent c9c6721a47
commit 702194cfef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 38 deletions

View File

@ -474,30 +474,32 @@ NWidgetBase *MakeSaturationLegendLinkGraphGUI(int *biggest_index)
NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index) NWidgetBase *MakeCargoesLegendLinkGraphGUI(int *biggest_index)
{ {
static const uint ENTRIES_PER_ROW = CeilDiv(NUM_CARGO, 5); uint num_cargo = static_cast<uint>(_sorted_cargo_specs.size());
NWidgetVertical *panel = new NWidgetVertical(NC_EQUALSIZE); static const uint ENTRIES_PER_COL = 5;
NWidgetHorizontal *row = nullptr; NWidgetHorizontal *panel = new NWidgetHorizontal(NC_EQUALSIZE);
for (uint i = 0; i < NUM_CARGO; ++i) { NWidgetVertical *col = nullptr;
if (i % ENTRIES_PER_ROW == 0) {
if (row) panel->Add(row); for (uint i = 0; i < num_cargo; ++i) {
row = new NWidgetHorizontal(NC_EQUALSIZE); if (i % ENTRIES_PER_COL == 0) {
if (col != nullptr) panel->Add(col);
col = new NWidgetVertical(NC_EQUALSIZE);
} }
NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST); NWidgetBackground * wid = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, i + WID_LGL_CARGO_FIRST);
wid->SetMinimalSize(25, 0); wid->SetMinimalSize(25, 0);
wid->SetMinimalTextLines(1, 0, FS_SMALL); wid->SetMinimalTextLines(1, 0, FS_SMALL);
wid->SetFill(1, 1); wid->SetFill(1, 1);
wid->SetResize(0, 0); wid->SetResize(0, 0);
row->Add(wid); col->Add(wid);
} }
/* Fill up last row */ /* Fill up last row */
for (uint i = 0; i < 4 - (NUM_CARGO - 1) % 5; ++i) { for (uint i = num_cargo; i < Ceil(num_cargo, ENTRIES_PER_COL); ++i) {
NWidgetSpacer *spc = new NWidgetSpacer(25, 0); NWidgetSpacer *spc = new NWidgetSpacer(25, 0);
spc->SetMinimalTextLines(1, 0, FS_SMALL); spc->SetMinimalTextLines(1, 0, FS_SMALL);
spc->SetFill(1, 1); spc->SetFill(1, 1);
spc->SetResize(0, 0); spc->SetResize(0, 0);
row->Add(spc); col->Add(spc);
} }
panel->Add(row); panel->Add(col);
*biggest_index = WID_LGL_CARGO_LAST; *biggest_index = WID_LGL_CARGO_LAST;
return panel; return panel;
} }
@ -553,6 +555,8 @@ void ShowLinkGraphLegend()
LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc) LinkGraphLegendWindow::LinkGraphLegendWindow(WindowDesc *desc, int window_number) : Window(desc)
{ {
this->num_cargo = _sorted_cargo_specs.size();
this->InitNested(window_number); this->InitNested(window_number);
this->InvalidateData(0); this->InvalidateData(0);
this->SetOverlay(GetMainWindow()->viewport->overlay); this->SetOverlay(GetMainWindow()->viewport->overlay);
@ -571,10 +575,8 @@ void LinkGraphLegendWindow::SetOverlay(std::shared_ptr<LinkGraphOverlay> overlay
} }
} }
CargoTypes cargoes = this->overlay->GetCargoMask(); CargoTypes cargoes = this->overlay->GetCargoMask();
for (uint c = 0; c < NUM_CARGO; c++) { for (uint c = 0; c < this->num_cargo; c++) {
if (!this->IsWidgetDisabled(WID_LGL_CARGO_FIRST + c)) { this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, _sorted_cargo_specs[c]->Index()));
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, HasBit(cargoes, c));
}
} }
} }
@ -597,13 +599,11 @@ void LinkGraphLegendWindow::UpdateWidgetSize(int widget, Dimension *size, const
} }
} }
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) { if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST); const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
if (cargo->IsValid()) { Dimension dim = GetStringBoundingBox(cargo->abbrev, FS_SMALL);
Dimension dim = GetStringBoundingBox(cargo->abbrev, FS_SMALL); dim.width += padding.width;
dim.width += padding.width; dim.height += padding.height;
dim.height += padding.height; *size = maxdim(*size, dim);
*size = maxdim(*size, dim);
}
} }
} }
@ -633,8 +633,7 @@ void LinkGraphLegendWindow::DrawWidget(const Rect &r, int widget) const
} }
} }
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) { if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
if (this->IsWidgetDisabled(widget)) return; const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
GfxFillRect(br, cargo->legend_colour); GfxFillRect(br, cargo->legend_colour);
DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, FONT_HEIGHT_SMALL), cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER, false, FS_SMALL); DrawString(br.left, br.right, CenterBounds(br.top, br.bottom, FONT_HEIGHT_SMALL), cargo->abbrev, GetContrastColour(cargo->legend_colour, 73), SA_HOR_CENTER, false, FS_SMALL);
} }
@ -655,8 +654,7 @@ bool LinkGraphLegendWindow::OnTooltip(Point pt, int widget, TooltipCloseConditio
return true; return true;
} }
if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) { if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
if (this->IsWidgetDisabled(widget)) return false; const CargoSpec *cargo = _sorted_cargo_specs[widget - WID_LGL_CARGO_FIRST];
CargoSpec *cargo = CargoSpec::Get(widget - WID_LGL_CARGO_FIRST);
GuiShowTooltips(this, cargo->name, 0, nullptr, close_cond); GuiShowTooltips(this, cargo->name, 0, nullptr, close_cond);
return true; return true;
} }
@ -683,10 +681,9 @@ void LinkGraphLegendWindow::UpdateOverlayCompanies()
void LinkGraphLegendWindow::UpdateOverlayCargoes() void LinkGraphLegendWindow::UpdateOverlayCargoes()
{ {
CargoTypes mask = 0; CargoTypes mask = 0;
for (uint c = 0; c < NUM_CARGO; c++) { for (uint c = 0; c < num_cargo; c++) {
if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue; if (!this->IsWidgetLowered(c + WID_LGL_CARGO_FIRST)) continue;
SetBit(mask, c); SetBit(mask, _sorted_cargo_specs[c]->Index());
} }
this->overlay->SetCargoMask(mask); this->overlay->SetCargoMask(mask);
} }
@ -707,13 +704,10 @@ void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
this->UpdateOverlayCompanies(); this->UpdateOverlayCompanies();
this->SetDirty(); this->SetDirty();
} else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) { } else if (IsInsideMM(widget, WID_LGL_CARGO_FIRST, WID_LGL_CARGO_LAST + 1)) {
if (!this->IsWidgetDisabled(widget)) { this->ToggleWidgetLoweredState(widget);
this->ToggleWidgetLoweredState(widget); this->UpdateOverlayCargoes();
this->UpdateOverlayCargoes();
}
} else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) { } else if (widget == WID_LGL_CARGOES_ALL || widget == WID_LGL_CARGOES_NONE) {
for (uint c = 0; c < NUM_CARGO; c++) { for (uint c = 0; c < this->num_cargo; c++) {
if (this->IsWidgetDisabled(c + WID_LGL_CARGO_FIRST)) continue;
this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL); this->SetWidgetLoweredState(WID_LGL_CARGO_FIRST + c, widget == WID_LGL_CARGOES_ALL);
} }
this->UpdateOverlayCargoes(); this->UpdateOverlayCargoes();
@ -728,11 +722,13 @@ void LinkGraphLegendWindow::OnClick(Point pt, int widget, int click_count)
*/ */
void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope) void LinkGraphLegendWindow::OnInvalidateData(int data, bool gui_scope)
{ {
if (this->num_cargo != _sorted_cargo_specs.size()) {
this->Close();
return;
}
/* Disable the companies who are not active */ /* Disable the companies who are not active */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) { for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i)); this->SetWidgetDisabledState(i + WID_LGL_COMPANY_FIRST, !Company::IsValidID(i));
} }
for (CargoID i = 0; i < NUM_CARGO; i++) {
this->SetWidgetDisabledState(i + WID_LGL_CARGO_FIRST, !CargoSpec::Get(i)->IsValid());
}
} }

View File

@ -116,6 +116,7 @@ public:
private: private:
std::shared_ptr<LinkGraphOverlay> overlay; std::shared_ptr<LinkGraphOverlay> overlay;
size_t num_cargo;
void UpdateOverlayCompanies(); void UpdateOverlayCompanies();
void UpdateOverlayCargoes(); void UpdateOverlayCargoes();