OpenTTD: src/company_gui.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

company_gui.cpp
Go to the documentation of this file.
1 /* $Id: company_gui.cpp 27134 2015-02-01 20:54:24Z frosch $ */
2
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 */
9
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"
39
40 #include "widgets/company_widget.h"
41
42 #include "safeguards.h"
43
44
46 static const uint EXP_LINESPACE = 2;
47 static const uint EXP_BLOCKSPACE = 10;
48
49 static void DoSelectCompanyManagerFace(Window *parent);
50 static void ShowCompanyInfrastructure(CompanyID company);
51
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,
67 };
68
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,
87 };
88
90 struct ExpensesList {
91 const ExpensesType *et;
92 const uint length;
93 const uint num_subtotals;
94
95 ExpensesList(ExpensesType *et, int length, int num_subtotals) : et(et), length(length), num_subtotals(num_subtotals)
96 {
97 }
98
99 uint GetHeight() const
100 {
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;
103 }
104
106 uint GetCategoriesWidth() const
107 {
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;
116 }
117 } else {
118 width = max(width, GetStringBoundingBox(STR_FINANCES_SECTION_CONSTRUCTION + et).width);
119 }
120 }
121 return width;
122 }
123 };
124
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),
128 };
129
135 static void DrawCategories(const Rect &r)
136 {
137 int y = r.top;
138
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;
141
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;
152 }
153 }
154
155 DrawString(r.left, r.right, y + EXP_LINESPACE, STR_FINANCES_TOTAL_CAPTION, TC_FROMSTRING, SA_RIGHT);
156 }
157
165 static void DrawPrice(Money amount, int left, int right, int top)
166 {
167 StringID str = STR_FINANCES_NEGATIVE_INCOME;
168 if (amount < 0) {
169 amount = -amount;
170 str++;
171 }
172 SetDParam(0, amount);
173 DrawString(left, right, top, str, TC_FROMSTRING, SA_RIGHT);
174 }
175
183 static void DrawYearColumn(const Rect &r, int year, const Money (*tbl)[EXPENSES_END])
184 {
185 int y = r.top;
186
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;
190
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;
209 }
210 }
211
212 GfxFillRect(r.left, y, r.right, y, PC_BLACK);
213 y += EXP_LINESPACE;
214 DrawPrice(sum, r.left, r.right, y);
215 }
216
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(),
269 };
270
275 struct CompanyFinancesWindow : Window {
276 static Money max_money;
277 bool small;
278
279 CompanyFinancesWindow(WindowDesc *desc, CompanyID company) : Window(desc)
280 {
281 this->small = false;
282 this->CreateNestedTree();
283 this->SetupWidgets();
284 this->FinishInitNested(company);
285
286 this->owner = (Owner)this->window_number;
287 }
288
289 virtual void SetStringParameters(int widget) const
290 {
291 switch (widget) {
292 case WID_CF_CAPTION:
293 SetDParam(0, (CompanyID)this->window_number);
294 SetDParam(1, (CompanyID)this->window_number);
295 break;
296
297 case WID_CF_MAXLOAN_VALUE:
298 SetDParam(0, _economy.max_loan);
299 break;
300
301 case WID_CF_INCREASE_LOAN:
302 case WID_CF_REPAY_LOAN:
303 SetDParam(0, LOAN_INTERVAL);
304 break;
305 }
306 }
307
308 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
309 {
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;
316
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;
328
329 case WID_CF_MAXLOAN_GAP:
330 size->height = FONT_HEIGHT_NORMAL;
331 break;
332 }
333 }
334
335 virtual void DrawWidget(const Rect &r, int widget) const
336 {
337 switch (widget) {
338 case WID_CF_EXPS_CATEGORY:
339 DrawCategories(r);
340 break;
341
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));
350 }
351 break;
352 }
353
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;
359 }
360
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;
366 }
367
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;
373 }
374
375 case WID_CF_LOAN_LINE:
376 GfxFillRect(r.left, r.top, r.right, r.top, PC_BLACK);
377 break;
378 }
379 }
380
385 void SetupWidgets()
386 {
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);
390
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);
394 }
395
396 virtual void OnPaint()
397 {
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;
406 }
407 }
408
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;
416 }
417
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.
421 }
422
423 this->DrawWidgets();
424 }
425
426 virtual void OnClick(Point pt, int widget, int click_count)
427 {
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();
438 }
439 break;
440
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;
444
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;
448
449 case WID_CF_INFRASTRUCTURE: // show infrastructure details
450 ShowCompanyInfrastructure((CompanyID)this->window_number);
451 break;
452 }
453 }
454
455 virtual void OnHundredthTick()
456 {
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();
462 }
463 }
464 };
465
467 Money CompanyFinancesWindow::max_money = INT32_MAX;
468
469 static WindowDesc _company_finances_desc(
470 WDP_AUTO, "company_finances", 0, 0,
471 WC_FINANCES, WC_NONE,
472 0,
473 _nested_company_finances_widgets, lengthof(_nested_company_finances_widgets)
474 );
475
481 void ShowCompanyFinances(CompanyID company)
482 {
483 if (!Company::IsValidID(company)) return;
484 if (BringWindowToFrontById(WC_FINANCES, company)) return;
485
486 new CompanyFinancesWindow(&_company_finances_desc, company);
487 }
488
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,
507 };
508
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,
517 };
518
519 class DropDownListColourItem : public DropDownListItem {
520 public:
521 DropDownListColourItem(int result, bool masked) : DropDownListItem(result, masked) {}
522
523 virtual ~DropDownListColourItem() {}
524
525 StringID String() const
526 {
527 return _colour_dropdown[this->result];
528 }
529
530 uint Height(uint width) const
531 {
532 return max(FONT_HEIGHT_NORMAL, ScaleGUITrad(12) + 2);
533 }
534
535 bool Selectable() const
536 {
537 return true;
538 }
539
540 void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
541 {
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);
552 }
553 };
554
556 struct SelectCompanyLiveryWindow : public Window {
557 private:
558 uint32 sel;
559 LiveryClass livery_class;
560 Dimension square;
561 Dimension box;
562 uint line_height;
563
564 void ShowColourDropDownMenu(uint32 widget)
565 {
566 uint32 used_colours = 0;
567 const Livery *livery;
568 LiveryScheme scheme;
569
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);
575 }
576 }
577
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;
581 }
582 if (scheme == LS_END) scheme = LS_DEFAULT;
583 livery = &Company::Get((CompanyID)this->window_number)->livery[scheme];
584
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));
588 }
589
590 ShowDropDownList(this, list, widget == WID_SCL_PRI_COL_DROPDOWN ? livery->colour1 : livery->colour2, widget);
591 }
592
593 public:
594 SelectCompanyLiveryWindow(WindowDesc *desc, CompanyID company) : Window(desc)
595 {
596 this->livery_class = LC_OTHER;
597 this->sel = 1;
598
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;
602
603 this->InitNested(company);
604 this->owner = company;
605 this->LowerWidget(WID_SCL_CLASS_GENERAL);
606 this->InvalidateData(1);
607 }
608
609 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
610 {
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));
617 }
618 size->width = max(size->width, 5 + this->box.width + d.width + WD_FRAMERECT_RIGHT);
619 break;
620 }
621
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++;
627 }
628 }
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;
632 }
633
634 case WID_SCL_SEC_COL_DROPDOWN:
635 if (!_loaded_newgrf_features.has_2CC) {
636 size->width = 0;
637 break;
638 }
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);
644 }
645 break;
646 }
647 }
648 }
649
650 virtual void OnPaint()
651 {
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);
655
656 this->DrawWidgets();
657 }
658
659 virtual void SetStringParameters(int widget) const
660 {
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;
666
667 if (this->sel != 0) {
668 for (scheme = LS_BEGIN; scheme < LS_END; scheme++) {
669 if (HasBit(this->sel, scheme)) break;
670 }
671 if (scheme == LS_END) scheme = LS_DEFAULT;
672 }
673 SetDParam(0, STR_COLOUR_DARK_BLUE + ((widget == WID_SCL_PRI_COL_DROPDOWN) ? c->livery[scheme].colour1 : c->livery[scheme].colour2));
674 break;
675 }
676 }
677 }
678
679 virtual void DrawWidget(const Rect &r, int widget) const
680 {
681 if (widget != WID_SCL_MATRIX) return;
682
683 bool rtl = _current_text_dir == TD_RTL;
684
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;
697
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);
700
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;
704
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;
710
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);
714 }
715 DrawString(sch_left + text_left, sch_right - text_right, y + text_offs, STR_LIVERY_DEFAULT + scheme, sel ? TC_WHITE : TC_BLACK);
716
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);
720
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);
725 }
726
727 y += this->line_height;
728 }
729 }
730 }
731
732 virtual void OnClick(Point pt, int widget, int click_count)
733 {
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);
744
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;
751 }
752 }
753
754 this->ReInit();
755 break;
756
757 case WID_SCL_PRI_COL_DROPDOWN: // First colour dropdown
758 ShowColourDropDownMenu(WID_SCL_PRI_COL_DROPDOWN);
759 break;
760
761 case WID_SCL_SEC_COL_DROPDOWN: // Second colour dropdown
762 ShowColourDropDownMenu(WID_SCL_SEC_COL_DROPDOWN);
763 break;
764
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);
768
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;
772 }
773 if (j >= LS_END) return;
774
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);
778 }
779
780 if (_ctrl_pressed) {
781 ToggleBit(this->sel, j);
782 } else {
783 this->sel = 1 << j;
784 }
785 this->SetDirty();
786 break;
787 }
788 }
789 }
790
791 virtual void OnDropdownSelect(int widget, int index)
792 {
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);
797 }
798 }
799 }
800
806 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
807 {
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);
810
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);
819 }
820 }
821 }
822
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();
828 }
829 }
830 };
831
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),
852 };
853
854 static WindowDesc _select_company_livery_desc(
855 WDP_AUTO, "company_livery", 0, 0,
856 WC_COMPANY_COLOUR, WC_NONE,
857 0,
858 _nested_select_company_livery_widgets, lengthof(_nested_select_company_livery_widgets)
859 );
860
868 void DrawCompanyManagerFace(CompanyManagerFace cmf, int colour, int x, int y)
869 {
870 GenderEthnicity ge = (GenderEthnicity)GetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN, GE_WM);
871
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;
876
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;
886 }
887 }
888
889 /* Draw the gradient (background) */
890 DrawSprite(SPR_GRADIENT, GENERAL_SPRITE_COLOUR(colour), x, y);
891
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;
900 }
901 DrawSprite(GetCompanyManagerFaceSprite(cmf, cmfv, ge), (cmfv == CMFV_EYEBROWS) ? pal : PAL_NONE, x, y);
902 }
903 }
904
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(),
1037 };
1038
1040 class SelectCompanyManagerFaceWindow : public Window
1041 {
1042 CompanyManagerFace face;
1043 bool advanced;
1044
1045 GenderEthnicity ge;
1046 bool is_female;
1047 bool is_moust_male;
1048
1049 Dimension yesno_dim;
1050 Dimension number_dim;
1051
1052 static const StringID PART_TEXTS_IS_FEMALE[];
1053 static const StringID PART_TEXTS[];
1054
1062 void DrawFaceStringLabel(byte widget_index, uint8 val, bool is_bool_widget) const
1063 {
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;
1074 }
1075
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);
1079 }
1080 }
1081
1082 void UpdateData()
1083 {
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
1087 }
1088
1089 public:
1090 SelectCompanyManagerFaceWindow(WindowDesc *desc, Window *parent) : Window(desc)
1091 {
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;
1099
1100 this->UpdateData();
1101 }
1102
1107 void SelectDisplayPlanes(bool advanced)
1108 {
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;
1113
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);
1119 }
1120 }
1121
1122 virtual void OnInit()
1123 {
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));
1133 }
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;
1140
1141 this->yesno_dim = yesno_dim;
1142 this->number_dim = number_dim;
1143 }
1144
1145 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1146 {
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;
1153 }
1154
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;
1162 }
1163
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;
1169
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;
1183
1184 case WID_SCMF_HAS_MOUSTACHE_EARRING:
1185 case WID_SCMF_HAS_GLASSES:
1186 *size = this->yesno_dim;
1187 break;
1188
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;
1201 }
1202 }
1203
1204 virtual void OnPaint()
1205 {
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);
1209
1210 /* advanced company manager face selection window */
1211
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));
1215
1216
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. */
1220
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);
1224
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);
1228
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);
1232
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);
1236
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);
1240
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);
1244
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);
1248
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);
1252
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);
1257
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);
1261
1262 this->DrawWidgets();
1263 }
1264
1265 virtual void DrawWidget(const Rect &r, int widget) const
1266 {
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;
1273 }
1274
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;
1278
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;
1290
1291
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);
1297 }
1298 break;
1299
1300 case WID_SCMF_TIE_EARRING:
1301 this->DrawFaceStringLabel(WID_SCMF_TIE_EARRING, GetCompanyManagerFaceBits(this->face, CMFV_TIE_EARRING, this->ge), false);
1302 break;
1303
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);
1309 }
1310 break;
1311
1312 case WID_SCMF_HAS_GLASSES:
1313 this->DrawFaceStringLabel(WID_SCMF_HAS_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_HAS_GLASSES, this->ge), true );
1314 break;
1315
1316 case WID_SCMF_HAIR:
1317 this->DrawFaceStringLabel(WID_SCMF_HAIR, GetCompanyManagerFaceBits(this->face, CMFV_HAIR, this->ge), false);
1318 break;
1319
1320 case WID_SCMF_EYEBROWS:
1321 this->DrawFaceStringLabel(WID_SCMF_EYEBROWS, GetCompanyManagerFaceBits(this->face, CMFV_EYEBROWS, this->ge), false);
1322 break;
1323
1324 case WID_SCMF_EYECOLOUR:
1325 this->DrawFaceStringLabel(WID_SCMF_EYECOLOUR, GetCompanyManagerFaceBits(this->face, CMFV_EYE_COLOUR, this->ge), false);
1326 break;
1327
1328 case WID_SCMF_GLASSES:
1329 this->DrawFaceStringLabel(WID_SCMF_GLASSES, GetCompanyManagerFaceBits(this->face, CMFV_GLASSES, this->ge), false);
1330 break;
1331
1332 case WID_SCMF_NOSE:
1333 this->DrawFaceStringLabel(WID_SCMF_NOSE, GetCompanyManagerFaceBits(this->face, CMFV_NOSE, this->ge), false);
1334 break;
1335
1336 case WID_SCMF_CHIN:
1337 this->DrawFaceStringLabel(WID_SCMF_CHIN, GetCompanyManagerFaceBits(this->face, CMFV_CHIN, this->ge), false);
1338 break;
1339
1340 case WID_SCMF_JACKET:
1341 this->DrawFaceStringLabel(WID_SCMF_JACKET, GetCompanyManagerFaceBits(this->face, CMFV_JACKET, this->ge), false);
1342 break;
1343
1344 case WID_SCMF_COLLAR:
1345 this->DrawFaceStringLabel(WID_SCMF_COLLAR, GetCompanyManagerFaceBits(this->face, CMFV_COLLAR, this->ge), false);
1346 break;
1347
1348 case WID_SCMF_FACE:
1349 DrawCompanyManagerFace(this->face, Company::Get((CompanyID)this->window_number)->colour, r.left, r.top);
1350 break;
1351 }
1352 }
1353
1354 virtual void OnClick(Point pt, int widget, int click_count)
1355 {
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;
1364
1365 /* OK button */
1366 case WID_SCMF_ACCEPT:
1367 DoCommandP(0, 0, this->face, CMD_SET_COMPANY_MANAGER_FACE);
1368 /* FALL THROUGH */
1369
1370 /* Cancel button */
1371 case WID_SCMF_CANCEL:
1372 delete this;
1373 break;
1374
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;
1383
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;
1389
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;
1395
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;
1406
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;
1413
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;
1422
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
1430
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
1436 }
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
1452 }
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);
1455 }
1456 this->UpdateData();
1457 this->SetDirty();
1458 }
1459 break;
1460 }
1461 }
1462
1463 virtual void OnQueryTextFinished(char *str)
1464 {
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);
1475 }
1476 }
1477 };
1478
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
1483 };
1484
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
1496 };
1497
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)
1504 );
1505
1514 static void DoSelectCompanyManagerFace(Window *parent)
1515 {
1516 if (!Company::IsValidID((CompanyID)parent->window_number)) return;
1517
1518 if (BringWindowToFrontById(WC_COMPANY_MANAGER_FACE, parent->window_number)) return;
1519 new SelectCompanyManagerFaceWindow(&_select_company_manager_face_desc, parent);
1520 }
1521
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(),
1553 };
1554
1558 struct CompanyInfrastructureWindow : Window
1559 {
1560 RailTypes railtypes;
1561 RoadTypes roadtypes;
1562
1563 uint total_width;
1564
1565 CompanyInfrastructureWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1566 {
1567 this->UpdateRailRoadTypes();
1568
1569 this->InitNested(window_number);
1570 this->owner = (Owner)this->window_number;
1571 }
1572
1573 void UpdateRailRoadTypes()
1574 {
1575 this->railtypes = RAILTYPES_NONE;
1576 this->roadtypes = ROADTYPES_ROAD; // Road is always available.
1577
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;
1582
1583 this->railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
1584 }
1585
1586 /* Get the date introduced railtypes as well. */
1587 this->railtypes = AddDateIntroducedRailTypes(this->railtypes, MAX_DAY);
1588
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;
1593
1594 this->roadtypes |= ROADTYPES_TRAM;
1595 break;
1596 }
1597 }
1598
1600 Money GetTotalMaintenanceCost() const
1601 {
1602 const Company *c = Company::Get((CompanyID)this->window_number);
1603 Money total;
1604
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);
1608 }
1609 total += SignalMaintenanceCost(c->infrastructure.signal);
1610
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]);
1613
1614 total += CanalMaintenanceCost(c->infrastructure.water);
1615 total += StationMaintenanceCost(c->infrastructure.station);
1616 total += AirportMaintenanceCost(c->index);
1617
1618 return total;
1619 }
1620
1621 virtual void SetStringParameters(int widget) const
1622 {
1623 switch (widget) {
1624 case WID_CI_CAPTION:
1625 SetDParam(0, (CompanyID)this->window_number);
1626 break;
1627 }
1628 }
1629
1630 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1631 {
1632 const Company *c = Company::Get((CompanyID)this->window_number);
1633
1634 switch (widget) {
1635 case WID_CI_RAIL_DESC: {
1636 uint lines = 1;
1637
1638 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT).width);
1639
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);
1645 }
1646 }
1647 if (this->railtypes != RAILTYPES_NONE) {
1648 lines++;
1649 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS).width + WD_FRAMERECT_LEFT);
1650 }
1651
1652 size->height = max(size->height, lines * FONT_HEIGHT_NORMAL);
1653 break;
1654 }
1655
1656 case WID_CI_ROAD_DESC: {
1657 uint lines = 1;
1658
1659 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT).width);
1660
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);
1664 }
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);
1668 }
1669
1670 size->height = max(size->height, lines * FONT_HEIGHT_NORMAL);
1671 break;
1672 }
1673
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;
1678
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;
1684
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));
1697 }
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]));
1703 }
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));
1710
1711 SetDParamMaxValue(0, max_val);
1712 uint count_width = GetStringBoundingBox(STR_WHITE_COMMA).width + 20; // Reserve some wiggle room
1713
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);
1718
1719 SetDParamMaxValue(0, max_cost * 12); // Convert to per year
1720 count_width += max(this->total_width, GetStringBoundingBox(STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL).width);
1721 }
1722
1723 size->width = max(size->width, count_width);
1724
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;
1728 }
1729 break;
1730 }
1731 }
1732 }
1733
1741 void DrawCountLine(const Rect &r, int &y, int count, Money monthly_cost) const
1742 {
1743 SetDParam(0, count);
1744 DrawString(r.left, r.right, y += FONT_HEIGHT_NORMAL, STR_WHITE_COMMA, TC_FROMSTRING, SA_RIGHT);
1745
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);
1750 }
1751 }
1752
1753 virtual void DrawWidget(const Rect &r, int widget) const
1754 {
1755 const Company *c = Company::Get((CompanyID)this->window_number);
1756 int y = r.top;
1757
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;
1760
1761 switch (widget) {
1762 case WID_CI_RAIL_DESC:
1763 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_RAIL_SECT);
1764
1765 if (this->railtypes != RAILTYPES_NONE) {
1766 /* Draw name of each valid railtype. */
1767 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
1768 if (HasBit(this->railtypes, rt)) {
1769 SetDParam(0, GetRailTypeInfo(rt)->strings.name);
1770 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_WHITE_STRING);
1771 }
1772 }
1773 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_SIGNALS);
1774 } else {
1775 /* No valid railtype. */
1776 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
1777 }
1778
1779 break;
1780
1781 case WID_CI_RAIL_COUNT: {
1782 /* Draw infrastructure count for each valid railtype. */
1783 uint32 rail_total = c->infrastructure.GetRailTotal();
1784 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
1785 if (HasBit(this->railtypes, rt)) {
1786 this->DrawCountLine(r, y, c->infrastructure.rail[rt], RailMaintenanceCost(rt, c->infrastructure.rail[rt], rail_total));
1787 }
1788 }
1789 if (this->railtypes != RAILTYPES_NONE) {
1790 this->DrawCountLine(r, y, c->infrastructure.signal, SignalMaintenanceCost(c->infrastructure.signal));
1791 }
1792 break;
1793 }
1794
1795 case WID_CI_ROAD_DESC:
1796 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD_SECT);
1797
1798 if (this->roadtypes != ROADTYPES_NONE) {
1799 if (HasBit(this->roadtypes, ROADTYPE_ROAD)) DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_ROAD);
1800 if (HasBit(this->roadtypes, ROADTYPE_TRAM)) DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_TRAMWAY);
1801 } else {
1802 /* No valid roadtypes. */
1803 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
1804 }
1805
1806 break;
1807
1808 case WID_CI_ROAD_COUNT:
1809 if (HasBit(this->roadtypes, ROADTYPE_ROAD)) {
1810 this->DrawCountLine(r, y, c->infrastructure.road[ROADTYPE_ROAD], RoadMaintenanceCost(ROADTYPE_ROAD, c->infrastructure.road[ROADTYPE_ROAD]));
1811 }
1812 if (HasBit(this->roadtypes, ROADTYPE_TRAM)) {
1813 this->DrawCountLine(r, y, c->infrastructure.road[ROADTYPE_TRAM], RoadMaintenanceCost(ROADTYPE_TRAM, c->infrastructure.road[ROADTYPE_TRAM]));
1814 }
1815 break;
1816
1817 case WID_CI_WATER_DESC:
1818 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_WATER_SECT);
1819 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_CANALS);
1820 break;
1821
1822 case WID_CI_WATER_COUNT:
1823 this->DrawCountLine(r, y, c->infrastructure.water, CanalMaintenanceCost(c->infrastructure.water));
1824 break;
1825
1826 case WID_CI_TOTAL:
1827 if (_settings_game.economy.infrastructure_maintenance) {
1828 int left = _current_text_dir == TD_RTL ? r.right - this->total_width : r.left;
1829 GfxFillRect(left, y, left + this->total_width, y, PC_WHITE);
1830 y += EXP_LINESPACE;
1831 SetDParam(0, this->GetTotalMaintenanceCost() * 12); // Convert to per year
1832 DrawString(left, left + this->total_width, y, STR_COMPANY_INFRASTRUCTURE_VIEW_TOTAL, TC_FROMSTRING, SA_RIGHT);
1833 }
1834 break;
1835
1836 case WID_CI_STATION_DESC:
1837 DrawString(r.left, r.right, y, STR_COMPANY_INFRASTRUCTURE_VIEW_STATION_SECT);
1838 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_STATIONS);
1839 DrawString(r.left + offs_left, r.right - offs_right, y += FONT_HEIGHT_NORMAL, STR_COMPANY_INFRASTRUCTURE_VIEW_AIRPORTS);
1840 break;
1841
1842 case WID_CI_STATION_COUNT:
1843 this->DrawCountLine(r, y, c->infrastructure.station, StationMaintenanceCost(c->infrastructure.station));
1844 this->DrawCountLine(r, y, c->infrastructure.airport, AirportMaintenanceCost(c->index));
1845 break;
1846 }
1847 }
1848
1854 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1855 {
1856 if (!gui_scope) return;
1857
1858 this->UpdateRailRoadTypes();
1859 this->ReInit();
1860 }
1861 };
1862
1863 static WindowDesc _company_infrastructure_desc(
1864 WDP_AUTO, "company_infrastructure", 0, 0,
1865 WC_COMPANY_INFRASTRUCTURE, WC_NONE,
1866 0,
1867 _nested_company_infrastructure_widgets, lengthof(_nested_company_infrastructure_widgets)
1868 );
1869
1874 static void ShowCompanyInfrastructure(CompanyID company)
1875 {
1876 if (!Company::IsValidID(company)) return;
1877 AllocateWindowDescFront<CompanyInfrastructureWindow>(&_company_infrastructure_desc, company);
1878 }
1879
1880 static const NWidgetPart _nested_company_widgets[] = {
1881 NWidget(NWID_HORIZONTAL),
1882 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1883 NWidget(WWT_CAPTION, COLOUR_GREY, WID_C_CAPTION), SetDataTip(STR_COMPANY_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1884 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1885 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1886 EndContainer(),
1887 NWidget(WWT_PANEL, COLOUR_GREY),
1888 NWidget(NWID_HORIZONTAL), SetPIP(4, 6, 4),
1889 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1890 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE), SetMinimalSize(92, 119), SetFill(1, 0),
1891 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_FACE_TITLE), SetFill(1, 1), SetMinimalTextLines(2, 0),
1892 EndContainer(),
1893 NWidget(NWID_VERTICAL),
1894 NWidget(NWID_HORIZONTAL),
1895 NWidget(NWID_VERTICAL), SetPIP(4, 5, 5),
1896 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INAUGURATION), SetDataTip(STR_COMPANY_VIEW_INAUGURATED_TITLE, STR_NULL), SetFill(1, 0),
1897 NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
1898 NWidget(WWT_LABEL, COLOUR_GREY, WID_C_DESC_COLOUR_SCHEME), SetDataTip(STR_COMPANY_VIEW_COLOUR_SCHEME_TITLE, STR_NULL),
1899 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_COLOUR_SCHEME_EXAMPLE), SetMinimalSize(30, 0), SetFill(0, 1),
1900 NWidget(NWID_SPACER), SetFill(1, 0),
1901 EndContainer(),
1902 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
1903 NWidget(NWID_VERTICAL),
1904 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_VEHICLE), SetDataTip(STR_COMPANY_VIEW_VEHICLES_TITLE, STR_NULL),
1905 NWidget(NWID_SPACER), SetFill(0, 1),
1906 EndContainer(),
1907 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_VEHICLE_COUNTS), SetMinimalTextLines(4, 0),
1908 NWidget(NWID_SPACER), SetFill(1, 0),
1909 EndContainer(),
1910 EndContainer(),
1911 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1912 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_VIEW_BUILD_HQ),
1913 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),
1914 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),
1915 EndContainer(),
1916 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_RELOCATE),
1917 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_C_RELOCATE_HQ), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_RELOCATE_HQ, STR_COMPANY_VIEW_RELOCATE_COMPANY_HEADQUARTERS),
1918 NWidget(NWID_SPACER), SetMinimalSize(90, 0),
1919 EndContainer(),
1920 NWidget(NWID_SPACER), SetFill(0, 1),
1921 EndContainer(),
1922 EndContainer(),
1923 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_COMPANY_VALUE), SetDataTip(STR_COMPANY_VIEW_COMPANY_VALUE, STR_NULL), SetFill(1, 0),
1924 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1925 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
1926 NWidget(NWID_VERTICAL),
1927 NWidget(WWT_TEXT, COLOUR_GREY, WID_C_DESC_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE, STR_NULL),
1928 NWidget(NWID_SPACER), SetFill(0, 1),
1929 EndContainer(),
1930 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_INFRASTRUCTURE_COUNTS), SetMinimalTextLines(5, 0), SetFill(1, 0),
1931 NWidget(NWID_VERTICAL),
1932 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_VIEW_INFRASTRUCTURE), SetDataTip(STR_COMPANY_VIEW_INFRASTRUCTURE_BUTTON, STR_COMPANY_VIEW_INFRASTRUCTURE_TOOLTIP),
1933 NWidget(NWID_SPACER), SetFill(0, 1), SetMinimalSize(90, 0),
1934 EndContainer(),
1935 EndContainer(),
1936 EndContainer(),
1937 NWidget(NWID_HORIZONTAL),
1938 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_DESC_OWNERS),
1939 NWidget(NWID_VERTICAL), SetPIP(5, 5, 4),
1940 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_C_DESC_OWNERS), SetMinimalTextLines(3, 0),
1941 NWidget(NWID_SPACER), SetFill(0, 1),
1942 EndContainer(),
1943 EndContainer(),
1944 NWidget(NWID_VERTICAL), SetPIP(4, 2, 4),
1945 NWidget(NWID_SPACER), SetMinimalSize(90, 0), SetFill(0, 1),
1946 /* Multi player buttons. */
1947 NWidget(NWID_HORIZONTAL),
1948 NWidget(WWT_EMPTY, COLOUR_GREY, WID_C_HAS_PASSWORD),
1949 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_MULTIPLAYER),
1950 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_PASSWORD), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_PASSWORD, STR_COMPANY_VIEW_PASSWORD_TOOLTIP),
1951 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_C_COMPANY_JOIN), SetFill(1, 0), SetDataTip(STR_COMPANY_VIEW_JOIN, STR_COMPANY_VIEW_JOIN_TOOLTIP),
1952 EndContainer(),
1953 EndContainer(),
1954 EndContainer(),
1955 EndContainer(),
1956 EndContainer(),
1957 EndContainer(),
1958 EndContainer(),
1959 /* Button bars at the bottom. */
1960 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_C_SELECT_BUTTONS),
1961 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1962 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),
1963 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),
1964 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),
1965 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),
1966 EndContainer(),
1967 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1968 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),
1969 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),
1970 EndContainer(),
1971 EndContainer(),
1972 };
1973
1974 int GetAmountOwnedBy(const Company *c, Owner owner)
1975 {
1976 return (c->share_owners[0] == owner) +
1977 (c->share_owners[1] == owner) +
1978 (c->share_owners[2] == owner) +
1979 (c->share_owners[3] == owner);
1980 }
1981
1983 static const StringID _company_view_vehicle_count_strings[] = {
1984 STR_COMPANY_VIEW_TRAINS, STR_COMPANY_VIEW_ROAD_VEHICLES, STR_COMPANY_VIEW_SHIPS, STR_COMPANY_VIEW_AIRCRAFT
1985 };
1986
1990 struct CompanyWindow : Window
1991 {
1992 CompanyWidgets query_widget;
1993
1995 enum CompanyWindowPlanes {
1996 /* Display planes of the #WID_C_SELECT_MULTIPLAYER selection widget. */
1997 CWP_MP_C_PWD = 0,
1998 CWP_MP_C_JOIN,
1999
2000 /* Display planes of the #WID_C_SELECT_VIEW_BUILD_HQ selection widget. */
2001 CWP_VB_VIEW = 0,
2002 CWP_VB_BUILD,
2003
2004 /* Display planes of the #WID_C_SELECT_RELOCATE selection widget. */
2005 CWP_RELOCATE_SHOW = 0,
2006 CWP_RELOCATE_HIDE,
2007
2008 /* Display planes of the #WID_C_SELECT_BUTTONS selection widget. */
2009 CWP_BUTTONS_LOCAL = 0,
2010 CWP_BUTTONS_OTHER,
2011 };
2012
2013 CompanyWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
2014 {
2015 this->InitNested(window_number);
2016 this->owner = (Owner)this->window_number;
2017 this->OnInvalidateData();
2018 }
2019
2020 virtual void OnPaint()
2021 {
2022 const Company *c = Company::Get((CompanyID)this->window_number);
2023 bool local = this->window_number == _local_company;
2024
2025 if (!this->IsShaded()) {
2026 bool reinit = false;
2027
2028 /* Button bar selection. */
2029 int plane = local ? CWP_BUTTONS_LOCAL : CWP_BUTTONS_OTHER;
2030 NWidgetStacked *wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_BUTTONS);
2031 if (plane != wi->shown_plane) {
2032 wi->SetDisplayedPlane(plane);
2033 this->InvalidateData();
2034 return;
2035 }
2036
2037 /* Build HQ button handling. */
2038 plane = (local && c->location_of_HQ == INVALID_TILE) ? CWP_VB_BUILD : CWP_VB_VIEW;
2039 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_VIEW_BUILD_HQ);
2040 if (plane != wi->shown_plane) {
2041 wi->SetDisplayedPlane(plane);
2042 this->SetDirty();
2043 return;
2044 }
2045
2046 this->SetWidgetDisabledState(WID_C_VIEW_HQ, c->location_of_HQ == INVALID_TILE);
2047
2048 /* Enable/disable 'Relocate HQ' button. */
2049 plane = (!local || c->location_of_HQ == INVALID_TILE) ? CWP_RELOCATE_HIDE : CWP_RELOCATE_SHOW;
2050 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_RELOCATE);
2051 if (plane != wi->shown_plane) {
2052 wi->SetDisplayedPlane(plane);
2053 this->SetDirty();
2054 return;
2055 }
2056
2057 /* Owners of company */
2058 plane = SZSP_HORIZONTAL;
2059 for (uint i = 0; i < lengthof(c->share_owners); i++) {
2060 if (c->share_owners[i] != INVALID_COMPANY) {
2061 plane = 0;
2062 break;
2063 }
2064 }
2065 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_DESC_OWNERS);
2066 if (plane != wi->shown_plane) {
2067 wi->SetDisplayedPlane(plane);
2068 reinit = true;
2069 }
2070
2071 /* Multiplayer buttons. */
2072 plane = ((!_networking) ? (int)SZSP_NONE : (int)(local ? CWP_MP_C_PWD : CWP_MP_C_JOIN));
2073 wi = this->GetWidget<NWidgetStacked>(WID_C_SELECT_MULTIPLAYER);
2074 if (plane != wi->shown_plane) {
2075 wi->SetDisplayedPlane(plane);
2076 reinit = true;
2077 }
2078 this->SetWidgetDisabledState(WID_C_COMPANY_JOIN, c->is_ai);
2079
2080 if (reinit) {
2081 this->ReInit();
2082 return;
2083 }
2084 }
2085
2086 this->DrawWidgets();
2087 }
2088
2089 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2090 {
2091 switch (widget) {
2092 case WID_C_FACE: {
2093 Dimension face_size = GetSpriteSize(SPR_GRADIENT);
2094 size->width = max(size->width, face_size.width);
2095 size->height = max(size->height, face_size.height);
2096 break;
2097 }
2098
2099 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2100 Point offset;
2101 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2102 d.width -= offset.x;
2103 d.height -= offset.y;
2104 *size = maxdim(*size, d);
2105 break;
2106 }
2107
2108 case WID_C_DESC_COMPANY_VALUE:
2109 SetDParam(0, INT64_MAX); // Arguably the maximum company value
2110 size->width = GetStringBoundingBox(STR_COMPANY_VIEW_COMPANY_VALUE).width;
2111 break;
2112
2113 case WID_C_DESC_VEHICLE_COUNTS:
2114 SetDParamMaxValue(0, 5000); // Maximum number of vehicles
2115 for (uint i = 0; i < lengthof(_company_view_vehicle_count_strings); i++) {
2116 size->width = max(size->width, GetStringBoundingBox(_company_view_vehicle_count_strings[i]).width);
2117 }
2118 break;
2119
2120 case WID_C_DESC_INFRASTRUCTURE_COUNTS:
2121 SetDParamMaxValue(0, UINT_MAX);
2122 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL).width);
2123 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD).width);
2124 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_WATER).width);
2125 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_STATION).width);
2126 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT).width);
2127 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_INFRASTRUCTURE_NONE).width);
2128 break;
2129
2130 case WID_C_DESC_OWNERS: {
2131 const Company *c2;
2132
2133 FOR_ALL_COMPANIES(c2) {
2134 SetDParamMaxValue(0, 75);
2135 SetDParam(1, c2->index);
2136
2137 size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_VIEW_SHARES_OWNED_BY).width);
2138 }
2139 break;
2140 }
2141
2142 #ifdef ENABLE_NETWORK
2143 case WID_C_HAS_PASSWORD:
2144 *size = maxdim(*size, GetSpriteSize(SPR_LOCK));
2145 break;
2146 #endif /* ENABLE_NETWORK */
2147 }
2148 }
2149
2150 virtual void DrawWidget(const Rect &r, int widget) const
2151 {
2152 const Company *c = Company::Get((CompanyID)this->window_number);
2153 switch (widget) {
2154 case WID_C_FACE:
2155 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
2156 break;
2157
2158 case WID_C_FACE_TITLE:
2159 SetDParam(0, c->index);
2160 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_COMPANY_VIEW_PRESIDENT_MANAGER_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
2161 break;
2162
2163 case WID_C_DESC_COLOUR_SCHEME_EXAMPLE: {
2164 Point offset;
2165 Dimension d = GetSpriteSize(SPR_VEH_BUS_SW_VIEW, &offset);
2166 d.height -= offset.y;
2167 DrawSprite(SPR_VEH_BUS_SW_VIEW, COMPANY_SPRITE_COLOUR(c->index), r.left - offset.x, (r.top + r.bottom - d.height) / 2 - offset.y);
2168 break;
2169 }
2170
2171 case WID_C_DESC_VEHICLE_COUNTS: {
2172 uint amounts[4];
2173 amounts[0] = c->group_all[VEH_TRAIN].num_vehicle;
2174 amounts[1] = c->group_all[VEH_ROAD].num_vehicle;
2175 amounts[2] = c->group_all[VEH_SHIP].num_vehicle;
2176 amounts[3] = c->group_all[VEH_AIRCRAFT].num_vehicle;
2177
2178 int y = r.top;
2179 if (amounts[0] + amounts[1] + amounts[2] + amounts[3] == 0) {
2180 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_VEHICLES_NONE);
2181 } else {
2182 assert_compile(lengthof(amounts) == lengthof(_company_view_vehicle_count_strings));
2183
2184 for (uint i = 0; i < lengthof(amounts); i++) {
2185 if (amounts[i] != 0) {
2186 SetDParam(0, amounts[i]);
2187 DrawString(r.left, r.right, y, _company_view_vehicle_count_strings[i]);
2188 y += FONT_HEIGHT_NORMAL;
2189 }
2190 }
2191 }
2192 break;
2193 }
2194
2195 case WID_C_DESC_INFRASTRUCTURE_COUNTS: {
2196 uint y = r.top;
2197
2198 /* Collect rail and road counts. */
2199 uint rail_pices = c->infrastructure.signal;
2200 uint road_pieces = 0;
2201 for (uint i = 0; i < lengthof(c->infrastructure.rail); i++) rail_pices += c->infrastructure.rail[i];
2202 for (uint i = 0; i < lengthof(c->infrastructure.road); i++) road_pieces += c->infrastructure.road[i];
2203
2204 if (rail_pices == 0 && road_pieces == 0 && c->infrastructure.water == 0 && c->infrastructure.station == 0 && c->infrastructure.airport == 0) {
2205 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_NONE);
2206 } else {
2207 if (rail_pices != 0) {
2208 SetDParam(0, rail_pices);
2209 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_RAIL);
2210 y += FONT_HEIGHT_NORMAL;
2211 }
2212 if (road_pieces != 0) {
2213 SetDParam(0, road_pieces);
2214 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_ROAD);
2215 y += FONT_HEIGHT_NORMAL;
2216 }
2217 if (c->infrastructure.water != 0) {
2218 SetDParam(0, c->infrastructure.water);
2219 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_WATER);
2220 y += FONT_HEIGHT_NORMAL;
2221 }
2222 if (c->infrastructure.station != 0) {
2223 SetDParam(0, c->infrastructure.station);
2224 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_STATION);
2225 y += FONT_HEIGHT_NORMAL;
2226 }
2227 if (c->infrastructure.airport != 0) {
2228 SetDParam(0, c->infrastructure.airport);
2229 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_INFRASTRUCTURE_AIRPORT);
2230 }
2231 }
2232
2233 break;
2234 }
2235
2236 case WID_C_DESC_OWNERS: {
2237 const Company *c2;
2238 uint y = r.top;
2239
2240 FOR_ALL_COMPANIES(c2) {
2241 uint amt = GetAmountOwnedBy(c, c2->index);
2242 if (amt != 0) {
2243 SetDParam(0, amt * 25);
2244 SetDParam(1, c2->index);
2245
2246 DrawString(r.left, r.right, y, STR_COMPANY_VIEW_SHARES_OWNED_BY);
2247 y += FONT_HEIGHT_NORMAL;
2248 }
2249 }
2250 break;
2251 }
2252
2253 #ifdef ENABLE_NETWORK
2254 case WID_C_HAS_PASSWORD:
2255 if (_networking && NetworkCompanyIsPassworded(c->index)) {
2256 DrawSprite(SPR_LOCK, PAL_NONE, r.left, r.top);
2257 }
2258 break;
2259 #endif /* ENABLE_NETWORK */
2260 }
2261 }
2262
2263 virtual void SetStringParameters(int widget) const
2264 {
2265 switch (widget) {
2266 case WID_C_CAPTION:
2267 SetDParam(0, (CompanyID)this->window_number);
2268 SetDParam(1, (CompanyID)this->window_number);
2269 break;
2270
2271 case WID_C_DESC_INAUGURATION:
2272 SetDParam(0, Company::Get((CompanyID)this->window_number)->inaugurated_year);
2273 break;
2274
2275 case WID_C_DESC_COMPANY_VALUE:
2276 SetDParam(0, CalculateCompanyValue(Company::Get((CompanyID)this->window_number)));
2277 break;
2278 }
2279 }
2280
2281 virtual void OnClick(Point pt, int widget, int click_count)
2282 {
2283 switch (widget) {
2284 case WID_C_NEW_FACE: DoSelectCompanyManagerFace(this); break;
2285
2286 case WID_C_COLOUR_SCHEME:
2287 if (BringWindowToFrontById(WC_COMPANY_COLOUR, this->window_number)) break;
2288 new SelectCompanyLiveryWindow(&_select_company_livery_desc, (CompanyID)this->window_number);
2289 break;
2290
2291 case WID_C_PRESIDENT_NAME:
2292 this->query_widget = WID_C_PRESIDENT_NAME;
2293 SetDParam(0, this->window_number);
2294 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);
2295 break;
2296
2297 case WID_C_COMPANY_NAME:
2298 this->query_widget = WID_C_COMPANY_NAME;
2299 SetDParam(0, this->window_number);
2300 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);
2301 break;
2302
2303 case WID_C_VIEW_HQ: {
2304 TileIndex tile = Company::Get((CompanyID)this->window_number)->location_of_HQ;
2305 if (_ctrl_pressed) {
2306 ShowExtraViewPortWindow(tile);
2307 } else {
2308 ScrollMainWindowToTile(tile);
2309 }
2310 break;
2311 }
2312
2313 case WID_C_BUILD_HQ:
2314 if ((byte)this->window_number != _local_company) return;
2315 if (this->IsWidgetLowered(WID_C_BUILD_HQ)) {
2316 ResetObjectToPlace();
2317 this->RaiseButtons();
2318 break;
2319 }
2320 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2321 SetTileSelectSize(2, 2);
2322 this->LowerWidget(WID_C_BUILD_HQ);
2323 this->SetWidgetDirty(WID_C_BUILD_HQ);
2324 break;
2325
2326 case WID_C_RELOCATE_HQ:
2327 if (this->IsWidgetLowered(WID_C_RELOCATE_HQ)) {
2328 ResetObjectToPlace();
2329 this->RaiseButtons();
2330 break;
2331 }
2332 SetObjectToPlaceWnd(SPR_CURSOR_HQ, PAL_NONE, HT_RECT, this);
2333 SetTileSelectSize(2, 2);
2334 this->LowerWidget(WID_C_RELOCATE_HQ);
2335 this->SetWidgetDirty(WID_C_RELOCATE_HQ);
2336 break;
2337
2338 case WID_C_VIEW_INFRASTRUCTURE:
2339 ShowCompanyInfrastructure((CompanyID)this->window_number);
2340 break;
2341
2342 case WID_C_BUY_SHARE:
2343 DoCommandP(0, this->window_number, 0, CMD_BUY_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_BUY_25_SHARE_IN_THIS));
2344 break;
2345
2346 case WID_C_SELL_SHARE:
2347 DoCommandP(0, this->window_number, 0, CMD_SELL_SHARE_IN_COMPANY | CMD_MSG(STR_ERROR_CAN_T_SELL_25_SHARE_IN));
2348 break;
2349
2350 #ifdef ENABLE_NETWORK
2351 case WID_C_COMPANY_PASSWORD:
2352 if (this->window_number == _local_company) ShowNetworkCompanyPasswordWindow(this);
2353 break;
2354
2355 case WID_C_COMPANY_JOIN: {
2356 this->query_widget = WID_C_COMPANY_JOIN;
2357 CompanyID company = (CompanyID)this->window_number;
2358 if (_network_server) {
2359 NetworkServerDoMove(CLIENT_ID_SERVER, company);
2360 MarkWholeScreenDirty();
2361 } else if (NetworkCompanyIsPassworded(company)) {
2362 /* ask for the password */
2363 ShowQueryString(STR_EMPTY, STR_NETWORK_NEED_COMPANY_PASSWORD_CAPTION, NETWORK_PASSWORD_LENGTH, this, CS_ALPHANUMERAL, QSF_NONE);
2364 } else {
2365 /* just send the join command */
2366 NetworkClientRequestMove(company);
2367 }
2368 break;
2369 }
2370 #endif /* ENABLE_NETWORK */
2371 }
2372 }
2373
2374 virtual void OnHundredthTick()
2375 {
2376 /* redraw the window every now and then */
2377 this->SetDirty();
2378 }
2379
2380 virtual void OnPlaceObject(Point pt, TileIndex tile)
2381 {
2382 if (DoCommandP(tile, OBJECT_HQ, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_COMPANY_HEADQUARTERS))) {
2383 ResetObjectToPlace();
2384 this->RaiseButtons();
2385 }
2386 }
2387
2388 virtual void OnPlaceObjectAbort()
2389 {
2390 this->RaiseButtons();
2391 }
2392
2393 virtual void OnQueryTextFinished(char *str)
2394 {
2395 if (str == NULL) return;
2396
2397 switch (this->query_widget) {
2398 default: NOT_REACHED();
2399
2400 case WID_C_PRESIDENT_NAME:
2401 DoCommandP(0, 0, 0, CMD_RENAME_PRESIDENT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_PRESIDENT), NULL, str);
2402 break;
2403
2404 case WID_C_COMPANY_NAME:
2405 DoCommandP(0, 0, 0, CMD_RENAME_COMPANY | CMD_MSG(STR_ERROR_CAN_T_CHANGE_COMPANY_NAME), NULL, str);
2406 break;
2407
2408 #ifdef ENABLE_NETWORK
2409 case WID_C_COMPANY_JOIN:
2410 NetworkClientRequestMove((CompanyID)this->window_number, str);
2411 break;
2412 #endif /* ENABLE_NETWORK */
2413 }
2414 }
2415
2416
2422 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
2423 {
2424 if (this->window_number == _local_company) return;
2425
2426 if (_settings_game.economy.allow_shares) { // Shares are allowed
2427 const Company *c = Company::Get(this->window_number);
2428
2429 /* If all shares are owned by someone (none by nobody), disable buy button */
2430 this->SetWidgetDisabledState(WID_C_BUY_SHARE, GetAmountOwnedBy(c, INVALID_OWNER) == 0 ||
2431 /* Only 25% left to buy. If the company is human, disable buying it up.. TODO issues! */
2432 (GetAmountOwnedBy(c, INVALID_OWNER) == 1 && !c->is_ai) ||
2433 /* Spectators cannot do anything of course */
2434 _local_company == COMPANY_SPECTATOR);
2435
2436 /* If the company doesn't own any shares, disable sell button */
2437 this->SetWidgetDisabledState(WID_C_SELL_SHARE, (GetAmountOwnedBy(c, _local_company) == 0) ||
2438 /* Spectators cannot do anything of course */
2439 _local_company == COMPANY_SPECTATOR);
2440 } else { // Shares are not allowed, disable buy/sell buttons
2441 this->DisableWidget(WID_C_BUY_SHARE);
2442 this->DisableWidget(WID_C_SELL_SHARE);
2443 }
2444 }
2445 };
2446
2447 static WindowDesc _company_desc(
2448 WDP_AUTO, "company", 0, 0,
2449 WC_COMPANY, WC_NONE,
2450 0,
2451 _nested_company_widgets, lengthof(_nested_company_widgets)
2452 );
2453
2458 void ShowCompany(CompanyID company)
2459 {
2460 if (!Company::IsValidID(company)) return;
2461
2462 AllocateWindowDescFront<CompanyWindow>(&_company_desc, company);
2463 }
2464
2469 void DirtyCompanyInfrastructureWindows(CompanyID company)
2470 {
2471 SetWindowDirty(WC_COMPANY, company);
2472 SetWindowDirty(WC_COMPANY_INFRASTRUCTURE, company);
2473 }
2474
2475 struct BuyCompanyWindow : Window {
2476 BuyCompanyWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
2477 {
2478 this->InitNested(window_number);
2479 }
2480
2481 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2482 {
2483 switch (widget) {
2484 case WID_BC_FACE:
2485 *size = GetSpriteSize(SPR_GRADIENT);
2486 break;
2487
2488 case WID_BC_QUESTION:
2489 const Company *c = Company::Get((CompanyID)this->window_number);
2490 SetDParam(0, c->index);
2491 SetDParam(1, c->bankrupt_value);
2492 size->height = GetStringHeight(STR_BUY_COMPANY_MESSAGE, size->width);
2493 break;
2494 }
2495 }
2496
2497 virtual void SetStringParameters(int widget) const
2498 {
2499 switch (widget) {
2500 case WID_BC_CAPTION:
2501 SetDParam(0, STR_COMPANY_NAME);
2502 SetDParam(1, Company::Get((CompanyID)this->window_number)->index);
2503 break;
2504 }
2505 }
2506
2507 virtual void DrawWidget(const Rect &r, int widget) const
2508 {
2509 switch (widget) {
2510 case WID_BC_FACE: {
2511 const Company *c = Company::Get((CompanyID)this->window_number);
2512 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
2513 break;
2514 }
2515
2516 case WID_BC_QUESTION: {
2517 const Company *c = Company::Get((CompanyID)this->window_number);
2518 SetDParam(0, c->index);
2519 SetDParam(1, c->bankrupt_value);
2520 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_BUY_COMPANY_MESSAGE, TC_FROMSTRING, SA_CENTER);
2521 break;
2522 }
2523 }
2524 }
2525
2526 virtual void OnClick(Point pt, int widget, int click_count)
2527 {
2528 switch (widget) {
2529 case WID_BC_NO:
2530 delete this;
2531 break;
2532
2533 case WID_BC_YES:
2534 DoCommandP(0, this->window_number, 0, CMD_BUY_COMPANY | CMD_MSG(STR_ERROR_CAN_T_BUY_COMPANY));
2535 break;
2536 }
2537 }
2538 };
2539
2540 static const NWidgetPart _nested_buy_company_widgets[] = {
2541 NWidget(NWID_HORIZONTAL),
2542 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
2543 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_BC_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2544 EndContainer(),
2545 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
2546 NWidget(NWID_VERTICAL), SetPIP(8, 8, 8),
2547 NWidget(NWID_HORIZONTAL), SetPIP(8, 10, 8),
2548 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_FACE), SetFill(0, 1),
2549 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BC_QUESTION), SetMinimalSize(240, 0), SetFill(1, 1),
2550 EndContainer(),
2551 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(100, 10, 100),
2552 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_NO), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
2553 NWidget(WWT_TEXTBTN, COLOUR_LIGHT_BLUE, WID_BC_YES), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
2554 EndContainer(),
2555 EndContainer(),
2556 EndContainer(),
2557 };
2558
2559 static WindowDesc _buy_company_desc(
2560 WDP_AUTO, NULL, 0, 0,
2561 WC_BUY_COMPANY, WC_NONE,
2562 WDF_CONSTRUCTION,
2563 _nested_buy_company_widgets, lengthof(_nested_buy_company_widgets)
2564 );
2565
2570 void ShowBuyCompanyDialog(CompanyID company)
2571 {
2572 AllocateWindowDescFront<BuyCompanyWindow>(&_buy_company_desc, company);
2573 }