(svn r27724) -Cleanup: Remove pointless usage of IsOpenTTDBaseGRF. System GRFs are...
[openttd.git] / src / company_gui.cpp
blob0be66795835fb5e688551d3ec6791f0950020225
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file company_gui.cpp %Company related GUIs. */
12 #include "stdafx.h"
13 #include "error.h"
14 #include "gui.h"
15 #include "window_gui.h"
16 #include "textbuf_gui.h"
17 #include "viewport_func.h"
18 #include "company_func.h"
19 #include "command_func.h"
20 #include "network/network.h"
21 #include "network/network_gui.h"
22 #include "network/network_func.h"
23 #include "newgrf.h"
24 #include "company_manager_face.h"
25 #include "strings_func.h"
26 #include "date_func.h"
27 #include "widgets/dropdown_type.h"
28 #include "tilehighlight_func.h"
29 #include "company_base.h"
30 #include "core/geometry_func.hpp"
31 #include "object_type.h"
32 #include "rail.h"
33 #include "engine_base.h"
34 #include "window_func.h"
35 #include "road_func.h"
36 #include "water.h"
37 #include "station_func.h"
38 #include "zoom_func.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(),
272 * Window class displaying the company finances.
273 * @todo #money_width should be calculated dynamically.
275 struct CompanyFinancesWindow : Window {
276 static Money max_money; ///< The maximum amount of money a company has had this 'run'
277 bool small; ///< Window is toggled to 'small'.
279 CompanyFinancesWindow(WindowDesc *desc, CompanyID company) : Window(desc)
281 this->small = false;
282 this->CreateNestedTree();
283 this->SetupWidgets();
284 this->FinishInitNested(company);
286 this->owner = (Owner)this->window_number;
289 virtual void SetStringParameters(int widget) const
291 switch (widget) {
292 case WID_CF_CAPTION:
293 SetDParam(0, (CompanyID)this->window_number);
294 SetDParam(1, (CompanyID)this->window_number);
295 break;
297 case WID_CF_MAXLOAN_VALUE:
298 SetDParam(0, _economy.max_loan);
299 break;
301 case WID_CF_INCREASE_LOAN:
302 case WID_CF_REPAY_LOAN:
303 SetDParam(0, LOAN_INTERVAL);
304 break;
308 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
310 int type = _settings_client.gui.expenses_layout;
311 switch (widget) {
312 case WID_CF_EXPS_CATEGORY:
313 size->width = _expenses_list_types[type].GetCategoriesWidth();
314 size->height = _expenses_list_types[type].GetHeight();
315 break;
317 case WID_CF_EXPS_PRICE1:
318 case WID_CF_EXPS_PRICE2:
319 case WID_CF_EXPS_PRICE3:
320 size->height = _expenses_list_types[type].GetHeight();
321 /* FALL THROUGH */
322 case WID_CF_BALANCE_VALUE:
323 case WID_CF_LOAN_VALUE:
324 case WID_CF_TOTAL_VALUE:
325 SetDParamMaxValue(0, CompanyFinancesWindow::max_money);
326 size->width = max(GetStringBoundingBox(STR_FINANCES_NEGATIVE_INCOME).width, GetStringBoundingBox(STR_FINANCES_POSITIVE_INCOME).width) + padding.width;
327 break;
329 case WID_CF_MAXLOAN_GAP:
330 size->height = FONT_HEIGHT_NORMAL;
331 break;
335 virtual void DrawWidget(const Rect &r, int widget) const
337 switch (widget) {
338 case WID_CF_EXPS_CATEGORY:
339 DrawCategories(r);
340 break;
342 case WID_CF_EXPS_PRICE1:
343 case WID_CF_EXPS_PRICE2:
344 case WID_CF_EXPS_PRICE3: {
345 const Company *c = Company::Get((CompanyID)this->window_number);
346 int age = min(_cur_year - c->inaugurated_year, 2);
347 int wid_offset = widget - WID_CF_EXPS_PRICE1;
348 if (wid_offset <= age) {
349 DrawYearColumn(r, _cur_year - (age - wid_offset), c->yearly_expenses + (age - wid_offset));
351 break;
354 case WID_CF_BALANCE_VALUE: {
355 const Company *c = Company::Get((CompanyID)this->window_number);
356 SetDParam(0, c->money);
357 DrawString(r.left, r.right, r.top, STR_FINANCES_TOTAL_CURRENCY, TC_FROMSTRING, SA_RIGHT);
358 break;
361 case WID_CF_LOAN_VALUE: {
362 const Company *c = Company::Get((CompanyID)this->window_number);
363 SetDParam(0, c->current_loan);
364 DrawString(r.left, r.right, r.top, STR_FINANCES_TOTAL_CURRENCY, TC_FROMSTRING, SA_RIGHT);
365 break;
368 case WID_CF_TOTAL_VALUE: {
369 const Company *c = Company::Get((CompanyID)this->window_number);
370 SetDParam(0, c->money - c->current_loan);
371 DrawString(r.left, r.right, r.top, STR_FINANCES_TOTAL_CURRENCY, TC_FROMSTRING, SA_RIGHT);
372 break;
375 case WID_CF_LOAN_LINE:
376 GfxFillRect(r.left, r.top, r.right, r.top, PC_BLACK);
377 break;
382 * Setup the widgets in the nested tree, such that the finances window is displayed properly.
383 * @note After setup, the window must be (re-)initialized.
385 void SetupWidgets()
387 int plane = this->small ? SZSP_NONE : 0;
388 this->GetWidget<NWidgetStacked>(WID_CF_SEL_PANEL)->SetDisplayedPlane(plane);
389 this->GetWidget<NWidgetStacked>(WID_CF_SEL_MAXLOAN)->SetDisplayedPlane(plane);
391 CompanyID company = (CompanyID)this->window_number;
392 plane = (company != _local_company) ? SZSP_NONE : 0;
393 this->GetWidget<NWidgetStacked>(WID_CF_SEL_BUTTONS)->SetDisplayedPlane(plane);
396 virtual void OnPaint()
398 if (!this->IsShaded()) {
399 if (!this->small) {
400 /* Check that the expenses panel height matches the height needed for the layout. */
401 int type = _settings_client.gui.expenses_layout;
402 if (_expenses_list_types[type].GetHeight() != this->GetWidget<NWidgetBase>(WID_CF_EXPS_CATEGORY)->current_y) {
403 this->SetupWidgets();
404 this->ReInit();
405 return;
409 /* Check that the loan buttons are shown only when the user owns the company. */
410 CompanyID company = (CompanyID)this->window_number;
411 int req_plane = (company != _local_company) ? SZSP_NONE : 0;
412 if (req_plane != this->GetWidget<NWidgetStacked>(WID_CF_SEL_BUTTONS)->shown_plane) {
413 this->SetupWidgets();
414 this->ReInit();
415 return;
418 const Company *c = Company::Get(company);
419 this->SetWidgetDisabledState(WID_CF_INCREASE_LOAN, c->current_loan == _economy.max_loan); // Borrow button only shows when there is any more money to loan.
420 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.
423 this->DrawWidgets();
426 virtual void OnClick(Point pt, int widget, int click_count)
428 switch (widget) {
429 case WID_CF_TOGGLE_SIZE: // toggle size
430 this->small = !this->small;
431 this->SetupWidgets();
432 if (this->IsShaded()) {
433 /* Finances window is not resizable, so size hints given during unshading have no effect
434 * on the changed appearance of the window. */
435 this->SetShaded(false);
436 } else {
437 this->ReInit();
439 break;
441 case WID_CF_INCREASE_LOAN: // increase loan
442 DoCommandP(0, 0, _ctrl_pressed, CMD_INCREASE_LOAN | CMD_MSG(STR_ERROR_CAN_T_BORROW_ANY_MORE_MONEY));
443 break;
445 case WID_CF_REPAY_LOAN: // repay loan
446 DoCommandP(0, 0, _ctrl_pressed, CMD_DECREASE_LOAN | CMD_MSG(STR_ERROR_CAN_T_REPAY_LOAN));
447 break;
449 case WID_CF_INFRASTRUCTURE: // show infrastructure details
450 ShowCompanyInfrastructure((CompanyID)this->window_number);
451 break;
455 virtual void OnHundredthTick()
457 const Company *c = Company::Get((CompanyID)this->window_number);
458 if (c->money > CompanyFinancesWindow::max_money) {
459 CompanyFinancesWindow::max_money = max(c->money * 2, CompanyFinancesWindow::max_money * 4);
460 this->SetupWidgets();
461 this->ReInit();
466 /** First conservative estimate of the maximum amount of money */
467 Money CompanyFinancesWindow::max_money = INT32_MAX;
469 static WindowDesc _company_finances_desc(
470 WDP_AUTO, "company_finances", 0, 0,
471 WC_FINANCES, WC_NONE,
473 _nested_company_finances_widgets, lengthof(_nested_company_finances_widgets)
477 * Open the finances window of a company.
478 * @param company Company to show finances of.
479 * @pre is company a valid company.
481 void ShowCompanyFinances(CompanyID company)
483 if (!Company::IsValidID(company)) return;
484 if (BringWindowToFrontById(WC_FINANCES, company)) return;
486 new CompanyFinancesWindow(&_company_finances_desc, company);
489 /* List of colours for the livery window */
490 static const StringID _colour_dropdown[] = {
491 STR_COLOUR_DARK_BLUE,
492 STR_COLOUR_PALE_GREEN,
493 STR_COLOUR_PINK,
494 STR_COLOUR_YELLOW,
495 STR_COLOUR_RED,
496 STR_COLOUR_LIGHT_BLUE,
497 STR_COLOUR_GREEN,
498 STR_COLOUR_DARK_GREEN,
499 STR_COLOUR_BLUE,
500 STR_COLOUR_CREAM,
501 STR_COLOUR_MAUVE,
502 STR_COLOUR_PURPLE,
503 STR_COLOUR_ORANGE,
504 STR_COLOUR_BROWN,
505 STR_COLOUR_GREY,
506 STR_COLOUR_WHITE,
509 /* Association of liveries to livery classes */
510 static const LiveryClass _livery_class[LS_END] = {
511 LC_OTHER,
512 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,
513 LC_ROAD, LC_ROAD,
514 LC_SHIP, LC_SHIP,
515 LC_AIRCRAFT, LC_AIRCRAFT, LC_AIRCRAFT,
516 LC_ROAD, LC_ROAD,
519 class DropDownListColourItem : public DropDownListItem {
520 public:
521 DropDownListColourItem(int result, bool masked) : DropDownListItem(result, masked) {}
523 virtual ~DropDownListColourItem() {}
525 StringID String() const
527 return _colour_dropdown[this->result];
530 uint Height(uint width) const
532 return max(FONT_HEIGHT_NORMAL, ScaleGUITrad(12) + 2);
535 bool Selectable() const
537 return true;
540 void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
542 bool rtl = _current_text_dir == TD_RTL;
543 int height = bottom - top;
544 int icon_y_offset = height / 2;
545 int text_y_offset = (height - FONT_HEIGHT_NORMAL) / 2 + 1;
546 DrawSprite(SPR_VEH_BUS_SIDE_VIEW, PALETTE_RECOLOUR_START + this->result,
547 rtl ? right - 2 - ScaleGUITrad(14) : left + ScaleGUITrad(14) + 2,
548 top + icon_y_offset);
549 DrawString(rtl ? left + 2 : left + ScaleGUITrad(28) + 4,
550 rtl ? right - ScaleGUITrad(28) - 4 : right - 2,
551 top + text_y_offset, this->String(), sel ? TC_WHITE : TC_BLACK);
555 /** Company livery colour scheme window. */
556 struct SelectCompanyLiveryWindow : public Window {
557 private:
558 uint32 sel;
559 LiveryClass livery_class;
560 Dimension square;
561 Dimension box;
562 uint line_height;
564 void ShowColourDropDownMenu(uint32 widget)
566 uint32 used_colours = 0;
567 const Livery *livery;
568 LiveryScheme scheme;
570 /* Disallow other company colours for the primary colour */
571 if (HasBit(this->sel, LS_DEFAULT) && widget == WID_SCL_PRI_COL_DROPDOWN) {
572 const Company *c;
573 FOR_ALL_COMPANIES(c) {
574 if (c->index != _local_company) SetBit(used_colours, c->colour);
578 /* Get the first selected livery to use as the default dropdown item */
579 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
580 if (HasBit(this->sel, scheme)) break;
582 if (scheme == LS_END) scheme = LS_DEFAULT;
583 livery = &Company::Get((CompanyID)this->window_number)->livery[scheme];
585 DropDownList *list = new DropDownList();
586 for (uint i = 0; i < lengthof(_colour_dropdown); i++) {
587 *list->Append() = new DropDownListColourItem(i, HasBit(used_colours, i));
590 ShowDropDownList(this, list, widget == WID_SCL_PRI_COL_DROPDOWN ? livery->colour1 : livery->colour2, widget);
593 public:
594 SelectCompanyLiveryWindow(WindowDesc *desc, CompanyID company) : Window(desc)
596 this->livery_class = LC_OTHER;
597 this->sel = 1;
599 this->square = GetSpriteSize(SPR_SQUARE);
600 this->box = maxdim(GetSpriteSize(SPR_BOX_CHECKED), GetSpriteSize(SPR_BOX_EMPTY));
601 this->line_height = max(max(this->square.height, this->box.height), (uint)FONT_HEIGHT_NORMAL) + 4;
603 this->InitNested(company);
604 this->owner = company;
605 this->LowerWidget(WID_SCL_CLASS_GENERAL);
606 this->InvalidateData(1);
609 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
611 switch (widget) {
612 case WID_SCL_SPACER_DROPDOWN: {
613 /* The matrix widget below needs enough room to print all the schemes. */
614 Dimension d = {0, 0};
615 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
616 d = maxdim(d, GetStringBoundingBox(STR_LIVERY_DEFAULT + scheme));
618 size->width = max(size->width, 5 + this->box.width + d.width + WD_FRAMERECT_RIGHT);
619 break;
622 case WID_SCL_MATRIX: {
623 uint livery_height = 0;
624 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
625 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
626 livery_height++;
629 size->height = livery_height * this->line_height;
630 this->GetWidget<NWidgetCore>(WID_SCL_MATRIX)->widget_data = (livery_height << MAT_ROW_START) | (1 << MAT_COL_START);
631 break;
634 case WID_SCL_SEC_COL_DROPDOWN:
635 if (!_loaded_newgrf_features.has_2CC) {
636 size->width = 0;
637 break;
639 /* FALL THROUGH */
640 case WID_SCL_PRI_COL_DROPDOWN: {
641 int padding = this->square.width + NWidgetScrollbar::GetVerticalDimension().width + 10;
642 for (const StringID *id = _colour_dropdown; id != endof(_colour_dropdown); id++) {
643 size->width = max(size->width, GetStringBoundingBox(*id).width + padding);
645 break;
650 virtual void OnPaint()
652 /* Disable dropdown controls if no scheme is selected */
653 this->SetWidgetDisabledState(WID_SCL_PRI_COL_DROPDOWN, this->sel == 0);
654 this->SetWidgetDisabledState(WID_SCL_SEC_COL_DROPDOWN, this->sel == 0);
656 this->DrawWidgets();
659 virtual void SetStringParameters(int widget) const
661 switch (widget) {
662 case WID_SCL_PRI_COL_DROPDOWN:
663 case WID_SCL_SEC_COL_DROPDOWN: {
664 const Company *c = Company::Get((CompanyID)this->window_number);
665 LiveryScheme scheme = LS_DEFAULT;
667 if (this->sel != 0) {
668 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
669 if (HasBit(this->sel, scheme)) break;
671 if (scheme == LS_END) scheme = LS_DEFAULT;
673 SetDParam(0, STR_COLOUR_DARK_BLUE + ((widget == WID_SCL_PRI_COL_DROPDOWN) ? c->livery[scheme].colour1 : c->livery[scheme].colour2));
674 break;
679 virtual void DrawWidget(const Rect &r, int widget) const
681 if (widget != WID_SCL_MATRIX) return;
683 bool rtl = _current_text_dir == TD_RTL;
685 /* Horizontal coordinates of scheme name column. */
686 const NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SCL_SPACER_DROPDOWN);
687 int sch_left = nwi->pos_x;
688 int sch_right = sch_left + nwi->current_x - 1;
689 /* Horizontal coordinates of first dropdown. */
690 nwi = this->GetWidget<NWidgetBase>(WID_SCL_PRI_COL_DROPDOWN);
691 int pri_left = nwi->pos_x;
692 int pri_right = pri_left + nwi->current_x - 1;
693 /* Horizontal coordinates of second dropdown. */
694 nwi = this->GetWidget<NWidgetBase>(WID_SCL_SEC_COL_DROPDOWN);
695 int sec_left = nwi->pos_x;
696 int sec_right = sec_left + nwi->current_x - 1;
698 int text_left = (rtl ? (uint)WD_FRAMERECT_LEFT : (this->box.width + 5));
699 int text_right = (rtl ? (this->box.width + 5) : (uint)WD_FRAMERECT_RIGHT);
701 int box_offs = (this->line_height - this->box.height) / 2;
702 int square_offs = (this->line_height - this->square.height) / 2 + 1;
703 int text_offs = (this->line_height - FONT_HEIGHT_NORMAL) / 2 + 1;
705 int y = r.top;
706 const Company *c = Company::Get((CompanyID)this->window_number);
707 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
708 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
709 bool sel = HasBit(this->sel, scheme) != 0;
711 /* Optional check box + scheme name. */
712 if (scheme != LS_DEFAULT) {
713 DrawSprite(c->livery[scheme].in_use ? SPR_BOX_CHECKED : SPR_BOX_EMPTY, PAL_NONE, (rtl ? sch_right - (this->box.width + 5) + WD_FRAMERECT_RIGHT : sch_left) + WD_FRAMERECT_LEFT, y + box_offs);
715 DrawString(sch_left + text_left, sch_right - text_right, y + text_offs, STR_LIVERY_DEFAULT + scheme, sel ? TC_WHITE : TC_BLACK);
717 /* Text below the first dropdown. */
718 DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOUR(c->livery[scheme].colour1), (rtl ? pri_right - (this->box.width + 5) + WD_FRAMERECT_RIGHT : pri_left) + WD_FRAMERECT_LEFT, y + square_offs);
719 DrawString(pri_left + text_left, pri_right - text_right, y + text_offs, STR_COLOUR_DARK_BLUE + c->livery[scheme].colour1, sel ? TC_WHITE : TC_GOLD);
721 /* Text below the second dropdown. */
722 if (sec_right > sec_left) { // Second dropdown has non-zero size.
723 DrawSprite(SPR_SQUARE, GENERAL_SPRITE_COLOUR(c->livery[scheme].colour2), (rtl ? sec_right - (this->box.width + 5) + WD_FRAMERECT_RIGHT : sec_left) + WD_FRAMERECT_LEFT, y + square_offs);
724 DrawString(sec_left + text_left, sec_right - text_right, y + text_offs, STR_COLOUR_DARK_BLUE + c->livery[scheme].colour2, sel ? TC_WHITE : TC_GOLD);
727 y += this->line_height;
732 virtual void OnClick(Point pt, int widget, int click_count)
734 switch (widget) {
735 /* Livery Class buttons */
736 case WID_SCL_CLASS_GENERAL:
737 case WID_SCL_CLASS_RAIL:
738 case WID_SCL_CLASS_ROAD:
739 case WID_SCL_CLASS_SHIP:
740 case WID_SCL_CLASS_AIRCRAFT:
741 this->RaiseWidget(this->livery_class + WID_SCL_CLASS_GENERAL);
742 this->livery_class = (LiveryClass)(widget - WID_SCL_CLASS_GENERAL);
743 this->LowerWidget(this->livery_class + WID_SCL_CLASS_GENERAL);
745 /* Select the first item in the list */
746 this->sel = 0;
747 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
748 if (_livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
749 this->sel = 1 << scheme;
750 break;
754 this->ReInit();
755 break;
757 case WID_SCL_PRI_COL_DROPDOWN: // First colour dropdown
758 ShowColourDropDownMenu(WID_SCL_PRI_COL_DROPDOWN);
759 break;
761 case WID_SCL_SEC_COL_DROPDOWN: // Second colour dropdown
762 ShowColourDropDownMenu(WID_SCL_SEC_COL_DROPDOWN);
763 break;
765 case WID_SCL_MATRIX: {
766 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SCL_MATRIX);
767 LiveryScheme j = (LiveryScheme)((pt.y - wid->pos_y) / this->line_height);
769 for (LiveryScheme scheme = LS_BEGIN; scheme <= j; scheme++) {
770 if (_livery_class[scheme] != this->livery_class || !HasBit(_loaded_newgrf_features.used_liveries, scheme)) j++;
771 if (scheme >= LS_END) return;
773 if (j >= LS_END) return;
775 /* If clicking on the left edge, toggle using the livery */
776 if (_current_text_dir == TD_RTL ? pt.x - wid->pos_x > wid->current_x - (this->box.width + 5) : pt.x - wid->pos_x < (this->box.width + 5)) {
777 DoCommandP(0, j | (2 << 8), !Company::Get((CompanyID)this->window_number)->livery[j].in_use, CMD_SET_COMPANY_COLOUR);
780 if (_ctrl_pressed) {
781 ToggleBit(this->sel, j);
782 } else {
783 this->sel = 1 << j;
785 this->SetDirty();
786 break;
791 virtual void OnDropdownSelect(int widget, int index)
793 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
794 /* Changed colour for the selected scheme, or all visible schemes if CTRL is pressed. */
795 if (HasBit(this->sel, scheme) || (_ctrl_pressed && _livery_class[scheme] == this->livery_class && HasBit(_loaded_newgrf_features.used_liveries, scheme))) {
796 DoCommandP(0, scheme | (widget == WID_SCL_PRI_COL_DROPDOWN ? 0 : 256), index, CMD_SET_COMPANY_COLOUR);
802 * Some data on this window has become invalid.
803 * @param data Information about the changed data.
804 * @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.
806 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
808 if (!gui_scope) return;
809 this->SetWidgetsDisabledState(true, WID_SCL_CLASS_RAIL, WID_SCL_CLASS_ROAD, WID_SCL_CLASS_SHIP, WID_SCL_CLASS_AIRCRAFT, WIDGET_LIST_END);
811 bool current_class_valid = this->livery_class == LC_OTHER;
812 if (_settings_client.gui.liveries == LIT_ALL || (_settings_client.gui.liveries == LIT_COMPANY && this->window_number == _local_company)) {
813 for (LiveryScheme scheme = LS_DEFAULT; scheme < LS_END; scheme++) {
814 if (HasBit(_loaded_newgrf_features.used_liveries, scheme)) {
815 if (_livery_class[scheme] == this->livery_class) current_class_valid = true;
816 this->EnableWidget(WID_SCL_CLASS_GENERAL + _livery_class[scheme]);
817 } else {
818 ClrBit(this->sel, scheme);
823 if (!current_class_valid) {
824 Point pt = {0, 0};
825 this->OnClick(pt, WID_SCL_CLASS_GENERAL, 1);
826 } else if (data == 0) {
827 this->ReInit();
832 static const NWidgetPart _nested_select_company_livery_widgets [] = {
833 NWidget(NWID_HORIZONTAL),
834 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
835 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCL_CAPTION), SetDataTip(STR_LIVERY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
836 EndContainer(),
837 NWidget(NWID_HORIZONTAL),
838 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_GENERAL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_LIVERY_GENERAL_TOOLTIP),
839 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_RAIL), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_LIVERY_TRAIN_TOOLTIP),
840 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_ROAD), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRUCKLIST, STR_LIVERY_ROAD_VEHICLE_TOOLTIP),
841 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_SHIP), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SHIPLIST, STR_LIVERY_SHIP_TOOLTIP),
842 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCL_CLASS_AIRCRAFT), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_AIRPLANESLIST, STR_LIVERY_AIRCRAFT_TOOLTIP),
843 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(90, 22), SetFill(1, 1), EndContainer(),
844 EndContainer(),
845 NWidget(NWID_HORIZONTAL),
846 NWidget(WWT_PANEL, COLOUR_GREY, WID_SCL_SPACER_DROPDOWN), SetMinimalSize(150, 12), SetFill(1, 1), EndContainer(),
847 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SCL_PRI_COL_DROPDOWN), SetMinimalSize(125, 12), SetFill(0, 1), SetDataTip(STR_BLACK_STRING, STR_LIVERY_PRIMARY_TOOLTIP),
848 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SCL_SEC_COL_DROPDOWN), SetMinimalSize(125, 12), SetFill(0, 1),
849 SetDataTip(STR_BLACK_STRING, STR_LIVERY_SECONDARY_TOOLTIP),
850 EndContainer(),
851 NWidget(WWT_MATRIX, COLOUR_GREY, WID_SCL_MATRIX), SetMinimalSize(275, 15), SetFill(1, 0), SetMatrixDataTip(1, 1, STR_LIVERY_PANEL_TOOLTIP),
854 static WindowDesc _select_company_livery_desc(
855 WDP_AUTO, "company_livery", 0, 0,
856 WC_COMPANY_COLOUR, WC_NONE,
858 _nested_select_company_livery_widgets, lengthof(_nested_select_company_livery_widgets)
862 * Draws the face of a company manager's face.
863 * @param cmf the company manager's face
864 * @param colour the (background) colour of the gradient
865 * @param x x-position to draw the face
866 * @param y y-position to draw the face
868 void DrawCompanyManagerFace(CompanyManagerFace cmf, int colour, int x, int y)
870 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
872 bool has_moustache = !HasBit(ge, GENDER_FEMALE) && GetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge) != 0;
873 bool has_tie_earring = !HasBit(ge, GENDER_FEMALE) || GetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge) != 0;
874 bool has_glasses = GetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge) != 0;
875 PaletteID pal;
877 /* Modify eye colour palette only if 2 or more valid values exist */
878 if (_cmf_info[CMFV_EYE_COLOUR].valid_values[ge] < 2) {
879 pal = PAL_NONE;
880 } else {
881 switch (GetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR, ge)) {
882 default: NOT_REACHED();
883 case 0: pal = PALETTE_TO_BROWN; break;
884 case 1: pal = PALETTE_TO_BLUE; break;
885 case 2: pal = PALETTE_TO_GREEN; break;
889 /* Draw the gradient (background) */
890 DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOUR(colour), x, y);
892 for (CompanyManagerFaceVariable cmfv = CMFV_CHEEKS; cmfv < CMFV_END; cmfv++) {
893 switch (cmfv) {
894 case CMFV_MOUSTACHE: if (!has_moustache) continue; break;
895 case CMFV_LIPS: // FALL THROUGH
896 case CMFV_NOSE: if (has_moustache) continue; break;
897 case CMFV_TIE_EARRING: if (!has_tie_earring) continue; break;
898 case CMFV_GLASSES: if (!has_glasses) continue; break;
899 default: break;
901 DrawSprite(GetCompanyManagerFaceSprite(cmf, cmfv, ge), (cmfv == CMFV_EYEBROWS) ? pal : PAL_NONE, x, y);
905 /** Nested widget description for the company manager face selection dialog */
906 static const NWidgetPart _nested_select_company_manager_face_widgets[] = {
907 NWidget(NWID_HORIZONTAL),
908 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
909 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCMF_CAPTION), SetDataTip(STR_FACE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
910 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_SCMF_TOGGLE_LARGE_SMALL), SetDataTip(SPR_LARGE_SMALL_WINDOW, STR_FACE_ADVANCED_TOOLTIP),
911 EndContainer(),
912 NWidget(WWT_PANEL, COLOUR_GREY, WID_SCMF_SELECT_FACE),
913 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
914 NWidget(NWID_HORIZONTAL), SetPIP(2, 2, 2),
915 NWidget(NWID_VERTICAL),
916 NWidget(NWID_HORIZONTAL),
917 NWidget(NWID_SPACER), SetFill(1, 0),
918 NWidget(WWT_EMPTY, COLOUR_GREY, WID_SCMF_FACE), SetMinimalSize(92, 119),
919 NWidget(NWID_SPACER), SetFill(1, 0),
920 EndContainer(),
921 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
922 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_RANDOM_NEW_FACE), SetFill(1, 0), SetDataTip(STR_FACE_NEW_FACE_BUTTON, STR_FACE_NEW_FACE_TOOLTIP),
923 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_LOADSAVE), // Load/number/save buttons under the portrait in the advanced view.
924 NWidget(NWID_VERTICAL),
925 NWidget(NWID_SPACER), SetMinimalSize(0, 5), SetFill(0, 1),
926 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_LOAD), SetFill(1, 0), SetDataTip(STR_FACE_LOAD, STR_FACE_LOAD_TOOLTIP),
927 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_FACECODE), SetFill(1, 0), SetDataTip(STR_FACE_FACECODE, STR_FACE_FACECODE_TOOLTIP),
928 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_SAVE), SetFill(1, 0), SetDataTip(STR_FACE_SAVE, STR_FACE_SAVE_TOOLTIP),
929 NWidget(NWID_SPACER), SetMinimalSize(0, 5), SetFill(0, 1),
930 EndContainer(),
931 EndContainer(),
932 EndContainer(),
933 NWidget(NWID_VERTICAL),
934 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON), SetFill(1, 0), SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP),
935 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
936 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_MALEFEMALE), // Simple male/female face setting.
937 NWidget(NWID_VERTICAL),
938 NWidget(NWID_SPACER), SetFill(0, 1),
939 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_MALE), SetFill(1, 0), SetDataTip(STR_FACE_MALE_BUTTON, STR_FACE_MALE_TOOLTIP),
940 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_FEMALE), SetFill(1, 0), SetDataTip(STR_FACE_FEMALE_BUTTON, STR_FACE_FEMALE_TOOLTIP),
941 NWidget(NWID_SPACER), SetFill(0, 1),
942 EndContainer(),
943 EndContainer(),
944 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SCMF_SEL_PARTS), // Advanced face parts setting.
945 NWidget(NWID_VERTICAL),
946 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
947 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
948 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_MALE2), SetFill(1, 0), SetDataTip(STR_FACE_MALE_BUTTON, STR_FACE_MALE_TOOLTIP),
949 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_FEMALE2), SetFill(1, 0), SetDataTip(STR_FACE_FEMALE_BUTTON, STR_FACE_FEMALE_TOOLTIP),
950 EndContainer(),
951 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
952 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
953 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_ETHNICITY_EUR), SetFill(1, 0), SetDataTip(STR_FACE_EUROPEAN, STR_FACE_SELECT_EUROPEAN),
954 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SCMF_ETHNICITY_AFR), SetFill(1, 0), SetDataTip(STR_FACE_AFRICAN, STR_FACE_SELECT_AFRICAN),
955 EndContainer(),
956 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
957 NWidget(NWID_HORIZONTAL),
958 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT), SetFill(1, 0),
959 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAS_MOUSTACHE_EARRING), SetDataTip(STR_EMPTY, STR_FACE_MOUSTACHE_EARRING_TOOLTIP),
960 EndContainer(),
961 NWidget(NWID_HORIZONTAL),
962 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_HAS_GLASSES_TEXT), SetFill(1, 0),
963 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAS_GLASSES), SetDataTip(STR_EMPTY, STR_FACE_GLASSES_TOOLTIP),
964 EndContainer(),
965 NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
966 NWidget(NWID_HORIZONTAL),
967 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_HAIR_TEXT), SetFill(1, 0),
968 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_HAIR_L), SetDataTip(AWV_DECREASE, STR_FACE_HAIR_TOOLTIP),
969 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_HAIR), SetDataTip(STR_EMPTY, STR_FACE_HAIR_TOOLTIP),
970 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_HAIR_R), SetDataTip(AWV_INCREASE, STR_FACE_HAIR_TOOLTIP),
971 EndContainer(),
972 NWidget(NWID_HORIZONTAL),
973 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_EYEBROWS_TEXT), SetFill(1, 0),
974 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYEBROWS_L), SetDataTip(AWV_DECREASE, STR_FACE_EYEBROWS_TOOLTIP),
975 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_EYEBROWS), SetDataTip(STR_EMPTY, STR_FACE_EYEBROWS_TOOLTIP),
976 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYEBROWS_R), SetDataTip(AWV_INCREASE, STR_FACE_EYEBROWS_TOOLTIP),
977 EndContainer(),
978 NWidget(NWID_HORIZONTAL),
979 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_EYECOLOUR_TEXT), SetFill(1, 0),
980 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR_L), SetDataTip(AWV_DECREASE, STR_FACE_EYECOLOUR_TOOLTIP),
981 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR), SetDataTip(STR_EMPTY, STR_FACE_EYECOLOUR_TOOLTIP),
982 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_EYECOLOUR_R), SetDataTip(AWV_INCREASE, STR_FACE_EYECOLOUR_TOOLTIP),
983 EndContainer(),
984 NWidget(NWID_HORIZONTAL),
985 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_GLASSES_TEXT), SetFill(1, 0),
986 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_GLASSES_L), SetDataTip(AWV_DECREASE, STR_FACE_GLASSES_TOOLTIP_2),
987 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_GLASSES), SetDataTip(STR_EMPTY, STR_FACE_GLASSES_TOOLTIP_2),
988 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_GLASSES_R), SetDataTip(AWV_INCREASE, STR_FACE_GLASSES_TOOLTIP_2),
989 EndContainer(),
990 NWidget(NWID_HORIZONTAL),
991 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_NOSE_TEXT), SetFill(1, 0),
992 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_NOSE_L), SetDataTip(AWV_DECREASE, STR_FACE_NOSE_TOOLTIP),
993 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_NOSE), SetDataTip(STR_EMPTY, STR_FACE_NOSE_TOOLTIP),
994 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_NOSE_R), SetDataTip(AWV_INCREASE, STR_FACE_NOSE_TOOLTIP),
995 EndContainer(),
996 NWidget(NWID_HORIZONTAL),
997 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_LIPS_MOUSTACHE_TEXT), SetFill(1, 0),
998 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE_L), SetDataTip(AWV_DECREASE, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
999 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE), SetDataTip(STR_EMPTY, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1000 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_LIPS_MOUSTACHE_R), SetDataTip(AWV_INCREASE, STR_FACE_LIPS_MOUSTACHE_TOOLTIP),
1001 EndContainer(),
1002 NWidget(NWID_HORIZONTAL),
1003 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_CHIN_TEXT), SetFill(1, 0),
1004 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_CHIN_L), SetDataTip(AWV_DECREASE, STR_FACE_CHIN_TOOLTIP),
1005 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_CHIN), SetDataTip(STR_EMPTY, STR_FACE_CHIN_TOOLTIP),
1006 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_CHIN_R), SetDataTip(AWV_INCREASE, STR_FACE_CHIN_TOOLTIP),
1007 EndContainer(),
1008 NWidget(NWID_HORIZONTAL),
1009 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_JACKET_TEXT), SetFill(1, 0),
1010 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_JACKET_L), SetDataTip(AWV_DECREASE, STR_FACE_JACKET_TOOLTIP),
1011 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_JACKET), SetDataTip(STR_EMPTY, STR_FACE_JACKET_TOOLTIP),
1012 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_JACKET_R), SetDataTip(AWV_INCREASE, STR_FACE_JACKET_TOOLTIP),
1013 EndContainer(),
1014 NWidget(NWID_HORIZONTAL),
1015 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_COLLAR_TEXT), SetFill(1, 0),
1016 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_COLLAR_L), SetDataTip(AWV_DECREASE, STR_FACE_COLLAR_TOOLTIP),
1017 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_COLLAR), SetDataTip(STR_EMPTY, STR_FACE_COLLAR_TOOLTIP),
1018 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_COLLAR_R), SetDataTip(AWV_INCREASE, STR_FACE_COLLAR_TOOLTIP),
1019 EndContainer(),
1020 NWidget(NWID_HORIZONTAL),
1021 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SCMF_TIE_EARRING_TEXT), SetFill(1, 0),
1022 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING_L), SetDataTip(AWV_DECREASE, STR_FACE_TIE_EARRING_TOOLTIP),
1023 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING), SetDataTip(STR_EMPTY, STR_FACE_TIE_EARRING_TOOLTIP),
1024 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_SCMF_TIE_EARRING_R), SetDataTip(AWV_INCREASE, STR_FACE_TIE_EARRING_TOOLTIP),
1025 EndContainer(),
1026 NWidget(NWID_SPACER), SetFill(0, 1),
1027 EndContainer(),
1028 EndContainer(),
1029 EndContainer(),
1030 EndContainer(),
1031 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1032 EndContainer(),
1033 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1034 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_CANCEL), SetFill(1, 0), SetDataTip(STR_BUTTON_CANCEL, STR_FACE_CANCEL_TOOLTIP),
1035 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCMF_ACCEPT), SetFill(1, 0), SetDataTip(STR_BUTTON_OK, STR_FACE_OK_TOOLTIP),
1036 EndContainer(),
1039 /** Management class for customizing the face of the company manager. */
1040 class SelectCompanyManagerFaceWindow : public Window
1042 CompanyManagerFace face; ///< company manager face bits
1043 bool advanced; ///< advanced company manager face selection window
1045 GenderEthnicity ge; ///< Gender and ethnicity.
1046 bool is_female; ///< Female face.
1047 bool is_moust_male; ///< Male face with a moustache.
1049 Dimension yesno_dim; ///< Dimension of a yes/no button of a part in the advanced face window.
1050 Dimension number_dim; ///< Dimension of a number widget of a part in the advanced face window.
1052 static const StringID PART_TEXTS_IS_FEMALE[]; ///< Strings depending on #is_female, used to describe parts (2 entries for a part).
1053 static const StringID PART_TEXTS[]; ///< Fixed strings to describe parts of the face.
1056 * Draw dynamic a label to the left of the button and a value in the button
1058 * @param widget_index index of this widget in the window
1059 * @param val the value which will be draw
1060 * @param is_bool_widget is it a bool button
1062 void DrawFaceStringLabel(byte widget_index, uint8 val, bool is_bool_widget) const
1064 StringID str;
1065 const NWidgetCore *nwi_widget = this->GetWidget<NWidgetCore>(widget_index);
1066 if (!nwi_widget->IsDisabled()) {
1067 if (is_bool_widget) {
1068 /* if it a bool button write yes or no */
1069 str = (val != 0) ? STR_FACE_YES : STR_FACE_NO;
1070 } else {
1071 /* else write the value + 1 */
1072 SetDParam(0, val + 1);
1073 str = STR_JUST_INT;
1076 /* Draw the value/bool in white (0xC). If the button clicked adds 1px to x and y text coordinates (IsWindowWidgetLowered()). */
1077 DrawString(nwi_widget->pos_x + nwi_widget->IsLowered(), nwi_widget->pos_x + nwi_widget->current_x - 1 - nwi_widget->IsLowered(),
1078 nwi_widget->pos_y + 1 + nwi_widget->IsLowered(), str, TC_WHITE, SA_HOR_CENTER);
1082 void UpdateData()
1084 this->ge = (GenderEthnicity)GB(this->face, _cmf_info[CMFV_GEN_ETHN].offset, _cmf_info[CMFV_GEN_ETHN].length); // get the gender and ethnicity
1085 this->is_female = HasBit(this->ge, GENDER_FEMALE); // get the gender: 0 == male and 1 == female
1086 this->is_moust_male = !is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge) != 0; // is a male face with moustache
1089 public:
1090 SelectCompanyManagerFaceWindow(WindowDesc *desc, Window *parent) : Window(desc)
1092 this->advanced = false;
1093 this->CreateNestedTree();
1094 this->SelectDisplayPlanes(this->advanced);
1095 this->FinishInitNested(parent->window_number);
1096 this->parent = parent;
1097 this->owner = (Owner)this->window_number;
1098 this->face = Company::Get((CompanyID)this->window_number)->face;
1100 this->UpdateData();
1104 * 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.
1105 * @param advanced Display advanced face management window.
1107 void SelectDisplayPlanes(bool advanced)
1109 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_LOADSAVE)->SetDisplayedPlane(advanced ? 0 : SZSP_NONE);
1110 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_PARTS)->SetDisplayedPlane(advanced ? 0 : SZSP_NONE);
1111 this->GetWidget<NWidgetStacked>(WID_SCMF_SEL_MALEFEMALE)->SetDisplayedPlane(advanced ? SZSP_NONE : 0);
1112 this->GetWidget<NWidgetCore>(WID_SCMF_RANDOM_NEW_FACE)->widget_data = advanced ? STR_FACE_RANDOM : STR_FACE_NEW_FACE_BUTTON;
1114 NWidgetCore *wi = this->GetWidget<NWidgetCore>(WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON);
1115 if (advanced) {
1116 wi->SetDataTip(STR_FACE_SIMPLE, STR_FACE_SIMPLE_TOOLTIP);
1117 } else {
1118 wi->SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP);
1122 virtual void OnInit()
1124 /* Size of the boolean yes/no button. */
1125 Dimension yesno_dim = maxdim(GetStringBoundingBox(STR_FACE_YES), GetStringBoundingBox(STR_FACE_NO));
1126 yesno_dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1127 yesno_dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1128 /* Size of the number button + arrows. */
1129 Dimension number_dim = {0, 0};
1130 for (int val = 1; val <= 12; val++) {
1131 SetDParam(0, val);
1132 number_dim = maxdim(number_dim, GetStringBoundingBox(STR_JUST_INT));
1134 uint arrows_width = GetSpriteSize(SPR_ARROW_LEFT).width + GetSpriteSize(SPR_ARROW_RIGHT).width + 2 * (WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT);
1135 number_dim.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + arrows_width;
1136 number_dim.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1137 /* Compute width of both buttons. */
1138 yesno_dim.width = max(yesno_dim.width, number_dim.width);
1139 number_dim.width = yesno_dim.width - arrows_width;
1141 this->yesno_dim = yesno_dim;
1142 this->number_dim = number_dim;
1145 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1147 switch (widget) {
1148 case WID_SCMF_FACE: {
1149 Dimension face_size = GetSpriteSize(SPR_GRADIENT);
1150 size->width = max(size->width, face_size.width);
1151 size->height = max(size->height, face_size.height);
1152 break;
1155 case WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT:
1156 case WID_SCMF_TIE_EARRING_TEXT: {
1157 int offset = (widget - WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT) * 2;
1158 *size = maxdim(GetStringBoundingBox(PART_TEXTS_IS_FEMALE[offset]), GetStringBoundingBox(PART_TEXTS_IS_FEMALE[offset + 1]));
1159 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1160 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1161 break;
1164 case WID_SCMF_LIPS_MOUSTACHE_TEXT:
1165 *size = maxdim(GetStringBoundingBox(STR_FACE_LIPS), GetStringBoundingBox(STR_FACE_MOUSTACHE));
1166 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1167 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1168 break;
1170 case WID_SCMF_HAS_GLASSES_TEXT:
1171 case WID_SCMF_HAIR_TEXT:
1172 case WID_SCMF_EYEBROWS_TEXT:
1173 case WID_SCMF_EYECOLOUR_TEXT:
1174 case WID_SCMF_GLASSES_TEXT:
1175 case WID_SCMF_NOSE_TEXT:
1176 case WID_SCMF_CHIN_TEXT:
1177 case WID_SCMF_JACKET_TEXT:
1178 case WID_SCMF_COLLAR_TEXT:
1179 *size = GetStringBoundingBox(PART_TEXTS[widget - WID_SCMF_HAS_GLASSES_TEXT]);
1180 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1181 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1182 break;
1184 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1185 case WID_SCMF_HAS_GLASSES:
1186 *size = this->yesno_dim;
1187 break;
1189 case WID_SCMF_EYECOLOUR:
1190 case WID_SCMF_CHIN:
1191 case WID_SCMF_EYEBROWS:
1192 case WID_SCMF_LIPS_MOUSTACHE:
1193 case WID_SCMF_NOSE:
1194 case WID_SCMF_HAIR:
1195 case WID_SCMF_JACKET:
1196 case WID_SCMF_COLLAR:
1197 case WID_SCMF_TIE_EARRING:
1198 case WID_SCMF_GLASSES:
1199 *size = this->number_dim;
1200 break;
1204 virtual void OnPaint()
1206 /* lower the non-selected gender button */
1207 this->SetWidgetsLoweredState(!this->is_female, WID_SCMF_MALE, WID_SCMF_MALE2, WIDGET_LIST_END);
1208 this->SetWidgetsLoweredState( this->is_female, WID_SCMF_FEMALE, WID_SCMF_FEMALE2, WIDGET_LIST_END);
1210 /* advanced company manager face selection window */
1212 /* lower the non-selected ethnicity button */
1213 this->SetWidgetLoweredState(WID_SCMF_ETHNICITY_EUR, !HasBit(this->ge, ETHNICITY_BLACK));
1214 this->SetWidgetLoweredState(WID_SCMF_ETHNICITY_AFR, HasBit(this->ge, ETHNICITY_BLACK));
1217 /* Disable dynamically the widgets which CompanyManagerFaceVariable has less than 2 options
1218 * (or in other words you haven't any choice).
1219 * If the widgets depend on a HAS-variable and this is false the widgets will be disabled, too. */
1221 /* Eye colour buttons */
1222 this->SetWidgetsDisabledState(_cmf_info[CMFV_EYE_COLOUR].valid_values[this->ge] < 2,
1223 WID_SCMF_EYECOLOUR, WID_SCMF_EYECOLOUR_L, WID_SCMF_EYECOLOUR_R, WIDGET_LIST_END);
1225 /* Chin buttons */
1226 this->SetWidgetsDisabledState(_cmf_info[CMFV_CHIN].valid_values[this->ge] < 2,
1227 WID_SCMF_CHIN, WID_SCMF_CHIN_L, WID_SCMF_CHIN_R, WIDGET_LIST_END);
1229 /* Eyebrows buttons */
1230 this->SetWidgetsDisabledState(_cmf_info[CMFV_EYEBROWS].valid_values[this->ge] < 2,
1231 WID_SCMF_EYEBROWS, WID_SCMF_EYEBROWS_L, WID_SCMF_EYEBROWS_R, WIDGET_LIST_END);
1233 /* Lips or (if it a male face with a moustache) moustache buttons */
1234 this->SetWidgetsDisabledState(_cmf_info[this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS].valid_values[this->ge] < 2,
1235 WID_SCMF_LIPS_MOUSTACHE, WID_SCMF_LIPS_MOUSTACHE_L, WID_SCMF_LIPS_MOUSTACHE_R, WIDGET_LIST_END);
1237 /* Nose buttons | male faces with moustache haven't any nose options */
1238 this->SetWidgetsDisabledState(_cmf_info[CMFV_NOSE].valid_values[this->ge] < 2 || this->is_moust_male,
1239 WID_SCMF_NOSE, WID_SCMF_NOSE_L, WID_SCMF_NOSE_R, WIDGET_LIST_END);
1241 /* Hair buttons */
1242 this->SetWidgetsDisabledState(_cmf_info[CMFV_HAIR].valid_values[this->ge] < 2,
1243 WID_SCMF_HAIR, WID_SCMF_HAIR_L, WID_SCMF_HAIR_R, WIDGET_LIST_END);
1245 /* Jacket buttons */
1246 this->SetWidgetsDisabledState(_cmf_info[CMFV_JACKET].valid_values[this->ge] < 2,
1247 WID_SCMF_JACKET, WID_SCMF_JACKET_L, WID_SCMF_JACKET_R, WIDGET_LIST_END);
1249 /* Collar buttons */
1250 this->SetWidgetsDisabledState(_cmf_info[CMFV_COLLAR].valid_values[this->ge] < 2,
1251 WID_SCMF_COLLAR, WID_SCMF_COLLAR_L, WID_SCMF_COLLAR_R, WIDGET_LIST_END);
1253 /* Tie/earring buttons | female faces without earring haven't any earring options */
1254 this->SetWidgetsDisabledState(_cmf_info[CMFV_TIE_EARRING].valid_values[this->ge] < 2 ||
1255 (this->is_female && GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge) == 0),
1256 WID_SCMF_TIE_EARRING, WID_SCMF_TIE_EARRING_L, WID_SCMF_TIE_EARRING_R, WIDGET_LIST_END);
1258 /* Glasses buttons | faces without glasses haven't any glasses options */
1259 this->SetWidgetsDisabledState(_cmf_info[CMFV_GLASSES].valid_values[this->ge] < 2 || GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge) == 0,
1260 WID_SCMF_GLASSES, WID_SCMF_GLASSES_L, WID_SCMF_GLASSES_R, WIDGET_LIST_END);
1262 this->DrawWidgets();
1265 virtual void DrawWidget(const Rect &r, int widget) const
1267 switch (widget) {
1268 case WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT:
1269 case WID_SCMF_TIE_EARRING_TEXT: {
1270 StringID str = PART_TEXTS_IS_FEMALE[(widget - WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT) * 2 + this->is_female];
1271 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_GOLD, SA_RIGHT);
1272 break;
1275 case WID_SCMF_LIPS_MOUSTACHE_TEXT:
1276 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);
1277 break;
1279 case WID_SCMF_HAS_GLASSES_TEXT:
1280 case WID_SCMF_HAIR_TEXT:
1281 case WID_SCMF_EYEBROWS_TEXT:
1282 case WID_SCMF_EYECOLOUR_TEXT:
1283 case WID_SCMF_GLASSES_TEXT:
1284 case WID_SCMF_NOSE_TEXT:
1285 case WID_SCMF_CHIN_TEXT:
1286 case WID_SCMF_JACKET_TEXT:
1287 case WID_SCMF_COLLAR_TEXT:
1288 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);
1289 break;
1292 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1293 if (this->is_female) { // Only for female faces
1294 this->DrawFaceStringLabel(WID_SCMF_HAS_MOUSTACHE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_HAS_TIE_EARRING, this->ge), true);
1295 } else { // Only for male faces
1296 this->DrawFaceStringLabel(WID_SCMF_HAS_MOUSTACHE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_HAS_MOUSTACHE, this->ge), true);
1298 break;
1300 case WID_SCMF_TIE_EARRING:
1301 this->DrawFaceStringLabel(WID_SCMF_TIE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_TIE_EARRING, this->ge), false);
1302 break;
1304 case WID_SCMF_LIPS_MOUSTACHE:
1305 if (this->is_moust_male) { // Only for male faces with moustache
1306 this->DrawFaceStringLabel(WID_SCMF_LIPS_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_MOUSTACHE, this->ge), false);
1307 } else { // Only for female faces or male faces without moustache
1308 this->DrawFaceStringLabel(WID_SCMF_LIPS_MOUSTACHE, GetCompanyManagerFaceBits(this->face, CMFV_LIPS, this->ge), false);
1310 break;
1312 case WID_SCMF_HAS_GLASSES:
1313 this->DrawFaceStringLabel(WID_SCMF_HAS_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge), true );
1314 break;
1316 case WID_SCMF_HAIR:
1317 this->DrawFaceStringLabel(WID_SCMF_HAIR, GetCompanyManagerFaceBits(this->face, CMFV_HAIR, this->ge), false);
1318 break;
1320 case WID_SCMF_EYEBROWS:
1321 this->DrawFaceStringLabel(WID_SCMF_EYEBROWS, GetCompanyManagerFaceBits(this->face, CMFV_EYEBROWS, this->ge), false);
1322 break;
1324 case WID_SCMF_EYECOLOUR:
1325 this->DrawFaceStringLabel(WID_SCMF_EYECOLOUR, GetCompanyManagerFaceBits(this->face, CMFV_EYE_COLOUR, this->ge), false);
1326 break;
1328 case WID_SCMF_GLASSES:
1329 this->DrawFaceStringLabel(WID_SCMF_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_GLASSES, this->ge), false);
1330 break;
1332 case WID_SCMF_NOSE:
1333 this->DrawFaceStringLabel(WID_SCMF_NOSE, GetCompanyManagerFaceBits(this->face, CMFV_NOSE, this->ge), false);
1334 break;
1336 case WID_SCMF_CHIN:
1337 this->DrawFaceStringLabel(WID_SCMF_CHIN, GetCompanyManagerFaceBits(this->face, CMFV_CHIN, this->ge), false);
1338 break;
1340 case WID_SCMF_JACKET:
1341 this->DrawFaceStringLabel(WID_SCMF_JACKET, GetCompanyManagerFaceBits(this->face, CMFV_JACKET, this->ge), false);
1342 break;
1344 case WID_SCMF_COLLAR:
1345 this->DrawFaceStringLabel(WID_SCMF_COLLAR, GetCompanyManagerFaceBits(this->face, CMFV_COLLAR, this->ge), false);
1346 break;
1348 case WID_SCMF_FACE:
1349 DrawCompanyManagerFace(this->face, Company::Get((CompanyID)this->window_number)->colour, r.left, r.top);
1350 break;
1354 virtual void OnClick(Point pt, int widget, int click_count)
1356 switch (widget) {
1357 /* Toggle size, advanced/simple face selection */
1358 case WID_SCMF_TOGGLE_LARGE_SMALL:
1359 case WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON:
1360 this->advanced = !this->advanced;
1361 this->SelectDisplayPlanes(this->advanced);
1362 this->ReInit();
1363 break;
1365 /* OK button */
1366 case WID_SCMF_ACCEPT:
1367 DoCommandP(0, 0, this->face, CMD_SET_COMPANY_MANAGER_FACE);
1368 /* FALL THROUGH */
1370 /* Cancel button */
1371 case WID_SCMF_CANCEL:
1372 delete this;
1373 break;
1375 /* Load button */
1376 case WID_SCMF_LOAD:
1377 this->face = _company_manager_face;
1378 ScaleAllCompanyManagerFaceBits(this->face);
1379 ShowErrorMessage(STR_FACE_LOAD_DONE, INVALID_STRING_ID, WL_INFO);
1380 this->UpdateData();
1381 this->SetDirty();
1382 break;
1384 /* 'Company manager face number' button, view and/or set company manager face number */
1385 case WID_SCMF_FACECODE:
1386 SetDParam(0, this->face);
1387 ShowQueryString(STR_JUST_INT, STR_FACE_FACECODE_CAPTION, 10 + 1, this, CS_NUMERAL, QSF_NONE);
1388 break;
1390 /* Save button */
1391 case WID_SCMF_SAVE:
1392 _company_manager_face = this->face;
1393 ShowErrorMessage(STR_FACE_SAVE_DONE, INVALID_STRING_ID, WL_INFO);
1394 break;
1396 /* Toggle gender (male/female) button */
1397 case WID_SCMF_MALE:
1398 case WID_SCMF_FEMALE:
1399 case WID_SCMF_MALE2:
1400 case WID_SCMF_FEMALE2:
1401 SetCompanyManagerFaceBits(this->face, CMFV_GENDER, this->ge, (widget == WID_SCMF_FEMALE || widget == WID_SCMF_FEMALE2));
1402 ScaleAllCompanyManagerFaceBits(this->face);
1403 this->UpdateData();
1404 this->SetDirty();
1405 break;
1407 /* Randomize face button */
1408 case WID_SCMF_RANDOM_NEW_FACE:
1409 RandomCompanyManagerFaceBits(this->face, this->ge, this->advanced);
1410 this->UpdateData();
1411 this->SetDirty();
1412 break;
1414 /* Toggle ethnicity (european/african) button */
1415 case WID_SCMF_ETHNICITY_EUR:
1416 case WID_SCMF_ETHNICITY_AFR:
1417 SetCompanyManagerFaceBits(this->face, CMFV_ETHNICITY, this->ge, widget - WID_SCMF_ETHNICITY_EUR);
1418 ScaleAllCompanyManagerFaceBits(this->face);
1419 this->UpdateData();
1420 this->SetDirty();
1421 break;
1423 default:
1424 /* Here all buttons from WID_SCMF_HAS_MOUSTACHE_EARRING to WID_SCMF_GLASSES_R are handled.
1425 * First it checks which CompanyManagerFaceVariable is being changed, and then either
1426 * a: invert the value for boolean variables, or
1427 * b: it checks inside of IncreaseCompanyManagerFaceBits() if a left (_L) butten is pressed and then decrease else increase the variable */
1428 if (widget >= WID_SCMF_HAS_MOUSTACHE_EARRING && widget <= WID_SCMF_GLASSES_R) {
1429 CompanyManagerFaceVariable cmfv; // which CompanyManagerFaceVariable shall be edited
1431 if (widget < WID_SCMF_EYECOLOUR_L) { // Bool buttons
1432 switch (widget - WID_SCMF_HAS_MOUSTACHE_EARRING) {
1433 default: NOT_REACHED();
1434 case 0: cmfv = this->is_female ? CMFV_HAS_TIE_EARRING : CMFV_HAS_MOUSTACHE; break; // Has earring/moustache button
1435 case 1: cmfv = CMFV_HAS_GLASSES; break; // Has glasses button
1437 SetCompanyManagerFaceBits(this->face, cmfv, this->ge, !GetCompanyManagerFaceBits(this->face, cmfv, this->ge));
1438 ScaleAllCompanyManagerFaceBits(this->face);
1439 } else { // Value buttons
1440 switch ((widget - WID_SCMF_EYECOLOUR_L) / 3) {
1441 default: NOT_REACHED();
1442 case 0: cmfv = CMFV_EYE_COLOUR; break; // Eye colour buttons
1443 case 1: cmfv = CMFV_CHIN; break; // Chin buttons
1444 case 2: cmfv = CMFV_EYEBROWS; break; // Eyebrows buttons
1445 case 3: cmfv = this->is_moust_male ? CMFV_MOUSTACHE : CMFV_LIPS; break; // Moustache or lips buttons
1446 case 4: cmfv = CMFV_NOSE; break; // Nose buttons
1447 case 5: cmfv = CMFV_HAIR; break; // Hair buttons
1448 case 6: cmfv = CMFV_JACKET; break; // Jacket buttons
1449 case 7: cmfv = CMFV_COLLAR; break; // Collar buttons
1450 case 8: cmfv = CMFV_TIE_EARRING; break; // Tie/earring buttons
1451 case 9: cmfv = CMFV_GLASSES; break; // Glasses buttons
1453 /* 0 == left (_L), 1 == middle or 2 == right (_R) - button click */
1454 IncreaseCompanyManagerFaceBits(this->face, cmfv, this->ge, (((widget - WID_SCMF_EYECOLOUR_L) % 3) != 0) ? 1 : -1);
1456 this->UpdateData();
1457 this->SetDirty();
1459 break;
1463 virtual void OnQueryTextFinished(char *str)
1465 if (str == NULL) return;
1466 /* Set a new company manager face number */
1467 if (!StrEmpty(str)) {
1468 this->face = strtoul(str, NULL, 10);
1469 ScaleAllCompanyManagerFaceBits(this->face);
1470 ShowErrorMessage(STR_FACE_FACECODE_SET, INVALID_STRING_ID, WL_INFO);
1471 this->UpdateData();
1472 this->SetDirty();
1473 } else {
1474 ShowErrorMessage(STR_FACE_FACECODE_ERR, INVALID_STRING_ID, WL_INFO);
1479 /** Both text values of parts of the face that depend on the #is_female boolean value. */
1480 const StringID SelectCompanyManagerFaceWindow::PART_TEXTS_IS_FEMALE[] = {
1481 STR_FACE_MOUSTACHE, STR_FACE_EARRING, // WID_SCMF_HAS_MOUSTACHE_EARRING_TEXT
1482 STR_FACE_TIE, STR_FACE_EARRING, // WID_SCMF_TIE_EARRING_TEXT
1485 /** Textual names for parts of the face. */
1486 const StringID SelectCompanyManagerFaceWindow::PART_TEXTS[] = {
1487 STR_FACE_GLASSES, // WID_SCMF_HAS_GLASSES_TEXT
1488 STR_FACE_HAIR, // WID_SCMF_HAIR_TEXT
1489 STR_FACE_EYEBROWS, // WID_SCMF_EYEBROWS_TEXT
1490 STR_FACE_EYECOLOUR, // WID_SCMF_EYECOLOUR_TEXT
1491 STR_FACE_GLASSES, // WID_SCMF_GLASSES_TEXT
1492 STR_FACE_NOSE, // WID_SCMF_NOSE_TEXT
1493 STR_FACE_CHIN, // WID_SCMF_CHIN_TEXT
1494 STR_FACE_JACKET, // WID_SCMF_JACKET_TEXT
1495 STR_FACE_COLLAR, // WID_SCMF_COLLAR_TEXT
1498 /** Company manager face selection window description */
1499 static WindowDesc _select_company_manager_face_desc(
1500 WDP_AUTO, "company_face", 0, 0,
1501 WC_COMPANY_MANAGER_FACE, WC_NONE,
1502 WDF_CONSTRUCTION,
1503 _nested_select_company_manager_face_widgets, lengthof(_nested_select_company_manager_face_widgets)
1507 * Open the simple/advanced company manager face selection window
1509 * @param parent the parent company window
1510 * @param adv simple or advanced face selection window
1511 * @param top previous top position of the window
1512 * @param left previous left position of the window
1514 static void DoSelectCompanyManagerFace(Window *parent)
1516 if (!Company::IsValidID((CompanyID)parent->window_number)) return;
1518 if (BringWindowToFrontById(WC_COMPANY_MANAGER_FACE, parent->window_number)) return;
1519 new SelectCompanyManagerFaceWindow(&_select_company_manager_face_desc, parent);
1522 static const NWidgetPart _nested_company_infrastructure_widgets[] = {
1523 NWidget(NWID_HORIZONTAL),
1524 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1525 NWidget(WWT_CAPTION, COLOUR_GREY, WID_CI_CAPTION), SetDataTip(STR_COMPANY_INFRASTRUCTURE_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1526 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1527 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1528 EndContainer(),
1529 NWidget(WWT_PANEL, COLOUR_GREY),
1530 NWidget(NWID_VERTICAL), SetPIP(WD_FRAMERECT_TOP, 4, WD_FRAMETEXT_BOTTOM),
1531 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1532 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_RAIL_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1533 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_RAIL_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1534 EndContainer(),
1535 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1536 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_ROAD_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1537 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_ROAD_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1538 EndContainer(),
1539 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1540 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_WATER_DESC), SetMinimalTextLines(2, 0), SetFill(1, 0),
1541 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_WATER_COUNT), SetMinimalTextLines(2, 0), SetFill(0, 1),
1542 EndContainer(),
1543 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1544 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_STATION_DESC), SetMinimalTextLines(3, 0), SetFill(1, 0),
1545 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_STATION_COUNT), SetMinimalTextLines(3, 0), SetFill(0, 1),
1546 EndContainer(),
1547 NWidget(NWID_HORIZONTAL), SetPIP(2, 4, 2),
1548 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TOTAL_DESC), SetFill(1, 0),
1549 NWidget(WWT_EMPTY, COLOUR_GREY, WID_CI_TOTAL), SetFill(0, 1),
1550 EndContainer(),
1551 EndContainer(),
1552 EndContainer(),
1556 * Window with detailed information about the company's infrastructure.
1558 struct CompanyInfrastructureWindow : Window
1560 RailTypes railtypes; ///< Valid railtypes.
1561 RoadTypes roadtypes; ///< Valid roadtypes.
1563 uint total_width; ///< String width of the total cost line.
1565 CompanyInfrastructureWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1567 this->UpdateRailRoadTypes();
1569 this->InitNested(window_number);
1570 this->owner = (Owner)this->window_number;
1573 void UpdateRailRoadTypes()
1575 this->railtypes = RAILTYPES_NONE;
1576 this->roadtypes = ROADTYPES_ROAD; // Road is always available.
1578 /* Find the used railtypes. */
1579 Engine *e;
1580 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
1581 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1583 this->railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1586 /* Get the date introduced railtypes as well. */
1587 this->railtypes = AddDateIntroducedRailTypes(this->railtypes, MAX_DAY);
1589 /* Tram is only visible when there will be a tram. */
1590 FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
1591 if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
1592 if (!HasBit(e->info.misc_flags, EF_ROAD_TRAM)) continue;
1594 this->roadtypes |= ROADTYPES_TRAM;
1595 break;
1599 /** Get total infrastructure maintenance cost. */
1600 Money GetTotalMaintenanceCost() const
1602 const Company *c = Company::Get((CompanyID)this->window_number);
1603 Money total;
1605 uint32 rail_total = c->infrastructure.GetRailTotal();
1606 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
1607 if (HasBit(this->railtypes, rt)) total += RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total);
1609 total += SignalMaintenanceCost(c->infrastructure.signal);
1611 if (HasBit(this->roadtypes, ROADTYPE_ROAD)) total += RoadMaintenanceCost(ROADTYPE_ROAD, c->infrastructure.road[ROADTYPE_ROAD]);
1612 if (HasBit(this->roadtypes, ROADTYPE_TRAM)) total += RoadMaintenanceCost(ROADTYPE_TRAM, c->infrastructure.road[ROADTYPE_TRAM]);
1614 total += CanalMaintenanceCost(c->infrastructure.water);
1615 total += StationMaintenanceCost(c->infrastructure.station);
1616 total += AirportMaintenanceCost(c->index);
1618 return total;
1621 virtual void SetStringParameters(int widget) const
1623 switch (widget) {
1624 case WID_CI_CAPTION:
1625 SetDParam(0, (CompanyID)this->window_number);
1626 break;
1630 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1632 const Company *c = Company::Get((CompanyID)this->window_number);
1634 switch (widget) {
1635 case WID_CI_RAIL_DESC: {
1636 uint lines = 1;
1638 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT).width);
1640 for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) {
1641 if (HasBit(this->railtypes, rt)) {
1642 lines++;
1643 SetDParam(0, GetRailTypeInfo(rt)->strings.name);
1644 size->width = max(size->width, GetStringBoundingBox(STR_WHITE_STRING).width + WD_FRAMERECT_LEFT);
1647 if (this->railtypes != RAILTYPES_NONE) {
1648 lines++;
1649 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS).width + WD_FRAMERECT_LEFT);
1652 size->height = max(size->height, lines * FONT_HEIGHT_NORMAL);
1653 break;
1656 case WID_CI_ROAD_DESC: {
1657 uint lines = 1;
1659 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT).width);
1661 if (HasBit(this->roadtypes, ROADTYPE_ROAD)) {
1662 lines++;
1663 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD).width + WD_FRAMERECT_LEFT);
1665 if (HasBit(this->roadtypes, ROADTYPE_TRAM)) {
1666 lines++;
1667 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_TRAMWAY).width + WD_FRAMERECT_LEFT);
1670 size->height = max(size->height, lines * FONT_HEIGHT_NORMAL);
1671 break;
1674 case WID_CI_WATER_DESC:
1675 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT).width);
1676 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS).width + WD_FRAMERECT_LEFT);
1677 break;
1679 case WID_CI_STATION_DESC:
1680 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT).width);
1681 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS).width + WD_FRAMERECT_LEFT);
1682 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS).width + WD_FRAMERECT_LEFT);
1683 break;
1685 case WID_CI_RAIL_COUNT:
1686 case WID_CI_ROAD_COUNT:
1687 case WID_CI_WATER_COUNT:
1688 case WID_CI_STATION_COUNT:
1689 case WID_CI_TOTAL: {
1690 /* Find the maximum count that is displayed. */
1691 uint32 max_val = 1000; // Some random number to reserve enough space.
1692 Money max_cost = 10000; // Some random number to reserve enough space.
1693 uint32 rail_total = c->infrastructure.GetRailTotal();
1694 for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) {
1695 max_val = max(max_val, c->infrastructure.rail[rt]);
1696 max_cost = max(max_cost, RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
1698 max_val = max(max_val, c->infrastructure.signal);
1699 max_cost = max(max_cost, SignalMaintenanceCost(c->infrastructure.signal));
1700 for (RoadType rt = ROADTYPE_BEGIN; rt < ROADTYPE_END; rt++) {
1701 max_val = max(max_val, c->infrastructure.road[rt]);
1702 max_cost = max(max_cost, RoadMaintenanceCost(rt, c->infrastructure.road[rt]));
1704 max_val = max(max_val, c->infrastructure.water);
1705 max_cost = max(max_cost, CanalMaintenanceCost(c->infrastructure.water));
1706 max_val = max(max_val, c->infrastructure.station);
1707 max_cost = max(max_cost, StationMaintenanceCost(c->infrastructure.station));
1708 max_val = max(max_val, c->infrastructure.airport);
1709 max_cost = max(max_cost, AirportMaintenanceCost(c->index));
1711 SetDParamMaxValue(0, max_val);
1712 uint count_width = GetStringBoundingBox(STR_WHITE_COMMA).width + 20; // Reserve some wiggle room
1714 if (_settings_game.economy.infrastructure_maintenance) {
1715 SetDParamMaxValue(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
1716 this->total_width = GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL).width + 20;
1717 size->width = max(size->width, this->total_width);
1719 SetDParamMaxValue(0, max_cost * 12); // Convert to per year
1720 count_width += max(this->total_width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL).width);
1723 size->width = max(size->width, count_width);
1725 /* Set height of the total line. */
1726 if (widget == WID_CI_TOTAL) {
1727 size->height = _settings_game.economy.infrastructure_maintenance ? max(size->height, EXP_LINESPACE + FONT_HEIGHT_NORMAL) : 0;
1729 break;
1735 * Helper for drawing the counts line.
1736 * @param r The bounds to draw in.
1737 * @param y The y position to draw at.
1738 * @param count The count to show on this line.
1739 * @param monthly_cost The monthly costs.
1741 void DrawCountLine(const Rect &r, int &y, int count, Money monthly_cost) const
1743 SetDParam(0, count);
1744 DrawString(r.left, r.right, y += FONT_HEIGHT_NORMAL, STR_WHITE_COMMA, TC_FROMSTRING, SA_RIGHT);
1746 if (_settings_game.economy.infrastructure_maintenance) {
1747 SetDParam(0, monthly_cost * 12); // Convert to per year
1748 int left = _current_text_dir == TD_RTL ? r.right - this->total_width : r.left;
1749 DrawString(left, left + this->total_width, y, STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL, TC_FROMSTRING, SA_RIGHT);
1753 virtual void DrawWidget(const Rect &r, int widget) const
1755 const Company *c = Company::Get((CompanyID)this->window_number);
1756 int y = r.top;
1758 int offs_left = _current_text_dir == TD_LTR ? WD_FRAMERECT_LEFT : 0;
1759 int offs_right = _current_text_dir == TD_LTR ? 0 : WD_FRAMERECT_LEFT;
1761 switch (widget) {
1762 case WID_CI_RAIL_DESC:
1763 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT);
1765 if (this->railtypes != RAILTYPES_NONE) {
1766 /* Draw name of each valid railtype. */
1767 RailType rt;
1768 FOR_ALL_SORTED_RAILTYPES(rt) {
1769 if (HasBit(this->railtypes, rt)) {
1770 SetDParam(0, GetRailTypeInfo(rt)->strings.name);
1771 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_WHITE_STRING);
1774 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS);
1775 } else {
1776 /* No valid railtype. */
1777 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
1780 break;
1782 case WID_CI_RAIL_COUNT: {
1783 /* Draw infrastructure count for each valid railtype. */
1784 uint32 rail_total = c->infrastructure.GetRailTotal();
1785 RailType rt;
1786 FOR_ALL_SORTED_RAILTYPES(rt) {
1787 if (HasBit(this->railtypes, rt)) {
1788 this->DrawCountLine(r, y, c->infrastructure.rail[rt], RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
1791 if (this->railtypes != RAILTYPES_NONE) {
1792 this->DrawCountLine(r, y, c->infrastructure.signal, SignalMaintenanceCost(c->infrastructure.signal));
1794 break;
1797 case WID_CI_ROAD_DESC:
1798 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT);
1800 if (this->roadtypes != ROADTYPES_NONE) {
1801 if (HasBit(this->roadtypes, ROADTYPE_ROAD)) DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD);
1802 if (HasBit(this->roadtypes, ROADTYPE_TRAM)) DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_TRAMWAY);
1803 } else {
1804 /* No valid roadtypes. */
1805 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
1808 break;
1810 case WID_CI_ROAD_COUNT:
1811 if (HasBit(this->roadtypes, ROADTYPE_ROAD)) {
1812 this->DrawCountLine(r, y, c->infrastructure.road[ROADTYPE_ROAD], RoadMaintenanceCost(ROADTYPE_ROAD, c->infrastructure.road[ROADTYPE_ROAD]));
1814 if (HasBit(this->roadtypes, ROADTYPE_TRAM)) {
1815 this->DrawCountLine(r, y, c->infrastructure.road[ROADTYPE_TRAM], RoadMaintenanceCost(ROADTYPE_TRAM, c->infrastructure.road[ROADTYPE_TRAM]));
1817 break;
1819 case WID_CI_WATER_DESC:
1820 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT);
1821 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS);
1822 break;
1824 case WID_CI_WATER_COUNT:
1825 this->DrawCountLine(r, y, c->infrastructure.water, CanalMaintenanceCost(c->infrastructure.water));
1826 break;
1828 case WID_CI_TOTAL:
1829 if (_settings_game.economy.infrastructure_maintenance) {
1830 int left = _current_text_dir == TD_RTL ? r.right - this->total_width : r.left;
1831 GfxFillRect(left, y, left + this->total_width, y, PC_WHITE);
1832 y += EXP_LINESPACE;
1833 SetDParam(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
1834 DrawString(left, left + this->total_width, y, STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL, TC_FROMSTRING, SA_RIGHT);
1836 break;
1838 case WID_CI_STATION_DESC:
1839 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT);
1840 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS);
1841 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS);
1842 break;
1844 case WID_CI_STATION_COUNT:
1845 this->DrawCountLine(r, y, c->infrastructure.station, StationMaintenanceCost(c->infrastructure.station));
1846 this->DrawCountLine(r, y, c->infrastructure.airport, AirportMaintenanceCost(c->index));
1847 break;
1852 * Some data on this window has become invalid.
1853 * @param data Information about the changed data.
1854 * @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.
1856 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1858 if (!gui_scope) return;
1860 this->UpdateRailRoadTypes();
1861 this->ReInit();
1865 static WindowDesc _company_infrastructure_desc(
1866 WDP_AUTO, "company_infrastructure", 0, 0,
1867 WC_COMPANY_INFRASTRUCTURE, WC_NONE,
1869 _nested_company_infrastructure_widgets, lengthof(_nested_company_infrastructure_widgets)
1873 * Open the infrastructure window of a company.
1874 * @param company Company to show infrastructure of.
1876 static void ShowCompanyInfrastructure(CompanyID company)
1878 if (!Company::IsValidID(company)) return;
1879 AllocateWindowDescFront<CompanyInfrastructureWindow>(&_company_infrastructure_desc, company);
1882 static const NWidgetPart _nested_company_widgets[] = {
1883 NWidget(NWID_HORIZONTAL),
1884 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1885 NWidget(WWT_CAPTION, COLOUR_GREY, WID_C_CAPTION), SetDataTip(STR_COMPANY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1886 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1887 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1888 EndContainer(),
1889 NWidget(WWT_PANEL, COLOUR_GREY),
1890 NWidget(NWID_HORIZONTAL), SetPIP(4, 6, 4),
1891 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1892 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE), SetMinimalSize(92, 119), SetFill(1, 0),
1893 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE_TITLE), SetFill(1, 1), SetMinimalTextLines(2, 0),
1894 EndContainer(),
1895 NWidget(NWID_VERTICAL),
1896 NWidget(NWID_HORIZONTAL),
1897 NWidget(NWID_VERTICAL), SetPIP(4, 5, 5),
1898 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INAUGURATION), SetDataTip(STR_COMPANY_VIEW_INAUGURATED_TITLE, STR_NULL), SetFill(1, 0),
1899 NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
1900 NWidget(WWT_LABEL, COLOUR_GREY, WID_C_DESC_COLOUR_SCHEME), SetDataTip(STR_COMPANY_VIEW_COLOUR_SCHEME_TITLE, STR_NULL),
1901 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_COLOUR_SCHEME_EXAMPLE), SetMinimalSize(30, 0), SetFill(0, 1),
1902 NWidget(NWID_SPACER), SetFill(1, 0),
1903 EndContainer(),
1904 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
1905 NWidget(NWID_VERTICAL),
1906 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_VEHICLE), SetDataTip(STR_COMPANY_VIEW_VEHICLES_TITLE, STR_NULL),
1907 NWidget(NWID_SPACER), SetFill(0, 1),
1908 EndContainer(),
1909 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_VEHICLE_COUNTS), SetMinimalTextLines(4, 0),
1910 NWidget(NWID_SPACER), SetFill(1, 0),
1911 EndContainer(),
1912 EndContainer(),
1913 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1914 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_VIEW_BUILD_HQ),
1915 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),
1916 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),
1917 EndContainer(),
1918 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_RELOCATE),
1919 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_C_RELOCATE_HQ), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_RELOCATE_HQ, STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS),
1920 NWidget(NWID_SPACER), SetMinimalSize(90, 0),
1921 EndContainer(),
1922 NWidget(NWID_SPACER), SetFill(0, 1),
1923 EndContainer(),
1924 EndContainer(),
1925 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_COMPANY_VALUE), SetDataTip(STR_COMPANY_VIEW_COMPANY_VALUE, STR_NULL), SetFill(1, 0),
1926 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1927 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
1928 NWidget(NWID_VERTICAL),
1929 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE, STR_NULL),
1930 NWidget(NWID_SPACER), SetFill(0, 1),
1931 EndContainer(),
1932 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_INFRASTRUCTURE_COUNTS), SetMinimalTextLines(5, 0), SetFill(1, 0),
1933 NWidget(NWID_VERTICAL),
1934 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_VIEW_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE_BUTTON, STR_COMPANY_VIEW_INFRASTRUCTURE_TOOLTIP),
1935 NWidget(NWID_SPACER), SetFill(0, 1), SetMinimalSize(90, 0),
1936 EndContainer(),
1937 EndContainer(),
1938 EndContainer(),
1939 NWidget(NWID_HORIZONTAL),
1940 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_DESC_OWNERS),
1941 NWidget(NWID_VERTICAL), SetPIP(5, 5, 4),
1942 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_OWNERS), SetMinimalTextLines(3, 0),
1943 NWidget(NWID_SPACER), SetFill(0, 1),
1944 EndContainer(),
1945 EndContainer(),
1946 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1947 NWidget(NWID_SPACER), SetMinimalSize(90, 0), SetFill(0, 1),
1948 /* Multi player buttons. */
1949 NWidget(NWID_HORIZONTAL),
1950 NWidget(WWT_EMPTY, COLOUR_GREY, WID_C_HAS_PASSWORD),
1951 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_MULTIPLAYER),
1952 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_PASSWORD), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_COMPANY_VIEW_PASSWORD_TOOLTIP),
1953 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_JOIN), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_JOIN, STR_COMPANY_VIEW_JOIN_TOOLTIP),
1954 EndContainer(),
1955 EndContainer(),
1956 EndContainer(),
1957 EndContainer(),
1958 EndContainer(),
1959 EndContainer(),
1960 EndContainer(),
1961 /* Button bars at the bottom. */
1962 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_BUTTONS),
1963 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1964 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),
1965 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),
1966 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),
1967 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),
1968 EndContainer(),
1969 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1970 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),
1971 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),
1972 EndContainer(),
1973 EndContainer(),
1976 int GetAmountOwnedBy(const Company *c, Owner owner)
1978 return (c->share_owners[0] == owner) +
1979 (c->share_owners[1] == owner) +
1980 (c->share_owners[2] == owner) +
1981 (c->share_owners[3] == owner);
1984 /** Strings for the company vehicle counts */
1985 static const StringID _company_view_vehicle_count_strings[] = {
1986 STR_COMPANY_VIEW_TRAINS, STR_COMPANY_VIEW_ROAD_VEHICLES, STR_COMPANY_VIEW_SHIPS, STR_COMPANY_VIEW_AIRCRAFT
1990 * Window with general information about a company
1992 struct CompanyWindow : Window
1994 CompanyWidgets query_widget;
1996 /** Display planes in the company window. */
1997 enum CompanyWindowPlanes {
1998 /* Display planes of the #WID_C_SELECT_MULTIPLAYER selection widget. */
1999 CWP_MP_C_PWD = 0, ///< Display the company password button.
2000 CWP_MP_C_JOIN, ///< Display the join company button.
2002 /* Display planes of the #WID_C_SELECT_VIEW_BUILD_HQ selection widget. */
2003 CWP_VB_VIEW = 0, ///< Display the view button
2004 CWP_VB_BUILD, ///< Display the build button
2006 /* Display planes of the #WID_C_SELECT_RELOCATE selection widget. */
2007 CWP_RELOCATE_SHOW = 0, ///< Show the relocate HQ button.
2008 CWP_RELOCATE_HIDE, ///< Hide the relocate HQ button.
2010 /* Display planes of the #WID_C_SELECT_BUTTONS selection widget. */
2011 CWP_BUTTONS_LOCAL = 0, ///< Buttons of the local company.
2012 CWP_BUTTONS_OTHER, ///< Buttons of the other companies.
2015 CompanyWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
2017 this->InitNested(window_number);
2018 this->owner = (Owner)this->window_number;
2019 this->OnInvalidateData();
2022 virtual void OnPaint()
2024 const Company *c = Company::Get((CompanyID)this->window_number);
2025 bool local = this->window_number == _local_company;
2027 if (!this->IsShaded()) {
2028 bool reinit = false;
2030 /* Button bar selection. */
2031 int plane = local ? CWP_BUTTONS_LOCAL : CWP_BUTTONS_OTHER;
2032 NWidgetStacked *wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_BUTTONS);
2033 if (plane != wi->shown_plane) {
2034 wi->SetDisplayedPlane(plane);
2035 this->InvalidateData();
2036 return;
2039 /* Build HQ button handling. */
2040 plane = (local && c->location_of_HQ == INVALID_TILE) ? CWP_VB_BUILD : CWP_VB_VIEW;
2041 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_VIEW_BUILD_HQ);
2042 if (plane != wi->shown_plane) {
2043 wi->SetDisplayedPlane(plane);
2044 this->SetDirty();
2045 return;
2048 this->SetWidgetDisabledState(WID_C_VIEW_HQ, c->location_of_HQ == INVALID_TILE);
2050 /* Enable/disable 'Relocate HQ' button. */
2051 plane = (!local || c->location_of_HQ == INVALID_TILE) ? CWP_RELOCATE_HIDE : CWP_RELOCATE_SHOW;
2052 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_RELOCATE);
2053 if (plane != wi->shown_plane) {
2054 wi->SetDisplayedPlane(plane);
2055 this->SetDirty();
2056 return;
2059 /* Owners of company */
2060 plane = SZSP_HORIZONTAL;
2061 for (uint i = 0; i < lengthof(c->share_owners); i++) {
2062 if (c->share_owners[i] != INVALID_COMPANY) {
2063 plane = 0;
2064 break;
2067 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_DESC_OWNERS);
2068 if (plane != wi->shown_plane) {
2069 wi->SetDisplayedPlane(plane);
2070 reinit = true;
2073 /* Multiplayer buttons. */
2074 plane = ((!_networking) ? (int)SZSP_NONE : (int)(local ? CWP_MP_C_PWD : CWP_MP_C_JOIN));
2075 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_MULTIPLAYER);
2076 if (plane != wi->shown_plane) {
2077 wi->SetDisplayedPlane(plane);
2078 reinit = true;
2080 this->SetWidgetDisabledState(WID_C_COMPANY_JOIN, c->is_ai);
2082 if (reinit) {
2083 this->ReInit();
2084 return;
2088 this->DrawWidgets();
2091 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2093 switch (widget) {
2094 case WID_C_FACE: {
2095 Dimension face_size = GetSpriteSize(SPR_GRADIENT);
2096 size->width = max(size->width, face_size.width);
2097 size->height = max(size->height, face_size.height);
2098 break;
2101 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2102 Point offset;
2103 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2104 d.width -= offset.x;
2105 d.height -= offset.y;
2106 *size = maxdim(*size, d);
2107 break;
2110 case WID_C_DESC_COMPANY_VALUE:
2111 SetDParam(0, INT64_MAX); // Arguably the maximum company value
2112 size->width = GetStringBoundingBox(STR_COMPANY_VIEW_COMPANY_VALUE).width;
2113 break;
2115 case WID_C_DESC_VEHICLE_COUNTS:
2116 SetDParamMaxValue(0, 5000); // Maximum number of vehicles
2117 for (uint i = 0; i < lengthof(_company_view_vehicle_count_strings); i++) {
2118 size->width = max(size->width, GetStringBoundingBox(_company_view_vehicle_count_strings[i]).width);
2120 break;
2122 case WID_C_DESC_INFRASTRUCTURE_COUNTS:
2123 SetDParamMaxValue(0, UINT_MAX);
2124 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL).width);
2125 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD).width);
2126 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_WATER).width);
2127 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_STATION).width);
2128 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT).width);
2129 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_NONE).width);
2130 break;
2132 case WID_C_DESC_OWNERS: {
2133 const Company *c2;
2135 FOR_ALL_COMPANIES(c2) {
2136 SetDParamMaxValue(0, 75);
2137 SetDParam(1, c2->index);
2139 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_SHARES_OWNED_BY).width);
2141 break;
2144 #ifdef ENABLE_NETWORK
2145 case WID_C_HAS_PASSWORD:
2146 *size = maxdim(*size, GetSpriteSize(SPR_LOCK));
2147 break;
2148 #endif /* ENABLE_NETWORK */
2152 virtual void DrawWidget(const Rect &r, int widget) const
2154 const Company *c = Company::Get((CompanyID)this->window_number);
2155 switch (widget) {
2156 case WID_C_FACE:
2157 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
2158 break;
2160 case WID_C_FACE_TITLE:
2161 SetDParam(0, c->index);
2162 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_COMPANY_VIEW_PRESIDENT_MANAGER_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
2163 break;
2165 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2166 Point offset;
2167 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2168 d.height -= offset.y;
2169 DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOUR(c->index), r.left - offset.x, (r.top + r.bottom - d.height) / 2 - offset.y);
2170 break;
2173 case WID_C_DESC_VEHICLE_COUNTS: {
2174 uint amounts[4];
2175 amounts[0] = c->group_all[VEH_TRAIN].num_vehicle;
2176 amounts[1] = c->group_all[VEH_ROAD].num_vehicle;
2177 amounts[2] = c->group_all[VEH_SHIP].num_vehicle;
2178 amounts[3] = c->group_all[VEH_AIRCRAFT].num_vehicle;
2180 int y = r.top;
2181 if (amounts[0] + amounts[1] + amounts[2] + amounts[3] == 0) {
2182 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_VEHICLES_NONE);
2183 } else {
2184 assert_compile(lengthof(amounts) == lengthof(_company_view_vehicle_count_strings));
2186 for (uint i = 0; i < lengthof(amounts); i++) {
2187 if (amounts[i] != 0) {
2188 SetDParam(0, amounts[i]);
2189 DrawString(r.left, r.right, y, _company_view_vehicle_count_strings[i]);
2190 y += FONT_HEIGHT_NORMAL;
2194 break;
2197 case WID_C_DESC_INFRASTRUCTURE_COUNTS: {
2198 uint y = r.top;
2200 /* Collect rail and road counts. */
2201 uint rail_pices = c->infrastructure.signal;
2202 uint road_pieces = 0;
2203 for (uint i = 0; i < lengthof(c->infrastructure.rail); i++) rail_pices += c->infrastructure.rail[i];
2204 for (uint i = 0; i < lengthof(c->infrastructure.road); i++) road_pieces += c->infrastructure.road[i];
2206 if (rail_pices == 0 && road_pieces == 0 && c->infrastructure.water == 0 && c->infrastructure.station == 0 && c->infrastructure.airport == 0) {
2207 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
2208 } else {
2209 if (rail_pices != 0) {
2210 SetDParam(0, rail_pices);
2211 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL);
2212 y += FONT_HEIGHT_NORMAL;
2214 if (road_pieces != 0) {
2215 SetDParam(0, road_pieces);
2216 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD);
2217 y += FONT_HEIGHT_NORMAL;
2219 if (c->infrastructure.water != 0) {
2220 SetDParam(0, c->infrastructure.water);
2221 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_WATER);
2222 y += FONT_HEIGHT_NORMAL;
2224 if (c->infrastructure.station != 0) {
2225 SetDParam(0, c->infrastructure.station);
2226 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_STATION);
2227 y += FONT_HEIGHT_NORMAL;
2229 if (c->infrastructure.airport != 0) {
2230 SetDParam(0, c->infrastructure.airport);
2231 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT);
2235 break;
2238 case WID_C_DESC_OWNERS: {
2239 const Company *c2;
2240 uint y = r.top;
2242 FOR_ALL_COMPANIES(c2) {
2243 uint amt = GetAmountOwnedBy(c, c2->index);
2244 if (amt != 0) {
2245 SetDParam(0, amt * 25);
2246 SetDParam(1, c2->index);
2248 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_SHARES_OWNED_BY);
2249 y += FONT_HEIGHT_NORMAL;
2252 break;
2255 #ifdef ENABLE_NETWORK
2256 case WID_C_HAS_PASSWORD:
2257 if (_networking && NetworkCompanyIsPassworded(c->index)) {
2258 DrawSprite(SPR_LOCK, PAL_NONE, r.left, r.top);
2260 break;
2261 #endif /* ENABLE_NETWORK */
2265 virtual void SetStringParameters(int widget) const
2267 switch (widget) {
2268 case WID_C_CAPTION:
2269 SetDParam(0, (CompanyID)this->window_number);
2270 SetDParam(1, (CompanyID)this->window_number);
2271 break;
2273 case WID_C_DESC_INAUGURATION:
2274 SetDParam(0, Company::Get((CompanyID)this->window_number)->inaugurated_year);
2275 break;
2277 case WID_C_DESC_COMPANY_VALUE:
2278 SetDParam(0, CalculateCompanyValue(Company::Get((CompanyID)this->window_number)));
2279 break;
2283 virtual void OnClick(Point pt, int widget, int click_count)
2285 switch (widget) {
2286 case WID_C_NEW_FACE: DoSelectCompanyManagerFace(this); break;
2288 case WID_C_COLOUR_SCHEME:
2289 if (BringWindowToFrontById(WC_COMPANY_COLOUR, this->window_number)) break;
2290 new SelectCompanyLiveryWindow(&_select_company_livery_desc, (CompanyID)this->window_number);
2291 break;
2293 case WID_C_PRESIDENT_NAME:
2294 this->query_widget = WID_C_PRESIDENT_NAME;
2295 SetDParam(0, this->window_number);
2296 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);
2297 break;
2299 case WID_C_COMPANY_NAME:
2300 this->query_widget = WID_C_COMPANY_NAME;
2301 SetDParam(0, this->window_number);
2302 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);
2303 break;
2305 case WID_C_VIEW_HQ: {
2306 TileIndex tile = Company::Get((CompanyID)this->window_number)->location_of_HQ;
2307 if (_ctrl_pressed) {
2308 ShowExtraViewPortWindow(tile);
2309 } else {
2310 ScrollMainWindowToTile(tile);
2312 break;
2315 case WID_C_BUILD_HQ:
2316 if ((byte)this->window_number != _local_company) return;
2317 if (this->IsWidgetLowered(WID_C_BUILD_HQ)) {
2318 ResetObjectToPlace();
2319 this->RaiseButtons();
2320 break;
2322 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2323 SetTileSelectSize(2, 2);
2324 this->LowerWidget(WID_C_BUILD_HQ);
2325 this->SetWidgetDirty(WID_C_BUILD_HQ);
2326 break;
2328 case WID_C_RELOCATE_HQ:
2329 if (this->IsWidgetLowered(WID_C_RELOCATE_HQ)) {
2330 ResetObjectToPlace();
2331 this->RaiseButtons();
2332 break;
2334 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2335 SetTileSelectSize(2, 2);
2336 this->LowerWidget(WID_C_RELOCATE_HQ);
2337 this->SetWidgetDirty(WID_C_RELOCATE_HQ);
2338 break;
2340 case WID_C_VIEW_INFRASTRUCTURE:
2341 ShowCompanyInfrastructure((CompanyID)this->window_number);
2342 break;
2344 case WID_C_BUY_SHARE:
2345 DoCommandP(0, this->window_number, 0, CMD_BUY_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_BUY_25_SHARE_IN_THIS));
2346 break;
2348 case WID_C_SELL_SHARE:
2349 DoCommandP(0, this->window_number, 0, CMD_SELL_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_SELL_25_SHARE_IN));
2350 break;
2352 #ifdef ENABLE_NETWORK
2353 case WID_C_COMPANY_PASSWORD:
2354 if (this->window_number == _local_company) ShowNetworkCompanyPasswordWindow(this);
2355 break;
2357 case WID_C_COMPANY_JOIN: {
2358 this->query_widget = WID_C_COMPANY_JOIN;
2359 CompanyID company = (CompanyID)this->window_number;
2360 if (_network_server) {
2361 NetworkServerDoMove(CLIENT_ID_SERVER, company);
2362 MarkWholeScreenDirty();
2363 } else if (NetworkCompanyIsPassworded(company)) {
2364 /* ask for the password */
2365 ShowQueryString(STR_EMPTY, STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION, NETWORK_PASSWORD_LENGTH, this, CS_ALPHANUMERAL, QSF_NONE);
2366 } else {
2367 /* just send the join command */
2368 NetworkClientRequestMove(company);
2370 break;
2372 #endif /* ENABLE_NETWORK */
2376 virtual void OnHundredthTick()
2378 /* redraw the window every now and then */
2379 this->SetDirty();
2382 virtual void OnPlaceObject(Point pt, TileIndex tile)
2384 if (DoCommandP(tile, OBJECT_HQ, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_COMPANY_HEADQUARTERS))) {
2385 ResetObjectToPlace();
2386 this->RaiseButtons();
2390 virtual void OnPlaceObjectAbort()
2392 this->RaiseButtons();
2395 virtual void OnQueryTextFinished(char *str)
2397 if (str == NULL) return;
2399 switch (this->query_widget) {
2400 default: NOT_REACHED();
2402 case WID_C_PRESIDENT_NAME:
2403 DoCommandP(0, 0, 0, CMD_RENAME_PRESIDENT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_PRESIDENT), NULL, str);
2404 break;
2406 case WID_C_COMPANY_NAME:
2407 DoCommandP(0, 0, 0, CMD_RENAME_COMPANY | CMD_MSG(STR_ERROR_CAN_T_CHANGE_COMPANY_NAME), NULL, str);
2408 break;
2410 #ifdef ENABLE_NETWORK
2411 case WID_C_COMPANY_JOIN:
2412 NetworkClientRequestMove((CompanyID)this->window_number, str);
2413 break;
2414 #endif /* ENABLE_NETWORK */
2420 * Some data on this window has become invalid.
2421 * @param data Information about the changed data.
2422 * @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.
2424 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
2426 if (this->window_number == _local_company) return;
2428 if (_settings_game.economy.allow_shares) { // Shares are allowed
2429 const Company *c = Company::Get(this->window_number);
2431 /* If all shares are owned by someone (none by nobody), disable buy button */
2432 this->SetWidgetDisabledState(WID_C_BUY_SHARE, GetAmountOwnedBy(c, INVALID_OWNER) == 0 ||
2433 /* Only 25% left to buy. If the company is human, disable buying it up.. TODO issues! */
2434 (GetAmountOwnedBy(c, INVALID_OWNER) == 1 && !c->is_ai) ||
2435 /* Spectators cannot do anything of course */
2436 _local_company == COMPANY_SPECTATOR);
2438 /* If the company doesn't own any shares, disable sell button */
2439 this->SetWidgetDisabledState(WID_C_SELL_SHARE, (GetAmountOwnedBy(c, _local_company) == 0) ||
2440 /* Spectators cannot do anything of course */
2441 _local_company == COMPANY_SPECTATOR);
2442 } else { // Shares are not allowed, disable buy/sell buttons
2443 this->DisableWidget(WID_C_BUY_SHARE);
2444 this->DisableWidget(WID_C_SELL_SHARE);
2449 static WindowDesc _company_desc(
2450 WDP_AUTO, "company", 0, 0,
2451 WC_COMPANY, WC_NONE,
2453 _nested_company_widgets, lengthof(_nested_company_widgets)
2457 * Show the window with the overview of the company.
2458 * @param company The company to show the window for.
2460 void ShowCompany(CompanyID company)
2462 if (!Company::IsValidID(company)) return;
2464 AllocateWindowDescFront<CompanyWindow>(&_company_desc, company);
2468 * Redraw all windows with company infrastructure counts.
2469 * @param company The company to redraw the windows of.
2471 void DirtyCompanyInfrastructureWindows(CompanyID company)
2473 SetWindowDirty(WC_COMPANY, company);
2474 SetWindowDirty(WC_COMPANY_INFRASTRUCTURE, company);
2477 struct BuyCompanyWindow : Window {
2478 BuyCompanyWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
2480 this->InitNested(window_number);
2483 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2485 switch (widget) {
2486 case WID_BC_FACE:
2487 *size = GetSpriteSize(SPR_GRADIENT);
2488 break;
2490 case WID_BC_QUESTION:
2491 const Company *c = Company::Get((CompanyID)this->window_number);
2492 SetDParam(0, c->index);
2493 SetDParam(1, c->bankrupt_value);
2494 size->height = GetStringHeight(STR_BUY_COMPANY_MESSAGE, size->width);
2495 break;
2499 virtual void SetStringParameters(int widget) const
2501 switch (widget) {
2502 case WID_BC_CAPTION:
2503 SetDParam(0, STR_COMPANY_NAME);
2504 SetDParam(1, Company::Get((CompanyID)this->window_number)->index);
2505 break;
2509 virtual void DrawWidget(const Rect &r, int widget) const
2511 switch (widget) {
2512 case WID_BC_FACE: {
2513 const Company *c = Company::Get((CompanyID)this->window_number);
2514 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
2515 break;
2518 case WID_BC_QUESTION: {
2519 const Company *c = Company::Get((CompanyID)this->window_number);
2520 SetDParam(0, c->index);
2521 SetDParam(1, c->bankrupt_value);
2522 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_BUY_COMPANY_MESSAGE, TC_FROMSTRING, SA_CENTER);
2523 break;
2528 virtual void OnClick(Point pt, int widget, int click_count)
2530 switch (widget) {
2531 case WID_BC_NO:
2532 delete this;
2533 break;
2535 case WID_BC_YES:
2536 DoCommandP(0, this->window_number, 0, CMD_BUY_COMPANY | CMD_MSG(STR_ERROR_CAN_T_BUY_COMPANY));
2537 break;
2542 static const NWidgetPart _nested_buy_company_widgets[] = {
2543 NWidget(NWID_HORIZONTAL),
2544 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
2545 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_BC_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2546 EndContainer(),
2547 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
2548 NWidget(NWID_VERTICAL), SetPIP(8, 8, 8),
2549 NWidget(NWID_HORIZONTAL), SetPIP(8, 10, 8),
2550 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_FACE), SetFill(0, 1),
2551 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_QUESTION), SetMinimalSize(240, 0), SetFill(1, 1),
2552 EndContainer(),
2553 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(100, 10, 100),
2554 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_NO), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
2555 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_YES), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
2556 EndContainer(),
2557 EndContainer(),
2558 EndContainer(),
2561 static WindowDesc _buy_company_desc(
2562 WDP_AUTO, NULL, 0, 0,
2563 WC_BUY_COMPANY, WC_NONE,
2564 WDF_CONSTRUCTION,
2565 _nested_buy_company_widgets, lengthof(_nested_buy_company_widgets)
2569 * Show the query to buy another company.
2570 * @param company The company to buy.
2572 void ShowBuyCompanyDialog(CompanyID company)
2574 AllocateWindowDescFront<BuyCompanyWindow>(&_buy_company_desc, company);