Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / triphistory_gui.cpp
blobd8978a755f7390dcce466e62e68e54bbac937cbd
1 /** @file triphistory_gui.cpp */
3 #include "stdafx.h"
4 #include "triphistory.h"
5 #include "strings_func.h"
6 #include "window_func.h"
7 #include "gfx_func.h"
8 #include "date_func.h"
9 #include "vehicle_base.h"
10 #include "table/strings.h"
12 /* Names of the widgets. Keep them in the same order as in the widget array */
13 enum VehicleTripWidgets {
14 VTH_CAPTION,
15 VTH_LABEL_RECEIVED,
16 VTH_LABEL_PROFIT,
17 VTH_LABEL_PERCHANGE,
18 VTH_LABEL_TBT,
19 VTH_LABEL_DAYCHANGE,
20 VTH_MATRIX_RECEIVED,
21 VTH_MATRIX_PROFIT,
22 VTH_MATRIX_PERCHANGE,
23 VTH_MATRIX_TBT,
24 VTH_MATRIX_DAYCHANGE,
25 VTH_SUMMARY,
28 static const NWidgetPart _vehicle_trip_history_widgets[] = {
29 NWidget(NWID_HORIZONTAL),
30 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
31 NWidget(WWT_CAPTION, COLOUR_GREY, VTH_CAPTION), SetDataTip(STR_TRIP_HISTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
32 NWidget(WWT_SHADEBOX, COLOUR_GREY),
33 NWidget(WWT_STICKYBOX, COLOUR_GREY),
34 EndContainer(),
35 NWidget(NWID_HORIZONTAL),
36 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_RECEIVED), SetMinimalSize(110, 0), SetMinimalTextLines(1, 2), SetResize(1, 0), SetFill(1, 0),
37 SetDataTip(STR_TRIP_HISTORY_RECEIVED_LABEL, STR_TRIP_HISTORY_RECEIVED_LABEL_TIP),
38 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_PROFIT), SetMinimalSize(110, 0), SetMinimalTextLines(1, 2), SetResize(1, 0), SetFill(1, 0),
39 SetDataTip(STR_TRIP_HISTORY_PROFIT_LABEL, STR_TRIP_HISTORY_PROFIT_LABEL_TIP),
40 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_PERCHANGE), SetMinimalSize(50, 0), SetMinimalTextLines(1, 2), SetResize(1, 0),SetFill(1, 0),
41 SetDataTip(STR_TRIP_HISTORY_DAYCHANGE_LABEL, STR_TRIP_HISTORY_PERCHANGE_LABEL_TIP),
42 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_TBT), SetMinimalSize(70, 0), SetMinimalTextLines(1, 2), SetResize(1, 0),SetFill(1, 0),
43 SetDataTip(STR_TRIP_HISTORY_TBT_LABEL, STR_TRIP_HISTORY_TBT_LABEL_TIP),
44 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_DAYCHANGE), SetMinimalSize(50, 0), SetMinimalTextLines(1, 2), SetResize(1, 0),SetFill(1, 0),
45 SetDataTip(STR_TRIP_HISTORY_DAYCHANGE_LABEL, STR_TRIP_HISTORY_DAYCHANGE_LABEL_TIP),
46 EndContainer(),
47 NWidget(NWID_HORIZONTAL),
48 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_RECEIVED), SetMinimalSize(110, 0), SetDataTip((10 << MAT_ROW_START) | (1 << MAT_COL_START), STR_NULL), SetResize(1, 1),SetFill(1, 0),
49 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_PROFIT), SetMinimalSize(110, 0), SetDataTip((10 << MAT_ROW_START) | (1 << MAT_COL_START), STR_NULL), SetResize(1, 1),SetFill(1, 0),
50 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_PERCHANGE), SetMinimalSize(50, 0), SetDataTip((10 << MAT_ROW_START) | (1 << MAT_COL_START), STR_NULL), SetResize(1, 1),SetFill(1, 0),
51 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_TBT), SetMinimalSize(70, 0), SetDataTip((10 << MAT_ROW_START) | (1 << MAT_COL_START), STR_NULL), SetResize(1, 1),SetFill(1, 0),
52 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_DAYCHANGE), SetMinimalSize(50, 0), SetDataTip((10 << MAT_ROW_START) | (1 << MAT_COL_START), STR_NULL), SetResize(1, 1),SetFill(1, 0),
53 EndContainer(),
54 NWidget(WWT_PANEL, COLOUR_GREY, VTH_SUMMARY), SetMinimalTextLines(3, 2), SetResize(1, 0), SetFill(1, 0), EndContainer(),
57 struct VehicleTripHistoryWindow : Window {
59 private:
60 uint8 valid_rows; // number of rows in trip history
61 public:
62 VehicleTripHistoryWindow(WindowDesc *desc, WindowNumber window_number) :
63 Window(desc), valid_rows( 0 )
65 const Vehicle *v = Vehicle::Get(window_number);
66 this->CreateNestedTree();
68 this->FinishInitNested(window_number);
69 this->owner = v->owner;
70 InvalidateData();
73 ~VehicleTripHistoryWindow() {
74 if (Vehicle::IsValidID(this->window_number)) {
75 Vehicle *v = Vehicle::Get(this->window_number);
76 free(v->trip_history_pchange_array);
77 free(v->trip_history_TBT_array);
78 free(v->trip_history_TBT_change_array);
79 v->trip_history_avg_daylength = 0;
80 v->trip_history_profitpd = 0;
81 v->trip_history_total_change = 0;
82 v->trip_history_total_profit = 0;
84 }*/
86 virtual void OnInvalidateData(int data = 0, bool gui_scope = true) {
87 Vehicle *v = Vehicle::Get(this->window_number);
88 valid_rows = (uint8)v->trip_history.UpdateCalculated();
89 this->SetDirty();
92 virtual void SetStringParameters(int widget) const
94 switch (widget) {
95 case VTH_CAPTION: SetDParam(0, this->window_number); break;
99 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
101 switch (widget) {
102 case VTH_SUMMARY: {
103 SetDParam(0, UINT64_MAX >> 2);
104 SetDParam(1, 100);
105 Dimension text_dim = GetStringBoundingBox(STR_TRIP_HISTORY_TOTALINCOME);
106 size->width = text_dim.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
107 break;
110 case VTH_LABEL_RECEIVED:
111 case VTH_MATRIX_RECEIVED: {
112 SetDParam(0, _date);
113 Dimension text_dim = GetStringBoundingBox(STR_TRIP_HISTORY_DATE);
114 size->width = text_dim.width + WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
115 break;
117 case VTH_MATRIX_PROFIT:
118 case VTH_MATRIX_PERCHANGE:
119 case VTH_MATRIX_TBT:
120 case VTH_MATRIX_DAYCHANGE:
121 resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
122 size->height = 10 * resize->height;
123 break;
126 virtual void DrawWidget(const Rect &r, int widget) const {
127 const Vehicle *v = Vehicle::Get(this->window_number);
128 int y = WD_FRAMERECT_TOP;
130 switch( widget ) {
131 case VTH_MATRIX_RECEIVED:
132 for(int i = 0; i <= valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
133 if (v->trip_history.t[i].date > 0) {
134 SetDParam(0, v->trip_history.t[i].date);
135 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, STR_TRIP_HISTORY_DATE, TC_BLACK, SA_RIGHT);
138 break;
139 case VTH_MATRIX_PROFIT:
140 for(int i = 0; i <= valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
141 if (v->trip_history.t[i].date > 0) {
142 if ( v->trip_history.t[i].profit > 0 ) {
143 SetDParam(0, v->trip_history.t[i].profit );
144 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, STR_TRIP_HISTORY_PROFIT, TC_BLACK, SA_RIGHT);
145 } else {
146 SetDParam(0, -v->trip_history.t[i].profit);
147 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, STR_TRIP_HISTORY_VIRTUAL_PROFIT, TC_BLACK, SA_RIGHT);
151 break;
152 case VTH_MATRIX_PERCHANGE:
153 for(int i = 0; i <= valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
154 if (v->trip_history.t[i+1].date > 0) {
155 SetDParam(0, v->trip_history.t[i].profit_change);
156 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y,
157 v->trip_history.t[i].profit_change >= 0 ?
158 STR_TRIP_HISTORY_PROFITCHANGEPOS :
159 STR_TRIP_HISTORY_PROFITCHANGENEG, TC_BLACK, SA_RIGHT
163 break;
164 case VTH_MATRIX_TBT:
165 for(int i = 0; i <= valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
166 if (v->trip_history.t[i].date > 0) {
167 SetDParam(0, v->trip_history.t[i].TBT);
168 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, STR_TRIP_HISTORY_TBT, TC_BLACK, SA_RIGHT);
171 break;
172 case VTH_MATRIX_DAYCHANGE:
173 for(int i = 0; i <= valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
174 if (v->trip_history.t[i+1].date > 0) {
175 SetDParam(0, v->trip_history.t[i].TBT_change);
176 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y,
177 v->trip_history.t[i].TBT_change > 0 ?
178 STR_TRIP_HISTORY_TBTCHANGEPOS :
179 STR_TRIP_HISTORY_TBTCHANGENEG, TC_BLACK, SA_RIGHT
183 break;
184 case VTH_SUMMARY:
185 SetDParam(0, valid_rows + 1);
186 SetDParam(1, v->trip_history.total_profit);
187 SetDParam(2, v->trip_history.profit_per_day);
188 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_TRIP_HISTORY_TOTALINCOME, TC_BLACK);
189 SetDParam(0, v->trip_history.avg_daylength);
190 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, STR_TRIP_HISTORY_DAYAVERAGE, TC_BLACK);
191 SetDParam(0, valid_rows + 1);
192 SetDParam(1, v->trip_history.total_change);
193 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + 2*FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, STR_TRIP_HISTORY_DAYAVERAGE_IMPROVEMENT, TC_BLACK);
194 break;
199 static WindowDesc _vehicle_trip_history(
200 WDP_AUTO, "trip_history", 380, 191,
201 WC_VEHICLE_TRIP_HISTORY,WC_VEHICLE_DETAILS,
203 _vehicle_trip_history_widgets,
204 lengthof(_vehicle_trip_history_widgets)
207 void ShowTripHistoryWindow(const Vehicle *v)
209 if (!BringWindowToFrontById(WC_VEHICLE_TRIP_HISTORY, v->index)) {
210 AllocateWindowDescFront<VehicleTripHistoryWindow>(&_vehicle_trip_history, v->index);