MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / MakeCompanyButtonRows

Function MakeCompanyButtonRows

src/widget.cpp:3471–3514  ·  view source on GitHub ↗

* Make a number of rows with button-like graphics, for enabling/disabling each company. * @param widget_first The first widget index to use. * @param widget_last The last widget index to use. * @param colour The colour in which to draw the button. * @param max_length Maximal number of company buttons in one row. * @param button_tooltip The tooltip-string of every button. * @param resizable W

Source from the content-addressed store, hash-verified

3469 * @return Panel with rows of company buttons.
3470 */
3471std::unique_ptr<NWidgetBase> MakeCompanyButtonRows(WidgetID widget_first, WidgetID widget_last, Colours button_colour, int max_length, StringID button_tooltip, bool resizable)
3472{
3473 assert(max_length >= 1);
3474 std::unique_ptr<NWidgetVertical> vert = nullptr; // Storage for all rows.
3475 std::unique_ptr<NWidgetHorizontal> hor = nullptr; // Storage for buttons in one row.
3476 int hor_length = 0;
3477
3478 Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON, nullptr, ZoomLevel::Normal);
3479 sprite_size.width += WidgetDimensions::unscaled.matrix.Horizontal();
3480 sprite_size.height += WidgetDimensions::unscaled.matrix.Vertical();
3481
3482 for (WidgetID widnum = widget_first; widnum <= widget_last; widnum++) {
3483 /* Ensure there is room in 'hor' for another button. */
3484 if (hor_length == max_length) {
3485 if (vert == nullptr) vert = std::make_unique<NWidgetVertical>();
3486 vert->Add(std::move(hor));
3487 hor = nullptr;
3488 hor_length = 0;
3489 }
3490 if (hor == nullptr) {
3491 hor = std::make_unique<NWidgetHorizontal>();
3492 hor_length = 0;
3493 }
3494
3495 auto panel = std::make_unique<NWidgetBackground>(WWT_PANEL, button_colour, widnum);
3496 panel->SetMinimalSize(sprite_size.width, sprite_size.height);
3497 panel->SetFill(1, 1);
3498 if (resizable) panel->SetResize(1, 0);
3499 panel->SetToolTip(button_tooltip);
3500 hor->Add(std::move(panel));
3501 hor_length++;
3502 }
3503 if (vert == nullptr) return hor; // All buttons fit in a single row.
3504
3505 if (hor_length > 0 && hor_length < max_length) {
3506 /* Last row is partial, add a spacer at the end to force all buttons to the left. */
3507 auto spc = std::make_unique<NWidgetSpacer>(sprite_size.width, sprite_size.height);
3508 spc->SetFill(1, 1);
3509 if (resizable) spc->SetResize(1, 0);
3510 hor->Add(std::move(spc));
3511 }
3512 if (hor != nullptr) vert->Add(std::move(hor));
3513 return vert;
3514}
3515
3516/**
3517 * Unfocuses the focused widget of the window,

Calls 8

GetSpriteSizeFunction · 0.85
HorizontalMethod · 0.80
VerticalMethod · 0.80
SetMinimalSizeMethod · 0.80
SetFillMethod · 0.80
SetResizeMethod · 0.80
SetToolTipMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected