27 #include "table/strings.h"
34 static CompanyMask _legend_excluded_companies;
35 static CargoTypes _legend_excluded_cargo;
39 static const uint INVALID_DATAPOINT_POS = UINT_MAX;
68 DrawCompanyIcon(cid, rtl ? r.right - d.width - 2 : r.left + 2, r.top + (r.bottom - r.top - d.height) / 2);
96 if (!gui_scope)
return;
99 SetBit(_legend_excluded_companies, data);
119 panel->
SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION_TOOLTIP);
126 static const NWidgetPart _nested_graph_legend_widgets[] = {
147 _nested_graph_legend_widgets,
lengthof(_nested_graph_legend_widgets)
150 static void ShowGraphLegend()
152 AllocateWindowDescFront<GraphLegendWindow>(&_graph_legend_desc, 0);
167 static const int GRAPH_MAX_DATASETS = 64;
168 static const int GRAPH_BASE_COLOUR =
GREY_SCALE(2);
169 static const int GRAPH_GRID_COLOUR =
GREY_SCALE(3);
170 static const int GRAPH_AXIS_LINE_COLOUR =
GREY_SCALE(1);
171 static const int GRAPH_ZERO_LINE_COLOUR =
GREY_SCALE(8);
172 static const int GRAPH_YEAR_LINE_COLOUR =
GREY_SCALE(5);
192 uint16 x_values_start;
193 uint16 x_values_increment;
197 byte colours[GRAPH_MAX_DATASETS];
208 assert(num_hori_lines > 0);
211 current_interval.
highest = INT64_MIN;
212 current_interval.
lowest = INT64_MAX;
214 for (
int i = 0; i < this->num_dataset; i++) {
215 if (
HasBit(this->excluded_data, i))
continue;
216 for (
int j = 0; j < this->num_on_x_axis; j++) {
219 if (datapoint != INVALID_DATAPOINT) {
220 current_interval.
highest = std::max(current_interval.
highest, datapoint);
221 current_interval.
lowest = std::min(current_interval.
lowest, datapoint);
228 current_interval.
lowest = (11 * current_interval.
lowest) / 10;
231 double abs_lower = (current_interval.
lowest > 0) ? 0 : (
double)
abs(current_interval.
lowest);
232 double abs_higher = (current_interval.
highest < 0) ? 0 : (
double)current_interval.
highest;
237 if (abs_lower != 0 || abs_higher != 0) {
239 num_pos_grids = (int)floor(0.5 + num_hori_lines * abs_higher / (abs_higher + abs_lower));
242 if (num_pos_grids == 0 && abs_higher != 0) num_pos_grids++;
243 if (num_pos_grids == num_hori_lines && abs_lower != 0) num_pos_grids--;
246 int64 grid_size_higher = (abs_higher > 0) ? ((int64)abs_higher + num_pos_grids - 1) / num_pos_grids : 0;
247 int64 grid_size_lower = (abs_lower > 0) ? ((int64)abs_lower + num_hori_lines - num_pos_grids - 1) / (num_hori_lines - num_pos_grids) : 0;
248 grid_size = std::max(grid_size_higher, grid_size_lower);
251 num_pos_grids = num_hori_lines / 2;
255 current_interval.
highest = num_pos_grids * grid_size;
256 current_interval.
lowest = -(num_hori_lines - num_pos_grids) * grid_size;
257 return current_interval;
268 int64 y_label = current_interval.
highest;
269 int64 y_label_separation = (current_interval.
highest - current_interval.
lowest) / num_hori_lines;
273 for (
int i = 0; i < (num_hori_lines + 1); i++) {
277 if (d.width > max_width) max_width = d.width;
279 y_label -= y_label_separation;
298 assert(this->num_vert_lines > 0);
317 r.left += label_width;
319 int x_sep = (r.right - r.left) / this->num_vert_lines;
320 int y_sep = (r.bottom - r.top) / num_hori_lines;
324 r.right = r.left + x_sep * this->num_vert_lines;
325 r.bottom = r.top + y_sep * num_hori_lines;
329 x_axis_offset = (int)((r.bottom - r.top) * (double)interval.
highest / (
double)interval_size);
332 GfxFillRect(r.left, r.top, r.right, r.bottom, GRAPH_BASE_COLOUR);
339 for (
int i = 0; i < this->num_vert_lines; i++) {
340 GfxFillRect(x, r.top, x, r.bottom, GRAPH_GRID_COLOUR);
347 for (
int i = 0; i < (num_hori_lines + 1); i++) {
348 GfxFillRect(r.left - 3, y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
349 GfxFillRect(r.left, y, r.right, y, GRAPH_GRID_COLOUR);
354 GfxFillRect(r.left, r.top, r.left, r.bottom, GRAPH_AXIS_LINE_COLOUR);
357 y = x_axis_offset + r.top;
358 GfxFillRect(r.left, y, r.right, y, GRAPH_ZERO_LINE_COLOUR);
361 if (this->num_on_x_axis == 0)
return;
363 assert(this->num_on_x_axis > 0);
364 assert(this->num_dataset > 0);
367 int64 y_label = interval.
highest;
368 int64 y_label_separation =
abs(interval.
highest - interval.
lowest) / num_hori_lines;
372 for (
int i = 0; i < (num_hori_lines + 1); i++) {
377 y_label -= y_label_separation;
382 if (this->month != 0xFF) {
385 byte month = this->month;
386 Year year = this->year;
387 for (
int i = 0; i < this->num_on_x_axis; i++) {
388 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
398 GfxFillRect(x + x_sep, r.top + 1, x + x_sep, r.bottom - 1, GRAPH_YEAR_LINE_COLOUR);
406 uint16 label = this->x_values_start;
408 for (
int i = 0; i < this->num_on_x_axis; i++) {
412 label += this->x_values_increment;
419 uint pointoffs1 = (linewidth + 1) / 2;
420 uint pointoffs2 = linewidth + 1 - pointoffs1;
421 for (
int i = 0; i < this->num_dataset; i++) {
422 if (!
HasBit(this->excluded_data, i)) {
424 x = r.left + (x_sep / 2);
426 byte colour = this->colours[i];
427 uint prev_x = INVALID_DATAPOINT_POS;
428 uint prev_y = INVALID_DATAPOINT_POS;
430 for (
int j = 0; j < this->num_on_x_axis; j++) {
433 if (datapoint != INVALID_DATAPOINT) {
446 int reduce_range = std::max(mult_range - 31, 0);
450 datapoint = -(
abs(datapoint) >> reduce_range);
452 datapoint >>= reduce_range;
454 y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
457 GfxFillRect(x - pointoffs1, y - pointoffs1, x + pointoffs2, y + pointoffs2, colour);
460 if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, colour, linewidth);
465 prev_x = INVALID_DATAPOINT_POS;
466 prev_y = INVALID_DATAPOINT_POS;
478 format_str_y_axis(format_str_y_axis)
481 this->num_vert_lines = 24;
482 this->graph_widget = widget;
496 if (widget != this->graph_widget)
return;
498 uint x_label_width = 0;
501 if (this->month != 0xFF) {
502 byte month = this->month;
503 Year year = this->year;
504 for (
int i = 0; i < this->num_on_x_axis; i++) {
505 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
507 x_label_width = std::max(x_label_width,
GetStringBoundingBox(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH).
width);
525 size->width = std::max<uint>(size->width, 5 + y_label_width + this->num_on_x_axis * (x_label_width + 5) + 9);
527 size->height = std::max<uint>(size->height, size->width / 3);
532 if (widget != this->graph_widget)
return;
539 return INVALID_DATAPOINT;
560 if (!gui_scope)
return;
570 CompanyMask excluded_companies = _legend_excluded_companies;
579 nums = std::min(this->num_vert_lines, std::max(nums, c->num_valid_stat_ent));
589 if (!initialize && this->excluded_data == excluded_companies && this->num_on_x_axis == nums &&
590 this->year == yr && this->month == mo) {
595 this->excluded_data = excluded_companies;
596 this->num_on_x_axis = nums;
605 for (
int j = this->num_on_x_axis, i = 0; --j >= 0;) {
606 this->cost[numd][i] = (j >= c->
num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
613 this->num_dataset = numd;
635 static const NWidgetPart _nested_operating_profit_widgets[] = {
656 WDP_AUTO,
"graph_operating_profit", 0, 0,
659 _nested_operating_profit_widgets,
lengthof(_nested_operating_profit_widgets)
663 void ShowOperatingProfitGraph()
665 AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
686 static const NWidgetPart _nested_income_graph_widgets[] = {
710 _nested_income_graph_widgets,
lengthof(_nested_income_graph_widgets)
713 void ShowIncomeGraph()
715 AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
735 static const NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
755 static WindowDesc _delivered_cargo_graph_desc(
756 WDP_AUTO,
"graph_delivered_cargo", 0, 0,
759 _nested_delivered_cargo_graph_widgets,
lengthof(_nested_delivered_cargo_graph_widgets)
762 void ShowDeliveredCargoGraph()
764 AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
790 static const NWidgetPart _nested_performance_history_widgets[] = {
812 WDP_AUTO,
"graph_performance", 0, 0,
815 _nested_performance_history_widgets,
lengthof(_nested_performance_history_widgets)
818 void ShowPerformanceHistoryGraph()
820 AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
840 static const NWidgetPart _nested_company_value_graph_widgets[] = {
861 WDP_AUTO,
"graph_company_value", 0, 0,
864 _nested_company_value_graph_widgets,
lengthof(_nested_company_value_graph_widgets)
867 void ShowCompanyValueGraph()
869 AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
883 this->num_on_x_axis = 20;
884 this->num_vert_lines = 20;
886 this->x_values_start = 10;
887 this->x_values_increment = 10;
899 void UpdateExcludedData()
928 this->line_height = size->height;
929 size->height = this->line_height * 11;
951 if (pos-- > 0)
continue;
952 if (--max < 0)
break;
954 bool lowered = !
HasBit(_legend_excluded_cargo, cs->
Index());
957 if (lowered)
DrawFrameRect(r.left, y, r.right, y + this->line_height - 1, COLOUR_BROWN, lowered ?
FR_LOWERED : FR_NONE);
959 byte clk_dif = lowered ? 1 : 0;
963 GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
965 DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
976 _legend_excluded_cargo = 0;
996 if (row >= this->vscroll->
GetCount())
return;
1000 if (row-- > 0)
continue;
1003 this->UpdateExcludedData();
1029 if (!gui_scope)
return;
1035 this->UpdateExcludedData();
1040 this->colours[i] = cs->legend_colour;
1041 for (uint j = 0; j != 20; j++) {
1042 this->
cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->
Index());
1046 this->num_dataset = i;
1050 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
1061 NWidget(
WWT_TEXT, COLOUR_BROWN,
WID_CPR_HEADER),
SetMinimalSize(0, 6),
SetPadding(2, 0, 2, 0),
SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE, STR_NULL),
1081 NWidget(
WWT_TEXT, COLOUR_BROWN,
WID_CPR_FOOTER),
SetMinimalSize(0, 6),
SetPadding(2, 0, 2, 0),
SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_X_LABEL, STR_NULL),
1089 WDP_AUTO,
"graph_cargo_payment_rates", 0, 0,
1092 _nested_cargo_payment_rates_widgets,
lengthof(_nested_cargo_payment_rates_widgets)
1096 void ShowCargoPaymentRates()
1098 AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
1105 static const StringID _performance_titles[] = {
1106 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1107 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1108 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1109 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1110 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1111 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1112 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1113 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1114 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1115 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1116 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1117 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1118 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1119 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1120 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
1121 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
1124 static inline StringID GetPerformanceTitleFromValue(uint value)
1126 return _performance_titles[std::min(value, 1000u) >> 6];
1144 this->companies.clear();
1147 this->companies.push_back(c);
1150 this->companies.shrink_to_fit();
1190 for (uint i = 0; i != this->companies.size(); i++) {
1191 const Company *c = this->companies[i];
1192 DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
1199 DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
1208 this->ordinal_width = 0;
1212 this->ordinal_width += 5;
1214 uint widest_width = 0;
1215 uint widest_title = 0;
1216 for (uint i = 0; i <
lengthof(_performance_titles); i++) {
1218 if (
width > widest_width) {
1220 widest_width =
width;
1225 this->icon_width = d.width + 2;
1231 SetDParam(2, _performance_titles[widest_title]);
1235 this->text_width = widest_width + 30;
1265 static const NWidgetPart _nested_company_league_widgets[] = {
1279 _nested_company_league_widgets,
lengthof(_nested_company_league_widgets)
1282 void ShowCompanyLeagueTable()
1284 AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
1297 this->UpdateCompanyStats();
1303 void UpdateCompanyStats()
1314 uint score_info_left;
1315 uint score_info_right;
1320 uint score_detail_left;
1321 uint score_detail_right;
1330 uint score_info_width = 0;
1331 for (uint i = SCORE_BEGIN; i <
SCORE_END; i++) {
1346 int max = -(999999999 - 500);
1359 if (_currency->rate < 1000) max /= _currency->rate;
1364 size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
1366 uint right = size->width - 7;
1369 this->score_info_left = rtl ? right - score_info_width :
left;
1370 this->score_info_right = rtl ? right :
left + score_info_width;
1372 this->score_detail_left = rtl ?
left : right - score_detail_width;
1373 this->score_detail_right = rtl ?
left + score_detail_width : right;
1375 this->bar_left =
left + (rtl ? score_detail_width : score_info_width) + 5;
1376 this->bar_right = this->bar_left + this->bar_width;
1389 int offset = (cid == this->company) ? 1 : 0;
1391 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
1404 int64 val = _score_part[company][score_type];
1415 uint text_top = bar_top + 2;
1417 DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
1421 DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING,
SA_RIGHT);
1424 uint x = Clamp<int64>(val, 0, needed) * this->bar_width / needed;
1427 x = this->bar_right - x;
1429 x = this->bar_left + x;
1433 if (x != this->bar_left)
GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
1434 if (x != this->bar_right)
GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
1437 SetDParam(0, Clamp<int64>(val, 0, needed) * 100 / needed);
1438 DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING,
SA_HOR_CENTER);
1441 if (score_type == SCORE_LOAN) val = needed - val;
1447 switch (score_type) {
1448 case SCORE_MIN_PROFIT:
1449 case SCORE_MIN_INCOME:
1450 case SCORE_MAX_INCOME:
1453 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
1456 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
1477 if (--this->timeout == 0) {
1478 this->UpdateCompanyStats();
1490 if (!gui_scope)
return;
1505 this->company = c->index;
1525 const StringID performance_tips[] = {
1526 STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
1527 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
1528 STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
1529 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
1530 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
1531 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
1532 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
1533 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
1534 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
1535 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
1557 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
1570 static WindowDesc _performance_rating_detail_desc(
1574 _nested_performance_rating_detail_widgets,
lengthof(_nested_performance_rating_detail_widgets)
1577 void ShowPerformanceRatingDetail()
1579 AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
1582 void InitializeGraphGui()
1584 _legend_excluded_companies = 0;
1585 _legend_excluded_cargo = 0;