OpenTTD Source 20250205-master-gfd85ab1e2c
timetable_gui.cpp
Go to the documentation of this file.
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 */
7
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"
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"
30
32
33#include "table/sprites.h"
34#include "table/strings.h"
35
36#include "safeguards.h"
37
43
50void SetTimetableParams(int param1, int param2, TimerGameTick::Ticks ticks)
51{
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();
67 }
68}
69
75{
77 case TimetableMode::Days:
78 return Ticks::DAY_TICKS;
79 case TimetableMode::Seconds:
81 case TimetableMode::Ticks:
82 return 1;
83 default:
84 NOT_REACHED();
85 }
86}
87
95{
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();
105 }
106}
107
114static bool CanDetermineTimeTaken(const Order *order, bool travelling)
115{
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) &&
123 return false;
124 }
125
126 return true;
127}
128
129
138static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, std::vector<TimetableArrivalDeparture> &table, TimerGameTick::Ticks offset)
139{
140 assert(!table.empty());
141 assert(v->GetNumOrders() >= 2);
142 assert(start < v->GetNumOrders());
143
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;
147 }
148
149 TimerGameTick::Ticks sum = offset;
150 VehicleOrderID i = start;
151 const Order *order = v->GetOrder(i);
152
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;
164 }
165
166 if (!CanDetermineTimeTaken(order, false)) return;
167 sum += order->GetTimetabledWait();
168 table[i].departure = sum;
169 }
170
171 ++i;
172 order = order->next;
173 if (i >= v->GetNumOrders()) {
174 i = 0;
175 assert(order == nullptr);
176 order = v->orders->GetFirstOrder();
177 }
178 } while (i != start);
179
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;
186 }
187}
188
189
195static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data)
196{
197 Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, GetStartTickFromDate(date));
198}
199
200
202 int sel_index;
209
211 Window(desc),
212 sel_index(-1),
215 {
216 this->CreateNestedTree();
217 this->vscroll = this->GetScrollbar(WID_VT_SCROLLBAR);
218
219 /* When using wallclock units, we must ensure the client displays timetables in seconds. */
221 _settings_client.gui.timetable_mode = TimetableMode::Seconds;
222 }
223
224 this->UpdateSelectionStates();
225 this->FinishInitNested(window_number);
226
227 this->owner = this->vehicle->owner;
228 }
229
236 static bool BuildArrivalDepartureList(const Vehicle *v, std::vector<TimetableArrivalDeparture> &table)
237 {
239
242
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;
245
247
248 return (travelling && v->lateness_counter < 0);
249 }
250
252 {
253 switch (widget) {
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. */
260 } else {
263 }
264 [[fallthrough]];
265
269 size.height = 8 * resize.height + padding.height;
270 break;
271
273 size.height = 2 * GetCharacterHeight(FS_NORMAL) + padding.height;
274 break;
275 }
276 }
277
278 int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v)
279 {
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;
284 }
285
291 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
292 {
293 switch (data) {
294 case VIWD_AUTOREPLACE:
295 /* Autoreplace replaced the vehicle */
296 this->vehicle = Vehicle::Get(this->window_number);
297 break;
298
300 /* Removed / replaced all orders (after deleting / sharing) */
301 if (this->sel_index == -1) break;
302
303 this->CloseChildWindows();
304 this->sel_index = -1;
305 break;
306
308 if (!gui_scope) break;
309 this->UpdateSelectionStates();
310 this->ReInit();
311 break;
312
313 default: {
314 if (gui_scope) break; // only do this once; from command scope
315
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;
319
320 VehicleOrderID from = GB(data, 0, 8);
321 VehicleOrderID to = GB(data, 8, 8);
322
323 if (from == to) break; // no need to change anything
324
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);
327
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
330
331 bool travel = HasBit(this->sel_index, 0);
332
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;
348 }
349 }
350
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;
356 }
357 }
358 }
359
360
361 void OnPaint() override
362 {
363 const Vehicle *v = this->vehicle;
364 int selected = this->sel_index;
365
366 this->vscroll->SetCount(v->GetNumOrders() * 2);
367
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));
376 }
377 }
378 bool disable_speed = disable || selected % 2 == 0 || v->type == VEH_AIRCRAFT;
379
382 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED, disable_speed);
383 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
385
386 this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders == nullptr);
388 this->SetWidgetDisabledState(WID_VT_AUTOFILL, v->orders == nullptr);
389 } else {
398 }
399
401
402 this->DrawWidgets();
403 }
404
405 void SetStringParameters(WidgetID widget) const override
406 {
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;
410 }
411 }
412
417 void DrawTimetablePanel(const Rect &r) const
418 {
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;
425
426 bool rtl = _current_text_dir == TD_RTL;
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;
430
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;
435
436 if (i % 2 == 0) {
437 DrawOrderString(v, order, order_id, tr.top, i == selected, true, tr.left, middle, tr.right);
438
439 order_id++;
440
441 if (order_id >= v->GetNumOrders()) {
442 order = v->GetOrder(0);
443 final_order = true;
444 } else {
445 order = order->next;
446 }
447 } else {
448 StringID string;
449 TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
450 if (order->IsType(OT_CONDITIONAL)) {
452 } else if (order->IsType(OT_IMPLICIT)) {
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 ?
461 } else {
462 string = order->GetMaxSpeed() != UINT16_MAX ?
465 }
466 } else {
468 string = order->GetMaxSpeed() != UINT16_MAX ?
470 }
471 SetDParam(2, PackVelocity(order->GetMaxSpeed(), v->type));
472
473 DrawString(rtl ? tr.left : middle, rtl ? middle : tr.right, tr.top, string, colour);
474
475 if (final_order) break;
476 }
477
478 i++;
480 }
481 }
482
487 void DrawArrivalDeparturePanel(const Rect &r) const
488 {
489 const Vehicle *v = this->vehicle;
490
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. */
495 if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
496
497 std::vector<TimetableArrivalDeparture> arr_dep(v->GetNumOrders());
499
501 int selected = this->sel_index;
502
503 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
504 bool show_late = this->show_expected && VehicleIsAboveLatenessThreshold(v->lateness_counter, true);
506
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;
510
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. */
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;
525 }
526
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. */
535 DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL_DATE, i == selected ? TC_WHITE : TC_BLACK);
536 }
537 }
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);
549 }
550 }
551 }
553 }
554 }
555
560 void DrawSummaryPanel(const Rect &r) const
561 {
562 const Vehicle *v = this->vehicle;
563 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
564
566 if (total_time != 0) {
568 if (!v->orders->IsCompleteTimetable()) {
570 } else if (total_time % TicksPerTimetableUnit() == 0) {
572 } else {
574 }
575 }
577
578 /* Draw the lateness display, or indicate that the timetable has not started yet. */
579 if (v->timetable_start != 0) {
580 /* We are running towards the first station so we can start the
581 * timetable at the given time. */
582 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
583 /* Real time units use seconds relative to now. */
586 } else {
587 /* Other units use dates. */
591 }
592 } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
593 /* We aren't running on a timetable yet. */
595 } else if (!VehicleIsAboveLatenessThreshold(abs(v->lateness_counter), false)) {
596 /* We are on time. */
598 } else {
599 /* We are late. */
602 }
603 }
604
605 void DrawWidget(const Rect &r, WidgetID widget) const override
606 {
607 switch (widget) {
609 this->DrawTimetablePanel(r);
610 break;
611 }
612
615 break;
616 }
617
619 this->DrawSummaryPanel(r);
620 break;
621 }
622 }
623 }
624
625 static inline std::tuple<VehicleOrderID, ModifyTimetableFlags> PackTimetableArgs(const Vehicle *v, uint selected, bool speed)
626 {
627 uint order_number = (selected + 1) / 2;
628 ModifyTimetableFlags mtf = (selected % 2 != 0) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME;
629
630 if (order_number >= v->GetNumOrders()) order_number = 0;
631
632 return { order_number, mtf };
633 }
634
635 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
636 {
637 const Vehicle *v = this->vehicle;
638
639 switch (widget) {
640 case WID_VT_ORDER_VIEW: // Order view button
641 ShowOrdersWindow(v);
642 break;
643
644 case WID_VT_TIMETABLE_PANEL: { // Main panel.
645 int selected = GetOrderFromTimetableWndPt(pt.y, v);
646
647 this->CloseChildWindows();
648 this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
649 break;
650 }
651
652 case WID_VT_START_DATE: // Change the date that the timetable starts.
653 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
654 this->query_widget = WID_VT_START_DATE;
655 this->change_timetable_all = _ctrl_pressed;
657 } else {
659 }
660 break;
661
662 case WID_VT_CHANGE_TIME: { // "Wait For" button.
663 this->query_widget = WID_VT_CHANGE_TIME;
664 int selected = this->sel_index;
665 VehicleOrderID real = (selected + 1) / 2;
666
667 if (real >= v->GetNumOrders()) real = 0;
668
669 const Order *order = v->GetOrder(real);
670 StringID current = STR_EMPTY;
671
672 if (order != nullptr) {
673 uint time = (selected % 2 != 0) ? order->GetTravelTime() : order->GetWaitTime();
674 time /= TicksPerTimetableUnit();
675
676 if (time != 0) {
677 SetDParam(0, time);
678 current = STR_JUST_INT;
679 }
680 }
681
682 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
684 break;
685 }
686
687 case WID_VT_CHANGE_SPEED: { // Change max speed button.
688 this->query_widget = WID_VT_CHANGE_SPEED;
689 int selected = this->sel_index;
690 VehicleOrderID real = (selected + 1) / 2;
691
692 if (real >= v->GetNumOrders()) real = 0;
693
694 StringID current = STR_EMPTY;
695 const Order *order = v->GetOrder(real);
696 if (order != nullptr) {
697 if (order->GetMaxSpeed() != UINT16_MAX) {
699 current = STR_JUST_INT;
700 }
701 }
702
703 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
704 ShowQueryString(current, STR_TIMETABLE_CHANGE_SPEED, 31, this, CS_NUMERAL, QSF_NONE);
705 break;
706 }
707
708 case WID_VT_CLEAR_TIME: { // Clear waiting time.
709 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, false);
710 if (_ctrl_pressed) {
712 } else {
714 }
715 break;
716 }
717
718 case WID_VT_CLEAR_SPEED: { // Clear max speed button.
719 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, true);
720 if (_ctrl_pressed) {
722 } else {
724 }
725 break;
726 }
727
728 case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
730 break;
731
732 case WID_VT_AUTOFILL: { // Autofill the timetable.
734 break;
735 }
736
737 case WID_VT_EXPECTED:
738 this->show_expected = !this->show_expected;
739 break;
740
742 ShowVehicleListWindow(v);
743 break;
744 }
745
746 this->SetDirty();
747 }
748
749 void OnQueryTextFinished(std::optional<std::string> str) override
750 {
751 if (!str.has_value()) return;
752
753 const Vehicle *v = this->vehicle;
754 uint64_t val = str->empty() ? 0 : std::strtoul(str->c_str(), nullptr, 10);
755 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, query_widget == WID_VT_CHANGE_SPEED);
756
757 switch (query_widget) {
758 case WID_VT_CHANGE_SPEED: {
760
761 if (this->change_timetable_all) {
763 } else {
765 }
766 break;
767 }
768
771
772 if (this->change_timetable_all) {
774 } else {
776 }
777 break;
778
779 case WID_VT_START_DATE: {
782 break;
783 }
784
785 default:
786 NOT_REACHED();
787 }
788 }
789
790 void OnResize() override
791 {
792 /* Update the scroll bar */
793 this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.Vertical());
794 }
795
804
809 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
810 this->SetDirty();
811 }
812 }};
813};
814
815static constexpr NWidgetPart _nested_timetable_widgets[] = {
817 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
818 NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION), SetStringTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
819 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetStringTip(STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
820 NWidget(WWT_SHADEBOX, COLOUR_GREY),
821 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
822 NWidget(WWT_STICKYBOX, COLOUR_GREY),
823 EndContainer(),
825 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_TIMETABLE_PANEL), SetMinimalSize(388, 82), SetResize(1, 10), SetToolTip(STR_TIMETABLE_TOOLTIP), SetScrollbar(WID_VT_SCROLLBAR), EndContainer(),
828 EndContainer(),
830 EndContainer(),
835 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CHANGE_TIME), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_CHANGE_TIME, STR_TIMETABLE_WAIT_TIME_TOOLTIP),
836 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CLEAR_TIME), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_CLEAR_TIME, STR_TIMETABLE_CLEAR_TIME_TOOLTIP),
837 EndContainer(),
839 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CHANGE_SPEED), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_CHANGE_SPEED, STR_TIMETABLE_CHANGE_SPEED_TOOLTIP),
840 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CLEAR_SPEED), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_CLEAR_SPEED, STR_TIMETABLE_CLEAR_SPEED_TOOLTIP),
841 EndContainer(),
843 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_START_DATE), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_START, STR_TIMETABLE_START_TOOLTIP),
844 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_RESET_LATENESS), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_RESET_LATENESS, STR_TIMETABLE_RESET_LATENESS_TOOLTIP),
845 EndContainer(),
847 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
849 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_JUST_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
850 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
851 EndContainer(),
852 EndContainer(),
853 EndContainer(),
855 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_VT_SHARED_ORDER_LIST), SetFill(0, 1), SetSpriteTip(SPR_SHARED_ORDERS_ICON, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP),
856 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
857 EndContainer(),
858 EndContainer(),
859};
860
861static WindowDesc _timetable_desc(
862 WDP_AUTO, "view_vehicle_timetable", 400, 130,
865 _nested_timetable_widgets
866);
867
873{
876 AllocateWindowDescFront<TimetableWindow>(_timetable_desc, v->index);
877}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
An interval timer will fire every interval, and will continue to fire until it is deleted.
Definition timer.h:76
Scrollbar data structure.
bool IsVisible(size_type item) const
Checks whether given current item is visible in the list.
void SetCount(size_t num)
Sets the number of elements in the list.
size_type GetScrolledRowFromWidget(int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition widget.cpp:2459
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition widget.cpp:2533
size_type GetPosition() const
Gets the position of the first visible element in the list.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
static constexpr TimerGameTick::Ticks INVALID_TICKS
Representation of an invalid number of ticks.
static constexpr TimerGameTick::Ticks TICKS_PER_SECOND
Estimation of how many ticks fit in a single second.
static constexpr TimerGame< struct Economy >::Year MAX_YEAR
MAX_YEAR, nicely rounded value of the number of years that can be encoded in a single 32 bits date,...
static Date date
Current date in days (day counter).
static Year year
Current year, starting at 0.
static bool UsingWallclockUnits(bool newgame=false)
Check if we are using wallclock units.
static DateFract date_fract
Fractional part of the day.
uint64_t TickCounter
The type that the tick counter is stored in.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
int32_t Ticks
The type to store ticks in.
@ NONE
These timers can be executed in any order; the order is not relevant.
static constexpr Date DateAtStartOfYear(Year year)
Calculate the date of the first day of a given year.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:28
int hsep_wide
Wide horizontal spacing.
Definition window_gui.h:62
Functions related to commands.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Functions related to companies.
void ShowSetDateWindow(Window *parent, int window_number, TimerGameEconomy::Date initial_date, TimerGameEconomy::Year min_year, TimerGameEconomy::Year max_year, SetDateCallback *callback, void *callback_data)
Create the new 'set date' window.
Definition date_gui.cpp:216
Functions related to the graphical selection of a date.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:922
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:851
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:657
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
Functions related to the gfx engine.
@ FS_SMALL
Index of the small font in the font tables.
Definition gfx_type.h:244
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:243
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:294
@ TC_NO_SHADE
Do not add shading to this text colour.
Definition gfx_type.h:318
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetToolTip(StringID tip)
Widget part function for setting tooltip and clearing the widget data.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:937
GUI functions that shouldn't be here.
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
void DrawOrderString(const Vehicle *v, const Order *order, int order_index, int y, bool selected, bool timetable, int left, int middle, int right)
Draws an order in order or timetable GUI.
uint8_t VehicleOrderID
The index of an order within its current vehicle (not pool related)
Definition order_type.h:15
static const OrderID INVALID_ORDER
Invalid order (sentinel)
Definition order_type.h:26
static const VehicleOrderID INVALID_VEH_ORDER_ID
Invalid vehicle order index (sentinel)
Definition order_type.h:21
@ ONSF_NO_STOP_AT_DESTINATION_STATION
The vehicle will stop at any station it passes except the destination.
Definition order_type.h:75
@ ONSF_STOP_EVERYWHERE
The vehicle will stop at any station it passes and the destination.
Definition order_type.h:73
ModifyTimetableFlags
Enumeration for the data to set in CmdChangeTimetable.
Definition order_type.h:171
@ MTF_TRAVEL_TIME
Set travel time.
Definition order_type.h:173
@ MTF_WAIT_TIME
Set wait time.
Definition order_type.h:172
@ MTF_TRAVEL_SPEED
Set max travel speed.
Definition order_type.h:174
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:56
Types related to global configuration settings.
This file contains all sprite-related enums and defines.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
@ CS_NUMERAL
Only numeric ones.
Definition string_type.h:26
void SetDParamMaxValue(size_t n, uint64_t max_value, uint min_count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition strings.cpp:127
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition strings.cpp:104
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
Convert the given display speed to the km/h-ish speed.
Definition strings.cpp:915
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
Convert the given km/h-ish speed to the display speed.
Definition strings.cpp:905
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
void SetDParamMaxDigits(size_t n, uint count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition strings.cpp:143
Functions related to OTTD's strings.
int64_t PackVelocity(uint speed, VehicleType type)
Pack velocity and vehicle type for use with SCC_VELOCITY string parameter.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
TimerGameTick::Ticks current_order_time
How many ticks have passed since this order started.
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
TimerGameTick::TickCounter timetable_start
At what tick of TimerGameTick::counter the vehicle should start its timetable.
TimerGameTick::Ticks lateness_counter
How many ticks late (or early if negative) this vehicle is.
uint16_t vehicle_flags
Used for gradual loading and other miscellaneous things (.
VehicleType type
Type of vehicle.
GUISettings gui
settings related to the GUI
Dimensions (a width and height) of a rectangle in 2D.
bool timetable_arrival_departure
show arrivals and departures in vehicle timetables
TimetableMode timetable_mode
Time units for timetables: days, seconds, or ticks.
Partial widget specification to allow NWidgets to be written nested.
TimerGameTick::Ticks GetTimetableDurationIncomplete() const
Gets the known duration of the vehicles timetable even if the timetable is not complete.
Definition order_base.h:373
bool IsCompleteTimetable() const
Checks whether all orders of the list have a filled timetable.
Order * GetFirstOrder() const
Get the first order of the order chain.
Definition order_base.h:296
uint16_t GetTimetabledTravel() const
Get the time in ticks a vehicle should take to reach the destination or 0 if it's not timetabled.
Definition order_base.h:190
uint16_t GetMaxSpeed() const
Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the destination.
Definition order_base.h:201
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:70
uint16_t GetWaitTime() const
Get the time in ticks a vehicle will probably wait at the destination (timetabled or not).
Definition order_base.h:192
bool IsWaitTimetabled() const
Does this order have an explicit wait time set?
Definition order_base.h:183
Order * next
Pointer to next order. If nullptr, end of list.
Definition order_base.h:59
bool IsTravelTimetabled() const
Does this order have an explicit travel time set?
Definition order_base.h:185
uint16_t GetTimetabledWait() const
Get the time in ticks a vehicle should wait at the destination or 0 if it's not timetabled.
Definition order_base.h:188
uint16_t GetTravelTime() const
Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not).
Definition order_base.h:194
OrderNonStopFlags GetNonStopType() const
At which stations must we stop?
Definition order_base.h:140
Coordinates of a point in 2D.
Tindex index
Index of this pool item.
static Titem * Get(size_t index)
Returns Titem with given index.
Specification of a rectangle with absolute coordinates of all edges.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Container for the arrival/departure dates of a vehicle.
TimerGameTick::Ticks arrival
The arrival time.
TimerGameTick::Ticks departure
The departure time.
void OnResize() override
Called after the window got resized.
void DrawTimetablePanel(const Rect &r) const
Helper function to draw the timetable panel.
bool set_start_date_all
Set start date using minutes text entry for all timetable entries (ctrl-click) action.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
IntervalTimer< TimerGameTick > redraw_interval
In real-time mode, the timetable GUI shows relative times and needs to be redrawn every second.
void DrawSummaryPanel(const Rect &r) const
Helper function to draw the summary panel.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
const Vehicle * vehicle
Vehicle monitored by the window.
void UpdateSelectionStates()
Update the selection state of the arrival/departure data.
Scrollbar * vscroll
The scrollbar.
void SetStringParameters(WidgetID widget) const override
Initialize string parameters for a widget.
bool show_expected
Whether we show expected arrival or scheduled.
void OnPaint() override
The window must be repainted.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
void DrawArrivalDeparturePanel(const Rect &r) const
Helper function to draw the arrival and departure panel.
VehicleTimetableWidgets query_widget
Which button was clicked to open the query text input?
bool change_timetable_all
Set wait time or speed for all timetable entries (ctrl-click) action.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
static bool BuildArrivalDepartureList(const Vehicle *v, std::vector< TimetableArrivalDeparture > &table)
Build the arrival-departure list for a given vehicle.
Vehicle data structure.
bool IsOrderListShared() const
Check if we share our orders with another vehicle.
Order * GetOrder(int index) const
Returns order 'index' of a vehicle or nullptr when it doesn't exists.
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
Order current_order
The current order (+ status, like: loading)
OrderList * orders
Pointer to the order list for this vehicle.
Owner owner
Which company owns the vehicle?
High level window description.
Definition window_gui.h:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:272
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:949
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1730
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:731
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1032
ResizeInfo resize
Resize information.
Definition window_gui.h:313
void DisableWidget(WidgetID widget_index)
Sets a widget to disabled.
Definition window_gui.h:390
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1720
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:315
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:440
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:970
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:311
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:380
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:310
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:301
Stuff related to the text buffer GUI.
@ QSF_ACCEPT_UNCHANGED
return success even when the text didn't change
Definition textbuf_gui.h:20
Definition of Interval and OneShot timers.
Definition of the game-economy-timer.
Definition of the tick-based game-timer.
Definition of the Window system.
Functions related to time tabling.
static const TimerGameEconomy::Year MAX_TIMETABLE_START_YEARS
The maximum start date offset, in economy years.
Definition timetable.h:17
TimerGameTick::TickCounter GetStartTickFromDate(TimerGameEconomy::Date start_date)
Get the TimerGameTick::TickCounter tick of a given date.
TimerGameEconomy::Date GetDateFromStartTick(TimerGameTick::TickCounter start_tick)
Get a date from a given start tick of timetable.
Command definitions related to timetables.
static TimerGameTick::Ticks TicksPerTimetableUnit()
Get the number of ticks in the current timetable display unit.
void ShowTimetableWindow(const Vehicle *v)
Show the timetable for a given vehicle.
static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, std::vector< TimetableArrivalDeparture > &table, TimerGameTick::Ticks offset)
Fill the table with arrivals and departures.
bool VehicleIsAboveLatenessThreshold(TimerGameTick::Ticks ticks, bool round_to_day)
Determine if a vehicle should be shown as late or early, using a threshold depending on the timetable...
static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data)
Callback for when a time has been chosen to start the time table.
void SetTimetableParams(int param1, int param2, TimerGameTick::Ticks ticks)
Set the timetable parameters in the format as described by the setting.
static bool CanDetermineTimeTaken(const Order *order, bool travelling)
Check whether it is possible to determine how long the order takes.
Types related to the timetable widgets.
VehicleTimetableWidgets
Widgets of the TimetableWindow class.
@ WID_VT_CHANGE_TIME
Change time button.
@ WID_VT_ARRIVAL_DEPARTURE_PANEL
Panel with the expected/scheduled arrivals.
@ WID_VT_ARRIVAL_DEPARTURE_SELECTION
Disable/hide the arrival departure panel.
@ WID_VT_CLEAR_SPEED
Clear speed limit button.
@ WID_VT_EXPECTED
Toggle between expected and scheduled arrivals.
@ WID_VT_CHANGE_SPEED
Change speed limit button.
@ WID_VT_TIMETABLE_PANEL
Timetable panel.
@ WID_VT_AUTOFILL
Autofill button.
@ WID_VT_SCROLLBAR
Scrollbar for the panel.
@ WID_VT_RESET_LATENESS
Reset lateness button.
@ WID_VT_SHARED_ORDER_LIST
Show the shared order list.
@ WID_VT_SUMMARY_PANEL
Summary panel.
@ WID_VT_CAPTION
Caption of the window.
@ WID_VT_START_DATE
Start date button.
@ WID_VT_EXPECTED_SELECTION
Disable/hide the expected selection button.
@ WID_VT_ORDER_VIEW
Order view.
@ WID_VT_CLEAR_TIME
Clear time button.
Base class for all vehicles.
@ VF_AUTOFILL_TIMETABLE
Whether the vehicle should fill in the timetable automatically.
@ VF_TIMETABLE_STARTED
Whether the vehicle has started running on the timetable yet.
Functions related to the vehicle's GUIs.
@ VIWD_AUTOREPLACE
Autoreplace replaced the vehicle.
Definition vehicle_gui.h:38
@ VIWD_MODIFY_ORDERS
Other order modifications.
Definition vehicle_gui.h:36
@ VIWD_REMOVE_ALL_ORDERS
Removed / replaced all orders (after deleting / sharing).
Definition vehicle_gui.h:35
@ VEH_AIRCRAFT
Aircraft vehicle type.
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:41
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:55
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:59
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:56
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71
@ SZSP_NONE
Display plane with zero size in both directions (none filling and resizing).
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1137
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ Construction
This window is used for construction; close it whenever changing company.
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_VEHICLE_ORDERS
Vehicle orders; Window numbers:
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers:
@ WC_VEHICLE_TIMETABLE
Vehicle timetable; Window numbers: