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_AXIS_LINE_COLOUR =
PC_BLACK;
187 uint16 x_values_start;
188 uint16 x_values_increment;
192 byte colours[GRAPH_MAX_DATASETS];
203 assert(num_hori_lines > 0);
206 current_interval.
highest = INT64_MIN;
207 current_interval.
lowest = INT64_MAX;
209 for (
int i = 0; i < this->num_dataset; i++) {
210 if (
HasBit(this->excluded_data, i))
continue;
211 for (
int j = 0; j < this->num_on_x_axis; j++) {
214 if (datapoint != INVALID_DATAPOINT) {
215 current_interval.
highest = std::max(current_interval.
highest, datapoint);
216 current_interval.
lowest = std::min(current_interval.
lowest, datapoint);
223 current_interval.
lowest = (11 * current_interval.
lowest) / 10;
226 double abs_lower = (current_interval.
lowest > 0) ? 0 : (
double)
abs(current_interval.
lowest);
227 double abs_higher = (current_interval.
highest < 0) ? 0 : (
double)current_interval.
highest;
232 if (abs_lower != 0 || abs_higher != 0) {
234 num_pos_grids = (int)floor(0.5 + num_hori_lines * abs_higher / (abs_higher + abs_lower));
237 if (num_pos_grids == 0 && abs_higher != 0) num_pos_grids++;
238 if (num_pos_grids == num_hori_lines && abs_lower != 0) num_pos_grids--;
241 int64 grid_size_higher = (abs_higher > 0) ? ((int64)abs_higher + num_pos_grids - 1) / num_pos_grids : 0;
242 int64 grid_size_lower = (abs_lower > 0) ? ((int64)abs_lower + num_hori_lines - num_pos_grids - 1) / (num_hori_lines - num_pos_grids) : 0;
243 grid_size = std::max(grid_size_higher, grid_size_lower);
246 num_pos_grids = num_hori_lines / 2;
250 current_interval.
highest = num_pos_grids * grid_size;
251 current_interval.
lowest = -(num_hori_lines - num_pos_grids) * grid_size;
252 return current_interval;
263 int64 y_label = current_interval.
highest;
264 int64 y_label_separation = (current_interval.
highest - current_interval.
lowest) / num_hori_lines;
268 for (
int i = 0; i < (num_hori_lines + 1); i++) {
272 if (d.width > max_width) max_width = d.width;
274 y_label -= y_label_separation;
293 assert(this->num_vert_lines > 0);
314 r.left += label_width;
316 int x_sep = (r.right - r.left) / this->num_vert_lines;
317 int y_sep = (r.bottom - r.top) / num_hori_lines;
321 r.right = r.left + x_sep * this->num_vert_lines;
322 r.bottom = r.top + y_sep * num_hori_lines;
326 x_axis_offset = (int)((r.bottom - r.top) * (double)interval.
highest / (
double)interval_size);
336 for (
int i = 0; i < this->num_vert_lines; i++) {
344 for (
int i = 0; i < (num_hori_lines + 1); i++) {
345 GfxFillRect(r.left - 3, y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
351 GfxFillRect(r.left, r.top, r.left, r.bottom, GRAPH_AXIS_LINE_COLOUR);
354 y = x_axis_offset + r.top;
355 GfxFillRect(r.left, y, r.right, y, GRAPH_AXIS_LINE_COLOUR);
358 if (this->num_on_x_axis == 0)
return;
360 assert(this->num_on_x_axis > 0);
361 assert(this->num_dataset > 0);
364 int64 y_label = interval.
highest;
365 int64 y_label_separation =
abs(interval.
highest - interval.
lowest) / num_hori_lines;
369 for (
int i = 0; i < (num_hori_lines + 1); i++) {
374 y_label -= y_label_separation;
379 if (this->month != 0xFF) {
382 byte month = this->month;
383 Year year = this->year;
384 for (
int i = 0; i < this->num_on_x_axis; i++) {
385 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
386 SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
401 uint16 label = this->x_values_start;
403 for (
int i = 0; i < this->num_on_x_axis; i++) {
407 label += this->x_values_increment;
414 uint pointoffs1 = (linewidth + 1) / 2;
415 uint pointoffs2 = linewidth + 1 - pointoffs1;
416 for (
int i = 0; i < this->num_dataset; i++) {
417 if (!
HasBit(this->excluded_data, i)) {
419 x = r.left + (x_sep / 2);
421 byte colour = this->colours[i];
422 uint prev_x = INVALID_DATAPOINT_POS;
423 uint prev_y = INVALID_DATAPOINT_POS;
425 for (
int j = 0; j < this->num_on_x_axis; j++) {
428 if (datapoint != INVALID_DATAPOINT) {
441 int reduce_range = std::max(mult_range - 31, 0);
445 datapoint = -(
abs(datapoint) >> reduce_range);
447 datapoint >>= reduce_range;
449 y = r.top + x_axis_offset - ((r.bottom - r.top) * datapoint) / (interval_size >> reduce_range);
452 GfxFillRect(x - pointoffs1, y - pointoffs1, x + pointoffs2, y + pointoffs2, colour);
455 if (prev_x != INVALID_DATAPOINT_POS) GfxDrawLine(prev_x, prev_y, x, y, colour, linewidth);
460 prev_x = INVALID_DATAPOINT_POS;
461 prev_y = INVALID_DATAPOINT_POS;
473 format_str_y_axis(format_str_y_axis)
476 this->num_vert_lines = 24;
477 this->graph_widget = widget;
491 if (widget != this->graph_widget)
return;
493 uint x_label_width = 0;
495 if (this->month != 0xFF) {
496 byte month = this->month;
497 Year year = this->year;
498 for (
int i = 0; i < this->num_on_x_axis; i++) {
499 SetDParam(0, month + STR_MONTH_ABBREV_JAN);
500 SetDParam(1, month + STR_MONTH_ABBREV_JAN + 2);
502 x_label_width = std::max(x_label_width,
GetStringBoundingBox(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH).
width);
520 size->width = std::max<uint>(size->width, 5 + y_label_width + this->num_on_x_axis * (x_label_width + 5) + 9);
522 size->height = std::max<uint>(size->height, size->width / 3);
527 if (widget != this->graph_widget)
return;
534 return INVALID_DATAPOINT;
555 if (!gui_scope)
return;
565 CompanyMask excluded_companies = _legend_excluded_companies;
574 nums = std::min(this->num_vert_lines, std::max(nums, c->num_valid_stat_ent));
584 if (!initialize && this->excluded_data == excluded_companies && this->num_on_x_axis == nums &&
585 this->year == yr && this->month == mo) {
590 this->excluded_data = excluded_companies;
591 this->num_on_x_axis = nums;
600 for (
int j = this->num_on_x_axis, i = 0; --j >= 0;) {
601 this->cost[numd][i] = (j >= c->
num_valid_stat_ent) ? INVALID_DATAPOINT : GetGraphData(c, j);
608 this->num_dataset = numd;
630 static const NWidgetPart _nested_operating_profit_widgets[] = {
651 WDP_AUTO,
"graph_operating_profit", 0, 0,
654 _nested_operating_profit_widgets,
lengthof(_nested_operating_profit_widgets)
658 void ShowOperatingProfitGraph()
660 AllocateWindowDescFront<OperatingProfitGraphWindow>(&_operating_profit_desc, 0);
681 static const NWidgetPart _nested_income_graph_widgets[] = {
705 _nested_income_graph_widgets,
lengthof(_nested_income_graph_widgets)
708 void ShowIncomeGraph()
710 AllocateWindowDescFront<IncomeGraphWindow>(&_income_graph_desc, 0);
730 static const NWidgetPart _nested_delivered_cargo_graph_widgets[] = {
750 static WindowDesc _delivered_cargo_graph_desc(
751 WDP_AUTO,
"graph_delivered_cargo", 0, 0,
754 _nested_delivered_cargo_graph_widgets,
lengthof(_nested_delivered_cargo_graph_widgets)
757 void ShowDeliveredCargoGraph()
759 AllocateWindowDescFront<DeliveredCargoGraphWindow>(&_delivered_cargo_graph_desc, 0);
785 static const NWidgetPart _nested_performance_history_widgets[] = {
807 WDP_AUTO,
"graph_performance", 0, 0,
810 _nested_performance_history_widgets,
lengthof(_nested_performance_history_widgets)
813 void ShowPerformanceHistoryGraph()
815 AllocateWindowDescFront<PerformanceHistoryGraphWindow>(&_performance_history_desc, 0);
835 static const NWidgetPart _nested_company_value_graph_widgets[] = {
856 WDP_AUTO,
"graph_company_value", 0, 0,
859 _nested_company_value_graph_widgets,
lengthof(_nested_company_value_graph_widgets)
862 void ShowCompanyValueGraph()
864 AllocateWindowDescFront<CompanyValueGraphWindow>(&_company_value_graph_desc, 0);
878 this->num_on_x_axis = 20;
879 this->num_vert_lines = 20;
881 this->x_values_start = 10;
882 this->x_values_increment = 10;
894 void UpdateExcludedData()
923 this->line_height = size->height;
924 size->height = this->line_height * 11;
946 if (pos-- > 0)
continue;
947 if (--max < 0)
break;
949 bool lowered = !
HasBit(_legend_excluded_cargo, cs->
Index());
952 if (lowered)
DrawFrameRect(r.left, y, r.right, y + this->line_height - 1, COLOUR_ORANGE, lowered ?
FR_LOWERED : FR_NONE);
954 byte clk_dif = lowered ? 1 : 0;
958 GfxFillRect(rect_x + 1, y + 1 + clk_dif, rect_x + 7, y + 4 + clk_dif, cs->legend_colour);
960 DrawString(rtl ? r.left : x + 14 + clk_dif, (rtl ? r.right - 14 + clk_dif : r.right), y + clk_dif, STR_GRAPH_CARGO_PAYMENT_CARGO);
971 _legend_excluded_cargo = 0;
991 if (row >= this->vscroll->
GetCount())
return;
995 if (row-- > 0)
continue;
998 this->UpdateExcludedData();
1024 if (!gui_scope)
return;
1030 this->UpdateExcludedData();
1035 this->colours[i] = cs->legend_colour;
1036 for (uint j = 0; j != 20; j++) {
1037 this->
cost[i][j] = GetTransportedGoodsIncome(10, 20, j * 4 + 4, cs->
Index());
1041 this->num_dataset = i;
1045 static const NWidgetPart _nested_cargo_payment_rates_widgets[] = {
1056 NWidget(
WWT_TEXT, COLOUR_GREY,
WID_CPR_HEADER),
SetMinimalSize(0, 6),
SetPadding(2, 0, 2, 0),
SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_TITLE, STR_NULL),
1076 NWidget(
WWT_TEXT, COLOUR_GREY,
WID_CPR_FOOTER),
SetMinimalSize(0, 6),
SetPadding(2, 0, 2, 0),
SetDataTip(STR_GRAPH_CARGO_PAYMENT_RATES_X_LABEL, STR_NULL),
1084 WDP_AUTO,
"graph_cargo_payment_rates", 0, 0,
1087 _nested_cargo_payment_rates_widgets,
lengthof(_nested_cargo_payment_rates_widgets)
1091 void ShowCargoPaymentRates()
1093 AllocateWindowDescFront<PaymentRatesGraphWindow>(&_cargo_payment_rates_desc, 0);
1100 static const StringID _performance_titles[] = {
1101 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1102 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ENGINEER,
1103 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1104 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRAFFIC_MANAGER,
1105 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1106 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TRANSPORT_COORDINATOR,
1107 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1108 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_ROUTE_SUPERVISOR,
1109 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1110 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_DIRECTOR,
1111 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1112 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHIEF_EXECUTIVE,
1113 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1114 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_CHAIRMAN,
1115 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_PRESIDENT,
1116 STR_COMPANY_LEAGUE_PERFORMANCE_TITLE_TYCOON,
1119 static inline StringID GetPerformanceTitleFromValue(uint value)
1121 return _performance_titles[std::min(value, 1000u) >> 6];
1139 this->companies.clear();
1142 this->companies.push_back(c);
1145 this->companies.shrink_to_fit();
1185 for (uint i = 0; i != this->companies.size(); i++) {
1186 const Company *c = this->companies[i];
1187 DrawString(ordinal_left, ordinal_right, y, i + STR_ORDINAL_NUMBER_1ST, i == 0 ? TC_WHITE : TC_YELLOW);
1194 DrawString(text_left, text_right, y, STR_COMPANY_LEAGUE_COMPANY_NAME);
1203 this->ordinal_width = 0;
1207 this->ordinal_width += 5;
1209 uint widest_width = 0;
1210 uint widest_title = 0;
1211 for (uint i = 0; i <
lengthof(_performance_titles); i++) {
1213 if (
width > widest_width) {
1215 widest_width =
width;
1220 this->icon_width = d.width + 2;
1226 SetDParam(2, _performance_titles[widest_title]);
1230 this->text_width = widest_width + 30;
1260 static const NWidgetPart _nested_company_league_widgets[] = {
1274 _nested_company_league_widgets,
lengthof(_nested_company_league_widgets)
1277 void ShowCompanyLeagueTable()
1279 AllocateWindowDescFront<CompanyLeagueWindow>(&_company_league_desc, 0);
1292 this->UpdateCompanyStats();
1298 void UpdateCompanyStats()
1309 uint score_info_left;
1310 uint score_info_right;
1315 uint score_detail_left;
1316 uint score_detail_right;
1325 uint score_info_width = 0;
1326 for (uint i = SCORE_BEGIN; i <
SCORE_END; i++) {
1341 int max = -(999999999 - 500);
1354 if (_currency->rate < 1000) max /= _currency->rate;
1359 size->width = 7 + score_info_width + 5 + this->bar_width + 5 + score_detail_width + 7;
1361 uint right = size->width - 7;
1364 this->score_info_left = rtl ? right - score_info_width :
left;
1365 this->score_info_right = rtl ? right :
left + score_info_width;
1367 this->score_detail_left = rtl ?
left : right - score_detail_width;
1368 this->score_detail_right = rtl ?
left + score_detail_width : right;
1370 this->bar_left =
left + (rtl ? score_detail_width : score_info_width) + 5;
1371 this->bar_right = this->bar_left + this->bar_width;
1384 int offset = (cid == this->company) ? 1 : 0;
1386 DrawCompanyIcon(cid, (r.left + r.right - sprite_size.width) / 2 + offset, (r.top + r.bottom - sprite_size.height) / 2 + offset);
1399 int64 val = _score_part[company][score_type];
1410 uint text_top = bar_top + 2;
1412 DrawString(this->score_info_left, this->score_info_right, text_top, STR_PERFORMANCE_DETAIL_VEHICLES + score_type);
1416 DrawString(this->score_info_left, this->score_info_right, text_top, STR_BLACK_COMMA, TC_FROMSTRING,
SA_RIGHT);
1419 uint x = Clamp<int64>(val, 0, needed) * this->bar_width / needed;
1422 x = this->bar_right - x;
1424 x = this->bar_left + x;
1428 if (x != this->bar_left)
GfxFillRect(this->bar_left, bar_top, x, bar_top + this->bar_height, rtl ? colour_notdone : colour_done);
1429 if (x != this->bar_right)
GfxFillRect(x, bar_top, this->bar_right, bar_top + this->bar_height, rtl ? colour_done : colour_notdone);
1432 SetDParam(0, Clamp<int64>(val, 0, needed) * 100 / needed);
1433 DrawString(this->bar_left, this->bar_right, text_top, STR_PERFORMANCE_DETAIL_PERCENT, TC_FROMSTRING,
SA_HOR_CENTER);
1436 if (score_type == SCORE_LOAN) val = needed - val;
1442 switch (score_type) {
1443 case SCORE_MIN_PROFIT:
1444 case SCORE_MIN_INCOME:
1445 case SCORE_MAX_INCOME:
1448 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_CURRENCY);
1451 DrawString(this->score_detail_left, this->score_detail_right, text_top, STR_PERFORMANCE_DETAIL_AMOUNT_INT);
1472 if (--this->timeout == 0) {
1473 this->UpdateCompanyStats();
1485 if (!gui_scope)
return;
1500 this->company = c->index;
1520 const StringID performance_tips[] = {
1521 STR_PERFORMANCE_DETAIL_VEHICLES_TOOLTIP,
1522 STR_PERFORMANCE_DETAIL_STATIONS_TOOLTIP,
1523 STR_PERFORMANCE_DETAIL_MIN_PROFIT_TOOLTIP,
1524 STR_PERFORMANCE_DETAIL_MIN_INCOME_TOOLTIP,
1525 STR_PERFORMANCE_DETAIL_MAX_INCOME_TOOLTIP,
1526 STR_PERFORMANCE_DETAIL_DELIVERED_TOOLTIP,
1527 STR_PERFORMANCE_DETAIL_CARGO_TOOLTIP,
1528 STR_PERFORMANCE_DETAIL_MONEY_TOOLTIP,
1529 STR_PERFORMANCE_DETAIL_LOAN_TOOLTIP,
1530 STR_PERFORMANCE_DETAIL_TOTAL_TOOLTIP,
1552 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
1565 static WindowDesc _performance_rating_detail_desc(
1569 _nested_performance_rating_detail_widgets,
lengthof(_nested_performance_rating_detail_widgets)
1572 void ShowPerformanceRatingDetail()
1574 AllocateWindowDescFront<PerformanceRatingDetailWindow>(&_performance_rating_detail_desc, 0);
1577 void InitializeGraphGui()
1579 _legend_excluded_companies = 0;
1580 _legend_excluded_cargo = 0;