1 /** @file triphistory_gui.cpp */
4 #include "triphistory.h"
5 #include "strings_func.h"
6 #include "window_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
{
20 VTH_LABEL_PROFIT_CHANGE
,
25 VTH_MATRIX_PROFIT_CHANGE
,
27 VTH_MATRIX_TBT_CHANGE
,
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
),
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),
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),
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),
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),
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),
65 NWidget(WWT_PANEL
, COLOUR_GREY
, VTH_SUMMARY
), SetMinimalTextLines(3, 2), SetResize(1, 0), SetFill(1, 0), EndContainer(),
68 struct VehicleTripHistoryWindow
: Window
{
71 uint8 valid_rows
; // number of rows in trip history
74 VehicleTripHistoryWindow(WindowDesc
*desc
, WindowNumber window_number
) :
75 Window(desc
), valid_rows(0)
77 const Vehicle
*v
= Vehicle::GetIfValid(window_number
);
81 this->CreateNestedTree();
83 this->FinishInitNested(window_number
);
84 this->owner
= v
->owner
;
88 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
90 Vehicle
*v
= Vehicle::GetIfValid(this->window_number
);
94 valid_rows
= (uint8
)v
->trip_history
.UpdateCalculated();
98 virtual void SetStringParameters(int widget
) const
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
)
109 SetDParam(0, UINT64_MAX
>> 2);
111 Dimension text_dim
= GetStringBoundingBox(STR_TRIP_HISTORY_TOTAL_INCOME
);
112 size
->width
= text_dim
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
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
;
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
:
131 case VTH_MATRIX_TBT_CHANGE
:
132 resize
->height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
133 size
->height
= 10 * resize
->height
;
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
;
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
);
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
);
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
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
);
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
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
);
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
);