Hotfix: Conditional order comparator dropdown list was broken.
[openttd-joker.git] / src / triphistory_gui.cpp
blob8b0cbe29e57787004936e905f7634fdc7b601d00
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 "widget_type.h"
11 #include "window_gui.h"
12 #include "triphistory.h"
13 #include "table/strings.h"
15 /* Names of the widgets. Keep them in the same order as in the widget array */
16 enum VehicleTripWidgets {
17 VTH_CAPTION,
18 VTH_LABEL_RECEIVED,
19 VTH_LABEL_PROFIT,
20 VTH_LABEL_PROFIT_CHANGE,
21 VTH_LABEL_TBT,
22 VTH_LABEL_TBT_CHANGE,
23 VTH_MATRIX_RECEIVED,
24 VTH_MATRIX_PROFIT,
25 VTH_MATRIX_PROFIT_CHANGE,
26 VTH_MATRIX_TBT,
27 VTH_MATRIX_TBT_CHANGE,
28 VTH_SUMMARY,
31 static const NWidgetPart _vehicle_trip_history_widgets[] = {
32 NWidget(NWID_HORIZONTAL),
33 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
34 NWidget(WWT_CAPTION, COLOUR_GREY, VTH_CAPTION), SetDataTip(STR_TRIP_HISTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
35 NWidget(WWT_SHADEBOX, COLOUR_GREY),
36 NWidget(WWT_STICKYBOX, COLOUR_GREY),
37 EndContainer(),
38 NWidget(NWID_HORIZONTAL),
39 NWidget(NWID_VERTICAL),
40 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_RECEIVED), SetMinimalSize(110, 0), SetMinimalTextLines(1, 2), SetResize(1, 0), SetFill(1, 0),
41 SetDataTip(STR_TRIP_HISTORY_RECEIVED_LABEL, STR_TRIP_HISTORY_RECEIVED_LABEL_TIP),
42 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_RECEIVED), SetMinimalSize(110, 0), SetMatrixDataTip(1, 10, STR_NULL), SetResize(1, 1),SetFill(1, 0),
43 EndContainer(),
44 NWidget(NWID_VERTICAL),
45 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_PROFIT), SetMinimalSize(110, 0), SetMinimalTextLines(1, 2), SetResize(1, 0), SetFill(1, 0),
46 SetDataTip(STR_TRIP_HISTORY_PROFIT_LABEL, STR_TRIP_HISTORY_PROFIT_LABEL_TIP),
47 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_PROFIT), SetMinimalSize(110, 0), SetMatrixDataTip(1, 10, STR_NULL), SetResize(1, 1),SetFill(1, 0),
48 EndContainer(),
49 NWidget(NWID_VERTICAL),
50 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_PROFIT_CHANGE), SetMinimalSize(50, 0), SetMinimalTextLines(1, 2), SetResize(1, 0),SetFill(1, 0),
51 SetDataTip(STR_TRIP_HISTORY_CHANGE_LABEL, STR_TRIP_HISTORY_CHANGE_LABEL_TIP),
52 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_PROFIT_CHANGE), SetMinimalSize(50, 0), SetMatrixDataTip(1, 10, STR_NULL), SetResize(1, 1),SetFill(1, 0),
53 EndContainer(),
54 NWidget(NWID_VERTICAL),
55 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_TBT), SetMinimalSize(110, 0), SetMinimalTextLines(1, 2), SetResize(1, 0),SetFill(1, 0),
56 SetDataTip(STR_TRIP_HISTORY_TBT_LABEL, STR_TRIP_HISTORY_TBT_LABEL_TIP),
57 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_TBT), SetMinimalSize(110, 0), SetMatrixDataTip(1, 10, STR_NULL), SetResize(1, 1),SetFill(1, 0),
58 EndContainer(),
59 NWidget(NWID_VERTICAL),
60 NWidget(WWT_TEXTBTN, COLOUR_GREY, VTH_LABEL_TBT_CHANGE), SetMinimalSize(50, 0), SetMinimalTextLines(1, 2), SetResize(1, 0),SetFill(1, 0),
61 SetDataTip(STR_TRIP_HISTORY_CHANGE_LABEL, STR_TRIP_HISTORY_CHANGE_LABEL_TIP),
62 NWidget(WWT_MATRIX, COLOUR_GREY, VTH_MATRIX_TBT_CHANGE), SetMinimalSize(50, 0), SetMatrixDataTip(1, 10, STR_NULL), SetResize(1, 1),SetFill(1, 0),
63 EndContainer(),
64 EndContainer(),
65 NWidget(WWT_PANEL, COLOUR_GREY, VTH_SUMMARY), SetMinimalTextLines(3, 2), SetResize(1, 0), SetFill(1, 0), EndContainer(),
68 struct VehicleTripHistoryWindow : Window {
70 private:
71 uint8 valid_rows; // number of rows in trip history
73 public:
74 VehicleTripHistoryWindow(WindowDesc *desc, WindowNumber window_number) :
75 Window(desc), valid_rows(0)
77 const Vehicle *v = Vehicle::GetIfValid(window_number);
79 assert(v != nullptr);
81 this->CreateNestedTree();
83 this->FinishInitNested(window_number);
84 this->owner = v->owner;
85 InvalidateData();
88 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
90 Vehicle *v = Vehicle::GetIfValid(this->window_number);
92 assert(v != nullptr);
94 valid_rows = (uint8)v->trip_history.UpdateCalculated();
95 this->SetDirty();
98 virtual void SetStringParameters(int widget) const
100 switch (widget) {
101 case VTH_CAPTION: SetDParam(0, this->window_number); break;
105 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
107 switch (widget) {
108 case VTH_SUMMARY: {
109 SetDParam(0, UINT64_MAX >> 2);
110 SetDParam(1, 100);
111 Dimension text_dim = GetStringBoundingBox(STR_TRIP_HISTORY_TOTAL_INCOME);
112 size->width = text_dim.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
113 break;
116 case VTH_LABEL_RECEIVED: {
117 SetDParam(0, INT32_MAX);
118 Dimension text_dim = GetStringBoundingBox(STR_TRIP_HISTORY_TIME);
119 size->width = text_dim.width + WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
120 break;
123 case VTH_MATRIX_RECEIVED: {
124 SetDParam(0, INT32_MAX);
125 Dimension text_dim = GetStringBoundingBox(STR_TRIP_HISTORY_TIME);
126 size->width = text_dim.width + WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
128 case VTH_MATRIX_PROFIT:
129 case VTH_MATRIX_PROFIT_CHANGE:
130 case VTH_MATRIX_TBT:
131 case VTH_MATRIX_TBT_CHANGE:
132 resize->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
133 size->height = 10 * resize->height;
134 break;
138 virtual void DrawWidget(const Rect &r, int widget) const
140 const Vehicle *v = Vehicle::Get(this->window_number);
141 int y = WD_FRAMERECT_TOP;
143 switch( widget ) {
144 case VTH_MATRIX_RECEIVED:
145 for (int i = 0; i < valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
146 if (v->trip_history.entries[i].ticks == 0) continue;
148 SetDParam(0, v->trip_history.entries[i].ticks);
149 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, i == 0 ? STR_TRIP_HISTORY_TEMPORARY_TIME : STR_TRIP_HISTORY_TIME, TC_BLACK, SA_RIGHT);
151 break;
153 case VTH_MATRIX_PROFIT:
154 for (int i = 0; i < valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
155 if (v->trip_history.entries[i].ticks == 0) continue;
157 SetDParam(0, v->trip_history.entries[i].profit);
158 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, i == 0 ? STR_TRIP_HISTORY_TEMPORARY_PROFIT : STR_TRIP_HISTORY_PROFIT, TC_BLACK, SA_RIGHT);
160 break;
162 case VTH_MATRIX_PROFIT_CHANGE:
163 for (int i = 0; i < valid_rows - 1; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
164 if (i == 0) continue;
165 if (v->trip_history.entries[i].ticks == 0) continue;
166 if (v->trip_history.entries[i + 1].ticks == 0) continue;
168 SetDParam(0, v->trip_history.entries[i].profit_change);
169 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y,
170 v->trip_history.entries[i].profit_change == 0 ? STR_TRIP_HISTORY_PROFITCHANGEZERO :
171 (v->trip_history.entries[i].profit_change >= 0 ?
172 STR_TRIP_HISTORY_PROFITCHANGEPOS :
173 STR_TRIP_HISTORY_PROFITCHANGENEG), TC_BLACK, SA_RIGHT
176 break;
178 case VTH_MATRIX_TBT:
179 for (int i = 0; i < valid_rows; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
180 if (i == 0) continue;
181 if (v->trip_history.entries[i].ticks == 0) continue;
182 if (v->trip_history.entries[i].time_between_trips == 0) continue;
184 SetDParam(0, v->trip_history.entries[i].time_between_trips);
185 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y, STR_TRIP_HISTORY_TBT, TC_BLACK, SA_RIGHT);
187 break;
189 case VTH_MATRIX_TBT_CHANGE:
190 for (int i = 0; i < valid_rows - 1; i++, y += FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM) {
191 if (i == 0) continue;
192 if (v->trip_history.entries[i].ticks == 0) continue;
193 if (v->trip_history.entries[i].time_between_trips == 0) continue;
194 if (v->trip_history.entries[i + 1].ticks == 0) continue;
195 if (v->trip_history.entries[i + 1].time_between_trips == 0) continue;
197 SetDParam(0, v->trip_history.entries[i].time_between_trips_change);
198 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + y,
199 v->trip_history.entries[i].time_between_trips_change > 0 ?
200 STR_TRIP_HISTORY_TBTCHANGEPOS :
201 STR_TRIP_HISTORY_TBTCHANGENEG, TC_BLACK, SA_RIGHT
204 break;
206 case VTH_SUMMARY:
207 SetDParam(0, (valid_rows > 0) ? (valid_rows - 1) : 0);
208 SetDParam(1, v->trip_history.total_profit);
209 SetDParam(2, v->trip_history.avg_profit);
210 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_TRIP_HISTORY_TOTAL_INCOME, TC_BLACK);
211 SetDParam(0, v->trip_history.avg_time_between_trips);
212 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, STR_TRIP_AVERAGE_TRIP_LENGTH, TC_BLACK);
213 break;
218 static WindowDesc _vehicle_trip_history_desc(
219 WDP_AUTO, "trip_history", 380, 191,
220 WC_VEHICLE_TRIP_HISTORY, WC_VEHICLE_DETAILS,
222 _vehicle_trip_history_widgets,
223 lengthof(_vehicle_trip_history_widgets)
226 void ShowTripHistoryWindow(const Vehicle *v)
228 if (!BringWindowToFrontById(WC_VEHICLE_TRIP_HISTORY, v->index)) {
229 AllocateWindowDescFront<VehicleTripHistoryWindow>(&_vehicle_trip_history_desc, v->index);