Update: Translations from eints
[openttd-github.git] / src / timetable_gui.cpp
blob629620241b6571cd8774a7c7e20867d37664456d
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file timetable_gui.cpp GUI for time tabling. */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "gui.h"
13 #include "window_gui.h"
14 #include "window_func.h"
15 #include "textbuf_gui.h"
16 #include "strings_func.h"
17 #include "vehicle_base.h"
18 #include "string_func.h"
19 #include "gfx_func.h"
20 #include "company_func.h"
21 #include "timer/timer.h"
22 #include "timer/timer_game_tick.h"
23 #include "timer/timer_game_economy.h"
24 #include "timer/timer_window.h"
25 #include "date_gui.h"
26 #include "vehicle_gui.h"
27 #include "settings_type.h"
28 #include "timetable_cmd.h"
29 #include "timetable.h"
31 #include "widgets/timetable_widget.h"
33 #include "table/sprites.h"
34 #include "table/strings.h"
36 #include "safeguards.h"
38 /** Container for the arrival/departure dates of a vehicle */
39 struct TimetableArrivalDeparture {
40 TimerGameTick::Ticks arrival; ///< The arrival time
41 TimerGameTick::Ticks departure; ///< The departure time
44 /**
45 * Set the timetable parameters in the format as described by the setting.
46 * @param param1 the first DParam to fill
47 * @param param2 the second DParam to fill
48 * @param ticks the number of ticks to 'draw'
50 void SetTimetableParams(int param1, int param2, TimerGameTick::Ticks ticks)
52 switch (_settings_client.gui.timetable_mode) {
53 case TimetableMode::Days:
54 SetDParam(param1, STR_UNITS_DAYS);
55 SetDParam(param2, ticks / Ticks::DAY_TICKS);
56 break;
57 case TimetableMode::Seconds:
58 SetDParam(param1, STR_UNITS_SECONDS);
59 SetDParam(param2, ticks / Ticks::TICKS_PER_SECOND);
60 break;
61 case TimetableMode::Ticks:
62 SetDParam(param1, STR_UNITS_TICKS);
63 SetDParam(param2, ticks);
64 break;
65 default:
66 NOT_REACHED();
70 /**
71 * Get the number of ticks in the current timetable display unit.
72 * @return The number of ticks per day, second, or tick, to match the timetable display.
74 static inline TimerGameTick::Ticks TicksPerTimetableUnit()
76 switch (_settings_client.gui.timetable_mode) {
77 case TimetableMode::Days:
78 return Ticks::DAY_TICKS;
79 case TimetableMode::Seconds:
80 return Ticks::TICKS_PER_SECOND;
81 case TimetableMode::Ticks:
82 return 1;
83 default:
84 NOT_REACHED();
88 /**
89 * Determine if a vehicle should be shown as late or early, using a threshold depending on the timetable display setting.
90 * @param ticks The number of ticks that the vehicle is late or early.
91 * @param round_to_day When using ticks, if we should round up to the nearest day.
92 * @return True if the vehicle is outside the "on time" threshold, either early or late.
94 bool VehicleIsAboveLatenessThreshold(TimerGameTick::Ticks ticks, bool round_to_day)
96 switch (_settings_client.gui.timetable_mode) {
97 case TimetableMode::Days:
98 return ticks > Ticks::DAY_TICKS;
99 case TimetableMode::Seconds:
100 return ticks > Ticks::TICKS_PER_SECOND;
101 case TimetableMode::Ticks:
102 return ticks > (round_to_day ? Ticks::DAY_TICKS : 0);
103 default:
104 NOT_REACHED();
109 * Check whether it is possible to determine how long the order takes.
110 * @param order the order to check.
111 * @param travelling whether we are interested in the travel or the wait part.
112 * @return true if the travel/wait time can be used.
114 static bool CanDetermineTimeTaken(const Order *order, bool travelling)
116 /* Current order is conditional */
117 if (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT)) return false;
118 /* No travel time and we have not already finished travelling */
119 if (travelling && !order->IsTravelTimetabled()) return false;
120 /* No wait time but we are loading at this timetabled station */
121 if (!travelling && !order->IsWaitTimetabled() && order->IsType(OT_GOTO_STATION) &&
122 !(order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) {
123 return false;
126 return true;
131 * Fill the table with arrivals and departures
132 * @param v Vehicle which must have at least 2 orders.
133 * @param start order index to start at
134 * @param travelling Are we still in the travelling part of the start order
135 * @param table Fill in arrival and departures including intermediate orders
136 * @param offset Add this value to result and all arrivals and departures
138 static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, std::vector<TimetableArrivalDeparture> &table, TimerGameTick::Ticks offset)
140 assert(!table.empty());
141 assert(v->GetNumOrders() >= 2);
142 assert(start < v->GetNumOrders());
144 /* Pre-initialize with unknown time */
145 for (int i = 0; i < v->GetNumOrders(); ++i) {
146 table[i].arrival = table[i].departure = Ticks::INVALID_TICKS;
149 TimerGameTick::Ticks sum = offset;
150 VehicleOrderID i = start;
151 const Order *order = v->GetOrder(i);
153 /* Cyclically loop over all orders until we reach the current one again.
154 * As we may start at the current order, do a post-checking loop */
155 do {
156 /* Automatic orders don't influence the overall timetable;
157 * they just add some untimetabled entries, but the time till
158 * the next non-implicit order can still be known. */
159 if (!order->IsType(OT_IMPLICIT)) {
160 if (travelling || i != start) {
161 if (!CanDetermineTimeTaken(order, true)) return;
162 sum += order->GetTimetabledTravel();
163 table[i].arrival = sum;
166 if (!CanDetermineTimeTaken(order, false)) return;
167 sum += order->GetTimetabledWait();
168 table[i].departure = sum;
171 ++i;
172 order = order->next;
173 if (i >= v->GetNumOrders()) {
174 i = 0;
175 assert(order == nullptr);
176 order = v->orders->GetFirstOrder();
178 } while (i != start);
180 /* When loading at a scheduled station we still have to treat the
181 * travelling part of the first order. */
182 if (!travelling) {
183 if (!CanDetermineTimeTaken(order, true)) return;
184 sum += order->GetTimetabledTravel();
185 table[i].arrival = sum;
191 * Callback for when a time has been chosen to start the time table
192 * @param w the window related to the setting of the date
193 * @param date the actually chosen date
195 static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data)
197 Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, (VehicleID)w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, GetStartTickFromDate(date));
201 struct TimetableWindow : Window {
202 int sel_index;
203 VehicleTimetableWidgets query_widget; ///< Which button was clicked to open the query text input?
204 const Vehicle *vehicle; ///< Vehicle monitored by the window.
205 bool show_expected; ///< Whether we show expected arrival or scheduled.
206 Scrollbar *vscroll; ///< The scrollbar.
207 bool set_start_date_all; ///< Set start date using minutes text entry for all timetable entries (ctrl-click) action.
208 bool change_timetable_all; ///< Set wait time or speed for all timetable entries (ctrl-click) action.
210 TimetableWindow(WindowDesc &desc, WindowNumber window_number) :
211 Window(desc),
212 sel_index(-1),
213 vehicle(Vehicle::Get(window_number)),
214 show_expected(true)
216 this->CreateNestedTree();
217 this->vscroll = this->GetScrollbar(WID_VT_SCROLLBAR);
219 /* When using wallclock units, we must ensure the client displays timetables in seconds. */
220 if (TimerGameEconomy::UsingWallclockUnits()) {
221 _settings_client.gui.timetable_mode = TimetableMode::Seconds;
224 this->UpdateSelectionStates();
225 this->FinishInitNested(window_number);
227 this->owner = this->vehicle->owner;
231 * Build the arrival-departure list for a given vehicle
232 * @param v the vehicle to make the list for
233 * @param table the table to fill
234 * @return if next arrival will be early
236 static bool BuildArrivalDepartureList(const Vehicle *v, std::vector<TimetableArrivalDeparture> &table)
238 assert(HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED));
240 bool travelling = (!v->current_order.IsType(OT_LOADING) || v->current_order.GetNonStopType() == ONSF_STOP_EVERYWHERE);
241 TimerGameTick::Ticks start_time = -v->current_order_time;
243 /* If arrival and departure times are in days, compensate for the current date_fract. */
244 if (_settings_client.gui.timetable_mode != TimetableMode::Seconds) start_time += TimerGameEconomy::date_fract;
246 FillTimetableArrivalDepartureTable(v, v->cur_real_order_index % v->GetNumOrders(), travelling, table, start_time);
248 return (travelling && v->lateness_counter < 0);
251 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
253 switch (widget) {
254 case WID_VT_ARRIVAL_DEPARTURE_PANEL:
255 /* We handle this differently depending on the timetable mode. */
256 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
257 /* A five-digit number would fit a timetable lasting 2.7 real-world hours, which should be plenty. */
258 SetDParamMaxDigits(1, 4, FS_SMALL);
259 size.width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_SECONDS_IN_FUTURE).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_SECONDS_IN_FUTURE).width) + WidgetDimensions::scaled.hsep_wide + padding.width;
260 } else {
261 SetDParamMaxValue(1, TimerGameEconomy::DateAtStartOfYear(EconomyTime::MAX_YEAR), 0, FS_SMALL);
262 size.width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_DATE).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_DATE).width) + WidgetDimensions::scaled.hsep_wide + padding.width;
264 [[fallthrough]];
266 case WID_VT_ARRIVAL_DEPARTURE_SELECTION:
267 case WID_VT_TIMETABLE_PANEL:
268 resize.height = GetCharacterHeight(FS_NORMAL);
269 size.height = 8 * resize.height + padding.height;
270 break;
272 case WID_VT_SUMMARY_PANEL:
273 size.height = 2 * GetCharacterHeight(FS_NORMAL) + padding.height;
274 break;
278 int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v)
280 int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top);
281 if (sel == INT32_MAX) return INVALID_ORDER;
282 assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2));
283 return sel;
287 * Some data on this window has become invalid.
288 * @param data Information about the changed data.
289 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
291 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
293 switch (data) {
294 case VIWD_AUTOREPLACE:
295 /* Autoreplace replaced the vehicle */
296 this->vehicle = Vehicle::Get(this->window_number);
297 break;
299 case VIWD_REMOVE_ALL_ORDERS:
300 /* Removed / replaced all orders (after deleting / sharing) */
301 if (this->sel_index == -1) break;
303 this->CloseChildWindows();
304 this->sel_index = -1;
305 break;
307 case VIWD_MODIFY_ORDERS:
308 if (!gui_scope) break;
309 this->UpdateSelectionStates();
310 this->ReInit();
311 break;
313 default: {
314 if (gui_scope) break; // only do this once; from command scope
316 /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
317 * the order is being created / removed */
318 if (this->sel_index == -1) break;
320 VehicleOrderID from = GB(data, 0, 8);
321 VehicleOrderID to = GB(data, 8, 8);
323 if (from == to) break; // no need to change anything
325 /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
326 uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
328 VehicleOrderID selected_order = (this->sel_index + 1) / 2;
329 if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
331 bool travel = HasBit(this->sel_index, 0);
333 if (from != selected_order) {
334 /* Moving from preceding order? */
335 selected_order -= (int)(from <= selected_order);
336 /* Moving to preceding order? */
337 selected_order += (int)(to <= selected_order);
338 } else {
339 /* Now we are modifying the selected order */
340 if (to == INVALID_VEH_ORDER_ID) {
341 /* Deleting selected order */
342 this->CloseChildWindows();
343 this->sel_index = -1;
344 break;
345 } else {
346 /* Moving selected order */
347 selected_order = to;
351 /* recompute new sel_index */
352 this->sel_index = 2 * selected_order - (int)travel;
353 /* travel time of first order needs special handling */
354 if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
355 break;
361 void OnPaint() override
363 const Vehicle *v = this->vehicle;
364 int selected = this->sel_index;
366 this->vscroll->SetCount(v->GetNumOrders() * 2);
368 if (v->owner == _local_company) {
369 bool disable = true;
370 if (selected != -1) {
371 const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
372 if (selected % 2 != 0) {
373 disable = order != nullptr && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT));
374 } else {
375 disable = order == nullptr || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
378 bool disable_speed = disable || selected % 2 == 0 || v->type == VEH_AIRCRAFT;
380 this->SetWidgetDisabledState(WID_VT_CHANGE_TIME, disable);
381 this->SetWidgetDisabledState(WID_VT_CLEAR_TIME, disable);
382 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED, disable_speed);
383 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
384 this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST, !v->IsOrderListShared());
386 this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders == nullptr);
387 this->SetWidgetDisabledState(WID_VT_RESET_LATENESS, v->orders == nullptr);
388 this->SetWidgetDisabledState(WID_VT_AUTOFILL, v->orders == nullptr);
389 } else {
390 this->DisableWidget(WID_VT_START_DATE);
391 this->DisableWidget(WID_VT_CHANGE_TIME);
392 this->DisableWidget(WID_VT_CLEAR_TIME);
393 this->DisableWidget(WID_VT_CHANGE_SPEED);
394 this->DisableWidget(WID_VT_CLEAR_SPEED);
395 this->DisableWidget(WID_VT_RESET_LATENESS);
396 this->DisableWidget(WID_VT_AUTOFILL);
397 this->DisableWidget(WID_VT_SHARED_ORDER_LIST);
400 this->SetWidgetLoweredState(WID_VT_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
402 this->DrawWidgets();
405 void SetStringParameters(WidgetID widget) const override
407 switch (widget) {
408 case WID_VT_CAPTION: SetDParam(0, this->vehicle->index); break;
409 case WID_VT_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
414 * Helper function to draw the timetable panel.
415 * @param r The rect to draw within.
417 void DrawTimetablePanel(const Rect &r) const
419 const Vehicle *v = this->vehicle;
420 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
421 int i = this->vscroll->GetPosition();
422 VehicleOrderID order_id = (i + 1) / 2;
423 bool final_order = false;
424 int selected = this->sel_index;
426 bool rtl = _current_text_dir == TD_RTL;
427 SetDParamMaxValue(0, v->GetNumOrders(), 2);
428 int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + WidgetDimensions::scaled.hsep_normal;
429 int middle = rtl ? tr.right - index_column_width : tr.left + index_column_width;
431 const Order *order = v->GetOrder(order_id);
432 while (order != nullptr) {
433 /* Don't draw anything if it extends past the end of the window. */
434 if (!this->vscroll->IsVisible(i)) break;
436 if (i % 2 == 0) {
437 DrawOrderString(v, order, order_id, tr.top, i == selected, true, tr.left, middle, tr.right);
439 order_id++;
441 if (order_id >= v->GetNumOrders()) {
442 order = v->GetOrder(0);
443 final_order = true;
444 } else {
445 order = order->next;
447 } else {
448 StringID string;
449 TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
450 if (order->IsType(OT_CONDITIONAL)) {
451 string = STR_TIMETABLE_NO_TRAVEL;
452 } else if (order->IsType(OT_IMPLICIT)) {
453 string = STR_TIMETABLE_NOT_TIMETABLEABLE;
454 colour = ((i == selected) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
455 } else if (!order->IsTravelTimetabled()) {
456 if (order->GetTravelTime() > 0) {
457 SetTimetableParams(0, 1, order->GetTravelTime());
458 string = order->GetMaxSpeed() != UINT16_MAX ?
459 STR_TIMETABLE_TRAVEL_FOR_SPEED_ESTIMATED :
460 STR_TIMETABLE_TRAVEL_FOR_ESTIMATED;
461 } else {
462 string = order->GetMaxSpeed() != UINT16_MAX ?
463 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED_SPEED :
464 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
466 } else {
467 SetTimetableParams(0, 1, order->GetTimetabledTravel());
468 string = order->GetMaxSpeed() != UINT16_MAX ?
469 STR_TIMETABLE_TRAVEL_FOR_SPEED : STR_TIMETABLE_TRAVEL_FOR;
471 SetDParam(2, PackVelocity(order->GetMaxSpeed(), v->type));
473 DrawString(rtl ? tr.left : middle, rtl ? middle : tr.right, tr.top, string, colour);
475 if (final_order) break;
478 i++;
479 tr.top += GetCharacterHeight(FS_NORMAL);
484 * Helper function to draw the arrival and departure panel.
485 * @param r The rect to draw within.
487 void DrawArrivalDeparturePanel(const Rect &r) const
489 const Vehicle *v = this->vehicle;
491 /* Arrival and departure times are handled in an all-or-nothing approach,
492 * i.e. are only shown if we can calculate all times.
493 * Excluding order lists with only one order makes some things easier. */
494 TimerGameTick::Ticks total_time = v->orders != nullptr ? v->orders->GetTimetableDurationIncomplete() : 0;
495 if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
497 std::vector<TimetableArrivalDeparture> arr_dep(v->GetNumOrders());
498 const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
500 VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
501 int selected = this->sel_index;
503 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
504 bool show_late = this->show_expected && VehicleIsAboveLatenessThreshold(v->lateness_counter, true);
505 TimerGameTick::Ticks offset = show_late ? 0 : -v->lateness_counter;
507 for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
508 /* Don't draw anything if it extends past the end of the window. */
509 if (!this->vscroll->IsVisible(i)) break;
511 /* TC_INVALID will skip the colour change. */
512 SetDParam(0, show_late ? TC_RED : TC_INVALID);
513 if (i % 2 == 0) {
514 /* Draw an arrival time. */
515 if (arr_dep[i / 2].arrival != Ticks::INVALID_TICKS) {
516 /* First set the offset and text colour based on the expected/scheduled mode and some other things. */
517 TimerGameTick::Ticks this_offset;
518 if (this->show_expected && i / 2 == earlyID) {
519 /* Show expected arrival. */
520 this_offset = 0;
521 SetDParam(0, TC_GREEN);
522 } else {
523 /* Show scheduled arrival. */
524 this_offset = offset;
527 /* Now actually draw the arrival time. */
528 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
529 /* Display seconds from now. */
530 SetDParam(1, ((arr_dep[i / 2].arrival + offset) / Ticks::TICKS_PER_SECOND));
531 DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_SECONDS_IN_FUTURE, i == selected ? TC_WHITE : TC_BLACK);
532 } else {
533 /* Show a date. */
534 SetDParam(1, TimerGameEconomy::date + (arr_dep[i / 2].arrival + this_offset) / Ticks::DAY_TICKS);
535 DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_DATE, i == selected ? TC_WHITE : TC_BLACK);
538 } else {
539 /* Draw a departure time. */
540 if (arr_dep[i / 2].departure != Ticks::INVALID_TICKS) {
541 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
542 /* Display seconds from now. */
543 SetDParam(1, ((arr_dep[i / 2].departure + offset) / Ticks::TICKS_PER_SECOND));
544 DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE_SECONDS_IN_FUTURE, i == selected ? TC_WHITE : TC_BLACK);
545 } else {
546 /* Show a date. */
547 SetDParam(1, TimerGameEconomy::date + (arr_dep[i / 2].departure + offset) / Ticks::DAY_TICKS);
548 DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE_DATE, i == selected ? TC_WHITE : TC_BLACK);
552 tr.top += GetCharacterHeight(FS_NORMAL);
557 * Helper function to draw the summary panel.
558 * @param r The rect to draw within.
560 void DrawSummaryPanel(const Rect &r) const
562 const Vehicle *v = this->vehicle;
563 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
565 TimerGameTick::Ticks total_time = v->orders != nullptr ? v->orders->GetTimetableDurationIncomplete() : 0;
566 if (total_time != 0) {
567 SetTimetableParams(0, 1, total_time);
568 DrawString(tr, v->orders->IsCompleteTimetable() ? STR_TIMETABLE_TOTAL_TIME : STR_TIMETABLE_TOTAL_TIME_INCOMPLETE);
570 tr.top += GetCharacterHeight(FS_NORMAL);
572 /* Draw the lateness display, or indicate that the timetable has not started yet. */
573 if (v->timetable_start != 0) {
574 /* We are running towards the first station so we can start the
575 * timetable at the given time. */
576 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
577 /* Real time units use seconds relative to now. */
578 SetDParam(0, (static_cast<TimerGameTick::Ticks>(v->timetable_start - TimerGameTick::counter) / Ticks::TICKS_PER_SECOND));
579 DrawString(tr, STR_TIMETABLE_STATUS_START_IN_SECONDS);
580 } else {
581 /* Other units use dates. */
582 SetDParam(0, STR_JUST_DATE_TINY);
583 SetDParam(1, GetDateFromStartTick(v->timetable_start));
584 DrawString(tr, STR_TIMETABLE_STATUS_START_AT_DATE);
586 } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
587 /* We aren't running on a timetable yet. */
588 DrawString(tr, STR_TIMETABLE_STATUS_NOT_STARTED);
589 } else if (!VehicleIsAboveLatenessThreshold(abs(v->lateness_counter), false)) {
590 /* We are on time. */
591 DrawString(tr, STR_TIMETABLE_STATUS_ON_TIME);
592 } else {
593 /* We are late. */
594 SetTimetableParams(0, 1, abs(v->lateness_counter));
595 DrawString(tr, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
599 void DrawWidget(const Rect &r, WidgetID widget) const override
601 switch (widget) {
602 case WID_VT_TIMETABLE_PANEL: {
603 this->DrawTimetablePanel(r);
604 break;
607 case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
608 this->DrawArrivalDeparturePanel(r);
609 break;
612 case WID_VT_SUMMARY_PANEL: {
613 this->DrawSummaryPanel(r);
614 break;
619 static inline std::tuple<VehicleOrderID, ModifyTimetableFlags> PackTimetableArgs(const Vehicle *v, uint selected, bool speed)
621 uint order_number = (selected + 1) / 2;
622 ModifyTimetableFlags mtf = (selected % 2 != 0) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME;
624 if (order_number >= v->GetNumOrders()) order_number = 0;
626 return { order_number, mtf };
629 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
631 const Vehicle *v = this->vehicle;
633 switch (widget) {
634 case WID_VT_ORDER_VIEW: // Order view button
635 ShowOrdersWindow(v);
636 break;
638 case WID_VT_TIMETABLE_PANEL: { // Main panel.
639 int selected = GetOrderFromTimetableWndPt(pt.y, v);
641 this->CloseChildWindows();
642 this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
643 break;
646 case WID_VT_START_DATE: // Change the date that the timetable starts.
647 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
648 this->query_widget = WID_VT_START_DATE;
649 this->change_timetable_all = _ctrl_pressed;
650 ShowQueryString(STR_EMPTY, STR_TIMETABLE_START_SECONDS_QUERY, 6, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
651 } else {
652 ShowSetDateWindow(this, v->index, TimerGameEconomy::date, TimerGameEconomy::year, TimerGameEconomy::year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void*>(static_cast<uintptr_t>(_ctrl_pressed)));
654 break;
656 case WID_VT_CHANGE_TIME: { // "Wait For" button.
657 this->query_widget = WID_VT_CHANGE_TIME;
658 int selected = this->sel_index;
659 VehicleOrderID real = (selected + 1) / 2;
661 if (real >= v->GetNumOrders()) real = 0;
663 const Order *order = v->GetOrder(real);
664 StringID current = STR_EMPTY;
666 if (order != nullptr) {
667 uint time = (selected % 2 != 0) ? order->GetTravelTime() : order->GetWaitTime();
668 time /= TicksPerTimetableUnit();
670 if (time != 0) {
671 SetDParam(0, time);
672 current = STR_JUST_INT;
676 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
677 ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
678 break;
681 case WID_VT_CHANGE_SPEED: { // Change max speed button.
682 this->query_widget = WID_VT_CHANGE_SPEED;
683 int selected = this->sel_index;
684 VehicleOrderID real = (selected + 1) / 2;
686 if (real >= v->GetNumOrders()) real = 0;
688 StringID current = STR_EMPTY;
689 const Order *order = v->GetOrder(real);
690 if (order != nullptr) {
691 if (order->GetMaxSpeed() != UINT16_MAX) {
692 SetDParam(0, ConvertKmhishSpeedToDisplaySpeed(order->GetMaxSpeed(), v->type));
693 current = STR_JUST_INT;
697 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
698 ShowQueryString(current, STR_TIMETABLE_CHANGE_SPEED, 31, this, CS_NUMERAL, QSF_NONE);
699 break;
702 case WID_VT_CLEAR_TIME: { // Clear waiting time.
703 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, false);
704 if (_ctrl_pressed) {
705 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, 0);
706 } else {
707 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, 0);
709 break;
712 case WID_VT_CLEAR_SPEED: { // Clear max speed button.
713 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, true);
714 if (_ctrl_pressed) {
715 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, UINT16_MAX);
716 } else {
717 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, UINT16_MAX);
719 break;
722 case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
723 Command<CMD_SET_VEHICLE_ON_TIME>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, _ctrl_pressed);
724 break;
726 case WID_VT_AUTOFILL: { // Autofill the timetable.
727 Command<CMD_AUTOFILL_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, !HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE), _ctrl_pressed);
728 break;
731 case WID_VT_EXPECTED:
732 this->show_expected = !this->show_expected;
733 break;
735 case WID_VT_SHARED_ORDER_LIST:
736 ShowVehicleListWindow(v);
737 break;
740 this->SetDirty();
743 void OnQueryTextFinished(std::optional<std::string> str) override
745 if (!str.has_value()) return;
747 const Vehicle *v = this->vehicle;
748 uint64_t val = str->empty() ? 0 : std::strtoul(str->c_str(), nullptr, 10);
749 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, query_widget == WID_VT_CHANGE_SPEED);
751 switch (query_widget) {
752 case WID_VT_CHANGE_SPEED: {
753 val = ConvertDisplaySpeedToKmhishSpeed(val, v->type);
755 if (this->change_timetable_all) {
756 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, ClampTo<uint16_t>(val));
757 } else {
758 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, ClampTo<uint16_t>(val));
760 break;
763 case WID_VT_CHANGE_TIME:
764 val *= TicksPerTimetableUnit();
766 if (this->change_timetable_all) {
767 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, ClampTo<uint16_t>(val));
768 } else {
769 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, ClampTo<uint16_t>(val));
771 break;
773 case WID_VT_START_DATE: {
774 TimerGameTick::TickCounter start_tick = TimerGameTick::counter + (val * Ticks::TICKS_PER_SECOND);
775 Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, this->change_timetable_all, start_tick);
776 break;
779 default:
780 NOT_REACHED();
784 void OnResize() override
786 /* Update the scroll bar */
787 this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.Vertical());
791 * Update the selection state of the arrival/departure data
793 void UpdateSelectionStates()
795 this->GetWidget<NWidgetStacked>(WID_VT_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
796 this->GetWidget<NWidgetStacked>(WID_VT_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
800 * In real-time mode, the timetable GUI shows relative times and needs to be redrawn every second.
802 IntervalTimer<TimerGameTick> redraw_interval = { { TimerGameTick::Priority::NONE, Ticks::TICKS_PER_SECOND }, [this](auto) {
803 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
804 this->SetDirty();
809 static constexpr NWidgetPart _nested_timetable_widgets[] = {
810 NWidget(NWID_HORIZONTAL),
811 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
812 NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
813 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
814 NWidget(WWT_SHADEBOX, COLOUR_GREY),
815 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
816 NWidget(WWT_STICKYBOX, COLOUR_GREY),
817 EndContainer(),
818 NWidget(NWID_HORIZONTAL),
819 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_TIMETABLE_PANEL), SetMinimalSize(388, 82), SetResize(1, 10), SetDataTip(STR_NULL, STR_TIMETABLE_TOOLTIP), SetScrollbar(WID_VT_SCROLLBAR), EndContainer(),
820 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_ARRIVAL_DEPARTURE_SELECTION),
821 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_ARRIVAL_DEPARTURE_PANEL), SetMinimalSize(110, 0), SetFill(0, 1), SetDataTip(STR_NULL, STR_TIMETABLE_TOOLTIP), SetScrollbar(WID_VT_SCROLLBAR), EndContainer(),
822 EndContainer(),
823 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_VT_SCROLLBAR),
824 EndContainer(),
825 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
826 NWidget(NWID_HORIZONTAL),
827 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
828 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
829 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CHANGE_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_TIME, STR_TIMETABLE_WAIT_TIME_TOOLTIP),
830 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CLEAR_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_TIME, STR_TIMETABLE_CLEAR_TIME_TOOLTIP),
831 EndContainer(),
832 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
833 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CHANGE_SPEED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_SPEED, STR_TIMETABLE_CHANGE_SPEED_TOOLTIP),
834 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CLEAR_SPEED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_SPEED, STR_TIMETABLE_CLEAR_SPEED_TOOLTIP),
835 EndContainer(),
836 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
837 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_START_DATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_START, STR_TIMETABLE_START_TOOLTIP),
838 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_RESET_LATENESS), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_RESET_LATENESS, STR_TIMETABLE_RESET_LATENESS_TOOLTIP),
839 EndContainer(),
840 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
841 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
842 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_EXPECTED_SELECTION),
843 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
844 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
845 EndContainer(),
846 EndContainer(),
847 EndContainer(),
848 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
849 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_VT_SHARED_ORDER_LIST), SetFill(0, 1), SetDataTip(SPR_SHARED_ORDERS_ICON, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP),
850 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
851 EndContainer(),
852 EndContainer(),
855 static WindowDesc _timetable_desc(
856 WDP_AUTO, "view_vehicle_timetable", 400, 130,
857 WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
858 WDF_CONSTRUCTION,
859 _nested_timetable_widgets
863 * Show the timetable for a given vehicle.
864 * @param v The vehicle to show the timetable for.
866 void ShowTimetableWindow(const Vehicle *v)
868 CloseWindowById(WC_VEHICLE_DETAILS, v->index, false);
869 CloseWindowById(WC_VEHICLE_ORDERS, v->index, false);
870 AllocateWindowDescFront<TimetableWindow>(_timetable_desc, v->index);