Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / company_gui.cpp
blob423efcb01f5c2679b9e15654bbcd29159da613c7
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 "error.h"
12 #include "gui.h"
13 #include "window_gui.h"
14 #include "textbuf_gui.h"
15 #include "viewport_func.h"
16 #include "company_func.h"
17 #include "command_func.h"
18 #include "network/network.h"
19 #include "network/network_gui.h"
20 #include "network/network_func.h"
21 #include "newgrf.h"
22 #include "company_manager_face.h"
23 #include "strings_func.h"
24 #include "date_func.h"
25 #include "widgets/dropdown_type.h"
26 #include "tilehighlight_func.h"
27 #include "company_base.h"
28 #include "core/geometry_func.hpp"
29 #include "object_type.h"
30 #include "rail.h"
31 #include "road.h"
32 #include "engine_base.h"
33 #include "window_func.h"
34 #include "road_func.h"
35 #include "water.h"
36 #include "station_func.h"
37 #include "zoom_func.h"
38 #include "sortlist_type.h"
40 #include "widgets/company_widget.h"
42 #include "safeguards.h"
45 /** Company GUI constants. */
46 static const uint EXP_LINESPACE = 2; ///< Amount of vertical space for a horizontal (sub-)total line.
47 static const uint EXP_BLOCKSPACE = 10; ///< Amount of vertical space between two blocks of numbers.
49 static void DoSelectCompanyManagerFace(Window *parent);
50 static void ShowCompanyInfrastructure(CompanyID company);
52 /** Standard unsorted list of expenses. */
53 static ExpensesType _expenses_list_1[] = {
54 EXPENSES_CONSTRUCTION,
55 EXPENSES_NEW_VEHICLES,
56 EXPENSES_TRAIN_RUN,
57 EXPENSES_ROADVEH_RUN,
58 EXPENSES_AIRCRAFT_RUN,
59 EXPENSES_SHIP_RUN,
60 EXPENSES_PROPERTY,
61 EXPENSES_TRAIN_INC,
62 EXPENSES_ROADVEH_INC,
63 EXPENSES_AIRCRAFT_INC,
64 EXPENSES_SHIP_INC,
65 EXPENSES_LOAN_INT,
66 EXPENSES_OTHER,
69 /** Grouped list of expenses. */
70 static ExpensesType _expenses_list_2[] = {
71 EXPENSES_TRAIN_INC,
72 EXPENSES_ROADVEH_INC,
73 EXPENSES_AIRCRAFT_INC,
74 EXPENSES_SHIP_INC,
75 INVALID_EXPENSES,
76 EXPENSES_TRAIN_RUN,
77 EXPENSES_ROADVEH_RUN,
78 EXPENSES_AIRCRAFT_RUN,
79 EXPENSES_SHIP_RUN,
80 EXPENSES_PROPERTY,
81 EXPENSES_LOAN_INT,
82 INVALID_EXPENSES,
83 EXPENSES_CONSTRUCTION,
84 EXPENSES_NEW_VEHICLES,
85 EXPENSES_OTHER,
86 INVALID_EXPENSES,
89 /** Expense list container. */
90 struct ExpensesList {
91 const ExpensesType *et; ///< Expenses items.
92 const uint length; ///< Number of items in list.
93 const uint num_subtotals; ///< Number of sub-totals in the list.
95 ExpensesList(ExpensesType *et, int length, int num_subtotals) : et(et), length(length), num_subtotals(num_subtotals)
99 uint GetHeight() const
101 /* heading + line + texts of expenses + sub-totals + total line + total text */
102 return FONT_HEIGHT_NORMAL + EXP_LINESPACE + this->length * FONT_HEIGHT_NORMAL + num_subtotals * (EXP_BLOCKSPACE + EXP_LINESPACE) + EXP_LINESPACE + FONT_HEIGHT_NORMAL;
105 /** Compute width of the expenses categories in pixels. */
106 uint GetCategoriesWidth() const
108 uint width = 0;
109 bool invalid_expenses_measured = false; // Measure 'Total' width only once.
110 for (uint i = 0; i < this->length; i++) {
111 ExpensesType et = this->et[i];
112 if (et == INVALID_EXPENSES) {
113 if (!invalid_expenses_measured) {
114 width = max(width, GetStringBoundingBox(STR_FINANCES_TOTAL_CAPTION).width);
115 invalid_expenses_measured = true;
117 } else {
118 width = max(width, GetStringBoundingBox(STR_FINANCES_SECTION_CONSTRUCTION + et).width);
121 return width;
125 static const ExpensesList _expenses_list_types[] = {
126 ExpensesList(_expenses_list_1, lengthof(_expenses_list_1), 0),
127 ExpensesList(_expenses_list_2, lengthof(_expenses_list_2), 3),
131 * Draw the expenses categories.
132 * @param r Available space for drawing.
133 * @note The environment must provide padding at the left and right of \a r.
135 static void DrawCategories(const Rect &r)
137 int y = r.top;
139 DrawString(r.left, r.right, y, STR_FINANCES_EXPENDITURE_INCOME_TITLE, TC_FROMSTRING, SA_HOR_CENTER, true);
140 y += FONT_HEIGHT_NORMAL + EXP_LINESPACE;
142 int type = _settings_client.gui.expenses_layout;
143 for (uint i = 0; i < _expenses_list_types[type].length; i++) {
144 const ExpensesType et = _expenses_list_types[type].et[i];
145 if (et == INVALID_EXPENSES) {
146 y += EXP_LINESPACE;
147 DrawString(r.left, r.right, y, STR_FINANCES_TOTAL_CAPTION, TC_FROMSTRING, SA_RIGHT);
148 y += FONT_HEIGHT_NORMAL + EXP_BLOCKSPACE;
149 } else {
150 DrawString(r.left, r.right, y, STR_FINANCES_SECTION_CONSTRUCTION + et);
151 y += FONT_HEIGHT_NORMAL;
155 DrawString(r.left, r.right, y + EXP_LINESPACE, STR_FINANCES_TOTAL_CAPTION, TC_FROMSTRING, SA_RIGHT);
159 * Draw an amount of money.
160 * @param amount Amount of money to draw,
161 * @param left Left coordinate of the space to draw in.
162 * @param right Right coordinate of the space to draw in.
163 * @param top Top coordinate of the space to draw in.
165 static void DrawPrice(Money amount, int left, int right, int top)
167 StringID str = STR_FINANCES_NEGATIVE_INCOME;
168 if (amount < 0) {
169 amount = -amount;
170 str++;
172 SetDParam(0, amount);
173 DrawString(left, right, top, str, TC_FROMSTRING, SA_RIGHT);
177 * Draw a column with prices.
178 * @param r Available space for drawing.
179 * @param year Year being drawn.
180 * @param tbl Pointer to table of amounts for \a year.
181 * @note The environment must provide padding at the left and right of \a r.
183 static void DrawYearColumn(const Rect &r, int year, const Money (*tbl)[EXPENSES_END])
185 int y = r.top;
187 SetDParam(0, year);
188 DrawString(r.left, r.right, y, STR_FINANCES_YEAR, TC_FROMSTRING, SA_RIGHT, true);
189 y += FONT_HEIGHT_NORMAL + EXP_LINESPACE;
191 Money sum = 0;
192 Money subtotal = 0;
193 int type = _settings_client.gui.expenses_layout;
194 for (uint i = 0; i < _expenses_list_types[type].length; i++) {
195 const ExpensesType et = _expenses_list_types[type].et[i];
196 if (et == INVALID_EXPENSES) {
197 Money cost = subtotal;
198 subtotal = 0;
199 GfxFillRect(r.left, y, r.right, y, PC_BLACK);
200 y += EXP_LINESPACE;
201 DrawPrice(cost, r.left, r.right, y);
202 y += FONT_HEIGHT_NORMAL + EXP_BLOCKSPACE;
203 } else {
204 Money cost = (*tbl)[et];
205 subtotal += cost;
206 sum += cost;
207 if (cost != 0) DrawPrice(cost, r.left, r.right, y);
208 y += FONT_HEIGHT_NORMAL;
212 GfxFillRect(r.left, y, r.right, y, PC_BLACK);
213 y += EXP_LINESPACE;
214 DrawPrice(sum, r.left, r.right, y);
217 static const NWidgetPart _nested_company_finances_widgets[] = {
218 NWidget(NWID_HORIZONTAL),
219 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
220 NWidget(WWT_CAPTION, COLOUR_GREY, WID_CF_CAPTION), SetDataTip(STR_FINANCES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
221 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_CF_TOGGLE_SIZE), SetDataTip(SPR_LARGE_SMALL_WINDOW, STR_TOOLTIP_TOGGLE_LARGE_SMALL_WINDOW),
222 NWidget(WWT_SHADEBOX, COLOUR_GREY),
223 NWidget(WWT_STICKYBOX, COLOUR_GREY),
224 EndContainer(),
225 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_CF_SEL_PANEL),
226 NWidget(WWT_PANEL, COLOUR_GREY),
227 NWidget(NWID_HORIZONTAL), SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_FRAMERECT_LEFT), SetPIP(0, 9, 0),
228 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_CATEGORY), SetMinimalSize(120, 0), SetFill(0, 0),
229 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_PRICE1), SetMinimalSize(86, 0), SetFill(0, 0),
230 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_PRICE2), SetMinimalSize(86, 0), SetFill(0, 0),
231 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_EXPS_PRICE3), SetMinimalSize(86, 0), SetFill(0, 0),
232 EndContainer(),
233 EndContainer(),
234 EndContainer(),
235 NWidget(WWT_PANEL, COLOUR_GREY),
236 NWidget(NWID_HORIZONTAL), SetPadding(WD_FRAMERECT_TOP, WD_FRAMERECT_RIGHT, WD_FRAMERECT_BOTTOM, WD_FRAMERECT_LEFT),
237 NWidget(NWID_VERTICAL), // Vertical column with 'bank balance', 'loan'
238 NWidget(WWT_TEXT, COLOUR_GREY), SetDataTip(STR_FINANCES_BANK_BALANCE_TITLE, STR_NULL), SetFill(1, 0),
239 NWidget(WWT_TEXT, COLOUR_GREY), SetDataTip(STR_FINANCES_LOAN_TITLE, STR_NULL), SetFill(1, 0),
240 NWidget(NWID_SPACER), SetFill(0, 1),
241 EndContainer(),
242 NWidget(NWID_SPACER), SetFill(0, 0), SetMinimalSize(30, 0),
243 NWidget(NWID_VERTICAL), // Vertical column with bank balance amount, loan amount, and total.
244 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_BALANCE_VALUE), SetDataTip(STR_NULL, STR_NULL),
245 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_LOAN_VALUE), SetDataTip(STR_NULL, STR_NULL),
246 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_LOAN_LINE), SetMinimalSize(0, 2), SetFill(1, 0),
247 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_TOTAL_VALUE), SetDataTip(STR_NULL, STR_NULL),
248 EndContainer(),
249 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_CF_SEL_MAXLOAN),
250 NWidget(NWID_HORIZONTAL),
251 NWidget(NWID_SPACER), SetFill(0, 1), SetMinimalSize(25, 0),
252 NWidget(NWID_VERTICAL), // Max loan information
253 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CF_MAXLOAN_GAP), SetFill(0, 0),
254 NWidget(WWT_TEXT, COLOUR_GREY, WID_CF_MAXLOAN_VALUE), SetDataTip(STR_FINANCES_MAX_LOAN, STR_NULL),
255 NWidget(NWID_SPACER), SetFill(0, 1),
256 EndContainer(),
257 EndContainer(),
258 EndContainer(),
259 NWidget(NWID_SPACER), SetFill(1, 1),
260 EndContainer(),
261 EndContainer(),
262 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_CF_SEL_BUTTONS),
263 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
264 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CF_INCREASE_LOAN), SetFill(1, 0), SetDataTip(STR_FINANCES_BORROW_BUTTON, STR_FINANCES_BORROW_TOOLTIP),
265 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CF_REPAY_LOAN), SetFill(1, 0), SetDataTip(STR_FINANCES_REPAY_BUTTON, STR_FINANCES_REPAY_TOOLTIP),
266 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CF_INFRASTRUCTURE), SetFill(1, 0), SetDataTip(STR_FINANCES_INFRASTRUCTURE_BUTTON, STR_COMPANY_VIEW_INFRASTRUCTURE_TOOLTIP),
267 EndContainer(),
268 EndContainer(),
271 /** Window class displaying the company finances. */
272 struct CompanyFinancesWindow : Window {
273 static Money max_money; ///< The maximum amount of money a company has had this 'run'
274 bool small; ///< Window is toggled to 'small'.
276 CompanyFinancesWindow(WindowDesc *desc, CompanyID company) : Window(desc)
278 this->small = false;
279 this->CreateNestedTree();
280 this->SetupWidgets();
281 this->FinishInitNested(company);
283 this->owner = (Owner)this->window_number;
286 void SetStringParameters(int widget) const override
288 switch (widget) {
289 case WID_CF_CAPTION:
290 SetDParam(0, (CompanyID)this->window_number);
291 SetDParam(1, (CompanyID)this->window_number);
292 break;
294 case WID_CF_MAXLOAN_VALUE:
295 SetDParam(0, _economy.max_loan);
296 break;
298 case WID_CF_INCREASE_LOAN:
299 case WID_CF_REPAY_LOAN:
300 SetDParam(0, LOAN_INTERVAL);
301 break;
305 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
307 int type = _settings_client.gui.expenses_layout;
308 switch (widget) {
309 case WID_CF_EXPS_CATEGORY:
310 size->width = _expenses_list_types[type].GetCategoriesWidth();
311 size->height = _expenses_list_types[type].GetHeight();
312 break;
314 case WID_CF_EXPS_PRICE1:
315 case WID_CF_EXPS_PRICE2:
316 case WID_CF_EXPS_PRICE3:
317 size->height = _expenses_list_types[type].GetHeight();
318 FALLTHROUGH;
320 case WID_CF_BALANCE_VALUE:
321 case WID_CF_LOAN_VALUE:
322 case WID_CF_TOTAL_VALUE:
323 SetDParamMaxValue(0, CompanyFinancesWindow::max_money);
324 size->width = max(GetStringBoundingBox(STR_FINANCES_NEGATIVE_INCOME).width, GetStringBoundingBox(STR_FINANCES_POSITIVE_INCOME).width) + padding.width;
325 break;
327 case WID_CF_MAXLOAN_GAP:
328 size->height = FONT_HEIGHT_NORMAL;
329 break;
333 void DrawWidget(const Rect &r, int widget) const override
335 switch (widget) {
336 case WID_CF_EXPS_CATEGORY:
337 DrawCategories(r);
338 break;
340 case WID_CF_EXPS_PRICE1:
341 case WID_CF_EXPS_PRICE2:
342 case WID_CF_EXPS_PRICE3: {
343 const Company *c = Company::Get((CompanyID)this->window_number);
344 int age = min(_cur_year - c->inaugurated_year, 2);
345 int wid_offset = widget - WID_CF_EXPS_PRICE1;
346 if (wid_offset <= age) {
347 DrawYearColumn(r, _cur_year - (age - wid_offset), c->yearly_expenses + (age - wid_offset));
349 break;
352 case WID_CF_BALANCE_VALUE: {
353 const Company *c = Company::Get((CompanyID)this->window_number);
354 SetDParam(0, c->money);
355 DrawString(r.left, r.right, r.top, STR_FINANCES_TOTAL_CURRENCY, TC_FROMSTRING, SA_RIGHT);
356 break;
359 case WID_CF_LOAN_VALUE: {
360 const Company *c = Company::Get((CompanyID)this->window_number);
361 SetDParam(0, c->current_loan);
362 DrawString(r.left, r.right, r.top, STR_FINANCES_TOTAL_CURRENCY, TC_FROMSTRING, SA_RIGHT);
363 break;
366 case WID_CF_TOTAL_VALUE: {
367 const Company *c = Company::Get((CompanyID)this->window_number);
368 SetDParam(0, c->money - c->current_loan);
369 DrawString(r.left, r.right, r.top, STR_FINANCES_TOTAL_CURRENCY, TC_FROMSTRING, SA_RIGHT);
370 break;
373 case WID_CF_LOAN_LINE:
374 GfxFillRect(r.left, r.top, r.right, r.top, PC_BLACK);
375 break;
380 * Setup the widgets in the nested tree, such that the finances window is displayed properly.
381 * @note After setup, the window must be (re-)initialized.
383 void SetupWidgets()
385 int plane = this->small ? SZSP_NONE : 0;
386 this->GetWidget<NWidgetStacked>(WID_CF_SEL_PANEL)->SetDisplayedPlane(plane);
387 this->GetWidget<NWidgetStacked>(WID_CF_SEL_MAXLOAN)->SetDisplayedPlane(plane);
389 CompanyID company = (CompanyID)this->window_number;
390 plane = (company != _local_company) ? SZSP_NONE : 0;
391 this->GetWidget<NWidgetStacked>(WID_CF_SEL_BUTTONS)->SetDisplayedPlane(plane);
394 void OnPaint() override
396 if (!this->IsShaded()) {
397 if (!this->small) {
398 /* Check that the expenses panel height matches the height needed for the layout. */
399 int type = _settings_client.gui.expenses_layout;
400 if (_expenses_list_types[type].GetHeight() != this->GetWidget<NWidgetBase>(WID_CF_EXPS_CATEGORY)->current_y) {
401 this->SetupWidgets();
402 this->ReInit();
403 return;
407 /* Check that the loan buttons are shown only when the user owns the company. */
408 CompanyID company = (CompanyID)this->window_number;
409 int req_plane = (company != _local_company) ? SZSP_NONE : 0;
410 if (req_plane != this->GetWidget<NWidgetStacked>(WID_CF_SEL_BUTTONS)->shown_plane) {
411 this->SetupWidgets();
412 this->ReInit();
413 return;
416 const Company *c = Company::Get(company);
417 this->SetWidgetDisabledState(WID_CF_INCREASE_LOAN, c->current_loan == _economy.max_loan); // Borrow button only shows when there is any more money to loan.
418 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.
421 this->DrawWidgets();
424 void OnClick(Point pt, int widget, int click_count) override
426 switch (widget) {
427 case WID_CF_TOGGLE_SIZE: // toggle size
428 this->small = !this->small;
429 this->SetupWidgets();
430 if (this->IsShaded()) {
431 /* Finances window is not resizable, so size hints given during unshading have no effect
432 * on the changed appearance of the window. */
433 this->SetShaded(false);
434 } else {
435 this->ReInit();
437 break;
439 case WID_CF_INCREASE_LOAN: // increase loan
440 DoCommandP(0, 0, _ctrl_pressed, CMD_INCREASE_LOAN | CMD_MSG(STR_ERROR_CAN_T_BORROW_ANY_MORE_MONEY));
441 break;
443 case WID_CF_REPAY_LOAN: // repay loan
444 DoCommandP(0, 0, _ctrl_pressed, CMD_DECREASE_LOAN | CMD_MSG(STR_ERROR_CAN_T_REPAY_LOAN));
445 break;
447 case WID_CF_INFRASTRUCTURE: // show infrastructure details
448 ShowCompanyInfrastructure((CompanyID)this->window_number);
449 break;
453 void OnHundredthTick() override
455 const Company *c = Company::Get((CompanyID)this->window_number);
456 if (c->money > CompanyFinancesWindow::max_money) {
457 CompanyFinancesWindow::max_money = max(c->money * 2, CompanyFinancesWindow::max_money * 4);
458 this->SetupWidgets();
459 this->ReInit();
464 /** First conservative estimate of the maximum amount of money */
465 Money CompanyFinancesWindow::max_money = INT32_MAX;
467 static WindowDesc _company_finances_desc(
468 WDP_AUTO, "company_finances", 0, 0,
469 WC_FINANCES, WC_NONE,
471 _nested_company_finances_widgets, lengthof(_nested_company_finances_widgets)
475 * Open the finances window of a company.
476 * @param company Company to show finances of.
477 * @pre is company a valid company.
479 void ShowCompanyFinances(CompanyID company)
481 if (!Company::IsValidID(company)) return;
482 if (BringWindowToFrontById(WC_FINANCES, company)) return;
484 new CompanyFinancesWindow(&_company_finances_desc, company);
487 /* List of colours for the livery window */
488 static const StringID _colour_dropdown[] = {
489 STR_COLOUR_DARK_BLUE,
490 STR_COLOUR_PALE_GREEN,
491 STR_COLOUR_PINK,
492 STR_COLOUR_YELLOW,
493 STR_COLOUR_RED,
494 STR_COLOUR_LIGHT_BLUE,
495 STR_COLOUR_GREEN,
496 STR_COLOUR_DARK_GREEN,
497 STR_COLOUR_BLUE,
498 STR_COLOUR_CREAM,
499 STR_COLOUR_MAUVE,
500 STR_COLOUR_PURPLE,
501 STR_COLOUR_ORANGE,
502 STR_COLOUR_BROWN,
503 STR_COLOUR_GREY,
504 STR_COLOUR_WHITE,
507 /* Association of liveries to livery classes */
508 static const LiveryClass _livery_class[LS_END] = {
509 LC_OTHER,
510 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,
511 LC_ROAD, LC_ROAD,
512 LC_SHIP, LC_SHIP,
513 LC_AIRCRAFT, LC_AIRCRAFT, LC_AIRCRAFT,
514 LC_ROAD, LC_ROAD,
517 class DropDownListColourItem : public DropDownListItem {
518 public:
519 DropDownListColourItem(int result, bool masked) : DropDownListItem(result, masked) {}
521 StringID String() const
523 return this->result >= COLOUR_END ? STR_COLOUR_DEFAULT : _colour_dropdown[this->result];
526 uint Height(uint width) const override
528 return max(FONT_HEIGHT_NORMAL, ScaleGUITrad(12) + 2);
531 bool Selectable() const override
533 return true;
536 void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override
538 bool rtl = _current_text_dir == TD_RTL;
539 int height = bottom - top;
540 int icon_y_offset = height / 2;
541 int text_y_offset = (height - FONT_HEIGHT_NORMAL) / 2 + 1;
542 DrawSprite(SPR_VEH_BUS_SIDE_VIEW, PALETTE_RECOLOUR_START + (this->result % COLOUR_END),
543 rtl ? right - 2 - ScaleGUITrad(14) : left + ScaleGUITrad(14) + 2,
544 top + icon_y_offset);
545 DrawString(rtl ? left + 2 : left + ScaleGUITrad(28) + 4,
546 rtl ? right - ScaleGUITrad(28) - 4 : right - 2,
547 top + text_y_offset, this->String(), sel ? TC_WHITE : TC_BLACK);
551 static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
553 typedef GUIList<const Group*> GUIGroupList;
555 /** Company livery colour scheme window. */
556 struct SelectCompanyLiveryWindow : public Window {
557 private:
558 uint32 sel;
559 LiveryClass livery_class;
560 Dimension square;
561 uint rows;
562 uint line_height;
563 GUIGroupList groups;
564 std::vector<int> indents;
565 Scrollbar *vscroll;
567 void ShowColourDropDownMenu(uint32 widget)
569 uint32 used_colours = 0;
570 const Company *c;
571 const Livery *livery, *default_livery = nullptr;
572 bool primary = widget == WID_SCL_PRI_COL_DROPDOWN;
573 byte default_col;
575 /* Disallow other company colours for the primary colour */
576 if (this->livery_class < LC_GROUP_RAIL && HasBit(this->sel, LS_DEFAULT) && primary) {
577 for (const Company *c : Company::Iterate()) {
578 if (c->index != _local_company) SetBit(used_colours, c->colour);
582 c = Company::Get((CompanyID)this->window_number);
584 if (this->livery_class < LC_GROUP_RAIL) {
585 /* Get the first selected livery to use as the default dropdown item */
586 LiveryScheme scheme;
587 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
588 if (HasBit(this->sel, scheme)) break;
590 if (scheme == LS_END) scheme = LS_DEFAULT;
591 livery = &c->livery[scheme];
592 if (scheme != LS_DEFAULT) default_livery = &c->livery[LS_DEFAULT];
593 } else {
594 const Group *g = Group::Get(this->sel);
595 livery = &g->livery;
596 if (g->parent == INVALID_GROUP) {
597 default_livery = &c->livery[LS_DEFAULT];
598 } else {
599 const Group *pg = Group::Get(g->parent);
600 default_livery = &pg->livery;
604 DropDownList list;
605 if (default_livery != nullptr) {
606 /* Add COLOUR_END to put the colour out of range, but also allow us to show what the default is */
607 default_col = (primary ? default_livery->colour1 : default_livery->colour2) + COLOUR_END;
608 list.emplace_back(new DropDownListColourItem(default_col, false));
610 for (uint i = 0; i < lengthof(_colour_dropdown); i++) {
611 list.emplace_back(new DropDownListColourItem(i, HasBit(used_colours, i)));
614 byte sel = (default_livery == nullptr || HasBit(livery->in_use, primary ? 0 : 1)) ? (primary ? livery->colour1 : livery->colour2) : default_col;
615 ShowDropDownList(this, std::move(list), sel, widget);
618 static bool GroupNameSorter(const Group * const &a, const Group * const &b)
620 static const Group *last_group[2] = { nullptr, nullptr };
621 static char last_name[2][64] = { "", "" };
623 if (a != last_group[0]) {
624 last_group[0] = a;
625 SetDParam(0, a->index);
626 GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
629 if (b != last_group[1]) {
630 last_group[1] = b;
631 SetDParam(0, b->index);
632 GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
635 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
636 if (r == 0) return a->index < b->index;
637 return r < 0;
640 void AddChildren(GUIGroupList *source, GroupID parent, int indent)
642 for (const Group *g : *source) {
643 if (g->parent != parent) continue;
644 this->groups.push_back(g);
645 this->indents.push_back(indent);
646 AddChildren(source, g->index, indent + 1);
650 void BuildGroupList(CompanyID owner)
652 if (!this->groups.NeedRebuild()) return;
654 this->groups.clear();
655 this->indents.clear();
657 if (this->livery_class >= LC_GROUP_RAIL) {
658 GUIGroupList list;
659 VehicleType vtype = (VehicleType)(this->livery_class - LC_GROUP_RAIL);
661 for (const Group *g : Group::Iterate()) {
662 if (g->owner == owner && g->vehicle_type == vtype) {
663 list.push_back(g);
667 list.ForceResort();
668 list.Sort(&GroupNameSorter);
670 AddChildren(&list, INVALID_GROUP, 0);
673 this->groups.shrink_to_fit();
674 this->groups.RebuildDone();
677 void SetRows()
679 if (this->livery_class < LC_GROUP_RAIL) {
680 this->rows = 0;
681 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
682 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
683 this->rows++;
686 } else {
687 this->rows = (uint)this->groups.size();
690 this->vscroll->SetCount(this->rows);
693 public:
694 SelectCompanyLiveryWindow(WindowDesc *desc, CompanyID company, GroupID group) : Window(desc)
696 this->CreateNestedTree();
697 this->vscroll = this->GetScrollbar(WID_SCL_MATRIX_SCROLLBAR);
699 if (group == INVALID_GROUP) {
700 this->livery_class = LC_OTHER;
701 this->sel = 1;
702 this->LowerWidget(WID_SCL_CLASS_GENERAL);
703 this->BuildGroupList(company);
704 this->SetRows();
705 } else {
706 this->SetSelectedGroup(company, group);
709 this->FinishInitNested(company);
710 this->owner = company;
711 this->InvalidateData(1);
714 void SetSelectedGroup(CompanyID company, GroupID group)
716 this->RaiseWidget(this->livery_class + WID_SCL_CLASS_GENERAL);
717 const Group *g = Group::Get(group);
718 switch (g->vehicle_type) {
719 case VEH_TRAIN: this->livery_class = LC_GROUP_RAIL; break;
720 case VEH_ROAD: this->livery_class = LC_GROUP_ROAD; break;
721 case VEH_SHIP: this->livery_class = LC_GROUP_SHIP; break;
722 case VEH_AIRCRAFT: this->livery_class = LC_GROUP_AIRCRAFT; break;
723 default: NOT_REACHED();
725 this->sel = group;
726 this->LowerWidget(this->livery_class + WID_SCL_CLASS_GENERAL);
728 this->groups.ForceRebuild();
729 this->BuildGroupList(company);
730 this->SetRows();
732 /* Position scrollbar to selected group */
733 for (uint i = 0; i < this->rows; i++) {
734 if (this->groups[i]->index == sel) {
735 this->vscroll->SetPosition(Clamp(i - this->vscroll->GetCapacity() / 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
736 break;
741 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
743 switch (widget) {
744 case WID_SCL_SPACER_DROPDOWN: {
745 /* The matrix widget below needs enough room to print all the schemes. */
746 Dimension d = {0, 0};
747 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
748 d = maxdim(d, GetStringBoundingBox(STR_LIVERY_DEFAULT + scheme));
751 /* And group names */
752 for (const Group *g : Group::Iterate()) {
753 if (g->owner == (CompanyID)this->window_number) {
754 SetDParam(0, g->index);
755 d = maxdim(d, GetStringBoundingBox(STR_GROUP_NAME));
759 size->width = max(size->width, 5 + d.width + WD_FRAMERECT_RIGHT);
760 break;
763 case WID_SCL_MATRIX: {
764 /* 11 items in the default rail class */
765 this->square = GetSpriteSize(SPR_SQUARE);
766 this->line_height = max(this->square.height, (uint)FONT_HEIGHT_NORMAL) + 4;
768 size->height = 11 * this->line_height;
769 resize->width = 1;
770 resize->height = this->line_height;
771 break;
774 case WID_SCL_SEC_COL_DROPDOWN:
775 if (!_loaded_newgrf_features.has_2CC) {
776 size->width = 0;
777 break;
779 FALLTHROUGH;
781 case WID_SCL_PRI_COL_DROPDOWN: {
782 this->square = GetSpriteSize(SPR_SQUARE);
783 int padding = this->square.width + NWidgetScrollbar::GetVerticalDimension().width + 10;
784 for (const StringID *id = _colour_dropdown; id != endof(_colour_dropdown); id++) {
785 size->width = max(size->width, GetStringBoundingBox(*id).width + padding);
787 size->width = max(size->width, GetStringBoundingBox(STR_COLOUR_DEFAULT).width + padding);
788 break;
793 void OnPaint() override
795 bool local = (CompanyID)this->window_number == _local_company;
797 /* Disable dropdown controls if no scheme is selected */
798 bool disabled = this->livery_class < LC_GROUP_RAIL ? (this->sel == 0) : (this->sel == INVALID_GROUP);
799 this->SetWidgetDisabledState(WID_SCL_PRI_COL_DROPDOWN, !local || disabled);
800 this->SetWidgetDisabledState(WID_SCL_SEC_COL_DROPDOWN, !local || disabled);
802 this->BuildGroupList((CompanyID)this->window_number);
804 this->DrawWidgets();
807 void SetStringParameters(int widget) const override
809 switch (widget) {
810 case WID_SCL_CAPTION:
811 SetDParam(0, (CompanyID)this->window_number);
812 break;
814 case WID_SCL_PRI_COL_DROPDOWN:
815 case WID_SCL_SEC_COL_DROPDOWN: {
816 const Company *c = Company::Get((CompanyID)this->window_number);
817 bool primary = widget == WID_SCL_PRI_COL_DROPDOWN;
818 StringID colour = STR_COLOUR_DEFAULT;
820 if (this->livery_class < LC_GROUP_RAIL) {
821 if (this->sel != 0) {
822 LiveryScheme scheme = LS_DEFAULT;
823 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
824 if (HasBit(this->sel, scheme)) break;
826 if (scheme == LS_END) scheme = LS_DEFAULT;
827 const Livery *livery = &c->livery[scheme];
828 if (scheme == LS_DEFAULT || HasBit(livery->in_use, primary ? 0 : 1)) {
829 colour = STR_COLOUR_DARK_BLUE + (primary ? livery->colour1 : livery->colour2);
832 } else {
833 if (this->sel != INVALID_GROUP) {
834 const Group *g = Group::Get(this->sel);
835 const Livery *livery = &g->livery;
836 if (HasBit(livery->in_use, primary ? 0 : 1)) {
837 colour = STR_COLOUR_DARK_BLUE + (primary ? livery->colour1 : livery->colour2);
841 SetDParam(0, colour);
842 break;
847 void DrawWidget(const Rect &r, int widget) const override
849 if (widget != WID_SCL_MATRIX) return;
851 bool rtl = _current_text_dir == TD_RTL;
853 /* Horizontal coordinates of scheme name column. */
854 const NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SCL_SPACER_DROPDOWN);
855 int sch_left = nwi->pos_x;
856 int sch_right = sch_left + nwi->current_x - 1;
857 /* Horizontal coordinates of first dropdown. */
858 nwi = this->GetWidget<NWidgetBase>(WID_SCL_PRI_COL_DROPDOWN);
859 int pri_left = nwi->pos_x;
860 int pri_right = pri_left + nwi->current_x - 1;
861 /* Horizontal coordinates of second dropdown. */
862 nwi = this->GetWidget<NWidgetBase>(WID_SCL_SEC_COL_DROPDOWN);
863 int sec_left = nwi->pos_x;
864 int sec_right = sec_left + nwi->current_x - 1;
866 int text_left = (rtl ? (uint)WD_FRAMERECT_LEFT : (this->square.width + 5));
867 int text_right = (rtl ? (this->square.width + 5) : (uint)WD_FRAMERECT_RIGHT);
869 int square_offs = (this->line_height - this->square.height) / 2 + 1;
870 int text_offs = (this->line_height - FONT_HEIGHT_NORMAL) / 2 + 1;
872 int y = r.top;
874 /* Helper function to draw livery info. */
875 auto draw_livery = [&](StringID str, const Livery &liv, bool sel, bool def, int indent) {
876 /* Livery Label. */
877 DrawString(sch_left + WD_FRAMERECT_LEFT + (rtl ? 0 : indent), sch_right - WD_FRAMERECT_RIGHT - (rtl ? indent : 0), y + text_offs, str, sel ? TC_WHITE : TC_BLACK);
879 /* Text below the first dropdown. */
880 DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOUR(liv.colour1), (rtl ? pri_right - (this->square.width + 5) + WD_FRAMERECT_RIGHT : pri_left) + WD_FRAMERECT_LEFT, y + square_offs);
881 DrawString(pri_left + text_left, pri_right - text_right, y + text_offs, (def || HasBit(liv.in_use, 0)) ? STR_COLOUR_DARK_BLUE + liv.colour1 : STR_COLOUR_DEFAULT, sel ? TC_WHITE : TC_GOLD);
883 /* Text below the second dropdown. */
884 if (sec_right > sec_left) { // Second dropdown has non-zero size.
885 DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOUR(liv.colour2), (rtl ? sec_right - (this->square.width + 5) + WD_FRAMERECT_RIGHT : sec_left) + WD_FRAMERECT_LEFT, y + square_offs);
886 DrawString(sec_left + text_left, sec_right - text_right, y + text_offs, (def || HasBit(liv.in_use, 1)) ? STR_COLOUR_DARK_BLUE + liv.colour2 : STR_COLOUR_DEFAULT, sel ? TC_WHITE : TC_GOLD);
889 y += this->line_height;
892 if (livery_class < LC_GROUP_RAIL) {
893 int pos = this->vscroll->GetPosition();
894 const Company *c = Company::Get((CompanyID)this->window_number);
895 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
896 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
897 if (pos-- > 0) continue;
898 draw_livery(STR_LIVERY_DEFAULT + scheme, c->livery[scheme], HasBit(this->sel, scheme), scheme == LS_DEFAULT, 0);
901 } else {
902 uint max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), (uint)this->groups.size());
903 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
904 const Group *g = this->groups[i];
905 SetDParam(0, g->index);
906 draw_livery(STR_GROUP_NAME, g->livery, this->sel == g->index, false, this->indents[i] * LEVEL_WIDTH);
911 void OnClick(Point pt, int widget, int click_count) override
913 switch (widget) {
914 /* Livery Class buttons */
915 case WID_SCL_CLASS_GENERAL:
916 case WID_SCL_CLASS_RAIL:
917 case WID_SCL_CLASS_ROAD:
918 case WID_SCL_CLASS_SHIP:
919 case WID_SCL_CLASS_AIRCRAFT:
920 case WID_SCL_GROUPS_RAIL:
921 case WID_SCL_GROUPS_ROAD:
922 case WID_SCL_GROUPS_SHIP:
923 case WID_SCL_GROUPS_AIRCRAFT:
924 this->RaiseWidget(this->livery_class + WID_SCL_CLASS_GENERAL);
925 this->livery_class = (LiveryClass)(widget - WID_SCL_CLASS_GENERAL);
926 this->LowerWidget(this->livery_class + WID_SCL_CLASS_GENERAL);
928 /* Select the first item in the list */
929 if (this->livery_class < LC_GROUP_RAIL) {
930 this->sel = 0;
931 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
932 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
933 this->sel = 1 << scheme;
934 break;
937 } else {
938 this->sel = INVALID_GROUP;
939 this->groups.ForceRebuild();
940 this->BuildGroupList((CompanyID)this->window_number);
942 if (this->groups.size() > 0) {
943 this->sel = this->groups[0]->index;
947 this->SetRows();
948 this->SetDirty();
949 break;
951 case WID_SCL_PRI_COL_DROPDOWN: // First colour dropdown
952 ShowColourDropDownMenu(WID_SCL_PRI_COL_DROPDOWN);
953 break;
955 case WID_SCL_SEC_COL_DROPDOWN: // Second colour dropdown
956 ShowColourDropDownMenu(WID_SCL_SEC_COL_DROPDOWN);
957 break;
959 case WID_SCL_MATRIX: {
960 uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SCL_MATRIX, 0, this->line_height);
961 if (row >= this->rows) return;
963 if (this->livery_class < LC_GROUP_RAIL) {
964 LiveryScheme j = (LiveryScheme)row;
966 for (LiveryScheme scheme = LS_BEGIN; scheme <= j && scheme < LS_END; scheme++) {
967 if (_livery_class[scheme] != this->livery_class || !HasBit(_loaded_newgrf_features.used_liveries, scheme)) j++;
969 assert(j < LS_END);
971 if (_ctrl_pressed) {
972 ToggleBit(this->sel, j);
973 } else {
974 this->sel = 1 << j;
976 } else {
977 this->sel = this->groups[row]->index;
979 this->SetDirty();
980 break;
985 void OnResize() override
987 this->vscroll->SetCapacityFromWidget(this, WID_SCL_MATRIX);
990 void OnDropdownSelect(int widget, int index) override
992 bool local = (CompanyID)this->window_number == _local_company;
993 if (!local) return;
995 if (index >= COLOUR_END) index = INVALID_COLOUR;
997 if (this->livery_class < LC_GROUP_RAIL) {
998 /* Set company colour livery */
999 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
1000 /* Changed colour for the selected scheme, or all visible schemes if CTRL is pressed. */
1001 if (HasBit(this->sel, scheme) || (_ctrl_pressed && _livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme))) {
1002 DoCommandP(0, scheme | (widget == WID_SCL_PRI_COL_DROPDOWN ? 0 : 256), index, CMD_SET_COMPANY_COLOUR);
1005 } else {
1006 /* Setting group livery */
1007 DoCommandP(0, this->sel, (widget == WID_SCL_PRI_COL_DROPDOWN ? 0 : 256) | (index << 16), CMD_SET_GROUP_LIVERY);
1012 * Some data on this window has become invalid.
1013 * @param data Information about the changed data.
1014 * @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.
1016 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1018 if (!gui_scope) return;
1020 if (data != -1) {
1021 /* data contains a VehicleType, rebuild list if it displayed */
1022 if (this->livery_class == data + LC_GROUP_RAIL) {
1023 this->groups.ForceRebuild();
1024 this->BuildGroupList((CompanyID)this->window_number);
1025 this->SetRows();
1027 if (!Group::IsValidID(this->sel)) {
1028 this->sel = INVALID_GROUP;
1029 if (this->groups.size() > 0) this->sel = this->groups[0]->index;
1032 this->SetDirty();
1034 return;
1037 this->SetWidgetsDisabledState(true, WID_SCL_CLASS_RAIL, WID_SCL_CLASS_ROAD, WID_SCL_CLASS_SHIP, WID_SCL_CLASS_AIRCRAFT, WIDGET_LIST_END);
1039 bool current_class_valid = this->livery_class == LC_OTHER || this->livery_class >= LC_GROUP_RAIL;
1040 if (_settings_client.gui.liveries == LIT_ALL || (_settings_client.gui.liveries == LIT_COMPANY && this->window_number == _local_company)) {
1041 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
1042 if (HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
1043 if (_livery_class[scheme] == this->livery_class) current_class_valid = true;
1044 this->EnableWidget(WID_SCL_CLASS_GENERAL + _livery_class[scheme]);
1045 } else if (this->livery_class < LC_GROUP_RAIL) {
1046 ClrBit(this->sel, scheme);
1051 if (!current_class_valid) {
1052 Point pt = {0, 0};
1053 this->OnClick(pt, WID_SCL_CLASS_GENERAL, 1);
1058 static const NWidgetPart _nested_select_company_livery_widgets [] = {
1059 NWidget(NWID_HORIZONTAL),
1060 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1061 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCL_CAPTION), SetDataTip(STR_LIVERY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1062 EndContainer(),
1063 NWidget(NWID_HORIZONTAL),
1064 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_GENERAL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_LIVERY_GENERAL_TOOLTIP),
1065 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_RAIL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_LIVERY_TRAIN_TOOLTIP),
1066 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_ROAD), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRUCKLIST, STR_LIVERY_ROAD_VEHICLE_TOOLTIP),
1067 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_SHIP), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SHIPLIST, STR_LIVERY_SHIP_TOOLTIP),
1068 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_AIRCRAFT), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_AIRPLANESLIST, STR_LIVERY_AIRCRAFT_TOOLTIP),
1069 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_RAIL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_TRAIN, STR_LIVERY_TRAIN_TOOLTIP),
1070 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_ROAD), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_ROADVEH, STR_LIVERY_ROAD_VEHICLE_TOOLTIP),
1071 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_SHIP), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_SHIP, STR_LIVERY_SHIP_TOOLTIP),
1072 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_GROUPS_AIRCRAFT), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_GROUP_LIVERY_AIRCRAFT, STR_LIVERY_AIRCRAFT_TOOLTIP),
1073 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(90, 22), SetFill(1, 1), EndContainer(),
1074 EndContainer(),
1075 NWidget(NWID_HORIZONTAL),
1076 NWidget(WWT_PANEL, COLOUR_GREY, WID_SCL_SPACER_DROPDOWN), SetMinimalSize(150, 12), SetFill(1, 1), EndContainer(),
1077 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SCL_PRI_COL_DROPDOWN), SetMinimalSize(125, 12), SetFill(0, 1), SetDataTip(STR_BLACK_STRING, STR_LIVERY_PRIMARY_TOOLTIP),
1078 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SCL_SEC_COL_DROPDOWN), SetMinimalSize(125, 12), SetFill(0, 1),
1079 SetDataTip(STR_BLACK_STRING, STR_LIVERY_SECONDARY_TOOLTIP),
1080 EndContainer(),
1081 NWidget(NWID_HORIZONTAL),
1082 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),
1083 NWidget(NWID_VERTICAL),
1084 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SCL_MATRIX_SCROLLBAR),
1085 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1086 EndContainer(),
1087 EndContainer(),
1090 static WindowDesc _select_company_livery_desc(
1091 WDP_AUTO, "company_livery", 0, 0,
1092 WC_COMPANY_COLOUR, WC_NONE,
1094 _nested_select_company_livery_widgets, lengthof(_nested_select_company_livery_widgets)
1097 void ShowCompanyLiveryWindow(CompanyID company, GroupID group)
1099 SelectCompanyLiveryWindow *w = (SelectCompanyLiveryWindow *)BringWindowToFrontById(WC_COMPANY_COLOUR, company);
1100 if (w == nullptr) {
1101 new SelectCompanyLiveryWindow(&_select_company_livery_desc, company, group);
1102 } else if (group != INVALID_GROUP) {
1103 w->SetSelectedGroup(company, group);
1108 * Draws the face of a company manager's face.
1109 * @param cmf the company manager's face
1110 * @param colour the (background) colour of the gradient
1111 * @param x x-position to draw the face
1112 * @param y y-position to draw the face
1114 void DrawCompanyManagerFace(CompanyManagerFace cmf, int colour, int x, int y)
1116 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
1118 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
1119 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
1120 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
1121 PaletteID pal;
1123 /* Modify eye colour palette only if 2 or more valid values exist */
1124 if (_cmf_info[CMFV_EYE_COLOUR].valid_values[ge] < 2) {
1125 pal = PAL_NONE;
1126 } else {
1127 switch (GetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge)) {
1128 default: NOT_REACHED();
1129 case 0: pal = PALETTE_TO_BROWN; break;
1130 case 1: pal = PALETTE_TO_BLUE; break;
1131 case 2: pal = PALETTE_TO_GREEN; break;
1135 /* Draw the gradient (background) */
1136 DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOUR(colour), x, y);
1138 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
1139 switch (cmfv) {
1140 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
1141 case CMFV_LIPS:
1142 case CMFV_NOSE: if (has_moustache) continue; break;
1143 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
1144 case CMFV_GLASSES: if (!has_glasses) continue; break;
1145 default: break;
1147 DrawSprite(GetCompanyManagerFaceSprite(cmf, cmfv, ge), (cmfv == CMFV_EYEBROWS) ? pal : PAL_NONE, x, y);
1151 /** Nested widget description for the company manager face selection dialog */
1152 static const NWidgetPart _nested_select_company_manager_face_widgets[] = {
1153 NWidget(NWID_HORIZONTAL),
1154 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1155 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCMF_CAPTION), SetDataTip(STR_FACE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1156 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCMF_TOGGLE_LARGE_SMALL), SetDataTip(SPR_LARGE_SMALL_WINDOW, STR_FACE_ADVANCED_TOOLTIP),
1157 EndContainer(),
1158 NWidget(WWT_PANEL, COLOUR_GREY, WID_SCMF_SELECT_FACE),
1159 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1160 NWidget(NWID_HORIZONTAL), SetPIP(2, 2, 2),
1161 NWidget(NWID_VERTICAL),
1162 NWidget(NWID_HORIZONTAL),
1163 NWidget(NWID_SPACER), SetFill(1, 0),
1164 NWidget(WWT_EMPTY, COLOUR_GREY, WID_SCMF_FACE), SetMinimalSize(92, 119),
1165 NWidget(NWID_SPACER), SetFill(1, 0),
1166 EndContainer(),
1167 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1168 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_RANDOM_NEW_FACE), SetFill(1, 0), SetDataTip(STR_FACE_NEW_FACE_BUTTON, STR_FACE_NEW_FACE_TOOLTIP),
1169 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_LOADSAVE), // Load/number/save buttons under the portrait in the advanced view.
1170 NWidget(NWID_VERTICAL),
1171 NWidget(NWID_SPACER), SetMinimalSize(0, 5), SetFill(0, 1),
1172 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_LOAD), SetFill(1, 0), SetDataTip(STR_FACE_LOAD, STR_FACE_LOAD_TOOLTIP),
1173 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_FACECODE), SetFill(1, 0), SetDataTip(STR_FACE_FACECODE, STR_FACE_FACECODE_TOOLTIP),
1174 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_SAVE), SetFill(1, 0), SetDataTip(STR_FACE_SAVE, STR_FACE_SAVE_TOOLTIP),
1175 NWidget(NWID_SPACER), SetMinimalSize(0, 5), SetFill(0, 1),
1176 EndContainer(),
1177 EndContainer(),
1178 EndContainer(),
1179 NWidget(NWID_VERTICAL),
1180 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON), SetFill(1, 0), SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP),
1181 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1182 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_MALEFEMALE), // Simple male/female face setting.
1183 NWidget(NWID_VERTICAL),
1184 NWidget(NWID_SPACER), SetFill(0, 1),
1185 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_MALE), SetFill(1, 0), SetDataTip(STR_FACE_MALE_BUTTON, STR_FACE_MALE_TOOLTIP),
1186 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_FEMALE), SetFill(1, 0), SetDataTip(STR_FACE_FEMALE_BUTTON, STR_FACE_FEMALE_TOOLTIP),
1187 NWidget(NWID_SPACER), SetFill(0, 1),
1188 EndContainer(),
1189 EndContainer(),
1190 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_PARTS), // Advanced face parts setting.
1191 NWidget(NWID_VERTICAL),
1192 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1193 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1194 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_MALE2), SetFill(1, 0), SetDataTip(STR_FACE_MALE_BUTTON, STR_FACE_MALE_TOOLTIP),
1195 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_FEMALE2), SetFill(1, 0), SetDataTip(STR_FACE_FEMALE_BUTTON, STR_FACE_FEMALE_TOOLTIP),
1196 EndContainer(),
1197 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1198 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1199 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_ETHNICITY_EUR), SetFill(1, 0), SetDataTip(STR_FACE_EUROPEAN, STR_FACE_SELECT_EUROPEAN),
1200 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_ETHNICITY_AFR), SetFill(1, 0), SetDataTip(STR_FACE_AFRICAN, STR_FACE_SELECT_AFRICAN),
1201 EndContainer(),
1202 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
1203 NWidget(NWID_HORIZONTAL),
1204 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT), SetFill(1, 0),
1205 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAS_MOUSTACHE_EARRING), SetDataTip(STR_EMPTY, STR_FACE_MOUSTACHE_EARRING_TOOLTIP),
1206 EndContainer(),
1207 NWidget(NWID_HORIZONTAL),
1208 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_HAS_GLASSES_TEXT), SetFill(1, 0),
1209 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAS_GLASSES), SetDataTip(STR_EMPTY, STR_FACE_GLASSES_TOOLTIP),
1210 EndContainer(),
1211 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
1212 NWidget(NWID_HORIZONTAL),
1213 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_HAIR_TEXT), SetFill(1, 0),
1214 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_HAIR_L), SetDataTip(AWV_DECREASE, STR_FACE_HAIR_TOOLTIP),
1215 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAIR), SetDataTip(STR_EMPTY, STR_FACE_HAIR_TOOLTIP),
1216 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_HAIR_R), SetDataTip(AWV_INCREASE, STR_FACE_HAIR_TOOLTIP),
1217 EndContainer(),
1218 NWidget(NWID_HORIZONTAL),
1219 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_EYEBROWS_TEXT), SetFill(1, 0),
1220 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYEBROWS_L), SetDataTip(AWV_DECREASE, STR_FACE_EYEBROWS_TOOLTIP),
1221 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_EYEBROWS), SetDataTip(STR_EMPTY, STR_FACE_EYEBROWS_TOOLTIP),
1222 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYEBROWS_R), SetDataTip(AWV_INCREASE, STR_FACE_EYEBROWS_TOOLTIP),
1223 EndContainer(),
1224 NWidget(NWID_HORIZONTAL),
1225 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_EYECOLOUR_TEXT), SetFill(1, 0),
1226 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR_L), SetDataTip(AWV_DECREASE, STR_FACE_EYECOLOUR_TOOLTIP),
1227 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR), SetDataTip(STR_EMPTY, STR_FACE_EYECOLOUR_TOOLTIP),
1228 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR_R), SetDataTip(AWV_INCREASE, STR_FACE_EYECOLOUR_TOOLTIP),
1229 EndContainer(),
1230 NWidget(NWID_HORIZONTAL),
1231 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_GLASSES_TEXT), SetFill(1, 0),
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_EMPTY, STR_FACE_GLASSES_TOOLTIP_2),
1234 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_GLASSES_R), SetDataTip(AWV_INCREASE, STR_FACE_GLASSES_TOOLTIP_2),
1235 EndContainer(),
1236 NWidget(NWID_HORIZONTAL),
1237 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_NOSE_TEXT), SetFill(1, 0),
1238 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_NOSE_L), SetDataTip(AWV_DECREASE, STR_FACE_NOSE_TOOLTIP),
1239 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_NOSE), SetDataTip(STR_EMPTY, STR_FACE_NOSE_TOOLTIP),
1240 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_NOSE_R), SetDataTip(AWV_INCREASE, STR_FACE_NOSE_TOOLTIP),
1241 EndContainer(),
1242 NWidget(NWID_HORIZONTAL),
1243 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_LIPS_MOUSTACHE_TEXT), SetFill(1, 0),
1244 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE_L), SetDataTip(AWV_DECREASE, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1245 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE), SetDataTip(STR_EMPTY, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1246 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE_R), SetDataTip(AWV_INCREASE, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1247 EndContainer(),
1248 NWidget(NWID_HORIZONTAL),
1249 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_CHIN_TEXT), SetFill(1, 0),
1250 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_CHIN_L), SetDataTip(AWV_DECREASE, STR_FACE_CHIN_TOOLTIP),
1251 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_CHIN), SetDataTip(STR_EMPTY, STR_FACE_CHIN_TOOLTIP),
1252 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_CHIN_R), SetDataTip(AWV_INCREASE, STR_FACE_CHIN_TOOLTIP),
1253 EndContainer(),
1254 NWidget(NWID_HORIZONTAL),
1255 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_JACKET_TEXT), SetFill(1, 0),
1256 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_JACKET_L), SetDataTip(AWV_DECREASE, STR_FACE_JACKET_TOOLTIP),
1257 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_JACKET), SetDataTip(STR_EMPTY, STR_FACE_JACKET_TOOLTIP),
1258 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_JACKET_R), SetDataTip(AWV_INCREASE, STR_FACE_JACKET_TOOLTIP),
1259 EndContainer(),
1260 NWidget(NWID_HORIZONTAL),
1261 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_COLLAR_TEXT), SetFill(1, 0),
1262 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_COLLAR_L), SetDataTip(AWV_DECREASE, STR_FACE_COLLAR_TOOLTIP),
1263 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_COLLAR), SetDataTip(STR_EMPTY, STR_FACE_COLLAR_TOOLTIP),
1264 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_COLLAR_R), SetDataTip(AWV_INCREASE, STR_FACE_COLLAR_TOOLTIP),
1265 EndContainer(),
1266 NWidget(NWID_HORIZONTAL),
1267 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_TIE_EARRING_TEXT), SetFill(1, 0),
1268 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING_L), SetDataTip(AWV_DECREASE, STR_FACE_TIE_EARRING_TOOLTIP),
1269 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING), SetDataTip(STR_EMPTY, STR_FACE_TIE_EARRING_TOOLTIP),
1270 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING_R), SetDataTip(AWV_INCREASE, STR_FACE_TIE_EARRING_TOOLTIP),
1271 EndContainer(),
1272 NWidget(NWID_SPACER), SetFill(0, 1),
1273 EndContainer(),
1274 EndContainer(),
1275 EndContainer(),
1276 EndContainer(),
1277 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1278 EndContainer(),
1279 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1280 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_FACE_CANCEL_TOOLTIP),
1281 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_ACCEPT), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_FACE_OK_TOOLTIP),
1282 EndContainer(),
1285 /** Management class for customizing the face of the company manager. */
1286 class SelectCompanyManagerFaceWindow : public Window
1288 CompanyManagerFace face; ///< company manager face bits
1289 bool advanced; ///< advanced company manager face selection window
1291 GenderEthnicity ge; ///< Gender and ethnicity.
1292 bool is_female; ///< Female face.
1293 bool is_moust_male; ///< Male face with a moustache.
1295 Dimension yesno_dim; ///< Dimension of a yes/no button of a part in the advanced face window.
1296 Dimension number_dim; ///< Dimension of a number widget of a part in the advanced face window.
1298 static const StringID PART_TEXTS_IS_FEMALE[]; ///< Strings depending on #is_female, used to describe parts (2 entries for a part).
1299 static const StringID PART_TEXTS[]; ///< Fixed strings to describe parts of the face.
1302 * Draw dynamic a label to the left of the button and a value in the button
1304 * @param widget_index index of this widget in the window
1305 * @param val the value which will be draw
1306 * @param is_bool_widget is it a bool button
1308 void DrawFaceStringLabel(byte widget_index, uint8 val, bool is_bool_widget) const
1310 StringID str;
1311 const NWidgetCore *nwi_widget = this->GetWidget<NWidgetCore>(widget_index);
1312 if (!nwi_widget->IsDisabled()) {
1313 if (is_bool_widget) {
1314 /* if it a bool button write yes or no */
1315 str = (val != 0) ? STR_FACE_YES : STR_FACE_NO;
1316 } else {
1317 /* else write the value + 1 */
1318 SetDParam(0, val + 1);
1319 str = STR_JUST_INT;
1322 /* Draw the value/bool in white (0xC). If the button clicked adds 1px to x and y text coordinates (IsWindowWidgetLowered()). */
1323 DrawString(nwi_widget->pos_x + nwi_widget->IsLowered(), nwi_widget->pos_x + nwi_widget->current_x - 1 - nwi_widget->IsLowered(),
1324 nwi_widget->pos_y + 1 + nwi_widget->IsLowered(), str, TC_WHITE, SA_HOR_CENTER);
1328 void UpdateData()
1330 this->ge = (GenderEthnicity)GB(this->face, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // get the gender and ethnicity
1331 this->is_female = HasBit(this->ge, GENDER_FEMALE); // get the gender: 0 == male and 1 == female
1332 this->is_moust_male = !is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge) != 0; // is a male face with moustache
1335 public:
1336 SelectCompanyManagerFaceWindow(WindowDesc *desc, Window *parent) : Window(desc)
1338 this->advanced = false;
1339 this->CreateNestedTree();
1340 this->SelectDisplayPlanes(this->advanced);
1341 this->FinishInitNested(parent->window_number);
1342 this->parent = parent;
1343 this->owner = (Owner)this->window_number;
1344 this->face = Company::Get((CompanyID)this->window_number)->face;
1346 this->UpdateData();
1350 * 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.
1351 * @param advanced Display advanced face management window.
1353 void SelectDisplayPlanes(bool advanced)
1355 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_LOADSAVE)->SetDisplayedPlane(advanced ? 0 : SZSP_NONE);
1356 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_PARTS)->SetDisplayedPlane(advanced ? 0 : SZSP_NONE);
1357 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_MALEFEMALE)->SetDisplayedPlane(advanced ? SZSP_NONE : 0);
1358 this->GetWidget<NWidgetCore>(WID_SCMF_RANDOM_NEW_FACE)->widget_data = advanced ? STR_FACE_RANDOM : STR_FACE_NEW_FACE_BUTTON;
1360 NWidgetCore *wi = this->GetWidget<NWidgetCore>(WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON);
1361 if (advanced) {
1362 wi->SetDataTip(STR_FACE_SIMPLE, STR_FACE_SIMPLE_TOOLTIP);
1363 } else {
1364 wi->SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP);
1368 void OnInit() override
1370 /* Size of the boolean yes/no button. */
1371 Dimension yesno_dim = maxdim(GetStringBoundingBox(STR_FACE_YES), GetStringBoundingBox(STR_FACE_NO));
1372 yesno_dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1373 yesno_dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1374 /* Size of the number button + arrows. */
1375 Dimension number_dim = {0, 0};
1376 for (int val = 1; val <= 12; val++) {
1377 SetDParam(0, val);
1378 number_dim = maxdim(number_dim, GetStringBoundingBox(STR_JUST_INT));
1380 uint arrows_width = GetSpriteSize(SPR_ARROW_LEFT).width + GetSpriteSize(SPR_ARROW_RIGHT).width + 2 * (WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1381 number_dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + arrows_width;
1382 number_dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1383 /* Compute width of both buttons. */
1384 yesno_dim.width = max(yesno_dim.width, number_dim.width);
1385 number_dim.width = yesno_dim.width - arrows_width;
1387 this->yesno_dim = yesno_dim;
1388 this->number_dim = number_dim;
1391 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1393 switch (widget) {
1394 case WID_SCMF_FACE: {
1395 Dimension face_size = GetSpriteSize(SPR_GRADIENT);
1396 size->width = max(size->width, face_size.width);
1397 size->height = max(size->height, face_size.height);
1398 break;
1401 case WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT:
1402 case WID_SCMF_TIE_EARRING_TEXT: {
1403 int offset = (widget - WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT) * 2;
1404 *size = maxdim(GetStringBoundingBox(PART_TEXTS_IS_FEMALE[offset]), GetStringBoundingBox(PART_TEXTS_IS_FEMALE[offset + 1]));
1405 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1406 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1407 break;
1410 case WID_SCMF_LIPS_MOUSTACHE_TEXT:
1411 *size = maxdim(GetStringBoundingBox(STR_FACE_LIPS), GetStringBoundingBox(STR_FACE_MOUSTACHE));
1412 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1413 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1414 break;
1416 case WID_SCMF_HAS_GLASSES_TEXT:
1417 case WID_SCMF_HAIR_TEXT:
1418 case WID_SCMF_EYEBROWS_TEXT:
1419 case WID_SCMF_EYECOLOUR_TEXT:
1420 case WID_SCMF_GLASSES_TEXT:
1421 case WID_SCMF_NOSE_TEXT:
1422 case WID_SCMF_CHIN_TEXT:
1423 case WID_SCMF_JACKET_TEXT:
1424 case WID_SCMF_COLLAR_TEXT:
1425 *size = GetStringBoundingBox(PART_TEXTS[widget - WID_SCMF_HAS_GLASSES_TEXT]);
1426 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1427 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1428 break;
1430 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1431 case WID_SCMF_HAS_GLASSES:
1432 *size = this->yesno_dim;
1433 break;
1435 case WID_SCMF_EYECOLOUR:
1436 case WID_SCMF_CHIN:
1437 case WID_SCMF_EYEBROWS:
1438 case WID_SCMF_LIPS_MOUSTACHE:
1439 case WID_SCMF_NOSE:
1440 case WID_SCMF_HAIR:
1441 case WID_SCMF_JACKET:
1442 case WID_SCMF_COLLAR:
1443 case WID_SCMF_TIE_EARRING:
1444 case WID_SCMF_GLASSES:
1445 *size = this->number_dim;
1446 break;
1450 void OnPaint() override
1452 /* lower the non-selected gender button */
1453 this->SetWidgetsLoweredState(!this->is_female, WID_SCMF_MALE, WID_SCMF_MALE2, WIDGET_LIST_END);
1454 this->SetWidgetsLoweredState( this->is_female, WID_SCMF_FEMALE, WID_SCMF_FEMALE2, WIDGET_LIST_END);
1456 /* advanced company manager face selection window */
1458 /* lower the non-selected ethnicity button */
1459 this->SetWidgetLoweredState(WID_SCMF_ETHNICITY_EUR, !HasBit(this->ge, ETHNICITY_BLACK));
1460 this->SetWidgetLoweredState(WID_SCMF_ETHNICITY_AFR, HasBit(this->ge, ETHNICITY_BLACK));
1463 /* Disable dynamically the widgets which CompanyManagerFaceVariable has less than 2 options
1464 * (or in other words you haven't any choice).
1465 * If the widgets depend on a HAS-variable and this is false the widgets will be disabled, too. */
1467 /* Eye colour buttons */
1468 this->SetWidgetsDisabledState(_cmf_info[CMFV_EYE_COLOUR].valid_values[this->ge] < 2,
1469 WID_SCMF_EYECOLOUR, WID_SCMF_EYECOLOUR_L, WID_SCMF_EYECOLOUR_R, WIDGET_LIST_END);
1471 /* Chin buttons */
1472 this->SetWidgetsDisabledState(_cmf_info[CMFV_CHIN].valid_values[this->ge] < 2,
1473 WID_SCMF_CHIN, WID_SCMF_CHIN_L, WID_SCMF_CHIN_R, WIDGET_LIST_END);
1475 /* Eyebrows buttons */
1476 this->SetWidgetsDisabledState(_cmf_info[CMFV_EYEBROWS].valid_values[this->ge] < 2,
1477 WID_SCMF_EYEBROWS, WID_SCMF_EYEBROWS_L, WID_SCMF_EYEBROWS_R, WIDGET_LIST_END);
1479 /* Lips or (if it a male face with a moustache) moustache buttons */
1480 this->SetWidgetsDisabledState(_cmf_info[this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS].valid_values[this->ge] < 2,
1481 WID_SCMF_LIPS_MOUSTACHE, WID_SCMF_LIPS_MOUSTACHE_L, WID_SCMF_LIPS_MOUSTACHE_R, WIDGET_LIST_END);
1483 /* Nose buttons | male faces with moustache haven't any nose options */
1484 this->SetWidgetsDisabledState(_cmf_info[CMFV_NOSE].valid_values[this->ge] < 2 || this->is_moust_male,
1485 WID_SCMF_NOSE, WID_SCMF_NOSE_L, WID_SCMF_NOSE_R, WIDGET_LIST_END);
1487 /* Hair buttons */
1488 this->SetWidgetsDisabledState(_cmf_info[CMFV_HAIR].valid_values[this->ge] < 2,
1489 WID_SCMF_HAIR, WID_SCMF_HAIR_L, WID_SCMF_HAIR_R, WIDGET_LIST_END);
1491 /* Jacket buttons */
1492 this->SetWidgetsDisabledState(_cmf_info[CMFV_JACKET].valid_values[this->ge] < 2,
1493 WID_SCMF_JACKET, WID_SCMF_JACKET_L, WID_SCMF_JACKET_R, WIDGET_LIST_END);
1495 /* Collar buttons */
1496 this->SetWidgetsDisabledState(_cmf_info[CMFV_COLLAR].valid_values[this->ge] < 2,
1497 WID_SCMF_COLLAR, WID_SCMF_COLLAR_L, WID_SCMF_COLLAR_R, WIDGET_LIST_END);
1499 /* Tie/earring buttons | female faces without earring haven't any earring options */
1500 this->SetWidgetsDisabledState(_cmf_info[CMFV_TIE_EARRING].valid_values[this->ge] < 2 ||
1501 (this->is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge) == 0),
1502 WID_SCMF_TIE_EARRING, WID_SCMF_TIE_EARRING_L, WID_SCMF_TIE_EARRING_R, WIDGET_LIST_END);
1504 /* Glasses buttons | faces without glasses haven't any glasses options */
1505 this->SetWidgetsDisabledState(_cmf_info[CMFV_GLASSES].valid_values[this->ge] < 2 || GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge) == 0,
1506 WID_SCMF_GLASSES, WID_SCMF_GLASSES_L, WID_SCMF_GLASSES_R, WIDGET_LIST_END);
1508 this->DrawWidgets();
1511 void DrawWidget(const Rect &r, int widget) const override
1513 switch (widget) {
1514 case WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT:
1515 case WID_SCMF_TIE_EARRING_TEXT: {
1516 StringID str = PART_TEXTS_IS_FEMALE[(widget - WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT) * 2 + this->is_female];
1517 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_GOLD, SA_RIGHT);
1518 break;
1521 case WID_SCMF_LIPS_MOUSTACHE_TEXT:
1522 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, (this->is_moust_male) ? STR_FACE_MOUSTACHE : STR_FACE_LIPS, TC_GOLD, SA_RIGHT);
1523 break;
1525 case WID_SCMF_HAS_GLASSES_TEXT:
1526 case WID_SCMF_HAIR_TEXT:
1527 case WID_SCMF_EYEBROWS_TEXT:
1528 case WID_SCMF_EYECOLOUR_TEXT:
1529 case WID_SCMF_GLASSES_TEXT:
1530 case WID_SCMF_NOSE_TEXT:
1531 case WID_SCMF_CHIN_TEXT:
1532 case WID_SCMF_JACKET_TEXT:
1533 case WID_SCMF_COLLAR_TEXT:
1534 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, PART_TEXTS[widget - WID_SCMF_HAS_GLASSES_TEXT], TC_GOLD, SA_RIGHT);
1535 break;
1538 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1539 if (this->is_female) { // Only for female faces
1540 this->DrawFaceStringLabel(WID_SCMF_HAS_MOUSTACHE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge), true);
1541 } else { // Only for male faces
1542 this->DrawFaceStringLabel(WID_SCMF_HAS_MOUSTACHE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge), true);
1544 break;
1546 case WID_SCMF_TIE_EARRING:
1547 this->DrawFaceStringLabel(WID_SCMF_TIE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_TIE_EARRING, this->ge), false);
1548 break;
1550 case WID_SCMF_LIPS_MOUSTACHE:
1551 if (this->is_moust_male) { // Only for male faces with moustache
1552 this->DrawFaceStringLabel(WID_SCMF_LIPS_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_MOUSTACHE, this->ge), false);
1553 } else { // Only for female faces or male faces without moustache
1554 this->DrawFaceStringLabel(WID_SCMF_LIPS_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_LIPS, this->ge), false);
1556 break;
1558 case WID_SCMF_HAS_GLASSES:
1559 this->DrawFaceStringLabel(WID_SCMF_HAS_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge), true );
1560 break;
1562 case WID_SCMF_HAIR:
1563 this->DrawFaceStringLabel(WID_SCMF_HAIR, GetCompanyManagerFaceBits(this->face, CMFV_HAIR, this->ge), false);
1564 break;
1566 case WID_SCMF_EYEBROWS:
1567 this->DrawFaceStringLabel(WID_SCMF_EYEBROWS, GetCompanyManagerFaceBits(this->face, CMFV_EYEBROWS, this->ge), false);
1568 break;
1570 case WID_SCMF_EYECOLOUR:
1571 this->DrawFaceStringLabel(WID_SCMF_EYECOLOUR, GetCompanyManagerFaceBits(this->face, CMFV_EYE_COLOUR, this->ge), false);
1572 break;
1574 case WID_SCMF_GLASSES:
1575 this->DrawFaceStringLabel(WID_SCMF_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_GLASSES, this->ge), false);
1576 break;
1578 case WID_SCMF_NOSE:
1579 this->DrawFaceStringLabel(WID_SCMF_NOSE, GetCompanyManagerFaceBits(this->face, CMFV_NOSE, this->ge), false);
1580 break;
1582 case WID_SCMF_CHIN:
1583 this->DrawFaceStringLabel(WID_SCMF_CHIN, GetCompanyManagerFaceBits(this->face, CMFV_CHIN, this->ge), false);
1584 break;
1586 case WID_SCMF_JACKET:
1587 this->DrawFaceStringLabel(WID_SCMF_JACKET, GetCompanyManagerFaceBits(this->face, CMFV_JACKET, this->ge), false);
1588 break;
1590 case WID_SCMF_COLLAR:
1591 this->DrawFaceStringLabel(WID_SCMF_COLLAR, GetCompanyManagerFaceBits(this->face, CMFV_COLLAR, this->ge), false);
1592 break;
1594 case WID_SCMF_FACE:
1595 DrawCompanyManagerFace(this->face, Company::Get((CompanyID)this->window_number)->colour, r.left, r.top);
1596 break;
1600 void OnClick(Point pt, int widget, int click_count) override
1602 switch (widget) {
1603 /* Toggle size, advanced/simple face selection */
1604 case WID_SCMF_TOGGLE_LARGE_SMALL:
1605 case WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON:
1606 this->advanced = !this->advanced;
1607 this->SelectDisplayPlanes(this->advanced);
1608 this->ReInit();
1609 break;
1611 /* OK button */
1612 case WID_SCMF_ACCEPT:
1613 DoCommandP(0, 0, this->face, CMD_SET_COMPANY_MANAGER_FACE);
1614 FALLTHROUGH;
1616 /* Cancel button */
1617 case WID_SCMF_CANCEL:
1618 delete this;
1619 break;
1621 /* Load button */
1622 case WID_SCMF_LOAD:
1623 this->face = _company_manager_face;
1624 ScaleAllCompanyManagerFaceBits(this->face);
1625 ShowErrorMessage(STR_FACE_LOAD_DONE, INVALID_STRING_ID, WL_INFO);
1626 this->UpdateData();
1627 this->SetDirty();
1628 break;
1630 /* 'Company manager face number' button, view and/or set company manager face number */
1631 case WID_SCMF_FACECODE:
1632 SetDParam(0, this->face);
1633 ShowQueryString(STR_JUST_INT, STR_FACE_FACECODE_CAPTION, 10 + 1, this, CS_NUMERAL, QSF_NONE);
1634 break;
1636 /* Save button */
1637 case WID_SCMF_SAVE:
1638 _company_manager_face = this->face;
1639 ShowErrorMessage(STR_FACE_SAVE_DONE, INVALID_STRING_ID, WL_INFO);
1640 break;
1642 /* Toggle gender (male/female) button */
1643 case WID_SCMF_MALE:
1644 case WID_SCMF_FEMALE:
1645 case WID_SCMF_MALE2:
1646 case WID_SCMF_FEMALE2:
1647 SetCompanyManagerFaceBits(this->face, CMFV_GENDER, this->ge, (widget == WID_SCMF_FEMALE || widget == WID_SCMF_FEMALE2));
1648 ScaleAllCompanyManagerFaceBits(this->face);
1649 this->UpdateData();
1650 this->SetDirty();
1651 break;
1653 /* Randomize face button */
1654 case WID_SCMF_RANDOM_NEW_FACE:
1655 RandomCompanyManagerFaceBits(this->face, this->ge, this->advanced);
1656 this->UpdateData();
1657 this->SetDirty();
1658 break;
1660 /* Toggle ethnicity (european/african) button */
1661 case WID_SCMF_ETHNICITY_EUR:
1662 case WID_SCMF_ETHNICITY_AFR:
1663 SetCompanyManagerFaceBits(this->face, CMFV_ETHNICITY, this->ge, widget - WID_SCMF_ETHNICITY_EUR);
1664 ScaleAllCompanyManagerFaceBits(this->face);
1665 this->UpdateData();
1666 this->SetDirty();
1667 break;
1669 default:
1670 /* Here all buttons from WID_SCMF_HAS_MOUSTACHE_EARRING to WID_SCMF_GLASSES_R are handled.
1671 * First it checks which CompanyManagerFaceVariable is being changed, and then either
1672 * a: invert the value for boolean variables, or
1673 * b: it checks inside of IncreaseCompanyManagerFaceBits() if a left (_L) butten is pressed and then decrease else increase the variable */
1674 if (widget >= WID_SCMF_HAS_MOUSTACHE_EARRING && widget <= WID_SCMF_GLASSES_R) {
1675 CompanyManagerFaceVariable cmfv; // which CompanyManagerFaceVariable shall be edited
1677 if (widget < WID_SCMF_EYECOLOUR_L) { // Bool buttons
1678 switch (widget - WID_SCMF_HAS_MOUSTACHE_EARRING) {
1679 default: NOT_REACHED();
1680 case 0: cmfv = this->is_female ? CMFV_HAS_TIE_EARRING : CMFV_HAS_MOUSTACHE; break; // Has earring/moustache button
1681 case 1: cmfv = CMFV_HAS_GLASSES; break; // Has glasses button
1683 SetCompanyManagerFaceBits(this->face, cmfv, this->ge, !GetCompanyManagerFaceBits(this->face, cmfv, this->ge));
1684 ScaleAllCompanyManagerFaceBits(this->face);
1685 } else { // Value buttons
1686 switch ((widget - WID_SCMF_EYECOLOUR_L) / 3) {
1687 default: NOT_REACHED();
1688 case 0: cmfv = CMFV_EYE_COLOUR; break; // Eye colour buttons
1689 case 1: cmfv = CMFV_CHIN; break; // Chin buttons
1690 case 2: cmfv = CMFV_EYEBROWS; break; // Eyebrows buttons
1691 case 3: cmfv = this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS; break; // Moustache or lips buttons
1692 case 4: cmfv = CMFV_NOSE; break; // Nose buttons
1693 case 5: cmfv = CMFV_HAIR; break; // Hair buttons
1694 case 6: cmfv = CMFV_JACKET; break; // Jacket buttons
1695 case 7: cmfv = CMFV_COLLAR; break; // Collar buttons
1696 case 8: cmfv = CMFV_TIE_EARRING; break; // Tie/earring buttons
1697 case 9: cmfv = CMFV_GLASSES; break; // Glasses buttons
1699 /* 0 == left (_L), 1 == middle or 2 == right (_R) - button click */
1700 IncreaseCompanyManagerFaceBits(this->face, cmfv, this->ge, (((widget - WID_SCMF_EYECOLOUR_L) % 3) != 0) ? 1 : -1);
1702 this->UpdateData();
1703 this->SetDirty();
1705 break;
1709 void OnQueryTextFinished(char *str) override
1711 if (str == nullptr) return;
1712 /* Set a new company manager face number */
1713 if (!StrEmpty(str)) {
1714 this->face = strtoul(str, nullptr, 10);
1715 ScaleAllCompanyManagerFaceBits(this->face);
1716 ShowErrorMessage(STR_FACE_FACECODE_SET, INVALID_STRING_ID, WL_INFO);
1717 this->UpdateData();
1718 this->SetDirty();
1719 } else {
1720 ShowErrorMessage(STR_FACE_FACECODE_ERR, INVALID_STRING_ID, WL_INFO);
1725 /** Both text values of parts of the face that depend on the #is_female boolean value. */
1726 const StringID SelectCompanyManagerFaceWindow::PART_TEXTS_IS_FEMALE[] = {
1727 STR_FACE_MOUSTACHE, STR_FACE_EARRING, // WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT
1728 STR_FACE_TIE, STR_FACE_EARRING, // WID_SCMF_TIE_EARRING_TEXT
1731 /** Textual names for parts of the face. */
1732 const StringID SelectCompanyManagerFaceWindow::PART_TEXTS[] = {
1733 STR_FACE_GLASSES, // WID_SCMF_HAS_GLASSES_TEXT
1734 STR_FACE_HAIR, // WID_SCMF_HAIR_TEXT
1735 STR_FACE_EYEBROWS, // WID_SCMF_EYEBROWS_TEXT
1736 STR_FACE_EYECOLOUR, // WID_SCMF_EYECOLOUR_TEXT
1737 STR_FACE_GLASSES, // WID_SCMF_GLASSES_TEXT
1738 STR_FACE_NOSE, // WID_SCMF_NOSE_TEXT
1739 STR_FACE_CHIN, // WID_SCMF_CHIN_TEXT
1740 STR_FACE_JACKET, // WID_SCMF_JACKET_TEXT
1741 STR_FACE_COLLAR, // WID_SCMF_COLLAR_TEXT
1744 /** Company manager face selection window description */
1745 static WindowDesc _select_company_manager_face_desc(
1746 WDP_AUTO, "company_face", 0, 0,
1747 WC_COMPANY_MANAGER_FACE, WC_NONE,
1748 WDF_CONSTRUCTION,
1749 _nested_select_company_manager_face_widgets, lengthof(_nested_select_company_manager_face_widgets)
1753 * Open the simple/advanced company manager face selection window
1755 * @param parent the parent company window
1757 static void DoSelectCompanyManagerFace(Window *parent)
1759 if (!Company::IsValidID((CompanyID)parent->window_number)) return;
1761 if (BringWindowToFrontById(WC_COMPANY_MANAGER_FACE, parent->window_number)) return;
1762 new SelectCompanyManagerFaceWindow(&_select_company_manager_face_desc, parent);
1765 static const NWidgetPart _nested_company_infrastructure_widgets[] = {
1766 NWidget(NWID_HORIZONTAL),
1767 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1768 NWidget(WWT_CAPTION, COLOUR_GREY, WID_CI_CAPTION), SetDataTip(STR_COMPANY_INFRASTRUCTURE_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1769 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1770 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1771 EndContainer(),
1772 NWidget(WWT_PANEL, COLOUR_GREY),
1773 NWidget(NWID_VERTICAL), SetPIP(WD_FRAMERECT_TOP, 4, WD_FRAMETEXT_BOTTOM),
1774 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1775 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_RAIL_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1776 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_RAIL_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1777 EndContainer(),
1778 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1779 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_ROAD_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1780 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_ROAD_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1781 EndContainer(),
1782 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1783 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TRAM_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1784 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TRAM_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1785 EndContainer(),
1786 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1787 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_WATER_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1788 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_WATER_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1789 EndContainer(),
1790 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1791 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_STATION_DESC), SetMinimalTextLines(3, 0), SetFill(1, 0),
1792 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_STATION_COUNT), SetMinimalTextLines(3, 0), SetFill(0, 1),
1793 EndContainer(),
1794 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1795 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TOTAL_DESC), SetFill(1, 0),
1796 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TOTAL), SetFill(0, 1),
1797 EndContainer(),
1798 EndContainer(),
1799 EndContainer(),
1803 * Window with detailed information about the company's infrastructure.
1805 struct CompanyInfrastructureWindow : Window
1807 RailTypes railtypes; ///< Valid railtypes.
1808 RoadTypes roadtypes; ///< Valid roadtypes.
1810 uint total_width; ///< String width of the total cost line.
1812 CompanyInfrastructureWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1814 this->UpdateRailRoadTypes();
1816 this->InitNested(window_number);
1817 this->owner = (Owner)this->window_number;
1820 void UpdateRailRoadTypes()
1822 this->railtypes = RAILTYPES_NONE;
1823 this->roadtypes = ROADTYPES_NONE;
1825 /* Find the used railtypes. */
1826 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) {
1827 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1829 this->railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1832 /* Get the date introduced railtypes as well. */
1833 this->railtypes = AddDateIntroducedRailTypes(this->railtypes, MAX_DAY);
1835 /* Find the used roadtypes. */
1836 for (const Engine *e : Engine::IterateType(VEH_ROAD)) {
1837 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1839 this->roadtypes |= GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes;
1842 /* Get the date introduced roadtypes as well. */
1843 this->roadtypes = AddDateIntroducedRoadTypes(this->roadtypes, MAX_DAY);
1844 this->roadtypes &= ~_roadtypes_hidden_mask;
1847 /** Get total infrastructure maintenance cost. */
1848 Money GetTotalMaintenanceCost() const
1850 const Company *c = Company::Get((CompanyID)this->window_number);
1851 Money total;
1853 uint32 rail_total = c->infrastructure.GetRailTotal();
1854 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
1855 if (HasBit(this->railtypes, rt)) total += RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total);
1857 total += SignalMaintenanceCost(c->infrastructure.signal);
1859 uint32 road_total = c->infrastructure.GetRoadTotal();
1860 uint32 tram_total = c->infrastructure.GetTramTotal();
1861 for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
1862 if (HasBit(this->roadtypes, rt)) total += RoadMaintenanceCost(rt, c->infrastructure.road[rt], RoadTypeIsRoad(rt) ? road_total : tram_total);
1865 total += CanalMaintenanceCost(c->infrastructure.water);
1866 total += StationMaintenanceCost(c->infrastructure.station);
1867 total += AirportMaintenanceCost(c->index);
1869 return total;
1872 void SetStringParameters(int widget) const override
1874 switch (widget) {
1875 case WID_CI_CAPTION:
1876 SetDParam(0, (CompanyID)this->window_number);
1877 break;
1881 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1883 const Company *c = Company::Get((CompanyID)this->window_number);
1885 switch (widget) {
1886 case WID_CI_RAIL_DESC: {
1887 uint lines = 1;
1889 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT).width);
1891 RailType rt;
1892 FOR_ALL_SORTED_RAILTYPES(rt) {
1893 if (HasBit(this->railtypes, rt)) {
1894 lines++;
1895 SetDParam(0, GetRailTypeInfo(rt)->strings.name);
1896 size->width = max(size->width, GetStringBoundingBox(STR_WHITE_STRING).width + WD_FRAMERECT_LEFT);
1899 if (this->railtypes != RAILTYPES_NONE) {
1900 lines++;
1901 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS).width + WD_FRAMERECT_LEFT);
1904 size->height = max(size->height, lines * FONT_HEIGHT_NORMAL);
1905 break;
1908 case WID_CI_ROAD_DESC:
1909 case WID_CI_TRAM_DESC: {
1910 uint lines = 0;
1912 size->width = max(size->width, GetStringBoundingBox(widget == WID_CI_ROAD_DESC ? STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT : STR_COMPANY_INFRASTRUCTURE_VIEW_TRAM_SECT).width);
1914 RoadType rt;
1915 FOR_ALL_SORTED_ROADTYPES(rt) {
1916 if (HasBit(this->roadtypes, rt) && RoadTypeIsRoad(rt) == (widget == WID_CI_ROAD_DESC)) {
1917 lines++;
1918 SetDParam(0, GetRoadTypeInfo(rt)->strings.name);
1919 size->width = max(size->width, GetStringBoundingBox(STR_WHITE_STRING).width + WD_FRAMERECT_LEFT);
1923 size->height = max(size->height, lines * FONT_HEIGHT_NORMAL);
1924 break;
1927 case WID_CI_WATER_DESC:
1928 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT).width);
1929 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS).width + WD_FRAMERECT_LEFT);
1930 break;
1932 case WID_CI_STATION_DESC:
1933 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT).width);
1934 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS).width + WD_FRAMERECT_LEFT);
1935 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS).width + WD_FRAMERECT_LEFT);
1936 break;
1938 case WID_CI_RAIL_COUNT:
1939 case WID_CI_ROAD_COUNT:
1940 case WID_CI_TRAM_COUNT:
1941 case WID_CI_WATER_COUNT:
1942 case WID_CI_STATION_COUNT:
1943 case WID_CI_TOTAL: {
1944 /* Find the maximum count that is displayed. */
1945 uint32 max_val = 1000; // Some random number to reserve enough space.
1946 Money max_cost = 10000; // Some random number to reserve enough space.
1947 uint32 rail_total = c->infrastructure.GetRailTotal();
1948 for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) {
1949 max_val = max(max_val, c->infrastructure.rail[rt]);
1950 max_cost = max(max_cost, RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
1952 max_val = max(max_val, c->infrastructure.signal);
1953 max_cost = max(max_cost, SignalMaintenanceCost(c->infrastructure.signal));
1954 uint32 road_total = c->infrastructure.GetRoadTotal();
1955 uint32 tram_total = c->infrastructure.GetTramTotal();
1956 for (RoadType rt = ROADTYPE_BEGIN; rt < ROADTYPE_END; rt++) {
1957 max_val = max(max_val, c->infrastructure.road[rt]);
1958 max_cost = max(max_cost, RoadMaintenanceCost(rt, c->infrastructure.road[rt], RoadTypeIsRoad(rt) ? road_total : tram_total));
1961 max_val = max(max_val, c->infrastructure.water);
1962 max_cost = max(max_cost, CanalMaintenanceCost(c->infrastructure.water));
1963 max_val = max(max_val, c->infrastructure.station);
1964 max_cost = max(max_cost, StationMaintenanceCost(c->infrastructure.station));
1965 max_val = max(max_val, c->infrastructure.airport);
1966 max_cost = max(max_cost, AirportMaintenanceCost(c->index));
1968 SetDParamMaxValue(0, max_val);
1969 uint count_width = GetStringBoundingBox(STR_WHITE_COMMA).width + 20; // Reserve some wiggle room
1971 if (_settings_game.economy.infrastructure_maintenance) {
1972 SetDParamMaxValue(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
1973 this->total_width = GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL).width + 20;
1974 size->width = max(size->width, this->total_width);
1976 SetDParamMaxValue(0, max_cost * 12); // Convert to per year
1977 count_width += max(this->total_width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL).width);
1980 size->width = max(size->width, count_width);
1982 /* Set height of the total line. */
1983 if (widget == WID_CI_TOTAL) {
1984 size->height = _settings_game.economy.infrastructure_maintenance ? max(size->height, EXP_LINESPACE + FONT_HEIGHT_NORMAL) : 0;
1986 break;
1992 * Helper for drawing the counts line.
1993 * @param r The bounds to draw in.
1994 * @param y The y position to draw at.
1995 * @param count The count to show on this line.
1996 * @param monthly_cost The monthly costs.
1998 void DrawCountLine(const Rect &r, int &y, int count, Money monthly_cost) const
2000 SetDParam(0, count);
2001 DrawString(r.left, r.right, y += FONT_HEIGHT_NORMAL, STR_WHITE_COMMA, TC_FROMSTRING, SA_RIGHT);
2003 if (_settings_game.economy.infrastructure_maintenance) {
2004 SetDParam(0, monthly_cost * 12); // Convert to per year
2005 int left = _current_text_dir == TD_RTL ? r.right - this->total_width : r.left;
2006 DrawString(left, left + this->total_width, y, STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL, TC_FROMSTRING, SA_RIGHT);
2010 void DrawWidget(const Rect &r, int widget) const override
2012 const Company *c = Company::Get((CompanyID)this->window_number);
2013 int y = r.top;
2015 int offs_left = _current_text_dir == TD_LTR ? WD_FRAMERECT_LEFT : 0;
2016 int offs_right = _current_text_dir == TD_LTR ? 0 : WD_FRAMERECT_LEFT;
2018 switch (widget) {
2019 case WID_CI_RAIL_DESC:
2020 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT);
2022 if (this->railtypes != RAILTYPES_NONE) {
2023 /* Draw name of each valid railtype. */
2024 RailType rt;
2025 FOR_ALL_SORTED_RAILTYPES(rt) {
2026 if (HasBit(this->railtypes, rt)) {
2027 SetDParam(0, GetRailTypeInfo(rt)->strings.name);
2028 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_WHITE_STRING);
2031 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS);
2032 } else {
2033 /* No valid railtype. */
2034 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
2037 break;
2039 case WID_CI_RAIL_COUNT: {
2040 /* Draw infrastructure count for each valid railtype. */
2041 uint32 rail_total = c->infrastructure.GetRailTotal();
2042 RailType rt;
2043 FOR_ALL_SORTED_RAILTYPES(rt) {
2044 if (HasBit(this->railtypes, rt)) {
2045 this->DrawCountLine(r, y, c->infrastructure.rail[rt], RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
2048 if (this->railtypes != RAILTYPES_NONE) {
2049 this->DrawCountLine(r, y, c->infrastructure.signal, SignalMaintenanceCost(c->infrastructure.signal));
2051 break;
2054 case WID_CI_ROAD_DESC:
2055 case WID_CI_TRAM_DESC: {
2056 DrawString(r.left, r.right, y, widget == WID_CI_ROAD_DESC ? STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT : STR_COMPANY_INFRASTRUCTURE_VIEW_TRAM_SECT);
2058 /* Draw name of each valid roadtype. */
2059 RoadType rt;
2060 FOR_ALL_SORTED_ROADTYPES(rt) {
2061 if (HasBit(this->roadtypes, rt) && RoadTypeIsRoad(rt) == (widget == WID_CI_ROAD_DESC)) {
2062 SetDParam(0, GetRoadTypeInfo(rt)->strings.name);
2063 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_WHITE_STRING);
2067 break;
2070 case WID_CI_ROAD_COUNT:
2071 case WID_CI_TRAM_COUNT: {
2072 uint32 road_tram_total = widget == WID_CI_ROAD_COUNT ? c->infrastructure.GetRoadTotal() : c->infrastructure.GetTramTotal();
2073 RoadType rt;
2074 FOR_ALL_SORTED_ROADTYPES(rt) {
2075 if (HasBit(this->roadtypes, rt) && RoadTypeIsRoad(rt) == (widget == WID_CI_ROAD_COUNT)) {
2076 this->DrawCountLine(r, y, c->infrastructure.road[rt], RoadMaintenanceCost(rt, c->infrastructure.road[rt], road_tram_total));
2079 break;
2082 case WID_CI_WATER_DESC:
2083 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT);
2084 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS);
2085 break;
2087 case WID_CI_WATER_COUNT:
2088 this->DrawCountLine(r, y, c->infrastructure.water, CanalMaintenanceCost(c->infrastructure.water));
2089 break;
2091 case WID_CI_TOTAL:
2092 if (_settings_game.economy.infrastructure_maintenance) {
2093 int left = _current_text_dir == TD_RTL ? r.right - this->total_width : r.left;
2094 GfxFillRect(left, y, left + this->total_width, y, PC_WHITE);
2095 y += EXP_LINESPACE;
2096 SetDParam(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
2097 DrawString(left, left + this->total_width, y, STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL, TC_FROMSTRING, SA_RIGHT);
2099 break;
2101 case WID_CI_STATION_DESC:
2102 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT);
2103 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS);
2104 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS);
2105 break;
2107 case WID_CI_STATION_COUNT:
2108 this->DrawCountLine(r, y, c->infrastructure.station, StationMaintenanceCost(c->infrastructure.station));
2109 this->DrawCountLine(r, y, c->infrastructure.airport, AirportMaintenanceCost(c->index));
2110 break;
2115 * Some data on this window has become invalid.
2116 * @param data Information about the changed data.
2117 * @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.
2119 void OnInvalidateData(int data = 0, bool gui_scope = true) override
2121 if (!gui_scope) return;
2123 this->UpdateRailRoadTypes();
2124 this->ReInit();
2128 static WindowDesc _company_infrastructure_desc(
2129 WDP_AUTO, "company_infrastructure", 0, 0,
2130 WC_COMPANY_INFRASTRUCTURE, WC_NONE,
2132 _nested_company_infrastructure_widgets, lengthof(_nested_company_infrastructure_widgets)
2136 * Open the infrastructure window of a company.
2137 * @param company Company to show infrastructure of.
2139 static void ShowCompanyInfrastructure(CompanyID company)
2141 if (!Company::IsValidID(company)) return;
2142 AllocateWindowDescFront<CompanyInfrastructureWindow>(&_company_infrastructure_desc, company);
2145 static const NWidgetPart _nested_company_widgets[] = {
2146 NWidget(NWID_HORIZONTAL),
2147 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2148 NWidget(WWT_CAPTION, COLOUR_GREY, WID_C_CAPTION), SetDataTip(STR_COMPANY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2149 NWidget(WWT_SHADEBOX, COLOUR_GREY),
2150 NWidget(WWT_STICKYBOX, COLOUR_GREY),
2151 EndContainer(),
2152 NWidget(WWT_PANEL, COLOUR_GREY),
2153 NWidget(NWID_HORIZONTAL), SetPIP(4, 6, 4),
2154 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
2155 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE), SetMinimalSize(92, 119), SetFill(1, 0),
2156 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE_TITLE), SetFill(1, 1), SetMinimalTextLines(2, 0),
2157 EndContainer(),
2158 NWidget(NWID_VERTICAL),
2159 NWidget(NWID_HORIZONTAL),
2160 NWidget(NWID_VERTICAL), SetPIP(4, 5, 5),
2161 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INAUGURATION), SetDataTip(STR_COMPANY_VIEW_INAUGURATED_TITLE, STR_NULL), SetFill(1, 0),
2162 NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
2163 NWidget(WWT_LABEL, COLOUR_GREY, WID_C_DESC_COLOUR_SCHEME), SetDataTip(STR_COMPANY_VIEW_COLOUR_SCHEME_TITLE, STR_NULL),
2164 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_COLOUR_SCHEME_EXAMPLE), SetMinimalSize(30, 0), SetFill(0, 1),
2165 NWidget(NWID_SPACER), SetFill(1, 0),
2166 EndContainer(),
2167 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
2168 NWidget(NWID_VERTICAL),
2169 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_VEHICLE), SetDataTip(STR_COMPANY_VIEW_VEHICLES_TITLE, STR_NULL),
2170 NWidget(NWID_SPACER), SetFill(0, 1),
2171 EndContainer(),
2172 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_VEHICLE_COUNTS), SetMinimalTextLines(4, 0),
2173 NWidget(NWID_SPACER), SetFill(1, 0),
2174 EndContainer(),
2175 EndContainer(),
2176 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
2177 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_VIEW_BUILD_HQ),
2178 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_VIEW_HQ), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_VIEW_HQ_BUTTON, STR_COMPANY_VIEW_VIEW_HQ_TOOLTIP),
2179 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_C_BUILD_HQ), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_BUILD_HQ_BUTTON, STR_COMPANY_VIEW_BUILD_HQ_TOOLTIP),
2180 EndContainer(),
2181 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_RELOCATE),
2182 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_C_RELOCATE_HQ), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_RELOCATE_HQ, STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS),
2183 NWidget(NWID_SPACER), SetMinimalSize(90, 0),
2184 EndContainer(),
2185 NWidget(NWID_SPACER), SetFill(0, 1),
2186 EndContainer(),
2187 EndContainer(),
2188 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_COMPANY_VALUE), SetDataTip(STR_COMPANY_VIEW_COMPANY_VALUE, STR_NULL), SetFill(1, 0),
2189 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
2190 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
2191 NWidget(NWID_VERTICAL),
2192 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE, STR_NULL),
2193 NWidget(NWID_SPACER), SetFill(0, 1),
2194 EndContainer(),
2195 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_INFRASTRUCTURE_COUNTS), SetMinimalTextLines(5, 0), SetFill(1, 0),
2196 NWidget(NWID_VERTICAL),
2197 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_VIEW_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE_BUTTON, STR_COMPANY_VIEW_INFRASTRUCTURE_TOOLTIP),
2198 NWidget(NWID_SPACER), SetFill(0, 1), SetMinimalSize(90, 0),
2199 EndContainer(),
2200 EndContainer(),
2201 EndContainer(),
2202 NWidget(NWID_HORIZONTAL),
2203 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_DESC_OWNERS),
2204 NWidget(NWID_VERTICAL), SetPIP(5, 5, 4),
2205 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_OWNERS), SetMinimalTextLines(3, 0),
2206 NWidget(NWID_SPACER), SetFill(0, 1),
2207 EndContainer(),
2208 EndContainer(),
2209 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
2210 NWidget(NWID_SPACER), SetMinimalSize(90, 0), SetFill(0, 1),
2211 /* Multi player buttons. */
2212 NWidget(NWID_HORIZONTAL),
2213 NWidget(WWT_EMPTY, COLOUR_GREY, WID_C_HAS_PASSWORD),
2214 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_MULTIPLAYER),
2215 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_PASSWORD), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_COMPANY_VIEW_PASSWORD_TOOLTIP),
2216 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_JOIN), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_JOIN, STR_COMPANY_VIEW_JOIN_TOOLTIP),
2217 EndContainer(),
2218 EndContainer(),
2219 EndContainer(),
2220 EndContainer(),
2221 EndContainer(),
2222 EndContainer(),
2223 EndContainer(),
2224 /* Button bars at the bottom. */
2225 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_BUTTONS),
2226 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2227 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),
2228 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),
2229 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),
2230 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),
2231 EndContainer(),
2232 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
2233 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_BUY_SHARE), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_BUY_SHARE_BUTTON, STR_COMPANY_VIEW_BUY_SHARE_TOOLTIP),
2234 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_SELL_SHARE), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_SELL_SHARE_BUTTON, STR_COMPANY_VIEW_SELL_SHARE_TOOLTIP),
2235 EndContainer(),
2236 EndContainer(),
2239 int GetAmountOwnedBy(const Company *c, Owner owner)
2241 return (c->share_owners[0] == owner) +
2242 (c->share_owners[1] == owner) +
2243 (c->share_owners[2] == owner) +
2244 (c->share_owners[3] == owner);
2247 /** Strings for the company vehicle counts */
2248 static const StringID _company_view_vehicle_count_strings[] = {
2249 STR_COMPANY_VIEW_TRAINS, STR_COMPANY_VIEW_ROAD_VEHICLES, STR_COMPANY_VIEW_SHIPS, STR_COMPANY_VIEW_AIRCRAFT
2253 * Window with general information about a company
2255 struct CompanyWindow : Window
2257 CompanyWidgets query_widget;
2259 /** Display planes in the company window. */
2260 enum CompanyWindowPlanes {
2261 /* Display planes of the #WID_C_SELECT_MULTIPLAYER selection widget. */
2262 CWP_MP_C_PWD = 0, ///< Display the company password button.
2263 CWP_MP_C_JOIN, ///< Display the join company button.
2265 /* Display planes of the #WID_C_SELECT_VIEW_BUILD_HQ selection widget. */
2266 CWP_VB_VIEW = 0, ///< Display the view button
2267 CWP_VB_BUILD, ///< Display the build button
2269 /* Display planes of the #WID_C_SELECT_RELOCATE selection widget. */
2270 CWP_RELOCATE_SHOW = 0, ///< Show the relocate HQ button.
2271 CWP_RELOCATE_HIDE, ///< Hide the relocate HQ button.
2273 /* Display planes of the #WID_C_SELECT_BUTTONS selection widget. */
2274 CWP_BUTTONS_LOCAL = 0, ///< Buttons of the local company.
2275 CWP_BUTTONS_OTHER, ///< Buttons of the other companies.
2278 CompanyWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
2280 this->InitNested(window_number);
2281 this->owner = (Owner)this->window_number;
2282 this->OnInvalidateData();
2285 void OnPaint() override
2287 const Company *c = Company::Get((CompanyID)this->window_number);
2288 bool local = this->window_number == _local_company;
2290 if (!this->IsShaded()) {
2291 bool reinit = false;
2293 /* Button bar selection. */
2294 int plane = local ? CWP_BUTTONS_LOCAL : CWP_BUTTONS_OTHER;
2295 NWidgetStacked *wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_BUTTONS);
2296 if (plane != wi->shown_plane) {
2297 wi->SetDisplayedPlane(plane);
2298 this->InvalidateData();
2299 return;
2302 /* Build HQ button handling. */
2303 plane = (local && c->location_of_HQ == INVALID_TILE) ? CWP_VB_BUILD : CWP_VB_VIEW;
2304 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_VIEW_BUILD_HQ);
2305 if (plane != wi->shown_plane) {
2306 wi->SetDisplayedPlane(plane);
2307 this->SetDirty();
2308 return;
2311 this->SetWidgetDisabledState(WID_C_VIEW_HQ, c->location_of_HQ == INVALID_TILE);
2313 /* Enable/disable 'Relocate HQ' button. */
2314 plane = (!local || c->location_of_HQ == INVALID_TILE) ? CWP_RELOCATE_HIDE : CWP_RELOCATE_SHOW;
2315 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_RELOCATE);
2316 if (plane != wi->shown_plane) {
2317 wi->SetDisplayedPlane(plane);
2318 this->SetDirty();
2319 return;
2322 /* Owners of company */
2323 plane = SZSP_HORIZONTAL;
2324 for (uint i = 0; i < lengthof(c->share_owners); i++) {
2325 if (c->share_owners[i] != INVALID_COMPANY) {
2326 plane = 0;
2327 break;
2330 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_DESC_OWNERS);
2331 if (plane != wi->shown_plane) {
2332 wi->SetDisplayedPlane(plane);
2333 reinit = true;
2336 /* Multiplayer buttons. */
2337 plane = ((!_networking) ? (int)SZSP_NONE : (int)(local ? CWP_MP_C_PWD : CWP_MP_C_JOIN));
2338 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_MULTIPLAYER);
2339 if (plane != wi->shown_plane) {
2340 wi->SetDisplayedPlane(plane);
2341 reinit = true;
2343 this->SetWidgetDisabledState(WID_C_COMPANY_JOIN, c->is_ai);
2345 if (reinit) {
2346 this->ReInit();
2347 return;
2351 this->DrawWidgets();
2354 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2356 switch (widget) {
2357 case WID_C_FACE: {
2358 Dimension face_size = GetSpriteSize(SPR_GRADIENT);
2359 size->width = max(size->width, face_size.width);
2360 size->height = max(size->height, face_size.height);
2361 break;
2364 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2365 Point offset;
2366 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2367 d.width -= offset.x;
2368 d.height -= offset.y;
2369 *size = maxdim(*size, d);
2370 break;
2373 case WID_C_DESC_COMPANY_VALUE:
2374 SetDParam(0, INT64_MAX); // Arguably the maximum company value
2375 size->width = GetStringBoundingBox(STR_COMPANY_VIEW_COMPANY_VALUE).width;
2376 break;
2378 case WID_C_DESC_VEHICLE_COUNTS:
2379 SetDParamMaxValue(0, 5000); // Maximum number of vehicles
2380 for (uint i = 0; i < lengthof(_company_view_vehicle_count_strings); i++) {
2381 size->width = max(size->width, GetStringBoundingBox(_company_view_vehicle_count_strings[i]).width);
2383 break;
2385 case WID_C_DESC_INFRASTRUCTURE_COUNTS:
2386 SetDParamMaxValue(0, UINT_MAX);
2387 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL).width);
2388 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD).width);
2389 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_WATER).width);
2390 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_STATION).width);
2391 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT).width);
2392 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_NONE).width);
2393 break;
2395 case WID_C_DESC_OWNERS: {
2396 for (const Company *c2 : Company::Iterate()) {
2397 SetDParamMaxValue(0, 75);
2398 SetDParam(1, c2->index);
2400 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_SHARES_OWNED_BY).width);
2402 break;
2405 case WID_C_HAS_PASSWORD:
2406 *size = maxdim(*size, GetSpriteSize(SPR_LOCK));
2407 break;
2411 void DrawWidget(const Rect &r, int widget) const override
2413 const Company *c = Company::Get((CompanyID)this->window_number);
2414 switch (widget) {
2415 case WID_C_FACE:
2416 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
2417 break;
2419 case WID_C_FACE_TITLE:
2420 SetDParam(0, c->index);
2421 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_COMPANY_VIEW_PRESIDENT_MANAGER_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
2422 break;
2424 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2425 Point offset;
2426 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2427 d.height -= offset.y;
2428 DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOUR(c->index), r.left - offset.x, (r.top + r.bottom - d.height) / 2 - offset.y);
2429 break;
2432 case WID_C_DESC_VEHICLE_COUNTS: {
2433 uint amounts[4];
2434 amounts[0] = c->group_all[VEH_TRAIN].num_vehicle;
2435 amounts[1] = c->group_all[VEH_ROAD].num_vehicle;
2436 amounts[2] = c->group_all[VEH_SHIP].num_vehicle;
2437 amounts[3] = c->group_all[VEH_AIRCRAFT].num_vehicle;
2439 int y = r.top;
2440 if (amounts[0] + amounts[1] + amounts[2] + amounts[3] == 0) {
2441 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_VEHICLES_NONE);
2442 } else {
2443 assert_compile(lengthof(amounts) == lengthof(_company_view_vehicle_count_strings));
2445 for (uint i = 0; i < lengthof(amounts); i++) {
2446 if (amounts[i] != 0) {
2447 SetDParam(0, amounts[i]);
2448 DrawString(r.left, r.right, y, _company_view_vehicle_count_strings[i]);
2449 y += FONT_HEIGHT_NORMAL;
2453 break;
2456 case WID_C_DESC_INFRASTRUCTURE_COUNTS: {
2457 uint y = r.top;
2459 /* Collect rail and road counts. */
2460 uint rail_pieces = c->infrastructure.signal;
2461 uint road_pieces = 0;
2462 for (uint i = 0; i < lengthof(c->infrastructure.rail); i++) rail_pieces += c->infrastructure.rail[i];
2463 for (uint i = 0; i < lengthof(c->infrastructure.road); i++) road_pieces += c->infrastructure.road[i];
2465 if (rail_pieces == 0 && road_pieces == 0 && c->infrastructure.water == 0 && c->infrastructure.station == 0 && c->infrastructure.airport == 0) {
2466 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
2467 } else {
2468 if (rail_pieces != 0) {
2469 SetDParam(0, rail_pieces);
2470 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL);
2471 y += FONT_HEIGHT_NORMAL;
2473 if (road_pieces != 0) {
2474 SetDParam(0, road_pieces);
2475 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD);
2476 y += FONT_HEIGHT_NORMAL;
2478 if (c->infrastructure.water != 0) {
2479 SetDParam(0, c->infrastructure.water);
2480 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_WATER);
2481 y += FONT_HEIGHT_NORMAL;
2483 if (c->infrastructure.station != 0) {
2484 SetDParam(0, c->infrastructure.station);
2485 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_STATION);
2486 y += FONT_HEIGHT_NORMAL;
2488 if (c->infrastructure.airport != 0) {
2489 SetDParam(0, c->infrastructure.airport);
2490 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT);
2494 break;
2497 case WID_C_DESC_OWNERS: {
2498 uint y = r.top;
2500 for (const Company *c2 : Company::Iterate()) {
2501 uint amt = GetAmountOwnedBy(c, c2->index);
2502 if (amt != 0) {
2503 SetDParam(0, amt * 25);
2504 SetDParam(1, c2->index);
2506 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_SHARES_OWNED_BY);
2507 y += FONT_HEIGHT_NORMAL;
2510 break;
2513 case WID_C_HAS_PASSWORD:
2514 if (_networking && NetworkCompanyIsPassworded(c->index)) {
2515 DrawSprite(SPR_LOCK, PAL_NONE, r.left, r.top);
2517 break;
2521 void SetStringParameters(int widget) const override
2523 switch (widget) {
2524 case WID_C_CAPTION:
2525 SetDParam(0, (CompanyID)this->window_number);
2526 SetDParam(1, (CompanyID)this->window_number);
2527 break;
2529 case WID_C_DESC_INAUGURATION:
2530 SetDParam(0, Company::Get((CompanyID)this->window_number)->inaugurated_year);
2531 break;
2533 case WID_C_DESC_COMPANY_VALUE:
2534 SetDParam(0, CalculateCompanyValue(Company::Get((CompanyID)this->window_number)));
2535 break;
2539 void OnClick(Point pt, int widget, int click_count) override
2541 switch (widget) {
2542 case WID_C_NEW_FACE: DoSelectCompanyManagerFace(this); break;
2544 case WID_C_COLOUR_SCHEME:
2545 ShowCompanyLiveryWindow((CompanyID)this->window_number, INVALID_GROUP);
2546 break;
2548 case WID_C_PRESIDENT_NAME:
2549 this->query_widget = WID_C_PRESIDENT_NAME;
2550 SetDParam(0, this->window_number);
2551 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);
2552 break;
2554 case WID_C_COMPANY_NAME:
2555 this->query_widget = WID_C_COMPANY_NAME;
2556 SetDParam(0, this->window_number);
2557 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);
2558 break;
2560 case WID_C_VIEW_HQ: {
2561 TileIndex tile = Company::Get((CompanyID)this->window_number)->location_of_HQ;
2562 if (_ctrl_pressed) {
2563 ShowExtraViewPortWindow(tile);
2564 } else {
2565 ScrollMainWindowToTile(tile);
2567 break;
2570 case WID_C_BUILD_HQ:
2571 if ((byte)this->window_number != _local_company) return;
2572 if (this->IsWidgetLowered(WID_C_BUILD_HQ)) {
2573 ResetObjectToPlace();
2574 this->RaiseButtons();
2575 break;
2577 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2578 SetTileSelectSize(2, 2);
2579 this->LowerWidget(WID_C_BUILD_HQ);
2580 this->SetWidgetDirty(WID_C_BUILD_HQ);
2581 break;
2583 case WID_C_RELOCATE_HQ:
2584 if (this->IsWidgetLowered(WID_C_RELOCATE_HQ)) {
2585 ResetObjectToPlace();
2586 this->RaiseButtons();
2587 break;
2589 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2590 SetTileSelectSize(2, 2);
2591 this->LowerWidget(WID_C_RELOCATE_HQ);
2592 this->SetWidgetDirty(WID_C_RELOCATE_HQ);
2593 break;
2595 case WID_C_VIEW_INFRASTRUCTURE:
2596 ShowCompanyInfrastructure((CompanyID)this->window_number);
2597 break;
2599 case WID_C_BUY_SHARE:
2600 DoCommandP(0, this->window_number, 0, CMD_BUY_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_BUY_25_SHARE_IN_THIS));
2601 break;
2603 case WID_C_SELL_SHARE:
2604 DoCommandP(0, this->window_number, 0, CMD_SELL_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_SELL_25_SHARE_IN));
2605 break;
2607 case WID_C_COMPANY_PASSWORD:
2608 if (this->window_number == _local_company) ShowNetworkCompanyPasswordWindow(this);
2609 break;
2611 case WID_C_COMPANY_JOIN: {
2612 this->query_widget = WID_C_COMPANY_JOIN;
2613 CompanyID company = (CompanyID)this->window_number;
2614 if (_network_server) {
2615 NetworkServerDoMove(CLIENT_ID_SERVER, company);
2616 MarkWholeScreenDirty();
2617 } else if (NetworkCompanyIsPassworded(company)) {
2618 /* ask for the password */
2619 ShowQueryString(STR_EMPTY, STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION, NETWORK_PASSWORD_LENGTH, this, CS_ALPHANUMERAL, QSF_PASSWORD);
2620 } else {
2621 /* just send the join command */
2622 NetworkClientRequestMove(company);
2624 break;
2629 void OnHundredthTick() override
2631 /* redraw the window every now and then */
2632 this->SetDirty();
2635 void OnPlaceObject(Point pt, TileIndex tile) override
2637 if (DoCommandP(tile, OBJECT_HQ, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_COMPANY_HEADQUARTERS)) && !_shift_pressed) {
2638 ResetObjectToPlace();
2639 this->RaiseButtons();
2643 void OnPlaceObjectAbort() override
2645 this->RaiseButtons();
2648 void OnQueryTextFinished(char *str) override
2650 if (str == nullptr) return;
2652 switch (this->query_widget) {
2653 default: NOT_REACHED();
2655 case WID_C_PRESIDENT_NAME:
2656 DoCommandP(0, 0, 0, CMD_RENAME_PRESIDENT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_PRESIDENT), nullptr, str);
2657 break;
2659 case WID_C_COMPANY_NAME:
2660 DoCommandP(0, 0, 0, CMD_RENAME_COMPANY | CMD_MSG(STR_ERROR_CAN_T_CHANGE_COMPANY_NAME), nullptr, str);
2661 break;
2663 case WID_C_COMPANY_JOIN:
2664 NetworkClientRequestMove((CompanyID)this->window_number, str);
2665 break;
2671 * Some data on this window has become invalid.
2672 * @param data Information about the changed data.
2673 * @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.
2675 void OnInvalidateData(int data = 0, bool gui_scope = true) override
2677 if (this->window_number == _local_company) return;
2679 if (_settings_game.economy.allow_shares) { // Shares are allowed
2680 const Company *c = Company::Get(this->window_number);
2682 /* If all shares are owned by someone (none by nobody), disable buy button */
2683 this->SetWidgetDisabledState(WID_C_BUY_SHARE, GetAmountOwnedBy(c, INVALID_OWNER) == 0 ||
2684 /* Only 25% left to buy. If the company is human, disable buying it up.. TODO issues! */
2685 (GetAmountOwnedBy(c, INVALID_OWNER) == 1 && !c->is_ai) ||
2686 /* Spectators cannot do anything of course */
2687 _local_company == COMPANY_SPECTATOR);
2689 /* If the company doesn't own any shares, disable sell button */
2690 this->SetWidgetDisabledState(WID_C_SELL_SHARE, (GetAmountOwnedBy(c, _local_company) == 0) ||
2691 /* Spectators cannot do anything of course */
2692 _local_company == COMPANY_SPECTATOR);
2693 } else { // Shares are not allowed, disable buy/sell buttons
2694 this->DisableWidget(WID_C_BUY_SHARE);
2695 this->DisableWidget(WID_C_SELL_SHARE);
2700 static WindowDesc _company_desc(
2701 WDP_AUTO, "company", 0, 0,
2702 WC_COMPANY, WC_NONE,
2704 _nested_company_widgets, lengthof(_nested_company_widgets)
2708 * Show the window with the overview of the company.
2709 * @param company The company to show the window for.
2711 void ShowCompany(CompanyID company)
2713 if (!Company::IsValidID(company)) return;
2715 AllocateWindowDescFront<CompanyWindow>(&_company_desc, company);
2719 * Redraw all windows with company infrastructure counts.
2720 * @param company The company to redraw the windows of.
2722 void DirtyCompanyInfrastructureWindows(CompanyID company)
2724 SetWindowDirty(WC_COMPANY, company);
2725 SetWindowDirty(WC_COMPANY_INFRASTRUCTURE, company);
2728 struct BuyCompanyWindow : Window {
2729 BuyCompanyWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
2731 this->InitNested(window_number);
2734 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2736 switch (widget) {
2737 case WID_BC_FACE:
2738 *size = GetSpriteSize(SPR_GRADIENT);
2739 break;
2741 case WID_BC_QUESTION:
2742 const Company *c = Company::Get((CompanyID)this->window_number);
2743 SetDParam(0, c->index);
2744 SetDParam(1, c->bankrupt_value);
2745 size->height = GetStringHeight(STR_BUY_COMPANY_MESSAGE, size->width);
2746 break;
2750 void SetStringParameters(int widget) const override
2752 switch (widget) {
2753 case WID_BC_CAPTION:
2754 SetDParam(0, STR_COMPANY_NAME);
2755 SetDParam(1, Company::Get((CompanyID)this->window_number)->index);
2756 break;
2760 void DrawWidget(const Rect &r, int widget) const override
2762 switch (widget) {
2763 case WID_BC_FACE: {
2764 const Company *c = Company::Get((CompanyID)this->window_number);
2765 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
2766 break;
2769 case WID_BC_QUESTION: {
2770 const Company *c = Company::Get((CompanyID)this->window_number);
2771 SetDParam(0, c->index);
2772 SetDParam(1, c->bankrupt_value);
2773 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_BUY_COMPANY_MESSAGE, TC_FROMSTRING, SA_CENTER);
2774 break;
2779 void OnClick(Point pt, int widget, int click_count) override
2781 switch (widget) {
2782 case WID_BC_NO:
2783 delete this;
2784 break;
2786 case WID_BC_YES:
2787 DoCommandP(0, this->window_number, 0, CMD_BUY_COMPANY | CMD_MSG(STR_ERROR_CAN_T_BUY_COMPANY));
2788 break;
2793 static const NWidgetPart _nested_buy_company_widgets[] = {
2794 NWidget(NWID_HORIZONTAL),
2795 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
2796 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_BC_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2797 EndContainer(),
2798 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
2799 NWidget(NWID_VERTICAL), SetPIP(8, 8, 8),
2800 NWidget(NWID_HORIZONTAL), SetPIP(8, 10, 8),
2801 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_FACE), SetFill(0, 1),
2802 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_QUESTION), SetMinimalSize(240, 0), SetFill(1, 1),
2803 EndContainer(),
2804 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(100, 10, 100),
2805 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_NO), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
2806 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_YES), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
2807 EndContainer(),
2808 EndContainer(),
2809 EndContainer(),
2812 static WindowDesc _buy_company_desc(
2813 WDP_AUTO, nullptr, 0, 0,
2814 WC_BUY_COMPANY, WC_NONE,
2815 WDF_CONSTRUCTION,
2816 _nested_buy_company_widgets, lengthof(_nested_buy_company_widgets)
2820 * Show the query to buy another company.
2821 * @param company The company to buy.
2823 void ShowBuyCompanyDialog(CompanyID company)
2825 AllocateWindowDescFront<BuyCompanyWindow>(&_buy_company_desc, company);