Update: Translations from eints
[openttd-github.git] / src / company_gui.cpp
blobeed9fa3f02cc393c31d67779bd778e19a1f11b7b
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file company_gui.cpp %Company related GUIs. */
10 #include "stdafx.h"
11 #include "currency.h"
12 #include "error.h"
13 #include "gui.h"
14 #include "window_gui.h"
15 #include "textbuf_gui.h"
16 #include "viewport_func.h"
17 #include "company_func.h"
18 #include "command_func.h"
19 #include "network/network.h"
20 #include "network/network_gui.h"
21 #include "network/network_func.h"
22 #include "newgrf.h"
23 #include "company_manager_face.h"
24 #include "strings_func.h"
25 #include "timer/timer_game_economy.h"
26 #include "dropdown_type.h"
27 #include "dropdown_common_type.h"
28 #include "tilehighlight_func.h"
29 #include "company_base.h"
30 #include "core/geometry_func.hpp"
31 #include "object_type.h"
32 #include "rail.h"
33 #include "road.h"
34 #include "engine_base.h"
35 #include "window_func.h"
36 #include "road_func.h"
37 #include "water.h"
38 #include "station_func.h"
39 #include "zoom_func.h"
40 #include "sortlist_type.h"
41 #include "company_cmd.h"
42 #include "economy_cmd.h"
43 #include "group_cmd.h"
44 #include "group_gui.h"
45 #include "misc_cmd.h"
46 #include "object_cmd.h"
47 #include "timer/timer.h"
48 #include "timer/timer_window.h"
50 #include "widgets/company_widget.h"
52 #include "safeguards.h"
55 /** Company GUI constants. */
56 static void DoSelectCompanyManagerFace(Window *parent);
57 static void ShowCompanyInfrastructure(CompanyID company);
59 /** List of revenues. */
60 static const std::initializer_list<ExpensesType> _expenses_list_revenue = {
61 EXPENSES_TRAIN_REVENUE,
62 EXPENSES_ROADVEH_REVENUE,
63 EXPENSES_AIRCRAFT_REVENUE,
64 EXPENSES_SHIP_REVENUE,
67 /** List of operating expenses. */
68 static const std::initializer_list<ExpensesType> _expenses_list_operating_costs = {
69 EXPENSES_TRAIN_RUN,
70 EXPENSES_ROADVEH_RUN,
71 EXPENSES_AIRCRAFT_RUN,
72 EXPENSES_SHIP_RUN,
73 EXPENSES_PROPERTY,
74 EXPENSES_LOAN_INTEREST,
77 /** List of capital expenses. */
78 static const std::initializer_list<ExpensesType> _expenses_list_capital_costs = {
79 EXPENSES_CONSTRUCTION,
80 EXPENSES_NEW_VEHICLES,
81 EXPENSES_OTHER,
84 /** Expense list container. */
85 struct ExpensesList {
86 const StringID title; ///< StringID of list title.
87 const std::initializer_list<ExpensesType> &items; ///< List of expenses types.
89 ExpensesList(StringID title, const std::initializer_list<ExpensesType> &list) : title(title), items(list)
93 uint GetHeight() const
95 /* Add up the height of all the lines. */
96 return static_cast<uint>(this->items.size()) * GetCharacterHeight(FS_NORMAL);
99 /** Compute width of the expenses categories in pixels. */
100 uint GetListWidth() const
102 uint width = 0;
103 for (const ExpensesType &et : this->items) {
104 width = std::max(width, GetStringBoundingBox(STR_FINANCES_SECTION_CONSTRUCTION + et).width);
106 return width;
110 /** Types of expense lists */
111 static const std::initializer_list<ExpensesList> _expenses_list_types = {
112 { STR_FINANCES_REVENUE_TITLE, _expenses_list_revenue },
113 { STR_FINANCES_OPERATING_EXPENSES_TITLE, _expenses_list_operating_costs },
114 { STR_FINANCES_CAPITAL_EXPENSES_TITLE, _expenses_list_capital_costs },
118 * Get the total height of the "categories" column.
119 * @return The total height in pixels.
121 static uint GetTotalCategoriesHeight()
123 /* There's an empty line and blockspace on the year row */
124 uint total_height = GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide;
126 for (const ExpensesList &list : _expenses_list_types) {
127 /* Title + expense list + total line + total + blockspace after category */
128 total_height += GetCharacterHeight(FS_NORMAL) + list.GetHeight() + WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide;
131 /* Total income */
132 total_height += WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide;
134 return total_height;
138 * Get the required width of the "categories" column, equal to the widest element.
139 * @return The required width in pixels.
141 static uint GetMaxCategoriesWidth()
143 uint max_width = GetStringBoundingBox(TimerGameEconomy::UsingWallclockUnits() ? STR_FINANCES_PERIOD_CAPTION : STR_FINANCES_YEAR_CAPTION).width;
145 /* Loop through categories to check max widths. */
146 for (const ExpensesList &list : _expenses_list_types) {
147 /* Title of category */
148 max_width = std::max(max_width, GetStringBoundingBox(list.title).width);
149 /* Entries in category */
150 max_width = std::max(max_width, list.GetListWidth() + WidgetDimensions::scaled.hsep_indent);
153 return max_width;
157 * Draw a category of expenses (revenue, operating expenses, capital expenses).
159 static void DrawCategory(const Rect &r, int start_y, const ExpensesList &list)
161 Rect tr = r.Indent(WidgetDimensions::scaled.hsep_indent, _current_text_dir == TD_RTL);
163 tr.top = start_y;
165 for (const ExpensesType &et : list.items) {
166 DrawString(tr, STR_FINANCES_SECTION_CONSTRUCTION + et);
167 tr.top += GetCharacterHeight(FS_NORMAL);
172 * Draw the expenses categories.
173 * @param r Available space for drawing.
174 * @note The environment must provide padding at the left and right of \a r.
176 static void DrawCategories(const Rect &r)
178 int y = r.top;
179 /* Draw description of 12-minute economic period. */
180 DrawString(r.left, r.right, y, (TimerGameEconomy::UsingWallclockUnits() ? STR_FINANCES_PERIOD_CAPTION : STR_FINANCES_YEAR_CAPTION), TC_FROMSTRING, SA_LEFT, true);
181 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide;
183 for (const ExpensesList &list : _expenses_list_types) {
184 /* Draw category title and advance y */
185 DrawString(r.left, r.right, y, list.title, TC_FROMSTRING, SA_LEFT);
186 y += GetCharacterHeight(FS_NORMAL);
188 /* Draw category items and advance y */
189 DrawCategory(r, y, list);
190 y += list.GetHeight();
192 /* Advance y by the height of the horizontal line between amounts and subtotal */
193 y += WidgetDimensions::scaled.vsep_normal;
195 /* Draw category total and advance y */
196 DrawString(r.left, r.right, y, STR_FINANCES_TOTAL_CAPTION, TC_FROMSTRING, SA_RIGHT);
197 y += GetCharacterHeight(FS_NORMAL);
199 /* Advance y by a blockspace after this category block */
200 y += WidgetDimensions::scaled.vsep_wide;
203 /* Draw total profit/loss */
204 y += WidgetDimensions::scaled.vsep_normal;
205 DrawString(r.left, r.right, y, STR_FINANCES_PROFIT, TC_FROMSTRING, SA_LEFT);
209 * Draw an amount of money.
210 * @param amount Amount of money to draw,
211 * @param left Left coordinate of the space to draw in.
212 * @param right Right coordinate of the space to draw in.
213 * @param top Top coordinate of the space to draw in.
214 * @param colour The TextColour of the string.
216 static void DrawPrice(Money amount, int left, int right, int top, TextColour colour)
218 StringID str = STR_FINANCES_NEGATIVE_INCOME;
219 if (amount == 0) {
220 str = STR_FINANCES_ZERO_INCOME;
221 } else if (amount < 0) {
222 amount = -amount;
223 str = STR_FINANCES_POSITIVE_INCOME;
225 SetDParam(0, amount);
226 DrawString(left, right, top, str, colour, SA_RIGHT);
230 * Draw a category of expenses/revenues in the year column.
231 * @return The income sum of the category.
233 static Money DrawYearCategory(const Rect &r, int start_y, const ExpensesList &list, const Expenses &tbl)
235 int y = start_y;
236 Money sum = 0;
238 for (const ExpensesType &et : list.items) {
239 Money cost = tbl[et];
240 sum += cost;
241 if (cost != 0) DrawPrice(cost, r.left, r.right, y, TC_BLACK);
242 y += GetCharacterHeight(FS_NORMAL);
245 /* Draw the total at the bottom of the category. */
246 GfxFillRect(r.left, y, r.right, y + WidgetDimensions::scaled.bevel.top - 1, PC_BLACK);
247 y += WidgetDimensions::scaled.vsep_normal;
248 if (sum != 0) DrawPrice(sum, r.left, r.right, y, TC_WHITE);
250 /* Return the sum for the yearly total. */
251 return sum;
256 * Draw a column with prices.
257 * @param r Available space for drawing.
258 * @param year Year being drawn.
259 * @param tbl Reference to table of amounts for \a year.
260 * @note The environment must provide padding at the left and right of \a r.
262 static void DrawYearColumn(const Rect &r, TimerGameEconomy::Year year, const Expenses &tbl)
264 int y = r.top;
265 Money sum;
267 /* Year header */
268 SetDParam(0, year);
269 DrawString(r.left, r.right, y, STR_FINANCES_YEAR, TC_FROMSTRING, SA_RIGHT, true);
270 y += GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide;
272 /* Categories */
273 for (const ExpensesList &list : _expenses_list_types) {
274 y += GetCharacterHeight(FS_NORMAL);
275 sum += DrawYearCategory(r, y, list, tbl);
276 /* Expense list + expense category title + expense category total + blockspace after category */
277 y += list.GetHeight() + WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_wide;
280 /* Total income. */
281 GfxFillRect(r.left, y, r.right, y + WidgetDimensions::scaled.bevel.top - 1, PC_BLACK);
282 y += WidgetDimensions::scaled.vsep_normal;
283 DrawPrice(sum, r.left, r.right, y, TC_WHITE);
286 static constexpr NWidgetPart _nested_company_finances_widgets[] = {
287 NWidget(NWID_HORIZONTAL),
288 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
289 NWidget(WWT_CAPTION, COLOUR_GREY, WID_CF_CAPTION), SetDataTip(STR_FINANCES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
290 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_CF_TOGGLE_SIZE), SetDataTip(SPR_LARGE_SMALL_WINDOW, STR_TOOLTIP_TOGGLE_LARGE_SMALL_WINDOW), SetAspect(WidgetDimensions::ASPECT_TOGGLE_SIZE),
291 NWidget(WWT_SHADEBOX, COLOUR_GREY),
292 NWidget(WWT_STICKYBOX, COLOUR_GREY),
293 EndContainer(),
294 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_CF_SEL_PANEL),
295 NWidget(WWT_PANEL, COLOUR_GREY),
296 NWidget(NWID_HORIZONTAL), SetPadding(WidgetDimensions::unscaled.framerect), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
297 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_CATEGORY), SetMinimalSize(120, 0), SetFill(0, 0),
298 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_PRICE1), SetMinimalSize(86, 0), SetFill(0, 0),
299 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_PRICE2), SetMinimalSize(86, 0), SetFill(0, 0),
300 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_PRICE3), SetMinimalSize(86, 0), SetFill(0, 0),
301 EndContainer(),
302 EndContainer(),
303 EndContainer(),
304 NWidget(WWT_PANEL, COLOUR_GREY),
305 NWidget(NWID_HORIZONTAL), SetPadding(WidgetDimensions::unscaled.framerect), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0), SetPIPRatio(0, 1, 2),
306 NWidget(NWID_VERTICAL), // Vertical column with 'bank balance', 'loan'
307 NWidget(WWT_TEXT, COLOUR_GREY), SetDataTip(STR_FINANCES_OWN_FUNDS_TITLE, STR_NULL),
308 NWidget(WWT_TEXT, COLOUR_GREY), SetDataTip(STR_FINANCES_LOAN_TITLE, STR_NULL),
309 NWidget(WWT_TEXT, COLOUR_GREY), SetDataTip(STR_FINANCES_BANK_BALANCE_TITLE, STR_NULL), SetPadding(WidgetDimensions::unscaled.vsep_normal, 0, 0, 0),
310 EndContainer(),
311 NWidget(NWID_VERTICAL), // Vertical column with bank balance amount, loan amount, and total.
312 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_OWN_VALUE), SetDataTip(STR_FINANCES_TOTAL_CURRENCY, STR_NULL), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
313 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_LOAN_VALUE), SetDataTip(STR_FINANCES_TOTAL_CURRENCY, STR_NULL), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
314 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_BALANCE_LINE), SetMinimalSize(0, WidgetDimensions::unscaled.vsep_normal),
315 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_BALANCE_VALUE), SetDataTip(STR_FINANCES_BANK_BALANCE, STR_NULL), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
316 EndContainer(),
317 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_CF_SEL_MAXLOAN),
318 NWidget(NWID_VERTICAL), SetPIPRatio(0, 0, 1), // Max loan information
319 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_INTEREST_RATE), SetDataTip(STR_FINANCES_INTEREST_RATE, STR_NULL),
320 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_MAXLOAN_VALUE), SetDataTip(STR_FINANCES_MAX_LOAN, STR_NULL),
321 EndContainer(),
322 EndContainer(),
323 EndContainer(),
324 EndContainer(),
325 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_CF_SEL_BUTTONS),
326 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
327 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CF_INCREASE_LOAN), SetFill(1, 0), SetDataTip(STR_FINANCES_BORROW_BUTTON, STR_FINANCES_BORROW_TOOLTIP),
328 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CF_REPAY_LOAN), SetFill(1, 0), SetDataTip(STR_FINANCES_REPAY_BUTTON, STR_FINANCES_REPAY_TOOLTIP),
329 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CF_INFRASTRUCTURE), SetFill(1, 0), SetDataTip(STR_FINANCES_INFRASTRUCTURE_BUTTON, STR_COMPANY_VIEW_INFRASTRUCTURE_TOOLTIP),
330 EndContainer(),
331 EndContainer(),
334 /** Window class displaying the company finances. */
335 struct CompanyFinancesWindow : Window {
336 static Money max_money; ///< The maximum amount of money a company has had this 'run'
337 bool small; ///< Window is toggled to 'small'.
339 CompanyFinancesWindow(WindowDesc &desc, CompanyID company) : Window(desc)
341 this->small = false;
342 this->CreateNestedTree();
343 this->SetupWidgets();
344 this->FinishInitNested(company);
346 this->owner = (Owner)this->window_number;
349 void SetStringParameters(WidgetID widget) const override
351 switch (widget) {
352 case WID_CF_CAPTION:
353 SetDParam(0, (CompanyID)this->window_number);
354 SetDParam(1, (CompanyID)this->window_number);
355 break;
357 case WID_CF_BALANCE_VALUE: {
358 const Company *c = Company::Get((CompanyID)this->window_number);
359 SetDParam(0, c->money);
360 break;
363 case WID_CF_LOAN_VALUE: {
364 const Company *c = Company::Get((CompanyID)this->window_number);
365 SetDParam(0, c->current_loan);
366 break;
369 case WID_CF_OWN_VALUE: {
370 const Company *c = Company::Get((CompanyID)this->window_number);
371 SetDParam(0, c->money - c->current_loan);
372 break;
375 case WID_CF_INTEREST_RATE:
376 SetDParam(0, _settings_game.difficulty.initial_interest);
377 break;
379 case WID_CF_MAXLOAN_VALUE: {
380 const Company *c = Company::Get((CompanyID)this->window_number);
381 SetDParam(0, c->GetMaxLoan());
382 break;
385 case WID_CF_INCREASE_LOAN:
386 case WID_CF_REPAY_LOAN:
387 SetDParam(0, LOAN_INTERVAL);
388 break;
392 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
394 switch (widget) {
395 case WID_CF_EXPS_CATEGORY:
396 size.width = GetMaxCategoriesWidth();
397 size.height = GetTotalCategoriesHeight();
398 break;
400 case WID_CF_EXPS_PRICE1:
401 case WID_CF_EXPS_PRICE2:
402 case WID_CF_EXPS_PRICE3:
403 size.height = GetTotalCategoriesHeight();
404 [[fallthrough]];
406 case WID_CF_BALANCE_VALUE:
407 case WID_CF_LOAN_VALUE:
408 case WID_CF_OWN_VALUE:
409 SetDParamMaxValue(0, CompanyFinancesWindow::max_money);
410 size.width = std::max(GetStringBoundingBox(STR_FINANCES_NEGATIVE_INCOME).width, GetStringBoundingBox(STR_FINANCES_POSITIVE_INCOME).width) + padding.width;
411 break;
413 case WID_CF_INTEREST_RATE:
414 size.height = GetCharacterHeight(FS_NORMAL);
415 break;
419 void DrawWidget(const Rect &r, WidgetID widget) const override
421 switch (widget) {
422 case WID_CF_EXPS_CATEGORY:
423 DrawCategories(r);
424 break;
426 case WID_CF_EXPS_PRICE1:
427 case WID_CF_EXPS_PRICE2:
428 case WID_CF_EXPS_PRICE3: {
429 const Company *c = Company::Get((CompanyID)this->window_number);
430 auto age = std::min(TimerGameEconomy::year - c->inaugurated_year, TimerGameEconomy::Year(2));
431 int wid_offset = widget - WID_CF_EXPS_PRICE1;
432 if (wid_offset <= age) {
433 DrawYearColumn(r, TimerGameEconomy::year - (age - wid_offset), c->yearly_expenses[(age - wid_offset).base()]);
435 break;
438 case WID_CF_BALANCE_LINE:
439 GfxFillRect(r.left, r.top, r.right, r.top + WidgetDimensions::scaled.bevel.top - 1, PC_BLACK);
440 break;
445 * Setup the widgets in the nested tree, such that the finances window is displayed properly.
446 * @note After setup, the window must be (re-)initialized.
448 void SetupWidgets()
450 int plane = this->small ? SZSP_NONE : 0;
451 this->GetWidget<NWidgetStacked>(WID_CF_SEL_PANEL)->SetDisplayedPlane(plane);
452 this->GetWidget<NWidgetStacked>(WID_CF_SEL_MAXLOAN)->SetDisplayedPlane(plane);
454 CompanyID company = (CompanyID)this->window_number;
455 plane = (company != _local_company) ? SZSP_NONE : 0;
456 this->GetWidget<NWidgetStacked>(WID_CF_SEL_BUTTONS)->SetDisplayedPlane(plane);
459 void OnPaint() override
461 if (!this->IsShaded()) {
462 if (!this->small) {
463 /* Check that the expenses panel height matches the height needed for the layout. */
464 if (GetTotalCategoriesHeight() != this->GetWidget<NWidgetBase>(WID_CF_EXPS_CATEGORY)->current_y) {
465 this->SetupWidgets();
466 this->ReInit();
467 return;
471 /* Check that the loan buttons are shown only when the user owns the company. */
472 CompanyID company = (CompanyID)this->window_number;
473 int req_plane = (company != _local_company) ? SZSP_NONE : 0;
474 if (req_plane != this->GetWidget<NWidgetStacked>(WID_CF_SEL_BUTTONS)->shown_plane) {
475 this->SetupWidgets();
476 this->ReInit();
477 return;
480 const Company *c = Company::Get(company);
481 this->SetWidgetDisabledState(WID_CF_INCREASE_LOAN, c->current_loan >= c->GetMaxLoan()); // Borrow button only shows when there is any more money to loan.
482 this->SetWidgetDisabledState(WID_CF_REPAY_LOAN, company != _local_company || c->current_loan == 0); // Repay button only shows when there is any more money to repay.
485 this->DrawWidgets();
488 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
490 switch (widget) {
491 case WID_CF_TOGGLE_SIZE: // toggle size
492 this->small = !this->small;
493 this->SetupWidgets();
494 if (this->IsShaded()) {
495 /* Finances window is not resizable, so size hints given during unshading have no effect
496 * on the changed appearance of the window. */
497 this->SetShaded(false);
498 } else {
499 this->ReInit();
501 break;
503 case WID_CF_INCREASE_LOAN: // increase loan
504 Command<CMD_INCREASE_LOAN>::Post(STR_ERROR_CAN_T_BORROW_ANY_MORE_MONEY, _ctrl_pressed ? LoanCommand::Max : LoanCommand::Interval, 0);
505 break;
507 case WID_CF_REPAY_LOAN: // repay loan
508 Command<CMD_DECREASE_LOAN>::Post(STR_ERROR_CAN_T_REPAY_LOAN, _ctrl_pressed ? LoanCommand::Max : LoanCommand::Interval, 0);
509 break;
511 case WID_CF_INFRASTRUCTURE: // show infrastructure details
512 ShowCompanyInfrastructure((CompanyID)this->window_number);
513 break;
518 * Check on a regular interval if the maximum amount of money has changed.
519 * If it has, rescale the window to fit the new amount.
521 IntervalTimer<TimerWindow> rescale_interval = {std::chrono::seconds(3), [this](auto) {
522 const Company *c = Company::Get((CompanyID)this->window_number);
523 if (c->money > CompanyFinancesWindow::max_money) {
524 CompanyFinancesWindow::max_money = std::max(c->money * 2, CompanyFinancesWindow::max_money * 4);
525 this->SetupWidgets();
526 this->ReInit();
531 /** First conservative estimate of the maximum amount of money */
532 Money CompanyFinancesWindow::max_money = INT32_MAX;
534 static WindowDesc _company_finances_desc(
535 WDP_AUTO, "company_finances", 0, 0,
536 WC_FINANCES, WC_NONE,
538 _nested_company_finances_widgets
542 * Open the finances window of a company.
543 * @param company Company to show finances of.
544 * @pre is company a valid company.
546 void ShowCompanyFinances(CompanyID company)
548 if (!Company::IsValidID(company)) return;
549 if (BringWindowToFrontById(WC_FINANCES, company)) return;
551 new CompanyFinancesWindow(_company_finances_desc, company);
554 /* Association of liveries to livery classes */
555 static const LiveryClass _livery_class[LS_END] = {
556 LC_OTHER,
557 LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL, LC_RAIL,
558 LC_ROAD, LC_ROAD,
559 LC_SHIP, LC_SHIP,
560 LC_AIRCRAFT, LC_AIRCRAFT, LC_AIRCRAFT,
561 LC_ROAD, LC_ROAD,
565 * Colour selection list item, with icon and string components.
566 * @tparam TSprite Recolourable sprite to draw as icon.
568 template <SpriteID TSprite = SPR_SQUARE>
569 class DropDownListColourItem : public DropDownIcon<DropDownString<DropDownListItem>> {
570 public:
571 DropDownListColourItem(int colour, bool masked) : DropDownIcon<DropDownString<DropDownListItem>>(TSprite, GENERAL_SPRITE_COLOUR(colour % COLOUR_END), colour < COLOUR_END ? (STR_COLOUR_DARK_BLUE + colour) : STR_COLOUR_DEFAULT, colour, masked)
576 /** Company livery colour scheme window. */
577 struct SelectCompanyLiveryWindow : public Window {
578 private:
579 uint32_t sel;
580 LiveryClass livery_class;
581 Dimension square;
582 uint rows;
583 uint line_height;
584 GUIGroupList groups;
585 Scrollbar *vscroll;
587 void ShowColourDropDownMenu(uint32_t widget)
589 uint32_t used_colours = 0;
590 const Livery *livery, *default_livery = nullptr;
591 bool primary = widget == WID_SCL_PRI_COL_DROPDOWN;
592 uint8_t default_col = 0;
594 /* Disallow other company colours for the primary colour */
595 if (this->livery_class < LC_GROUP_RAIL && HasBit(this->sel, LS_DEFAULT) && primary) {
596 for (const Company *c : Company::Iterate()) {
597 if (c->index != _local_company) SetBit(used_colours, c->colour);
601 const Company *c = Company::Get((CompanyID)this->window_number);
603 if (this->livery_class < LC_GROUP_RAIL) {
604 /* Get the first selected livery to use as the default dropdown item */
605 LiveryScheme scheme;
606 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
607 if (HasBit(this->sel, scheme)) break;
609 if (scheme == LS_END) scheme = LS_DEFAULT;
610 livery = &c->livery[scheme];
611 if (scheme != LS_DEFAULT) default_livery = &c->livery[LS_DEFAULT];
612 } else {
613 const Group *g = Group::Get(this->sel);
614 livery = &g->livery;
615 if (g->parent == INVALID_GROUP) {
616 default_livery = &c->livery[LS_DEFAULT];
617 } else {
618 const Group *pg = Group::Get(g->parent);
619 default_livery = &pg->livery;
623 DropDownList list;
624 if (default_livery != nullptr) {
625 /* Add COLOUR_END to put the colour out of range, but also allow us to show what the default is */
626 default_col = (primary ? default_livery->colour1 : default_livery->colour2) + COLOUR_END;
627 list.push_back(std::make_unique<DropDownListColourItem<>>(default_col, false));
629 for (Colours colour = COLOUR_BEGIN; colour != COLOUR_END; colour++) {
630 list.push_back(std::make_unique<DropDownListColourItem<>>(colour, HasBit(used_colours, colour)));
633 uint8_t sel;
634 if (default_livery == nullptr || HasBit(livery->in_use, primary ? 0 : 1)) {
635 sel = primary ? livery->colour1 : livery->colour2;
636 } else {
637 sel = default_col;
639 ShowDropDownList(this, std::move(list), sel, widget);
642 void BuildGroupList(CompanyID owner)
644 if (!this->groups.NeedRebuild()) return;
646 this->groups.clear();
648 if (this->livery_class >= LC_GROUP_RAIL) {
649 VehicleType vtype = (VehicleType)(this->livery_class - LC_GROUP_RAIL);
650 BuildGuiGroupList(this->groups, false, owner, vtype);
653 this->groups.RebuildDone();
656 void SetRows()
658 if (this->livery_class < LC_GROUP_RAIL) {
659 this->rows = 0;
660 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
661 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
662 this->rows++;
665 } else {
666 this->rows = (uint)this->groups.size();
669 this->vscroll->SetCount(this->rows);
672 public:
673 SelectCompanyLiveryWindow(WindowDesc &desc, CompanyID company, GroupID group) : Window(desc)
675 this->CreateNestedTree();
676 this->vscroll = this->GetScrollbar(WID_SCL_MATRIX_SCROLLBAR);
678 if (group == INVALID_GROUP) {
679 this->livery_class = LC_OTHER;
680 this->sel = 1;
681 this->LowerWidget(WID_SCL_CLASS_GENERAL);
682 this->BuildGroupList(company);
683 this->SetRows();
684 } else {
685 this->SetSelectedGroup(company, group);
688 this->FinishInitNested(company);
689 this->owner = company;
690 this->InvalidateData(1);
693 void SetSelectedGroup(CompanyID company, GroupID group)
695 this->RaiseWidget(WID_SCL_CLASS_GENERAL + this->livery_class);
696 const Group *g = Group::Get(group);
697 switch (g->vehicle_type) {
698 case VEH_TRAIN: this->livery_class = LC_GROUP_RAIL; break;
699 case VEH_ROAD: this->livery_class = LC_GROUP_ROAD; break;
700 case VEH_SHIP: this->livery_class = LC_GROUP_SHIP; break;
701 case VEH_AIRCRAFT: this->livery_class = LC_GROUP_AIRCRAFT; break;
702 default: NOT_REACHED();
704 this->sel = group;
705 this->LowerWidget(WID_SCL_CLASS_GENERAL + this->livery_class);
707 this->groups.ForceRebuild();
708 this->BuildGroupList(company);
709 this->SetRows();
711 /* Position scrollbar to selected group */
712 for (uint i = 0; i < this->rows; i++) {
713 if (this->groups[i].group->index == sel) {
714 this->vscroll->SetPosition(i - this->vscroll->GetCapacity() / 2);
715 break;
720 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
722 switch (widget) {
723 case WID_SCL_SPACER_DROPDOWN: {
724 /* The matrix widget below needs enough room to print all the schemes. */
725 Dimension d = {0, 0};
726 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
727 d = maxdim(d, GetStringBoundingBox(STR_LIVERY_DEFAULT + scheme));
730 /* And group names */
731 for (const Group *g : Group::Iterate()) {
732 if (g->owner == (CompanyID)this->window_number) {
733 SetDParam(0, g->index);
734 d = maxdim(d, GetStringBoundingBox(STR_GROUP_NAME));
738 size.width = std::max(size.width, 5 + d.width + padding.width);
739 break;
742 case WID_SCL_MATRIX: {
743 /* 11 items in the default rail class */
744 this->square = GetSpriteSize(SPR_SQUARE);
745 this->line_height = std::max(this->square.height, (uint)GetCharacterHeight(FS_NORMAL)) + padding.height;
747 size.height = 5 * this->line_height;
748 resize.width = 1;
749 resize.height = this->line_height;
750 break;
753 case WID_SCL_SEC_COL_DROPDOWN:
754 if (!_loaded_newgrf_features.has_2CC) {
755 size.width = 0;
756 break;
758 [[fallthrough]];
760 case WID_SCL_PRI_COL_DROPDOWN: {
761 this->square = GetSpriteSize(SPR_SQUARE);
762 int string_padding = this->square.width + WidgetDimensions::scaled.hsep_normal + padding.width;
763 for (Colours colour = COLOUR_BEGIN; colour != COLOUR_END; colour++) {
764 size.width = std::max(size.width, GetStringBoundingBox(STR_COLOUR_DARK_BLUE + colour).width + string_padding);
766 size.width = std::max(size.width, GetStringBoundingBox(STR_COLOUR_DEFAULT).width + string_padding);
767 break;
772 void OnPaint() override
774 bool local = (CompanyID)this->window_number == _local_company;
776 /* Disable dropdown controls if no scheme is selected */
777 bool disabled = this->livery_class < LC_GROUP_RAIL ? (this->sel == 0) : (this->sel == INVALID_GROUP);
778 this->SetWidgetDisabledState(WID_SCL_PRI_COL_DROPDOWN, !local || disabled);
779 this->SetWidgetDisabledState(WID_SCL_SEC_COL_DROPDOWN, !local || disabled);
781 this->BuildGroupList((CompanyID)this->window_number);
783 this->DrawWidgets();
786 void SetStringParameters(WidgetID widget) const override
788 switch (widget) {
789 case WID_SCL_CAPTION:
790 SetDParam(0, (CompanyID)this->window_number);
791 break;
793 case WID_SCL_PRI_COL_DROPDOWN:
794 case WID_SCL_SEC_COL_DROPDOWN: {
795 const Company *c = Company::Get((CompanyID)this->window_number);
796 bool primary = widget == WID_SCL_PRI_COL_DROPDOWN;
797 StringID colour = STR_COLOUR_DEFAULT;
799 if (this->livery_class < LC_GROUP_RAIL) {
800 if (this->sel != 0) {
801 LiveryScheme scheme = LS_DEFAULT;
802 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
803 if (HasBit(this->sel, scheme)) break;
805 if (scheme == LS_END) scheme = LS_DEFAULT;
806 const Livery *livery = &c->livery[scheme];
807 if (scheme == LS_DEFAULT || HasBit(livery->in_use, primary ? 0 : 1)) {
808 colour = STR_COLOUR_DARK_BLUE + (primary ? livery->colour1 : livery->colour2);
811 } else {
812 if (this->sel != INVALID_GROUP) {
813 const Group *g = Group::Get(this->sel);
814 const Livery *livery = &g->livery;
815 if (HasBit(livery->in_use, primary ? 0 : 1)) {
816 colour = STR_COLOUR_DARK_BLUE + (primary ? livery->colour1 : livery->colour2);
820 SetDParam(0, colour);
821 break;
826 void DrawWidget(const Rect &r, WidgetID widget) const override
828 if (widget != WID_SCL_MATRIX) return;
830 bool rtl = _current_text_dir == TD_RTL;
832 /* Coordinates of scheme name column. */
833 const NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SCL_SPACER_DROPDOWN);
834 Rect sch = nwi->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
835 /* Coordinates of first dropdown. */
836 nwi = this->GetWidget<NWidgetBase>(WID_SCL_PRI_COL_DROPDOWN);
837 Rect pri = nwi->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
838 /* Coordinates of second dropdown. */
839 nwi = this->GetWidget<NWidgetBase>(WID_SCL_SEC_COL_DROPDOWN);
840 Rect sec = nwi->GetCurrentRect().Shrink(WidgetDimensions::scaled.framerect);
842 Rect pri_squ = pri.WithWidth(this->square.width, rtl);
843 Rect sec_squ = sec.WithWidth(this->square.width, rtl);
845 pri = pri.Indent(this->square.width + WidgetDimensions::scaled.hsep_normal, rtl);
846 sec = sec.Indent(this->square.width + WidgetDimensions::scaled.hsep_normal, rtl);
848 Rect ir = r.WithHeight(this->resize.step_height).Shrink(WidgetDimensions::scaled.matrix);
849 int square_offs = (ir.Height() - this->square.height) / 2;
850 int text_offs = (ir.Height() - GetCharacterHeight(FS_NORMAL)) / 2;
852 int y = ir.top;
854 /* Helper function to draw livery info. */
855 auto draw_livery = [&](StringID str, const Livery &livery, bool is_selected, bool is_default_scheme, int indent) {
856 /* Livery Label. */
857 DrawString(sch.left + (rtl ? 0 : indent), sch.right - (rtl ? indent : 0), y + text_offs, str, is_selected ? TC_WHITE : TC_BLACK);
859 /* Text below the first dropdown. */
860 DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOUR(livery.colour1), pri_squ.left, y + square_offs);
861 DrawString(pri.left, pri.right, y + text_offs, (is_default_scheme || HasBit(livery.in_use, 0)) ? STR_COLOUR_DARK_BLUE + livery.colour1 : STR_COLOUR_DEFAULT, is_selected ? TC_WHITE : TC_GOLD);
863 /* Text below the second dropdown. */
864 if (sec.right > sec.left) { // Second dropdown has non-zero size.
865 DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOUR(livery.colour2), sec_squ.left, y + square_offs);
866 DrawString(sec.left, sec.right, y + text_offs, (is_default_scheme || HasBit(livery.in_use, 1)) ? STR_COLOUR_DARK_BLUE + livery.colour2 : STR_COLOUR_DEFAULT, is_selected ? TC_WHITE : TC_GOLD);
869 y += this->line_height;
872 const Company *c = Company::Get((CompanyID)this->window_number);
874 if (livery_class < LC_GROUP_RAIL) {
875 int pos = this->vscroll->GetPosition();
876 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
877 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
878 if (pos-- > 0) continue;
879 draw_livery(STR_LIVERY_DEFAULT + scheme, c->livery[scheme], HasBit(this->sel, scheme), scheme == LS_DEFAULT, 0);
882 } else {
883 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->groups);
884 for (auto it = first; it != last; ++it) {
885 const Group *g = it->group;
886 SetDParam(0, g->index);
887 draw_livery(STR_GROUP_NAME, g->livery, this->sel == g->index, false, it->indent * WidgetDimensions::scaled.hsep_indent);
890 if (this->vscroll->GetCount() == 0) {
891 const StringID empty_labels[] = { STR_LIVERY_TRAIN_GROUP_EMPTY, STR_LIVERY_ROAD_VEHICLE_GROUP_EMPTY, STR_LIVERY_SHIP_GROUP_EMPTY, STR_LIVERY_AIRCRAFT_GROUP_EMPTY };
892 VehicleType vtype = (VehicleType)(this->livery_class - LC_GROUP_RAIL);
893 DrawString(ir.left, ir.right, y + text_offs, empty_labels[vtype], TC_BLACK);
898 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
900 switch (widget) {
901 /* Livery Class buttons */
902 case WID_SCL_CLASS_GENERAL:
903 case WID_SCL_CLASS_RAIL:
904 case WID_SCL_CLASS_ROAD:
905 case WID_SCL_CLASS_SHIP:
906 case WID_SCL_CLASS_AIRCRAFT:
907 case WID_SCL_GROUPS_RAIL:
908 case WID_SCL_GROUPS_ROAD:
909 case WID_SCL_GROUPS_SHIP:
910 case WID_SCL_GROUPS_AIRCRAFT:
911 this->RaiseWidget(WID_SCL_CLASS_GENERAL + this->livery_class);
912 this->livery_class = (LiveryClass)(widget - WID_SCL_CLASS_GENERAL);
913 this->LowerWidget(WID_SCL_CLASS_GENERAL + this->livery_class);
915 /* Select the first item in the list */
916 if (this->livery_class < LC_GROUP_RAIL) {
917 this->sel = 0;
918 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
919 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
920 this->sel = 1 << scheme;
921 break;
924 } else {
925 this->sel = INVALID_GROUP;
926 this->groups.ForceRebuild();
927 this->BuildGroupList((CompanyID)this->window_number);
929 if (!this->groups.empty()) {
930 this->sel = this->groups[0].group->index;
934 this->SetRows();
935 this->SetDirty();
936 break;
938 case WID_SCL_PRI_COL_DROPDOWN: // First colour dropdown
939 ShowColourDropDownMenu(WID_SCL_PRI_COL_DROPDOWN);
940 break;
942 case WID_SCL_SEC_COL_DROPDOWN: // Second colour dropdown
943 ShowColourDropDownMenu(WID_SCL_SEC_COL_DROPDOWN);
944 break;
946 case WID_SCL_MATRIX: {
947 if (this->livery_class < LC_GROUP_RAIL) {
948 uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget);
949 if (row >= this->rows) return;
951 LiveryScheme j = (LiveryScheme)row;
953 for (LiveryScheme scheme = LS_BEGIN; scheme <= j && scheme < LS_END; scheme++) {
954 if (_livery_class[scheme] != this->livery_class || !HasBit(_loaded_newgrf_features.used_liveries, scheme)) j++;
956 assert(j < LS_END);
958 if (_ctrl_pressed) {
959 ToggleBit(this->sel, j);
960 } else {
961 this->sel = 1 << j;
963 } else {
964 auto it = this->vscroll->GetScrolledItemFromWidget(this->groups, pt.y, this, widget);
965 if (it == std::end(this->groups)) return;
967 this->sel = it->group->index;
969 this->SetDirty();
970 break;
975 void OnResize() override
977 this->vscroll->SetCapacityFromWidget(this, WID_SCL_MATRIX);
980 void OnDropdownSelect(WidgetID widget, int index) override
982 bool local = (CompanyID)this->window_number == _local_company;
983 if (!local) return;
985 Colours colour = static_cast<Colours>(index);
986 if (colour >= COLOUR_END) colour = INVALID_COLOUR;
988 if (this->livery_class < LC_GROUP_RAIL) {
989 /* Set company colour livery */
990 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
991 /* Changed colour for the selected scheme, or all visible schemes if CTRL is pressed. */
992 if (HasBit(this->sel, scheme) || (_ctrl_pressed && _livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme))) {
993 Command<CMD_SET_COMPANY_COLOUR>::Post(scheme, widget == WID_SCL_PRI_COL_DROPDOWN, colour);
996 } else {
997 /* Setting group livery */
998 Command<CMD_SET_GROUP_LIVERY>::Post(this->sel, widget == WID_SCL_PRI_COL_DROPDOWN, colour);
1003 * Some data on this window has become invalid.
1004 * @param data Information about the changed data.
1005 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1007 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
1009 if (!gui_scope) return;
1011 if (data != -1) {
1012 /* data contains a VehicleType, rebuild list if it displayed */
1013 if (this->livery_class == data + LC_GROUP_RAIL) {
1014 this->groups.ForceRebuild();
1015 this->BuildGroupList((CompanyID)this->window_number);
1016 this->SetRows();
1018 if (!Group::IsValidID(this->sel)) {
1019 this->sel = INVALID_GROUP;
1020 if (!this->groups.empty()) this->sel = this->groups[0].group->index;
1023 this->SetDirty();
1025 return;
1028 this->SetWidgetsDisabledState(true, WID_SCL_CLASS_RAIL, WID_SCL_CLASS_ROAD, WID_SCL_CLASS_SHIP, WID_SCL_CLASS_AIRCRAFT);
1030 bool current_class_valid = this->livery_class == LC_OTHER || this->livery_class >= LC_GROUP_RAIL;
1031 if (_settings_client.gui.liveries == LIT_ALL || (_settings_client.gui.liveries == LIT_COMPANY && this->window_number == _local_company)) {
1032 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
1033 if (HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
1034 if (_livery_class[scheme] == this->livery_class) current_class_valid = true;
1035 this->EnableWidget(WID_SCL_CLASS_GENERAL + _livery_class[scheme]);
1036 } else if (this->livery_class < LC_GROUP_RAIL) {
1037 ClrBit(this->sel, scheme);
1042 if (!current_class_valid) {
1043 Point pt = {0, 0};
1044 this->OnClick(pt, WID_SCL_CLASS_GENERAL, 1);
1049 static constexpr NWidgetPart _nested_select_company_livery_widgets[] = {
1050 NWidget(NWID_HORIZONTAL),
1051 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1052 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCL_CAPTION), SetDataTip(STR_LIVERY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1053 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1054 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1055 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1056 EndContainer(),
1057 NWidget(NWID_HORIZONTAL),
1058 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_GENERAL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_LIVERY_GENERAL_TOOLTIP),
1059 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_RAIL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_LIVERY_TRAIN_TOOLTIP),
1060 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_ROAD), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRUCKLIST, STR_LIVERY_ROAD_VEHICLE_TOOLTIP),
1061 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_SHIP), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SHIPLIST, STR_LIVERY_SHIP_TOOLTIP),
1062 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_AIRCRAFT), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_AIRPLANESLIST, STR_LIVERY_AIRCRAFT_TOOLTIP),
1063 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_RAIL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_TRAIN, STR_LIVERY_TRAIN_GROUP_TOOLTIP),
1064 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_ROAD), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_ROADVEH, STR_LIVERY_ROAD_VEHICLE_GROUP_TOOLTIP),
1065 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_SHIP), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_SHIP, STR_LIVERY_SHIP_GROUP_TOOLTIP),
1066 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_AIRCRAFT), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_AIRCRAFT, STR_LIVERY_AIRCRAFT_GROUP_TOOLTIP),
1067 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
1068 EndContainer(),
1069 NWidget(NWID_HORIZONTAL),
1070 NWidget(WWT_MATRIX, COLOUR_GREY, WID_SCL_MATRIX), SetMinimalSize(275, 0), SetResize(1, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_LIVERY_PANEL_TOOLTIP), SetScrollbar(WID_SCL_MATRIX_SCROLLBAR),
1071 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SCL_MATRIX_SCROLLBAR),
1072 EndContainer(),
1073 NWidget(NWID_HORIZONTAL),
1074 NWidget(WWT_PANEL, COLOUR_GREY, WID_SCL_SPACER_DROPDOWN), SetFill(1, 1), SetResize(1, 0), EndContainer(),
1075 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SCL_PRI_COL_DROPDOWN), SetFill(0, 1), SetDataTip(STR_JUST_STRING, STR_LIVERY_PRIMARY_TOOLTIP),
1076 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SCL_SEC_COL_DROPDOWN), SetFill(0, 1), SetDataTip(STR_JUST_STRING, STR_LIVERY_SECONDARY_TOOLTIP),
1077 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1078 EndContainer(),
1081 static WindowDesc _select_company_livery_desc(
1082 WDP_AUTO, "company_color_scheme", 0, 0,
1083 WC_COMPANY_COLOUR, WC_NONE,
1085 _nested_select_company_livery_widgets
1088 void ShowCompanyLiveryWindow(CompanyID company, GroupID group)
1090 SelectCompanyLiveryWindow *w = (SelectCompanyLiveryWindow *)BringWindowToFrontById(WC_COMPANY_COLOUR, company);
1091 if (w == nullptr) {
1092 new SelectCompanyLiveryWindow(_select_company_livery_desc, company, group);
1093 } else if (group != INVALID_GROUP) {
1094 w->SetSelectedGroup(company, group);
1099 * Draws the face of a company manager's face.
1100 * @param cmf the company manager's face
1101 * @param colour the (background) colour of the gradient
1102 * @param r position to draw the face
1104 void DrawCompanyManagerFace(CompanyManagerFace cmf, Colours colour, const Rect &r)
1106 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
1108 /* Determine offset from centre of drawing rect. */
1109 Dimension d = GetSpriteSize(SPR_GRADIENT);
1110 int x = CenterBounds(r.left, r.right, d.width);
1111 int y = CenterBounds(r.top, r.bottom, d.height);
1113 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
1114 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
1115 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
1116 PaletteID pal;
1118 /* Modify eye colour palette only if 2 or more valid values exist */
1119 if (_cmf_info[CMFV_EYE_COLOUR].valid_values[ge] < 2) {
1120 pal = PAL_NONE;
1121 } else {
1122 switch (GetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge)) {
1123 default: NOT_REACHED();
1124 case 0: pal = PALETTE_TO_BROWN; break;
1125 case 1: pal = PALETTE_TO_BLUE; break;
1126 case 2: pal = PALETTE_TO_GREEN; break;
1130 /* Draw the gradient (background) */
1131 DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOUR(colour), x, y);
1133 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
1134 switch (cmfv) {
1135 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
1136 case CMFV_LIPS:
1137 case CMFV_NOSE: if (has_moustache) continue; break;
1138 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
1139 case CMFV_GLASSES: if (!has_glasses) continue; break;
1140 default: break;
1142 DrawSprite(GetCompanyManagerFaceSprite(cmf, cmfv, ge), (cmfv == CMFV_EYEBROWS) ? pal : PAL_NONE, x, y);
1146 /** Nested widget description for the company manager face selection dialog */
1147 static constexpr NWidgetPart _nested_select_company_manager_face_widgets[] = {
1148 NWidget(NWID_HORIZONTAL),
1149 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1150 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCMF_CAPTION), SetDataTip(STR_FACE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1151 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCMF_TOGGLE_LARGE_SMALL), SetDataTip(SPR_LARGE_SMALL_WINDOW, STR_FACE_ADVANCED_TOOLTIP), SetAspect(WidgetDimensions::ASPECT_TOGGLE_SIZE),
1152 EndContainer(),
1153 NWidget(WWT_PANEL, COLOUR_GREY, WID_SCMF_SELECT_FACE),
1154 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0), SetPadding(2),
1155 /* Left side */
1156 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
1157 NWidget(NWID_HORIZONTAL), SetPIPRatio(1, 0, 1),
1158 NWidget(WWT_EMPTY, COLOUR_GREY, WID_SCMF_FACE), SetMinimalSize(92, 119), SetFill(1, 0),
1159 EndContainer(),
1160 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_RANDOM_NEW_FACE), SetFill(1, 0), SetDataTip(STR_FACE_NEW_FACE_BUTTON, STR_FACE_NEW_FACE_TOOLTIP),
1161 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_LOADSAVE), // Load/number/save buttons under the portrait in the advanced view.
1162 NWidget(NWID_VERTICAL), SetPIP(0, 0, 0), SetPIPRatio(1, 0, 1),
1163 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_LOAD), SetFill(1, 0), SetDataTip(STR_FACE_LOAD, STR_FACE_LOAD_TOOLTIP),
1164 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_FACECODE), SetFill(1, 0), SetDataTip(STR_FACE_FACECODE, STR_FACE_FACECODE_TOOLTIP),
1165 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_SAVE), SetFill(1, 0), SetDataTip(STR_FACE_SAVE, STR_FACE_SAVE_TOOLTIP),
1166 EndContainer(),
1167 EndContainer(),
1168 EndContainer(),
1169 /* Right side */
1170 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
1171 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON), SetFill(1, 0), SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP),
1172 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_MALEFEMALE), // Simple male/female face setting.
1173 NWidget(NWID_VERTICAL), SetPIPRatio(1, 0, 1),
1174 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_MALE), SetFill(1, 0), SetDataTip(STR_FACE_MALE_BUTTON, STR_FACE_MALE_TOOLTIP),
1175 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_FEMALE), SetFill(1, 0), SetDataTip(STR_FACE_FEMALE_BUTTON, STR_FACE_FEMALE_TOOLTIP),
1176 EndContainer(),
1177 EndContainer(),
1178 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_PARTS), // Advanced face parts setting.
1179 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
1180 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1181 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_MALE2), SetFill(1, 0), SetDataTip(STR_FACE_MALE_BUTTON, STR_FACE_MALE_TOOLTIP),
1182 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_FEMALE2), SetFill(1, 0), SetDataTip(STR_FACE_FEMALE_BUTTON, STR_FACE_FEMALE_TOOLTIP),
1183 EndContainer(),
1184 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1185 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_ETHNICITY_EUR), SetFill(1, 0), SetDataTip(STR_FACE_EUROPEAN, STR_FACE_SELECT_EUROPEAN),
1186 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_ETHNICITY_AFR), SetFill(1, 0), SetDataTip(STR_FACE_AFRICAN, STR_FACE_SELECT_AFRICAN),
1187 EndContainer(),
1188 NWidget(NWID_VERTICAL),
1189 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1190 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT), SetFill(1, 0),
1191 SetDataTip(STR_FACE_EYECOLOUR, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1192 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAS_MOUSTACHE_EARRING), SetDataTip(STR_JUST_STRING1, STR_FACE_MOUSTACHE_EARRING_TOOLTIP), SetTextStyle(TC_WHITE),
1193 EndContainer(),
1194 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1195 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_HAS_GLASSES_TEXT), SetFill(1, 0),
1196 SetDataTip(STR_FACE_GLASSES, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1197 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAS_GLASSES), SetDataTip(STR_JUST_STRING1, STR_FACE_GLASSES_TOOLTIP), SetTextStyle(TC_WHITE),
1198 EndContainer(),
1199 EndContainer(),
1200 NWidget(NWID_VERTICAL),
1201 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1202 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_HAIR_TEXT), SetFill(1, 0),
1203 SetDataTip(STR_FACE_HAIR, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1204 NWidget(NWID_HORIZONTAL),
1205 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_HAIR_L), SetDataTip(AWV_DECREASE, STR_FACE_HAIR_TOOLTIP),
1206 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAIR), SetDataTip(STR_JUST_STRING1, STR_FACE_HAIR_TOOLTIP), SetTextStyle(TC_WHITE),
1207 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_HAIR_R), SetDataTip(AWV_INCREASE, STR_FACE_HAIR_TOOLTIP),
1208 EndContainer(),
1209 EndContainer(),
1210 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1211 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_EYEBROWS_TEXT), SetFill(1, 0),
1212 SetDataTip(STR_FACE_EYEBROWS, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1213 NWidget(NWID_HORIZONTAL),
1214 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYEBROWS_L), SetDataTip(AWV_DECREASE, STR_FACE_EYEBROWS_TOOLTIP),
1215 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_EYEBROWS), SetDataTip(STR_JUST_STRING1, STR_FACE_EYEBROWS_TOOLTIP), SetTextStyle(TC_WHITE),
1216 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYEBROWS_R), SetDataTip(AWV_INCREASE, STR_FACE_EYEBROWS_TOOLTIP),
1217 EndContainer(),
1218 EndContainer(),
1219 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1220 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_EYECOLOUR_TEXT), SetFill(1, 0),
1221 SetDataTip(STR_FACE_EYECOLOUR, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1222 NWidget(NWID_HORIZONTAL),
1223 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR_L), SetDataTip(AWV_DECREASE, STR_FACE_EYECOLOUR_TOOLTIP),
1224 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR), SetDataTip(STR_JUST_STRING1, STR_FACE_EYECOLOUR_TOOLTIP), SetTextStyle(TC_WHITE),
1225 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR_R), SetDataTip(AWV_INCREASE, STR_FACE_EYECOLOUR_TOOLTIP),
1226 EndContainer(),
1227 EndContainer(),
1228 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1229 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_GLASSES_TEXT), SetFill(1, 0),
1230 SetDataTip(STR_FACE_GLASSES, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1231 NWidget(NWID_HORIZONTAL),
1232 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_GLASSES_L), SetDataTip(AWV_DECREASE, STR_FACE_GLASSES_TOOLTIP_2),
1233 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_GLASSES), SetDataTip(STR_JUST_STRING1, STR_FACE_GLASSES_TOOLTIP_2), SetTextStyle(TC_WHITE),
1234 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_GLASSES_R), SetDataTip(AWV_INCREASE, STR_FACE_GLASSES_TOOLTIP_2),
1235 EndContainer(),
1236 EndContainer(),
1237 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1238 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_NOSE_TEXT), SetFill(1, 0),
1239 SetDataTip(STR_FACE_NOSE, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1240 NWidget(NWID_HORIZONTAL),
1241 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_NOSE_L), SetDataTip(AWV_DECREASE, STR_FACE_NOSE_TOOLTIP),
1242 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_NOSE), SetDataTip(STR_JUST_STRING1, STR_FACE_NOSE_TOOLTIP), SetTextStyle(TC_WHITE),
1243 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_NOSE_R), SetDataTip(AWV_INCREASE, STR_FACE_NOSE_TOOLTIP),
1244 EndContainer(),
1245 EndContainer(),
1246 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1247 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_LIPS_MOUSTACHE_TEXT), SetFill(1, 0),
1248 SetDataTip(STR_FACE_MOUSTACHE, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1249 NWidget(NWID_HORIZONTAL),
1250 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE_L), SetDataTip(AWV_DECREASE, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1251 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE), SetDataTip(STR_JUST_STRING1, STR_FACE_LIPS_MOUSTACHE_TOOLTIP), SetTextStyle(TC_WHITE),
1252 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE_R), SetDataTip(AWV_INCREASE, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1253 EndContainer(),
1254 EndContainer(),
1255 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1256 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_CHIN_TEXT), SetFill(1, 0),
1257 SetDataTip(STR_FACE_CHIN, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1258 NWidget(NWID_HORIZONTAL),
1259 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_CHIN_L), SetDataTip(AWV_DECREASE, STR_FACE_CHIN_TOOLTIP),
1260 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_CHIN), SetDataTip(STR_JUST_STRING1, STR_FACE_CHIN_TOOLTIP), SetTextStyle(TC_WHITE),
1261 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_CHIN_R), SetDataTip(AWV_INCREASE, STR_FACE_CHIN_TOOLTIP),
1262 EndContainer(),
1263 EndContainer(),
1264 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1265 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_JACKET_TEXT), SetFill(1, 0),
1266 SetDataTip(STR_FACE_JACKET, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1267 NWidget(NWID_HORIZONTAL),
1268 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_JACKET_L), SetDataTip(AWV_DECREASE, STR_FACE_JACKET_TOOLTIP),
1269 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_JACKET), SetDataTip(STR_JUST_STRING1, STR_FACE_JACKET_TOOLTIP), SetTextStyle(TC_WHITE),
1270 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_JACKET_R), SetDataTip(AWV_INCREASE, STR_FACE_JACKET_TOOLTIP),
1271 EndContainer(),
1272 EndContainer(),
1273 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1274 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_COLLAR_TEXT), SetFill(1, 0),
1275 SetDataTip(STR_FACE_COLLAR, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1276 NWidget(NWID_HORIZONTAL),
1277 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_COLLAR_L), SetDataTip(AWV_DECREASE, STR_FACE_COLLAR_TOOLTIP),
1278 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_COLLAR), SetDataTip(STR_JUST_STRING1, STR_FACE_COLLAR_TOOLTIP), SetTextStyle(TC_WHITE),
1279 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_COLLAR_R), SetDataTip(AWV_INCREASE, STR_FACE_COLLAR_TOOLTIP),
1280 EndContainer(),
1281 EndContainer(),
1282 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
1283 NWidget(WWT_TEXT, INVALID_COLOUR, WID_SCMF_TIE_EARRING_TEXT), SetFill(1, 0),
1284 SetDataTip(STR_FACE_EARRING, STR_NULL), SetTextStyle(TC_GOLD), SetAlignment(SA_VERT_CENTER | SA_RIGHT),
1285 NWidget(NWID_HORIZONTAL),
1286 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING_L), SetDataTip(AWV_DECREASE, STR_FACE_TIE_EARRING_TOOLTIP),
1287 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING), SetDataTip(STR_JUST_STRING1, STR_FACE_TIE_EARRING_TOOLTIP), SetTextStyle(TC_WHITE),
1288 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING_R), SetDataTip(AWV_INCREASE, STR_FACE_TIE_EARRING_TOOLTIP),
1289 EndContainer(),
1290 EndContainer(),
1291 EndContainer(),
1292 EndContainer(),
1293 EndContainer(),
1294 EndContainer(),
1295 EndContainer(),
1296 EndContainer(),
1297 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1298 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_FACE_CANCEL_TOOLTIP),
1299 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_ACCEPT), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_FACE_OK_TOOLTIP),
1300 EndContainer(),
1303 /** Management class for customizing the face of the company manager. */
1304 class SelectCompanyManagerFaceWindow : public Window
1306 CompanyManagerFace face; ///< company manager face bits
1307 bool advanced; ///< advanced company manager face selection window
1309 GenderEthnicity ge; ///< Gender and ethnicity.
1310 bool is_female; ///< Female face.
1311 bool is_moust_male; ///< Male face with a moustache.
1313 Dimension yesno_dim; ///< Dimension of a yes/no button of a part in the advanced face window.
1314 Dimension number_dim; ///< Dimension of a number widget of a part in the advanced face window.
1317 * Set parameters for value of face control buttons.
1319 * @param widget_index index of this widget in the window
1320 * @param val the value which will be displayed
1321 * @param is_bool_widget is it a bool button
1323 void SetFaceStringParameters(WidgetID widget_index, uint8_t val, bool is_bool_widget) const
1325 const NWidgetCore *nwi_widget = this->GetWidget<NWidgetCore>(widget_index);
1326 if (nwi_widget->IsDisabled()) {
1327 SetDParam(0, STR_EMPTY);
1328 } else {
1329 if (is_bool_widget) {
1330 /* if it a bool button write yes or no */
1331 SetDParam(0, (val != 0) ? STR_FACE_YES : STR_FACE_NO);
1332 } else {
1333 /* else write the value + 1 */
1334 SetDParam(0, STR_JUST_INT);
1335 SetDParam(1, val + 1);
1340 void UpdateData()
1342 this->ge = (GenderEthnicity)GB(this->face, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // get the gender and ethnicity
1343 this->is_female = HasBit(this->ge, GENDER_FEMALE); // get the gender: 0 == male and 1 == female
1344 this->is_moust_male = !is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge) != 0; // is a male face with moustache
1346 this->GetWidget<NWidgetCore>(WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT)->widget_data = this->is_female ? STR_FACE_EARRING : STR_FACE_MOUSTACHE;
1347 this->GetWidget<NWidgetCore>(WID_SCMF_TIE_EARRING_TEXT)->widget_data = this->is_female ? STR_FACE_EARRING : STR_FACE_TIE;
1348 this->GetWidget<NWidgetCore>(WID_SCMF_LIPS_MOUSTACHE_TEXT)->widget_data = this->is_moust_male ? STR_FACE_MOUSTACHE : STR_FACE_LIPS;
1351 public:
1352 SelectCompanyManagerFaceWindow(WindowDesc &desc, Window *parent) : Window(desc)
1354 this->advanced = false;
1355 this->CreateNestedTree();
1356 this->SelectDisplayPlanes(this->advanced);
1357 this->FinishInitNested(parent->window_number);
1358 this->parent = parent;
1359 this->owner = (Owner)this->window_number;
1360 this->face = Company::Get((CompanyID)this->window_number)->face;
1362 this->UpdateData();
1366 * Select planes to display to the user with the #NWID_SELECTION widgets #WID_SCMF_SEL_LOADSAVE, #WID_SCMF_SEL_MALEFEMALE, and #WID_SCMF_SEL_PARTS.
1367 * @param advanced Display advanced face management window.
1369 void SelectDisplayPlanes(bool advanced)
1371 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_LOADSAVE)->SetDisplayedPlane(advanced ? 0 : SZSP_NONE);
1372 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_PARTS)->SetDisplayedPlane(advanced ? 0 : SZSP_NONE);
1373 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_MALEFEMALE)->SetDisplayedPlane(advanced ? SZSP_NONE : 0);
1374 this->GetWidget<NWidgetCore>(WID_SCMF_RANDOM_NEW_FACE)->widget_data = advanced ? STR_FACE_RANDOM : STR_FACE_NEW_FACE_BUTTON;
1376 NWidgetCore *wi = this->GetWidget<NWidgetCore>(WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON);
1377 if (advanced) {
1378 wi->SetDataTip(STR_FACE_SIMPLE, STR_FACE_SIMPLE_TOOLTIP);
1379 } else {
1380 wi->SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP);
1384 void OnInit() override
1386 /* Size of the boolean yes/no button. */
1387 Dimension yesno_dim = maxdim(GetStringBoundingBox(STR_FACE_YES), GetStringBoundingBox(STR_FACE_NO));
1388 yesno_dim.width += WidgetDimensions::scaled.framerect.Horizontal();
1389 yesno_dim.height += WidgetDimensions::scaled.framerect.Vertical();
1390 /* Size of the number button + arrows. */
1391 Dimension number_dim = {0, 0};
1392 for (int val = 1; val <= 12; val++) {
1393 SetDParam(0, val);
1394 number_dim = maxdim(number_dim, GetStringBoundingBox(STR_JUST_INT));
1396 uint arrows_width = GetSpriteSize(SPR_ARROW_LEFT).width + GetSpriteSize(SPR_ARROW_RIGHT).width + 2 * (WidgetDimensions::scaled.imgbtn.Horizontal());
1397 number_dim.width += WidgetDimensions::scaled.framerect.Horizontal() + arrows_width;
1398 number_dim.height += WidgetDimensions::scaled.framerect.Vertical();
1399 /* Compute width of both buttons. */
1400 yesno_dim.width = std::max(yesno_dim.width, number_dim.width);
1401 number_dim.width = yesno_dim.width - arrows_width;
1403 this->yesno_dim = yesno_dim;
1404 this->number_dim = number_dim;
1407 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
1409 switch (widget) {
1410 case WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT:
1411 size = maxdim(size, GetStringBoundingBox(STR_FACE_EARRING));
1412 size = maxdim(size, GetStringBoundingBox(STR_FACE_MOUSTACHE));
1413 break;
1415 case WID_SCMF_TIE_EARRING_TEXT:
1416 size = maxdim(size, GetStringBoundingBox(STR_FACE_EARRING));
1417 size = maxdim(size, GetStringBoundingBox(STR_FACE_TIE));
1418 break;
1420 case WID_SCMF_LIPS_MOUSTACHE_TEXT:
1421 size = maxdim(size, GetStringBoundingBox(STR_FACE_LIPS));
1422 size = maxdim(size, GetStringBoundingBox(STR_FACE_MOUSTACHE));
1423 break;
1425 case WID_SCMF_FACE:
1426 size = maxdim(size, GetScaledSpriteSize(SPR_GRADIENT));
1427 break;
1429 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1430 case WID_SCMF_HAS_GLASSES:
1431 size = this->yesno_dim;
1432 break;
1434 case WID_SCMF_EYECOLOUR:
1435 case WID_SCMF_CHIN:
1436 case WID_SCMF_EYEBROWS:
1437 case WID_SCMF_LIPS_MOUSTACHE:
1438 case WID_SCMF_NOSE:
1439 case WID_SCMF_HAIR:
1440 case WID_SCMF_JACKET:
1441 case WID_SCMF_COLLAR:
1442 case WID_SCMF_TIE_EARRING:
1443 case WID_SCMF_GLASSES:
1444 size = this->number_dim;
1445 break;
1449 void OnPaint() override
1451 /* lower the non-selected gender button */
1452 this->SetWidgetsLoweredState(!this->is_female, WID_SCMF_MALE, WID_SCMF_MALE2);
1453 this->SetWidgetsLoweredState( this->is_female, WID_SCMF_FEMALE, WID_SCMF_FEMALE2);
1455 /* advanced company manager face selection window */
1457 /* lower the non-selected ethnicity button */
1458 this->SetWidgetLoweredState(WID_SCMF_ETHNICITY_EUR, !HasBit(this->ge, ETHNICITY_BLACK));
1459 this->SetWidgetLoweredState(WID_SCMF_ETHNICITY_AFR, HasBit(this->ge, ETHNICITY_BLACK));
1462 /* Disable dynamically the widgets which CompanyManagerFaceVariable has less than 2 options
1463 * (or in other words you haven't any choice).
1464 * If the widgets depend on a HAS-variable and this is false the widgets will be disabled, too. */
1466 /* Eye colour buttons */
1467 this->SetWidgetsDisabledState(_cmf_info[CMFV_EYE_COLOUR].valid_values[this->ge] < 2,
1468 WID_SCMF_EYECOLOUR, WID_SCMF_EYECOLOUR_L, WID_SCMF_EYECOLOUR_R);
1470 /* Chin buttons */
1471 this->SetWidgetsDisabledState(_cmf_info[CMFV_CHIN].valid_values[this->ge] < 2,
1472 WID_SCMF_CHIN, WID_SCMF_CHIN_L, WID_SCMF_CHIN_R);
1474 /* Eyebrows buttons */
1475 this->SetWidgetsDisabledState(_cmf_info[CMFV_EYEBROWS].valid_values[this->ge] < 2,
1476 WID_SCMF_EYEBROWS, WID_SCMF_EYEBROWS_L, WID_SCMF_EYEBROWS_R);
1478 /* Lips or (if it a male face with a moustache) moustache buttons */
1479 this->SetWidgetsDisabledState(_cmf_info[this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS].valid_values[this->ge] < 2,
1480 WID_SCMF_LIPS_MOUSTACHE, WID_SCMF_LIPS_MOUSTACHE_L, WID_SCMF_LIPS_MOUSTACHE_R);
1482 /* Nose buttons | male faces with moustache haven't any nose options */
1483 this->SetWidgetsDisabledState(_cmf_info[CMFV_NOSE].valid_values[this->ge] < 2 || this->is_moust_male,
1484 WID_SCMF_NOSE, WID_SCMF_NOSE_L, WID_SCMF_NOSE_R);
1486 /* Hair buttons */
1487 this->SetWidgetsDisabledState(_cmf_info[CMFV_HAIR].valid_values[this->ge] < 2,
1488 WID_SCMF_HAIR, WID_SCMF_HAIR_L, WID_SCMF_HAIR_R);
1490 /* Jacket buttons */
1491 this->SetWidgetsDisabledState(_cmf_info[CMFV_JACKET].valid_values[this->ge] < 2,
1492 WID_SCMF_JACKET, WID_SCMF_JACKET_L, WID_SCMF_JACKET_R);
1494 /* Collar buttons */
1495 this->SetWidgetsDisabledState(_cmf_info[CMFV_COLLAR].valid_values[this->ge] < 2,
1496 WID_SCMF_COLLAR, WID_SCMF_COLLAR_L, WID_SCMF_COLLAR_R);
1498 /* Tie/earring buttons | female faces without earring haven't any earring options */
1499 this->SetWidgetsDisabledState(_cmf_info[CMFV_TIE_EARRING].valid_values[this->ge] < 2 ||
1500 (this->is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge) == 0),
1501 WID_SCMF_TIE_EARRING, WID_SCMF_TIE_EARRING_L, WID_SCMF_TIE_EARRING_R);
1503 /* Glasses buttons | faces without glasses haven't any glasses options */
1504 this->SetWidgetsDisabledState(_cmf_info[CMFV_GLASSES].valid_values[this->ge] < 2 || GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge) == 0,
1505 WID_SCMF_GLASSES, WID_SCMF_GLASSES_L, WID_SCMF_GLASSES_R);
1507 this->DrawWidgets();
1510 void SetStringParameters(WidgetID widget) const override
1512 switch (widget) {
1513 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1514 if (this->is_female) { // Only for female faces
1515 this->SetFaceStringParameters(WID_SCMF_HAS_MOUSTACHE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge), true);
1516 } else { // Only for male faces
1517 this->SetFaceStringParameters(WID_SCMF_HAS_MOUSTACHE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge), true);
1519 break;
1521 case WID_SCMF_TIE_EARRING:
1522 this->SetFaceStringParameters(WID_SCMF_TIE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_TIE_EARRING, this->ge), false);
1523 break;
1525 case WID_SCMF_LIPS_MOUSTACHE:
1526 if (this->is_moust_male) { // Only for male faces with moustache
1527 this->SetFaceStringParameters(WID_SCMF_LIPS_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_MOUSTACHE, this->ge), false);
1528 } else { // Only for female faces or male faces without moustache
1529 this->SetFaceStringParameters(WID_SCMF_LIPS_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_LIPS, this->ge), false);
1531 break;
1533 case WID_SCMF_HAS_GLASSES:
1534 this->SetFaceStringParameters(WID_SCMF_HAS_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge), true );
1535 break;
1537 case WID_SCMF_HAIR:
1538 this->SetFaceStringParameters(WID_SCMF_HAIR, GetCompanyManagerFaceBits(this->face, CMFV_HAIR, this->ge), false);
1539 break;
1541 case WID_SCMF_EYEBROWS:
1542 this->SetFaceStringParameters(WID_SCMF_EYEBROWS, GetCompanyManagerFaceBits(this->face, CMFV_EYEBROWS, this->ge), false);
1543 break;
1545 case WID_SCMF_EYECOLOUR:
1546 this->SetFaceStringParameters(WID_SCMF_EYECOLOUR, GetCompanyManagerFaceBits(this->face, CMFV_EYE_COLOUR, this->ge), false);
1547 break;
1549 case WID_SCMF_GLASSES:
1550 this->SetFaceStringParameters(WID_SCMF_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_GLASSES, this->ge), false);
1551 break;
1553 case WID_SCMF_NOSE:
1554 this->SetFaceStringParameters(WID_SCMF_NOSE, GetCompanyManagerFaceBits(this->face, CMFV_NOSE, this->ge), false);
1555 break;
1557 case WID_SCMF_CHIN:
1558 this->SetFaceStringParameters(WID_SCMF_CHIN, GetCompanyManagerFaceBits(this->face, CMFV_CHIN, this->ge), false);
1559 break;
1561 case WID_SCMF_JACKET:
1562 this->SetFaceStringParameters(WID_SCMF_JACKET, GetCompanyManagerFaceBits(this->face, CMFV_JACKET, this->ge), false);
1563 break;
1565 case WID_SCMF_COLLAR:
1566 this->SetFaceStringParameters(WID_SCMF_COLLAR, GetCompanyManagerFaceBits(this->face, CMFV_COLLAR, this->ge), false);
1567 break;
1571 void DrawWidget(const Rect &r, WidgetID widget) const override
1573 switch (widget) {
1574 case WID_SCMF_FACE:
1575 DrawCompanyManagerFace(this->face, Company::Get((CompanyID)this->window_number)->colour, r);
1576 break;
1580 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
1582 switch (widget) {
1583 /* Toggle size, advanced/simple face selection */
1584 case WID_SCMF_TOGGLE_LARGE_SMALL:
1585 case WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON:
1586 this->advanced = !this->advanced;
1587 this->SelectDisplayPlanes(this->advanced);
1588 this->ReInit();
1589 break;
1591 /* OK button */
1592 case WID_SCMF_ACCEPT:
1593 Command<CMD_SET_COMPANY_MANAGER_FACE>::Post(this->face);
1594 [[fallthrough]];
1596 /* Cancel button */
1597 case WID_SCMF_CANCEL:
1598 this->Close();
1599 break;
1601 /* Load button */
1602 case WID_SCMF_LOAD:
1603 this->face = _company_manager_face;
1604 ScaleAllCompanyManagerFaceBits(this->face);
1605 ShowErrorMessage(STR_FACE_LOAD_DONE, INVALID_STRING_ID, WL_INFO);
1606 this->UpdateData();
1607 this->SetDirty();
1608 break;
1610 /* 'Company manager face number' button, view and/or set company manager face number */
1611 case WID_SCMF_FACECODE:
1612 SetDParam(0, this->face);
1613 ShowQueryString(STR_JUST_INT, STR_FACE_FACECODE_CAPTION, 10 + 1, this, CS_NUMERAL, QSF_NONE);
1614 break;
1616 /* Save button */
1617 case WID_SCMF_SAVE:
1618 _company_manager_face = this->face;
1619 ShowErrorMessage(STR_FACE_SAVE_DONE, INVALID_STRING_ID, WL_INFO);
1620 break;
1622 /* Toggle gender (male/female) button */
1623 case WID_SCMF_MALE:
1624 case WID_SCMF_FEMALE:
1625 case WID_SCMF_MALE2:
1626 case WID_SCMF_FEMALE2:
1627 SetCompanyManagerFaceBits(this->face, CMFV_GENDER, this->ge, (widget == WID_SCMF_FEMALE || widget == WID_SCMF_FEMALE2));
1628 ScaleAllCompanyManagerFaceBits(this->face);
1629 this->UpdateData();
1630 this->SetDirty();
1631 break;
1633 /* Randomize face button */
1634 case WID_SCMF_RANDOM_NEW_FACE:
1635 RandomCompanyManagerFaceBits(this->face, this->ge, this->advanced, _interactive_random);
1636 this->UpdateData();
1637 this->SetDirty();
1638 break;
1640 /* Toggle ethnicity (european/african) button */
1641 case WID_SCMF_ETHNICITY_EUR:
1642 case WID_SCMF_ETHNICITY_AFR:
1643 SetCompanyManagerFaceBits(this->face, CMFV_ETHNICITY, this->ge, widget - WID_SCMF_ETHNICITY_EUR);
1644 ScaleAllCompanyManagerFaceBits(this->face);
1645 this->UpdateData();
1646 this->SetDirty();
1647 break;
1649 default:
1650 /* Here all buttons from WID_SCMF_HAS_MOUSTACHE_EARRING to WID_SCMF_GLASSES_R are handled.
1651 * First it checks which CompanyManagerFaceVariable is being changed, and then either
1652 * a: invert the value for boolean variables, or
1653 * b: it checks inside of IncreaseCompanyManagerFaceBits() if a left (_L) butten is pressed and then decrease else increase the variable */
1654 if (widget >= WID_SCMF_HAS_MOUSTACHE_EARRING && widget <= WID_SCMF_GLASSES_R) {
1655 CompanyManagerFaceVariable cmfv; // which CompanyManagerFaceVariable shall be edited
1657 if (widget < WID_SCMF_EYECOLOUR_L) { // Bool buttons
1658 switch (widget - WID_SCMF_HAS_MOUSTACHE_EARRING) {
1659 default: NOT_REACHED();
1660 case 0: cmfv = this->is_female ? CMFV_HAS_TIE_EARRING : CMFV_HAS_MOUSTACHE; break; // Has earring/moustache button
1661 case 1: cmfv = CMFV_HAS_GLASSES; break; // Has glasses button
1663 SetCompanyManagerFaceBits(this->face, cmfv, this->ge, !GetCompanyManagerFaceBits(this->face, cmfv, this->ge));
1664 ScaleAllCompanyManagerFaceBits(this->face);
1665 } else { // Value buttons
1666 switch ((widget - WID_SCMF_EYECOLOUR_L) / 3) {
1667 default: NOT_REACHED();
1668 case 0: cmfv = CMFV_EYE_COLOUR; break; // Eye colour buttons
1669 case 1: cmfv = CMFV_CHIN; break; // Chin buttons
1670 case 2: cmfv = CMFV_EYEBROWS; break; // Eyebrows buttons
1671 case 3: cmfv = this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS; break; // Moustache or lips buttons
1672 case 4: cmfv = CMFV_NOSE; break; // Nose buttons
1673 case 5: cmfv = CMFV_HAIR; break; // Hair buttons
1674 case 6: cmfv = CMFV_JACKET; break; // Jacket buttons
1675 case 7: cmfv = CMFV_COLLAR; break; // Collar buttons
1676 case 8: cmfv = CMFV_TIE_EARRING; break; // Tie/earring buttons
1677 case 9: cmfv = CMFV_GLASSES; break; // Glasses buttons
1679 /* 0 == left (_L), 1 == middle or 2 == right (_R) - button click */
1680 IncreaseCompanyManagerFaceBits(this->face, cmfv, this->ge, (((widget - WID_SCMF_EYECOLOUR_L) % 3) != 0) ? 1 : -1);
1682 this->UpdateData();
1683 this->SetDirty();
1685 break;
1689 void OnQueryTextFinished(std::optional<std::string> str) override
1691 if (!str.has_value()) return;
1692 /* Set a new company manager face number */
1693 if (!str->empty()) {
1694 this->face = std::strtoul(str->c_str(), nullptr, 10);
1695 ScaleAllCompanyManagerFaceBits(this->face);
1696 ShowErrorMessage(STR_FACE_FACECODE_SET, INVALID_STRING_ID, WL_INFO);
1697 this->UpdateData();
1698 this->SetDirty();
1699 } else {
1700 ShowErrorMessage(STR_FACE_FACECODE_ERR, INVALID_STRING_ID, WL_INFO);
1705 /** Company manager face selection window description */
1706 static WindowDesc _select_company_manager_face_desc(
1707 WDP_AUTO, nullptr, 0, 0,
1708 WC_COMPANY_MANAGER_FACE, WC_NONE,
1709 WDF_CONSTRUCTION,
1710 _nested_select_company_manager_face_widgets
1714 * Open the simple/advanced company manager face selection window
1716 * @param parent the parent company window
1718 static void DoSelectCompanyManagerFace(Window *parent)
1720 if (!Company::IsValidID((CompanyID)parent->window_number)) return;
1722 if (BringWindowToFrontById(WC_COMPANY_MANAGER_FACE, parent->window_number)) return;
1723 new SelectCompanyManagerFaceWindow(_select_company_manager_face_desc, parent);
1726 static constexpr NWidgetPart _nested_company_infrastructure_widgets[] = {
1727 NWidget(NWID_HORIZONTAL),
1728 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1729 NWidget(WWT_CAPTION, COLOUR_GREY, WID_CI_CAPTION), SetDataTip(STR_COMPANY_INFRASTRUCTURE_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1730 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1731 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1732 EndContainer(),
1733 NWidget(WWT_PANEL, COLOUR_GREY),
1734 NWidget(NWID_VERTICAL), SetPadding(WidgetDimensions::unscaled.framerect), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
1735 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
1736 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_RAIL_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1737 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_RAIL_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1738 EndContainer(),
1739 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
1740 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_ROAD_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1741 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_ROAD_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1742 EndContainer(),
1743 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
1744 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TRAM_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1745 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TRAM_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1746 EndContainer(),
1747 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
1748 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_WATER_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1749 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_WATER_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1750 EndContainer(),
1751 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
1752 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_STATION_DESC), SetMinimalTextLines(3, 0), SetFill(1, 0),
1753 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_STATION_COUNT), SetMinimalTextLines(3, 0), SetFill(0, 1),
1754 EndContainer(),
1755 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
1756 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TOTAL_DESC), SetFill(1, 0),
1757 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TOTAL), SetFill(0, 1),
1758 EndContainer(),
1759 EndContainer(),
1760 EndContainer(),
1764 * Window with detailed information about the company's infrastructure.
1766 struct CompanyInfrastructureWindow : Window
1768 RailTypes railtypes; ///< Valid railtypes.
1769 RoadTypes roadtypes; ///< Valid roadtypes.
1771 uint total_width; ///< String width of the total cost line.
1773 CompanyInfrastructureWindow(WindowDesc &desc, WindowNumber window_number) : Window(desc)
1775 this->UpdateRailRoadTypes();
1777 this->InitNested(window_number);
1778 this->owner = (Owner)this->window_number;
1781 void UpdateRailRoadTypes()
1783 this->railtypes = RAILTYPES_NONE;
1784 this->roadtypes = ROADTYPES_NONE;
1786 /* Find the used railtypes. */
1787 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
1788 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1790 this->railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1793 /* Get the date introduced railtypes as well. */
1794 this->railtypes = AddDateIntroducedRailTypes(this->railtypes, CalendarTime::MAX_DATE);
1796 /* Find the used roadtypes. */
1797 for (const Engine *e : Engine::IterateType(VEH_ROAD)) {
1798 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1800 this->roadtypes |= GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes;
1803 /* Get the date introduced roadtypes as well. */
1804 this->roadtypes = AddDateIntroducedRoadTypes(this->roadtypes, CalendarTime::MAX_DATE);
1805 this->roadtypes &= ~_roadtypes_hidden_mask;
1808 /** Get total infrastructure maintenance cost. */
1809 Money GetTotalMaintenanceCost() const
1811 const Company *c = Company::Get((CompanyID)this->window_number);
1812 Money total;
1814 uint32_t rail_total = c->infrastructure.GetRailTotal();
1815 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
1816 if (HasBit(this->railtypes, rt)) total += RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total);
1818 total += SignalMaintenanceCost(c->infrastructure.signal);
1820 uint32_t road_total = c->infrastructure.GetRoadTotal();
1821 uint32_t tram_total = c->infrastructure.GetTramTotal();
1822 for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
1823 if (HasBit(this->roadtypes, rt)) total += RoadMaintenanceCost(rt, c->infrastructure.road[rt], RoadTypeIsRoad(rt) ? road_total : tram_total);
1826 total += CanalMaintenanceCost(c->infrastructure.water);
1827 total += StationMaintenanceCost(c->infrastructure.station);
1828 total += AirportMaintenanceCost(c->index);
1830 return total;
1833 void SetStringParameters(WidgetID widget) const override
1835 switch (widget) {
1836 case WID_CI_CAPTION:
1837 SetDParam(0, (CompanyID)this->window_number);
1838 break;
1842 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
1844 const Company *c = Company::Get((CompanyID)this->window_number);
1846 switch (widget) {
1847 case WID_CI_RAIL_DESC: {
1848 uint lines = 1; // Starts at 1 because a line is also required for the section title
1850 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT).width + padding.width);
1852 for (const auto &rt : _sorted_railtypes) {
1853 if (HasBit(this->railtypes, rt)) {
1854 lines++;
1855 size.width = std::max(size.width, GetStringBoundingBox(GetRailTypeInfo(rt)->strings.name).width + padding.width + WidgetDimensions::scaled.hsep_indent);
1858 if (this->railtypes != RAILTYPES_NONE) {
1859 lines++;
1860 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS).width + padding.width + WidgetDimensions::scaled.hsep_indent);
1863 size.height = std::max(size.height, lines * GetCharacterHeight(FS_NORMAL));
1864 break;
1867 case WID_CI_ROAD_DESC:
1868 case WID_CI_TRAM_DESC: {
1869 uint lines = 1; // Starts at 1 because a line is also required for the section title
1871 size.width = std::max(size.width, GetStringBoundingBox(widget == WID_CI_ROAD_DESC ? STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT : STR_COMPANY_INFRASTRUCTURE_VIEW_TRAM_SECT).width + padding.width);
1873 for (const auto &rt : _sorted_roadtypes) {
1874 if (HasBit(this->roadtypes, rt) && RoadTypeIsRoad(rt) == (widget == WID_CI_ROAD_DESC)) {
1875 lines++;
1876 size.width = std::max(size.width, GetStringBoundingBox(GetRoadTypeInfo(rt)->strings.name).width + padding.width + WidgetDimensions::scaled.hsep_indent);
1880 size.height = std::max(size.height, lines * GetCharacterHeight(FS_NORMAL));
1881 break;
1884 case WID_CI_WATER_DESC:
1885 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT).width + padding.width);
1886 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS).width + padding.width + WidgetDimensions::scaled.hsep_indent);
1887 break;
1889 case WID_CI_STATION_DESC:
1890 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT).width + padding.width);
1891 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS).width + padding.width + WidgetDimensions::scaled.hsep_indent);
1892 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS).width + padding.width + WidgetDimensions::scaled.hsep_indent);
1893 break;
1895 case WID_CI_RAIL_COUNT:
1896 case WID_CI_ROAD_COUNT:
1897 case WID_CI_TRAM_COUNT:
1898 case WID_CI_WATER_COUNT:
1899 case WID_CI_STATION_COUNT:
1900 case WID_CI_TOTAL: {
1901 /* Find the maximum count that is displayed. */
1902 uint32_t max_val = 1000; // Some random number to reserve enough space.
1903 Money max_cost = 10000; // Some random number to reserve enough space.
1904 uint32_t rail_total = c->infrastructure.GetRailTotal();
1905 for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) {
1906 max_val = std::max(max_val, c->infrastructure.rail[rt]);
1907 max_cost = std::max(max_cost, RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
1909 max_val = std::max(max_val, c->infrastructure.signal);
1910 max_cost = std::max(max_cost, SignalMaintenanceCost(c->infrastructure.signal));
1911 uint32_t road_total = c->infrastructure.GetRoadTotal();
1912 uint32_t tram_total = c->infrastructure.GetTramTotal();
1913 for (RoadType rt = ROADTYPE_BEGIN; rt < ROADTYPE_END; rt++) {
1914 max_val = std::max(max_val, c->infrastructure.road[rt]);
1915 max_cost = std::max(max_cost, RoadMaintenanceCost(rt, c->infrastructure.road[rt], RoadTypeIsRoad(rt) ? road_total : tram_total));
1918 max_val = std::max(max_val, c->infrastructure.water);
1919 max_cost = std::max(max_cost, CanalMaintenanceCost(c->infrastructure.water));
1920 max_val = std::max(max_val, c->infrastructure.station);
1921 max_cost = std::max(max_cost, StationMaintenanceCost(c->infrastructure.station));
1922 max_val = std::max(max_val, c->infrastructure.airport);
1923 max_cost = std::max(max_cost, AirportMaintenanceCost(c->index));
1925 SetDParamMaxValue(0, max_val);
1926 uint count_width = GetStringBoundingBox(STR_JUST_COMMA).width + WidgetDimensions::scaled.hsep_indent; // Reserve some wiggle room
1928 if (_settings_game.economy.infrastructure_maintenance) {
1929 StringID str_total = TimerGameEconomy::UsingWallclockUnits() ? STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL_PERIOD : STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL_YEAR;
1930 SetDParamMaxValue(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
1931 this->total_width = GetStringBoundingBox(str_total).width + WidgetDimensions::scaled.hsep_indent * 2;
1932 size.width = std::max(size.width, this->total_width);
1934 SetDParamMaxValue(0, max_cost * 12); // Convert to per year
1935 count_width += std::max(this->total_width, GetStringBoundingBox(str_total).width);
1938 size.width = std::max(size.width, count_width);
1940 /* Set height of the total line. */
1941 if (widget == WID_CI_TOTAL) {
1942 size.height = _settings_game.economy.infrastructure_maintenance ? std::max<uint>(size.height, WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL)) : 0;
1944 break;
1950 * Helper for drawing the counts line.
1951 * @param r The bounds to draw in.
1952 * @param y The y position to draw at.
1953 * @param count The count to show on this line.
1954 * @param monthly_cost The monthly costs.
1956 void DrawCountLine(const Rect &r, int &y, int count, Money monthly_cost) const
1958 SetDParam(0, count);
1959 DrawString(r.left, r.right, y += GetCharacterHeight(FS_NORMAL), STR_JUST_COMMA, TC_WHITE, SA_RIGHT);
1961 if (_settings_game.economy.infrastructure_maintenance) {
1962 SetDParam(0, monthly_cost * 12); // Convert to per year
1963 Rect tr = r.WithWidth(this->total_width, _current_text_dir == TD_RTL);
1964 DrawString(tr.left, tr.right, y,
1965 TimerGameEconomy::UsingWallclockUnits() ? STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL_PERIOD : STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL_YEAR,
1966 TC_FROMSTRING, SA_RIGHT);
1970 void DrawWidget(const Rect &r, WidgetID widget) const override
1972 const Company *c = Company::Get((CompanyID)this->window_number);
1974 int y = r.top;
1976 Rect ir = r.Indent(WidgetDimensions::scaled.hsep_indent, _current_text_dir == TD_RTL);
1977 switch (widget) {
1978 case WID_CI_RAIL_DESC:
1979 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT);
1981 if (this->railtypes != RAILTYPES_NONE) {
1982 /* Draw name of each valid railtype. */
1983 for (const auto &rt : _sorted_railtypes) {
1984 if (HasBit(this->railtypes, rt)) {
1985 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), GetRailTypeInfo(rt)->strings.name, TC_WHITE);
1988 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS);
1989 } else {
1990 /* No valid railtype. */
1991 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
1994 break;
1996 case WID_CI_RAIL_COUNT: {
1997 /* Draw infrastructure count for each valid railtype. */
1998 uint32_t rail_total = c->infrastructure.GetRailTotal();
1999 for (const auto &rt : _sorted_railtypes) {
2000 if (HasBit(this->railtypes, rt)) {
2001 this->DrawCountLine(r, y, c->infrastructure.rail[rt], RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
2004 if (this->railtypes != RAILTYPES_NONE) {
2005 this->DrawCountLine(r, y, c->infrastructure.signal, SignalMaintenanceCost(c->infrastructure.signal));
2007 break;
2010 case WID_CI_ROAD_DESC:
2011 case WID_CI_TRAM_DESC: {
2012 DrawString(r.left, r.right, y, widget == WID_CI_ROAD_DESC ? STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT : STR_COMPANY_INFRASTRUCTURE_VIEW_TRAM_SECT);
2014 /* Draw name of each valid roadtype. */
2015 for (const auto &rt : _sorted_roadtypes) {
2016 if (HasBit(this->roadtypes, rt) && RoadTypeIsRoad(rt) == (widget == WID_CI_ROAD_DESC)) {
2017 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), GetRoadTypeInfo(rt)->strings.name, TC_WHITE);
2021 break;
2024 case WID_CI_ROAD_COUNT:
2025 case WID_CI_TRAM_COUNT: {
2026 uint32_t road_tram_total = widget == WID_CI_ROAD_COUNT ? c->infrastructure.GetRoadTotal() : c->infrastructure.GetTramTotal();
2027 for (const auto &rt : _sorted_roadtypes) {
2028 if (HasBit(this->roadtypes, rt) && RoadTypeIsRoad(rt) == (widget == WID_CI_ROAD_COUNT)) {
2029 this->DrawCountLine(r, y, c->infrastructure.road[rt], RoadMaintenanceCost(rt, c->infrastructure.road[rt], road_tram_total));
2032 break;
2035 case WID_CI_WATER_DESC:
2036 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT);
2037 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS);
2038 break;
2040 case WID_CI_WATER_COUNT:
2041 this->DrawCountLine(r, y, c->infrastructure.water, CanalMaintenanceCost(c->infrastructure.water));
2042 break;
2044 case WID_CI_TOTAL:
2045 if (_settings_game.economy.infrastructure_maintenance) {
2046 Rect tr = r.WithWidth(this->total_width, _current_text_dir == TD_RTL);
2047 GfxFillRect(tr.left, y, tr.right, y + WidgetDimensions::scaled.bevel.top - 1, PC_WHITE);
2048 y += WidgetDimensions::scaled.vsep_normal;
2049 SetDParam(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
2050 DrawString(tr.left, tr.right, y,
2051 TimerGameEconomy::UsingWallclockUnits() ? STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL_PERIOD : STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL_YEAR,
2052 TC_FROMSTRING, SA_RIGHT);
2054 break;
2056 case WID_CI_STATION_DESC:
2057 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT);
2058 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS);
2059 DrawString(ir.left, ir.right, y += GetCharacterHeight(FS_NORMAL), STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS);
2060 break;
2062 case WID_CI_STATION_COUNT:
2063 this->DrawCountLine(r, y, c->infrastructure.station, StationMaintenanceCost(c->infrastructure.station));
2064 this->DrawCountLine(r, y, c->infrastructure.airport, AirportMaintenanceCost(c->index));
2065 break;
2070 * Some data on this window has become invalid.
2071 * @param data Information about the changed data.
2072 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
2074 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
2076 if (!gui_scope) return;
2078 this->UpdateRailRoadTypes();
2079 this->ReInit();
2083 static WindowDesc _company_infrastructure_desc(
2084 WDP_AUTO, "company_infrastructure", 0, 0,
2085 WC_COMPANY_INFRASTRUCTURE, WC_NONE,
2087 _nested_company_infrastructure_widgets
2091 * Open the infrastructure window of a company.
2092 * @param company Company to show infrastructure of.
2094 static void ShowCompanyInfrastructure(CompanyID company)
2096 if (!Company::IsValidID(company)) return;
2097 AllocateWindowDescFront<CompanyInfrastructureWindow>(_company_infrastructure_desc, company);
2100 static constexpr NWidgetPart _nested_company_widgets[] = {
2101 NWidget(NWID_HORIZONTAL),
2102 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2103 NWidget(WWT_CAPTION, COLOUR_GREY, WID_C_CAPTION), SetDataTip(STR_COMPANY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2104 NWidget(WWT_SHADEBOX, COLOUR_GREY),
2105 NWidget(WWT_STICKYBOX, COLOUR_GREY),
2106 EndContainer(),
2107 NWidget(WWT_PANEL, COLOUR_GREY),
2108 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0), SetPadding(4),
2109 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
2110 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE), SetMinimalSize(92, 119), SetFill(1, 0),
2111 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE_TITLE), SetFill(1, 1), SetMinimalTextLines(2, 0),
2112 EndContainer(),
2113 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
2114 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
2115 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
2116 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INAUGURATION), SetDataTip(STR_JUST_STRING2, STR_NULL), SetFill(1, 0),
2117 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
2118 NWidget(WWT_LABEL, COLOUR_GREY, WID_C_DESC_COLOUR_SCHEME), SetDataTip(STR_COMPANY_VIEW_COLOUR_SCHEME_TITLE, STR_NULL),
2119 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_COLOUR_SCHEME_EXAMPLE), SetMinimalSize(30, 0), SetFill(1, 1),
2120 EndContainer(),
2121 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
2122 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_VEHICLE), SetDataTip(STR_COMPANY_VIEW_VEHICLES_TITLE, STR_NULL), SetAlignment(SA_LEFT | SA_TOP),
2123 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_VEHICLE_COUNTS), SetMinimalTextLines(4, 0), SetFill(1, 1),
2124 EndContainer(),
2125 EndContainer(),
2126 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
2127 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_VIEW_BUILD_HQ),
2128 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_VIEW_HQ), SetDataTip(STR_COMPANY_VIEW_VIEW_HQ_BUTTON, STR_COMPANY_VIEW_VIEW_HQ_TOOLTIP),
2129 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_C_BUILD_HQ), SetDataTip(STR_COMPANY_VIEW_BUILD_HQ_BUTTON, STR_COMPANY_VIEW_BUILD_HQ_TOOLTIP),
2130 EndContainer(),
2131 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_RELOCATE),
2132 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_C_RELOCATE_HQ), SetDataTip(STR_COMPANY_VIEW_RELOCATE_HQ, STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS),
2133 NWidget(NWID_SPACER),
2134 EndContainer(),
2135 EndContainer(),
2136 EndContainer(),
2138 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_COMPANY_VALUE), SetDataTip(STR_COMPANY_VIEW_COMPANY_VALUE, STR_NULL), SetFill(1, 0),
2140 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0),
2141 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE, STR_NULL), SetAlignment(SA_LEFT | SA_TOP),
2142 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_INFRASTRUCTURE_COUNTS), SetMinimalTextLines(5, 0), SetFill(1, 0),
2143 NWidget(NWID_VERTICAL), SetPIPRatio(0, 0, 1),
2144 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_VIEW_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE_BUTTON, STR_COMPANY_VIEW_INFRASTRUCTURE_TOOLTIP),
2145 EndContainer(),
2146 EndContainer(),
2148 /* Multi player buttons. */
2149 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_normal, 0), SetPIPRatio(1, 0, 0),
2150 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_normal, 0),
2151 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_HOSTILE_TAKEOVER),
2152 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_HOSTILE_TAKEOVER), SetDataTip(STR_COMPANY_VIEW_HOSTILE_TAKEOVER_BUTTON, STR_COMPANY_VIEW_HOSTILE_TAKEOVER_TOOLTIP),
2153 EndContainer(),
2154 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_GIVE_MONEY),
2155 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_GIVE_MONEY), SetDataTip(STR_COMPANY_VIEW_GIVE_MONEY_BUTTON, STR_COMPANY_VIEW_GIVE_MONEY_TOOLTIP),
2156 EndContainer(),
2157 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_MULTIPLAYER),
2158 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_JOIN), SetDataTip(STR_COMPANY_VIEW_JOIN, STR_COMPANY_VIEW_JOIN_TOOLTIP),
2159 EndContainer(),
2160 EndContainer(),
2161 EndContainer(),
2162 EndContainer(),
2163 EndContainer(),
2164 EndContainer(),
2165 /* Button bars at the bottom. */
2166 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_BUTTONS),
2167 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2168 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_NEW_FACE), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_NEW_FACE_BUTTON, STR_COMPANY_VIEW_NEW_FACE_TOOLTIP),
2169 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COLOUR_SCHEME), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_COLOUR_SCHEME_BUTTON, STR_COMPANY_VIEW_COLOUR_SCHEME_TOOLTIP),
2170 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_PRESIDENT_NAME), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_PRESIDENT_NAME_BUTTON, STR_COMPANY_VIEW_PRESIDENT_NAME_TOOLTIP),
2171 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_NAME), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_COMPANY_NAME_BUTTON, STR_COMPANY_VIEW_COMPANY_NAME_TOOLTIP),
2172 EndContainer(),
2173 EndContainer(),
2176 /** Strings for the company vehicle counts */
2177 static const StringID _company_view_vehicle_count_strings[] = {
2178 STR_COMPANY_VIEW_TRAINS, STR_COMPANY_VIEW_ROAD_VEHICLES, STR_COMPANY_VIEW_SHIPS, STR_COMPANY_VIEW_AIRCRAFT
2182 * Window with general information about a company
2184 struct CompanyWindow : Window
2186 CompanyWidgets query_widget;
2188 /** Display planes in the company window. */
2189 enum CompanyWindowPlanes {
2190 /* Display planes of the #WID_C_SELECT_VIEW_BUILD_HQ selection widget. */
2191 CWP_VB_VIEW = 0, ///< Display the view button
2192 CWP_VB_BUILD, ///< Display the build button
2194 /* Display planes of the #WID_C_SELECT_RELOCATE selection widget. */
2195 CWP_RELOCATE_SHOW = 0, ///< Show the relocate HQ button.
2196 CWP_RELOCATE_HIDE, ///< Hide the relocate HQ button.
2199 CompanyWindow(WindowDesc &desc, WindowNumber window_number) : Window(desc)
2201 this->InitNested(window_number);
2202 this->owner = (Owner)this->window_number;
2203 this->OnInvalidateData();
2206 void OnPaint() override
2208 const Company *c = Company::Get((CompanyID)this->window_number);
2209 bool local = this->window_number == _local_company;
2211 if (!this->IsShaded()) {
2212 bool reinit = false;
2214 /* Button bar selection. */
2215 reinit |= this->GetWidget<NWidgetStacked>(WID_C_SELECT_BUTTONS)->SetDisplayedPlane(local ? 0 : SZSP_NONE);
2217 /* Build HQ button handling. */
2218 reinit |= this->GetWidget<NWidgetStacked>(WID_C_SELECT_VIEW_BUILD_HQ)->SetDisplayedPlane((local && c->location_of_HQ == INVALID_TILE) ? CWP_VB_BUILD : CWP_VB_VIEW);
2220 this->SetWidgetDisabledState(WID_C_VIEW_HQ, c->location_of_HQ == INVALID_TILE);
2222 /* Enable/disable 'Relocate HQ' button. */
2223 reinit |= this->GetWidget<NWidgetStacked>(WID_C_SELECT_RELOCATE)->SetDisplayedPlane((!local || c->location_of_HQ == INVALID_TILE) ? CWP_RELOCATE_HIDE : CWP_RELOCATE_SHOW);
2224 /* Enable/disable 'Give money' button. */
2225 reinit |= this->GetWidget<NWidgetStacked>(WID_C_SELECT_GIVE_MONEY)->SetDisplayedPlane((local || _local_company == COMPANY_SPECTATOR || !_settings_game.economy.give_money) ? SZSP_NONE : 0);
2226 /* Enable/disable 'Hostile Takeover' button. */
2227 reinit |= this->GetWidget<NWidgetStacked>(WID_C_SELECT_HOSTILE_TAKEOVER)->SetDisplayedPlane((local || _local_company == COMPANY_SPECTATOR || !c->is_ai || _networking) ? SZSP_NONE : 0);
2229 /* Multiplayer buttons. */
2230 reinit |= this->GetWidget<NWidgetStacked>(WID_C_SELECT_MULTIPLAYER)->SetDisplayedPlane((!_networking || !NetworkCanJoinCompany(c->index) || _local_company == c->index) ? (int)SZSP_NONE : 0);
2232 this->SetWidgetDisabledState(WID_C_COMPANY_JOIN, c->is_ai);
2234 if (reinit) {
2235 this->ReInit();
2236 return;
2240 this->DrawWidgets();
2243 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
2245 switch (widget) {
2246 case WID_C_FACE:
2247 size = maxdim(size, GetScaledSpriteSize(SPR_GRADIENT));
2248 break;
2250 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2251 Point offset;
2252 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2253 d.width -= offset.x;
2254 d.height -= offset.y;
2255 size = maxdim(size, d);
2256 break;
2259 case WID_C_DESC_COMPANY_VALUE:
2260 SetDParam(0, INT64_MAX); // Arguably the maximum company value
2261 size.width = GetStringBoundingBox(STR_COMPANY_VIEW_COMPANY_VALUE).width;
2262 break;
2264 case WID_C_DESC_VEHICLE_COUNTS:
2265 SetDParamMaxValue(0, 5000); // Maximum number of vehicles
2266 for (const auto &count_string : _company_view_vehicle_count_strings) {
2267 size.width = std::max(size.width, GetStringBoundingBox(count_string).width + padding.width);
2269 break;
2271 case WID_C_DESC_INFRASTRUCTURE_COUNTS:
2272 SetDParamMaxValue(0, UINT_MAX);
2273 size.width = GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL).width;
2274 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD).width);
2275 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_WATER).width);
2276 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_STATION).width);
2277 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT).width);
2278 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_NONE).width);
2279 size.width += padding.width;
2280 break;
2282 case WID_C_VIEW_HQ:
2283 case WID_C_BUILD_HQ:
2284 case WID_C_RELOCATE_HQ:
2285 case WID_C_VIEW_INFRASTRUCTURE:
2286 case WID_C_GIVE_MONEY:
2287 case WID_C_HOSTILE_TAKEOVER:
2288 case WID_C_COMPANY_JOIN:
2289 size.width = GetStringBoundingBox(STR_COMPANY_VIEW_VIEW_HQ_BUTTON).width;
2290 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_BUILD_HQ_BUTTON).width);
2291 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_RELOCATE_HQ).width);
2292 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_BUTTON).width);
2293 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_GIVE_MONEY_BUTTON).width);
2294 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_HOSTILE_TAKEOVER_BUTTON).width);
2295 size.width = std::max(size.width, GetStringBoundingBox(STR_COMPANY_VIEW_JOIN).width);
2296 size.width += padding.width;
2297 break;
2301 void DrawVehicleCountsWidget(const Rect &r, const Company *c) const
2303 static_assert(VEH_COMPANY_END == lengthof(_company_view_vehicle_count_strings));
2305 int y = r.top;
2306 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
2307 uint amount = c->group_all[type].num_vehicle;
2308 if (amount != 0) {
2309 SetDParam(0, amount);
2310 DrawString(r.left, r.right, y, _company_view_vehicle_count_strings[type]);
2311 y += GetCharacterHeight(FS_NORMAL);
2315 if (y == r.top) {
2316 /* No String was emited before, so there must be no vehicles at all. */
2317 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_VEHICLES_NONE);
2321 void DrawInfrastructureCountsWidget(const Rect &r, const Company *c) const
2323 int y = r.top;
2325 uint rail_pieces = c->infrastructure.signal + c->infrastructure.GetRailTotal();
2326 if (rail_pieces != 0) {
2327 SetDParam(0, rail_pieces);
2328 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL);
2329 y += GetCharacterHeight(FS_NORMAL);
2332 /* GetRoadTotal() skips tram pieces, but we actually want road and tram here. */
2333 uint road_pieces = std::accumulate(std::begin(c->infrastructure.road), std::end(c->infrastructure.road), 0U);
2334 if (road_pieces != 0) {
2335 SetDParam(0, road_pieces);
2336 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD);
2337 y += GetCharacterHeight(FS_NORMAL);
2340 if (c->infrastructure.water != 0) {
2341 SetDParam(0, c->infrastructure.water);
2342 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_WATER);
2343 y += GetCharacterHeight(FS_NORMAL);
2346 if (c->infrastructure.station != 0) {
2347 SetDParam(0, c->infrastructure.station);
2348 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_STATION);
2349 y += GetCharacterHeight(FS_NORMAL);
2352 if (c->infrastructure.airport != 0) {
2353 SetDParam(0, c->infrastructure.airport);
2354 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT);
2355 y += GetCharacterHeight(FS_NORMAL);
2358 if (y == r.top) {
2359 /* No String was emited before, so there must be no infrastructure at all. */
2360 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
2364 void DrawWidget(const Rect &r, WidgetID widget) const override
2366 const Company *c = Company::Get((CompanyID)this->window_number);
2367 switch (widget) {
2368 case WID_C_FACE:
2369 DrawCompanyManagerFace(c->face, c->colour, r);
2370 break;
2372 case WID_C_FACE_TITLE:
2373 SetDParam(0, c->index);
2374 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_COMPANY_VIEW_PRESIDENT_MANAGER_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
2375 break;
2377 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2378 Point offset;
2379 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2380 d.height -= offset.y;
2381 DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOUR(c->index), r.left - offset.x, CenterBounds(r.top, r.bottom, d.height) - offset.y);
2382 break;
2385 case WID_C_DESC_VEHICLE_COUNTS:
2386 DrawVehicleCountsWidget(r, c);
2387 break;
2389 case WID_C_DESC_INFRASTRUCTURE_COUNTS:
2390 DrawInfrastructureCountsWidget(r, c);
2391 break;
2395 void SetStringParameters(WidgetID widget) const override
2397 switch (widget) {
2398 case WID_C_CAPTION:
2399 SetDParam(0, (CompanyID)this->window_number);
2400 SetDParam(1, (CompanyID)this->window_number);
2401 break;
2403 case WID_C_DESC_INAUGURATION:
2404 if (TimerGameEconomy::UsingWallclockUnits()) {
2405 SetDParam(0, STR_COMPANY_VIEW_INAUGURATED_TITLE_WALLCLOCK);
2406 SetDParam(1, Company::Get(static_cast<CompanyID>(this->window_number))->inaugurated_year_calendar);
2407 SetDParam(2, Company::Get(static_cast<CompanyID>(this->window_number))->inaugurated_year);
2408 } else {
2409 SetDParam(0, STR_COMPANY_VIEW_INAUGURATED_TITLE);
2410 SetDParam(1, Company::Get(static_cast<CompanyID>(this->window_number))->inaugurated_year);
2412 break;
2414 case WID_C_DESC_COMPANY_VALUE:
2415 SetDParam(0, CalculateCompanyValue(Company::Get((CompanyID)this->window_number)));
2416 break;
2420 void OnResize() override
2422 NWidgetResizeBase *wid = this->GetWidget<NWidgetResizeBase>(WID_C_FACE_TITLE);
2423 SetDParam(0, this->owner);
2424 int y = GetStringHeight(STR_COMPANY_VIEW_PRESIDENT_MANAGER_TITLE, wid->current_x);
2425 if (wid->UpdateVerticalSize(y)) this->ReInit(0, 0);
2428 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
2430 switch (widget) {
2431 case WID_C_NEW_FACE: DoSelectCompanyManagerFace(this); break;
2433 case WID_C_COLOUR_SCHEME:
2434 ShowCompanyLiveryWindow((CompanyID)this->window_number, INVALID_GROUP);
2435 break;
2437 case WID_C_PRESIDENT_NAME:
2438 this->query_widget = WID_C_PRESIDENT_NAME;
2439 SetDParam(0, this->window_number);
2440 ShowQueryString(STR_PRESIDENT_NAME, STR_COMPANY_VIEW_PRESIDENT_S_NAME_QUERY_CAPTION, MAX_LENGTH_PRESIDENT_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
2441 break;
2443 case WID_C_COMPANY_NAME:
2444 this->query_widget = WID_C_COMPANY_NAME;
2445 SetDParam(0, this->window_number);
2446 ShowQueryString(STR_COMPANY_NAME, STR_COMPANY_VIEW_COMPANY_NAME_QUERY_CAPTION, MAX_LENGTH_COMPANY_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
2447 break;
2449 case WID_C_VIEW_HQ: {
2450 TileIndex tile = Company::Get((CompanyID)this->window_number)->location_of_HQ;
2451 if (_ctrl_pressed) {
2452 ShowExtraViewportWindow(tile);
2453 } else {
2454 ScrollMainWindowToTile(tile);
2456 break;
2459 case WID_C_BUILD_HQ:
2460 if ((uint8_t)this->window_number != _local_company) return;
2461 if (this->IsWidgetLowered(WID_C_BUILD_HQ)) {
2462 ResetObjectToPlace();
2463 this->RaiseButtons();
2464 break;
2466 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2467 SetTileSelectSize(2, 2);
2468 this->LowerWidget(WID_C_BUILD_HQ);
2469 this->SetWidgetDirty(WID_C_BUILD_HQ);
2470 break;
2472 case WID_C_RELOCATE_HQ:
2473 if (this->IsWidgetLowered(WID_C_RELOCATE_HQ)) {
2474 ResetObjectToPlace();
2475 this->RaiseButtons();
2476 break;
2478 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2479 SetTileSelectSize(2, 2);
2480 this->LowerWidget(WID_C_RELOCATE_HQ);
2481 this->SetWidgetDirty(WID_C_RELOCATE_HQ);
2482 break;
2484 case WID_C_VIEW_INFRASTRUCTURE:
2485 ShowCompanyInfrastructure((CompanyID)this->window_number);
2486 break;
2488 case WID_C_GIVE_MONEY:
2489 this->query_widget = WID_C_GIVE_MONEY;
2490 ShowQueryString(STR_EMPTY, STR_COMPANY_VIEW_GIVE_MONEY_QUERY_CAPTION, 30, this, CS_NUMERAL, QSF_NONE);
2491 break;
2493 case WID_C_HOSTILE_TAKEOVER:
2494 ShowBuyCompanyDialog((CompanyID)this->window_number, true);
2495 break;
2497 case WID_C_COMPANY_JOIN: {
2498 this->query_widget = WID_C_COMPANY_JOIN;
2499 CompanyID company = (CompanyID)this->window_number;
2500 if (_network_server) {
2501 NetworkServerDoMove(CLIENT_ID_SERVER, company);
2502 MarkWholeScreenDirty();
2503 } else {
2504 /* just send the join command */
2505 NetworkClientRequestMove(company);
2507 break;
2512 /** Redraw the window on a regular interval. */
2513 IntervalTimer<TimerWindow> redraw_interval = {std::chrono::seconds(3), [this](auto) {
2514 this->SetDirty();
2517 void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override
2519 if (Command<CMD_BUILD_OBJECT>::Post(STR_ERROR_CAN_T_BUILD_COMPANY_HEADQUARTERS, tile, OBJECT_HQ, 0) && !_shift_pressed) {
2520 ResetObjectToPlace();
2521 this->RaiseButtons();
2525 void OnPlaceObjectAbort() override
2527 this->RaiseButtons();
2530 void OnQueryTextFinished(std::optional<std::string> str) override
2532 if (!str.has_value()) return;
2534 switch (this->query_widget) {
2535 default: NOT_REACHED();
2537 case WID_C_GIVE_MONEY: {
2538 Money money = std::strtoull(str->c_str(), nullptr, 10) / GetCurrency().rate;
2539 Command<CMD_GIVE_MONEY>::Post(STR_ERROR_CAN_T_GIVE_MONEY, money, (CompanyID)this->window_number);
2540 break;
2543 case WID_C_PRESIDENT_NAME:
2544 Command<CMD_RENAME_PRESIDENT>::Post(STR_ERROR_CAN_T_CHANGE_PRESIDENT, *str);
2545 break;
2547 case WID_C_COMPANY_NAME:
2548 Command<CMD_RENAME_COMPANY>::Post(STR_ERROR_CAN_T_CHANGE_COMPANY_NAME, *str);
2549 break;
2553 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
2555 if (gui_scope && data == 1) {
2556 /* Manually call OnResize to adjust minimum height of president name widget. */
2557 OnResize();
2562 static WindowDesc _company_desc(
2563 WDP_AUTO, "company", 0, 0,
2564 WC_COMPANY, WC_NONE,
2566 _nested_company_widgets
2570 * Show the window with the overview of the company.
2571 * @param company The company to show the window for.
2573 void ShowCompany(CompanyID company)
2575 if (!Company::IsValidID(company)) return;
2577 AllocateWindowDescFront<CompanyWindow>(_company_desc, company);
2581 * Redraw all windows with company infrastructure counts.
2582 * @param company The company to redraw the windows of.
2584 void DirtyCompanyInfrastructureWindows(CompanyID company)
2586 SetWindowDirty(WC_COMPANY, company);
2587 SetWindowDirty(WC_COMPANY_INFRASTRUCTURE, company);
2590 struct BuyCompanyWindow : Window {
2591 BuyCompanyWindow(WindowDesc &desc, WindowNumber window_number, bool hostile_takeover) : Window(desc), hostile_takeover(hostile_takeover)
2593 this->InitNested(window_number);
2595 const Company *c = Company::Get((CompanyID)this->window_number);
2596 this->company_value = hostile_takeover ? CalculateHostileTakeoverValue(c) : c->bankrupt_value;
2599 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
2601 switch (widget) {
2602 case WID_BC_FACE:
2603 size = GetScaledSpriteSize(SPR_GRADIENT);
2604 break;
2606 case WID_BC_QUESTION:
2607 const Company *c = Company::Get((CompanyID)this->window_number);
2608 SetDParam(0, c->index);
2609 SetDParam(1, this->company_value);
2610 size.height = GetStringHeight(this->hostile_takeover ? STR_BUY_COMPANY_HOSTILE_TAKEOVER : STR_BUY_COMPANY_MESSAGE, size.width);
2611 break;
2615 void SetStringParameters(WidgetID widget) const override
2617 switch (widget) {
2618 case WID_BC_CAPTION:
2619 SetDParam(0, STR_COMPANY_NAME);
2620 SetDParam(1, Company::Get((CompanyID)this->window_number)->index);
2621 break;
2625 void DrawWidget(const Rect &r, WidgetID widget) const override
2627 switch (widget) {
2628 case WID_BC_FACE: {
2629 const Company *c = Company::Get((CompanyID)this->window_number);
2630 DrawCompanyManagerFace(c->face, c->colour, r);
2631 break;
2634 case WID_BC_QUESTION: {
2635 const Company *c = Company::Get((CompanyID)this->window_number);
2636 SetDParam(0, c->index);
2637 SetDParam(1, this->company_value);
2638 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->hostile_takeover ? STR_BUY_COMPANY_HOSTILE_TAKEOVER : STR_BUY_COMPANY_MESSAGE, TC_FROMSTRING, SA_CENTER);
2639 break;
2644 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
2646 switch (widget) {
2647 case WID_BC_NO:
2648 this->Close();
2649 break;
2651 case WID_BC_YES:
2652 Command<CMD_BUY_COMPANY>::Post(STR_ERROR_CAN_T_BUY_COMPANY, (CompanyID)this->window_number, this->hostile_takeover);
2653 break;
2658 * Check on a regular interval if the company value has changed.
2660 IntervalTimer<TimerWindow> rescale_interval = {std::chrono::seconds(3), [this](auto) {
2661 /* Value can't change when in bankruptcy. */
2662 if (!this->hostile_takeover) return;
2664 const Company *c = Company::Get((CompanyID)this->window_number);
2665 auto new_value = CalculateHostileTakeoverValue(c);
2666 if (new_value != this->company_value) {
2667 this->company_value = new_value;
2668 this->ReInit();
2672 private:
2673 bool hostile_takeover; ///< Whether the window is showing a hostile takeover.
2674 Money company_value; ///< The value of the company for which the user can buy it.
2677 static constexpr NWidgetPart _nested_buy_company_widgets[] = {
2678 NWidget(NWID_HORIZONTAL),
2679 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
2680 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_BC_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2681 EndContainer(),
2682 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
2683 NWidget(NWID_VERTICAL), SetPIP(0, WidgetDimensions::unscaled.vsep_wide, 0), SetPadding(WidgetDimensions::unscaled.modalpopup),
2684 NWidget(NWID_HORIZONTAL), SetPIP(0, WidgetDimensions::unscaled.hsep_wide, 0),
2685 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_FACE), SetFill(0, 1),
2686 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_QUESTION), SetMinimalSize(240, 0), SetFill(1, 1),
2687 EndContainer(),
2688 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(100, WidgetDimensions::unscaled.hsep_wide, 100),
2689 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_NO), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
2690 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_YES), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
2691 EndContainer(),
2692 EndContainer(),
2693 EndContainer(),
2696 static WindowDesc _buy_company_desc(
2697 WDP_AUTO, nullptr, 0, 0,
2698 WC_BUY_COMPANY, WC_NONE,
2699 WDF_CONSTRUCTION,
2700 _nested_buy_company_widgets
2704 * Show the query to buy another company.
2705 * @param company The company to buy.
2706 * @param hostile_takeover Whether this is a hostile takeover.
2708 void ShowBuyCompanyDialog(CompanyID company, bool hostile_takeover)
2710 auto window = BringWindowToFrontById(WC_BUY_COMPANY, company);
2711 if (window == nullptr) {
2712 new BuyCompanyWindow(_buy_company_desc, company, hostile_takeover);