OpenTTD Source 20250312-master-gcdcc6b491d
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
49std::pair<StringParameter, StringParameter> GetTimetableParameters(TimerGameTick::Ticks ticks)
50{
52 case TimetableMode::Days: return {STR_UNITS_DAYS, ticks / Ticks::DAY_TICKS};
53 case TimetableMode::Seconds: return {STR_UNITS_SECONDS, ticks / Ticks::TICKS_PER_SECOND};
54 case TimetableMode::Ticks: return {STR_UNITS_TICKS, ticks};
55 default:
56 NOT_REACHED();
57 }
58}
59
65{
67 case TimetableMode::Days:
68 return Ticks::DAY_TICKS;
69 case TimetableMode::Seconds:
71 case TimetableMode::Ticks:
72 return 1;
73 default:
74 NOT_REACHED();
75 }
76}
77
85{
87 case TimetableMode::Days:
88 return ticks > Ticks::DAY_TICKS;
89 case TimetableMode::Seconds:
90 return ticks > Ticks::TICKS_PER_SECOND;
91 case TimetableMode::Ticks:
92 return ticks > (round_to_day ? Ticks::DAY_TICKS : 0);
93 default:
94 NOT_REACHED();
95 }
96}
97
104static bool CanDetermineTimeTaken(const Order *order, bool travelling)
105{
106 /* Current order is conditional */
107 if (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT)) return false;
108 /* No travel time and we have not already finished travelling */
109 if (travelling && !order->IsTravelTimetabled()) return false;
110 /* No wait time but we are loading at this timetabled station */
111 if (!travelling && !order->IsWaitTimetabled() && order->IsType(OT_GOTO_STATION) &&
113 return false;
114 }
115
116 return true;
117}
118
119
128static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, std::vector<TimetableArrivalDeparture> &table, TimerGameTick::Ticks offset)
129{
130 assert(!table.empty());
131 assert(v->GetNumOrders() >= 2);
132 assert(start < v->GetNumOrders());
133
134 /* Pre-initialize with unknown time */
135 for (int i = 0; i < v->GetNumOrders(); ++i) {
136 table[i].arrival = table[i].departure = Ticks::INVALID_TICKS;
137 }
138
139 TimerGameTick::Ticks sum = offset;
140 VehicleOrderID i = start;
141 const Order *order = v->GetOrder(i);
142
143 /* Cyclically loop over all orders until we reach the current one again.
144 * As we may start at the current order, do a post-checking loop */
145 do {
146 /* Automatic orders don't influence the overall timetable;
147 * they just add some untimetabled entries, but the time till
148 * the next non-implicit order can still be known. */
149 if (!order->IsType(OT_IMPLICIT)) {
150 if (travelling || i != start) {
151 if (!CanDetermineTimeTaken(order, true)) return;
152 sum += order->GetTimetabledTravel();
153 table[i].arrival = sum;
154 }
155
156 if (!CanDetermineTimeTaken(order, false)) return;
157 sum += order->GetTimetabledWait();
158 table[i].departure = sum;
159 }
160
161 ++i;
162 order = order->next;
163 if (i >= v->GetNumOrders()) {
164 i = 0;
165 assert(order == nullptr);
166 order = v->orders->GetFirstOrder();
167 }
168 } while (i != start);
169
170 /* When loading at a scheduled station we still have to treat the
171 * travelling part of the first order. */
172 if (!travelling) {
173 if (!CanDetermineTimeTaken(order, true)) return;
174 sum += order->GetTimetabledTravel();
175 table[i].arrival = sum;
176 }
177}
178
179
185static void ChangeTimetableStartCallback(const Window *w, TimerGameEconomy::Date date, void *data)
186{
187 Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, GetStartTickFromDate(date));
188}
189
190
192 int sel_index = -1;
194 const Vehicle *vehicle = nullptr;
195 bool show_expected = true;
196 Scrollbar *vscroll = nullptr;
197 bool set_start_date_all = false;
198 bool change_timetable_all = false;
199
201 Window(desc),
203 {
204 this->CreateNestedTree();
205 this->vscroll = this->GetScrollbar(WID_VT_SCROLLBAR);
206
207 /* When using wallclock units, we must ensure the client displays timetables in seconds. */
209 _settings_client.gui.timetable_mode = TimetableMode::Seconds;
210 }
211
212 this->UpdateSelectionStates();
213 this->FinishInitNested(window_number);
214
215 this->owner = this->vehicle->owner;
216 }
217
224 static bool BuildArrivalDepartureList(const Vehicle *v, std::vector<TimetableArrivalDeparture> &table)
225 {
227
230
231 /* If arrival and departure times are in days, compensate for the current date_fract. */
232 if (_settings_client.gui.timetable_mode != TimetableMode::Seconds) start_time += TimerGameEconomy::date_fract;
233
235
236 return (travelling && v->lateness_counter < 0);
237 }
238
240 {
241 switch (widget) {
243 /* We handle this differently depending on the timetable mode. */
244 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
245 /* A five-digit number would fit a timetable lasting 2.7 real-world hours, which should be plenty. */
247 size.width = std::max(
250 + WidgetDimensions::scaled.hsep_wide + padding.width;
251 } else {
253 size.width = std::max(
256 + WidgetDimensions::scaled.hsep_wide + padding.width;
257 }
258 [[fallthrough]];
259
263 size.height = 8 * resize.height + padding.height;
264 break;
265
267 size.height = 2 * GetCharacterHeight(FS_NORMAL) + padding.height;
268 break;
269 }
270 }
271
272 int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v)
273 {
274 int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top);
275 if (sel == INT32_MAX) return OrderID::Invalid().base();
276 assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2));
277 return sel;
278 }
279
285 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
286 {
287 switch (data) {
288 case VIWD_AUTOREPLACE:
289 /* Autoreplace replaced the vehicle */
290 this->vehicle = Vehicle::Get(this->window_number);
291 break;
292
294 /* Removed / replaced all orders (after deleting / sharing) */
295 if (this->sel_index == -1) break;
296
297 this->CloseChildWindows();
298 this->sel_index = -1;
299 break;
300
302 if (!gui_scope) break;
303 this->UpdateSelectionStates();
304 this->ReInit();
305 break;
306
307 default: {
308 if (gui_scope) break; // only do this once; from command scope
309
310 /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
311 * the order is being created / removed */
312 if (this->sel_index == -1) break;
313
314 VehicleOrderID from = GB(data, 0, 8);
315 VehicleOrderID to = GB(data, 8, 8);
316
317 if (from == to) break; // no need to change anything
318
319 /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
320 uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
321
322 VehicleOrderID selected_order = (this->sel_index + 1) / 2;
323 if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
324
325 bool travel = HasBit(this->sel_index, 0);
326
327 if (from != selected_order) {
328 /* Moving from preceding order? */
329 selected_order -= (int)(from <= selected_order);
330 /* Moving to preceding order? */
331 selected_order += (int)(to <= selected_order);
332 } else {
333 /* Now we are modifying the selected order */
334 if (to == INVALID_VEH_ORDER_ID) {
335 /* Deleting selected order */
336 this->CloseChildWindows();
337 this->sel_index = -1;
338 break;
339 } else {
340 /* Moving selected order */
341 selected_order = to;
342 }
343 }
344
345 /* recompute new sel_index */
346 this->sel_index = 2 * selected_order - (int)travel;
347 /* travel time of first order needs special handling */
348 if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
349 break;
350 }
351 }
352 }
353
354
355 void OnPaint() override
356 {
357 const Vehicle *v = this->vehicle;
358 int selected = this->sel_index;
359
360 this->vscroll->SetCount(v->GetNumOrders() * 2);
361
362 if (v->owner == _local_company) {
363 bool disable = true;
364 if (selected != -1) {
365 const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
366 if (selected % 2 != 0) {
367 disable = order != nullptr && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT));
368 } else {
369 disable = order == nullptr || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
370 }
371 }
372 bool disable_speed = disable || selected % 2 == 0 || v->type == VEH_AIRCRAFT;
373
376 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED, disable_speed);
377 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
379
380 this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders == nullptr);
382 this->SetWidgetDisabledState(WID_VT_AUTOFILL, v->orders == nullptr);
383 } else {
392 }
393
395
396 this->DrawWidgets();
397 }
398
399 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
400 {
401 switch (widget) {
402 case WID_VT_CAPTION: return GetString(STR_TIMETABLE_TITLE, this->vehicle->index);
403 case WID_VT_EXPECTED: return GetString(this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED);
404 default: return this->Window::GetWidgetString(widget, stringid);
405 }
406 }
407
408 std::string GetTimetableTravelString(const Order &order, int i, TextColour &colour) const
409 {
410 colour = (i == this->sel_index) ? TC_WHITE : TC_BLACK;
411
412 if (order.IsType(OT_CONDITIONAL)) {
414 }
415
416 if (order.IsType(OT_IMPLICIT)) {
417 colour = ((i == this->sel_index) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
419 }
420
421 if (order.IsTravelTimetabled()) {
422 auto [str, value] = GetTimetableParameters(order.GetTimetabledTravel());
423 return order.GetMaxSpeed() != UINT16_MAX
424 ? GetString(STR_TIMETABLE_TRAVEL_FOR_SPEED, str, value, PackVelocity(order.GetMaxSpeed(), this->vehicle->type))
425 : GetString(STR_TIMETABLE_TRAVEL_FOR, str, value);
426 }
427
428 if (order.GetTravelTime() > 0) {
429 auto [str, value] = GetTimetableParameters(order.GetTravelTime());
430 return order.GetMaxSpeed() != UINT16_MAX
433 }
434
435 return order.GetMaxSpeed() != UINT16_MAX
438 }
439
444 void DrawTimetablePanel(const Rect &r) const
445 {
446 const Vehicle *v = this->vehicle;
447 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
448 int i = this->vscroll->GetPosition();
449 VehicleOrderID order_id = (i + 1) / 2;
450 bool final_order = false;
451 int selected = this->sel_index;
452
453 bool rtl = _current_text_dir == TD_RTL;
454 int index_column_width = GetStringBoundingBox(GetString(STR_ORDER_INDEX, GetParamMaxValue(v->GetNumOrders(), 2))).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + WidgetDimensions::scaled.hsep_normal;
455 int middle = rtl ? tr.right - index_column_width : tr.left + index_column_width;
456
457 const Order *order = v->GetOrder(order_id);
458 while (order != nullptr) {
459 /* Don't draw anything if it extends past the end of the window. */
460 if (!this->vscroll->IsVisible(i)) break;
461
462 if (i % 2 == 0) {
463 DrawOrderString(v, order, order_id, tr.top, i == selected, true, tr.left, middle, tr.right);
464
465 order_id++;
466
467 if (order_id >= v->GetNumOrders()) {
468 order = v->GetOrder(0);
469 final_order = true;
470 } else {
471 order = order->next;
472 }
473 } else {
474 TextColour colour;
475 std::string string = GetTimetableTravelString(*order, i, colour);
476
477 DrawString(rtl ? tr.left : middle, rtl ? middle : tr.right, tr.top, string, colour);
478
479 if (final_order) break;
480 }
481
482 i++;
484 }
485 }
486
491 void DrawArrivalDeparturePanel(const Rect &r) const
492 {
493 const Vehicle *v = this->vehicle;
494
495 /* Arrival and departure times are handled in an all-or-nothing approach,
496 * i.e. are only shown if we can calculate all times.
497 * Excluding order lists with only one order makes some things easier. */
499 if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
500
501 std::vector<TimetableArrivalDeparture> arr_dep(v->GetNumOrders());
503
505 int selected = this->sel_index;
506
507 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
508 bool show_late = this->show_expected && VehicleIsAboveLatenessThreshold(v->lateness_counter, true);
510
511 for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
512 /* Don't draw anything if it extends past the end of the window. */
513 if (!this->vscroll->IsVisible(i)) break;
514
515 /* TC_INVALID will skip the colour change. */
516 TextColour tc = show_late ? TC_RED : TC_INVALID;
517 if (i % 2 == 0) {
518 /* Draw an arrival time. */
519 if (arr_dep[i / 2].arrival != Ticks::INVALID_TICKS) {
520 /* First set the offset and text colour based on the expected/scheduled mode and some other things. */
522 if (this->show_expected && i / 2 == earlyID) {
523 /* Show expected arrival. */
524 this_offset = 0;
525 tc = TC_GREEN;
526 } else {
527 /* Show scheduled arrival. */
528 this_offset = offset;
529 }
530
531 /* Now actually draw the arrival time. */
532 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
533 /* Display seconds from now. */
536 i == selected ? TC_WHITE : TC_BLACK);
537 } else {
538 /* Show a date. */
541 i == selected ? TC_WHITE : TC_BLACK);
542 }
543 }
544 } else {
545 /* Draw a departure time. */
546 if (arr_dep[i / 2].departure != Ticks::INVALID_TICKS) {
547 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
548 /* Display seconds from now. */
551 i == selected ? TC_WHITE : TC_BLACK);
552 } else {
553 /* Show a date. */
556 i == selected ? TC_WHITE : TC_BLACK);
557 }
558 }
559 }
561 }
562 }
563
564 std::string GetTimetableTotalTimeString(TimerGameTick::Ticks total_time) const
565 {
566 auto [str, value] = GetTimetableParameters(total_time);
567 if (!this->vehicle->orders->IsCompleteTimetable()) return GetString(STR_TIMETABLE_TOTAL_TIME_INCOMPLETE, str, value);
568 if (total_time % TicksPerTimetableUnit() == 0) return GetString(STR_TIMETABLE_TOTAL_TIME, str, value);
569 return GetString(STR_TIMETABLE_APPROX_TIME, str, value);
570 }
571
576 void DrawSummaryPanel(const Rect &r) const
577 {
578 const Vehicle *v = this->vehicle;
579 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
580
582 if (total_time != 0) {
583 DrawString(tr, GetTimetableTotalTimeString(total_time));
584 }
586
587 /* Draw the lateness display, or indicate that the timetable has not started yet. */
588 if (v->timetable_start != 0) {
589 /* We are running towards the first station so we can start the
590 * timetable at the given time. */
591 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
592 /* Real time units use seconds relative to now. */
594 } else {
595 /* Other units use dates. */
597 }
598 } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
599 /* We aren't running on a timetable yet. */
601 } else if (!VehicleIsAboveLatenessThreshold(abs(v->lateness_counter), false)) {
602 /* We are on time. */
604 } else {
605 /* We are late. */
606 auto [str, value] = GetTimetableParameters(abs(v->lateness_counter));
608 }
609 }
610
611 void DrawWidget(const Rect &r, WidgetID widget) const override
612 {
613 switch (widget) {
615 this->DrawTimetablePanel(r);
616 break;
617 }
618
621 break;
622 }
623
625 this->DrawSummaryPanel(r);
626 break;
627 }
628 }
629 }
630
631 static inline std::tuple<VehicleOrderID, ModifyTimetableFlags> PackTimetableArgs(const Vehicle *v, uint selected, bool speed)
632 {
633 uint order_number = (selected + 1) / 2;
634 ModifyTimetableFlags mtf = (selected % 2 != 0) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME;
635
636 if (order_number >= v->GetNumOrders()) order_number = 0;
637
638 return { order_number, mtf };
639 }
640
641 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
642 {
643 const Vehicle *v = this->vehicle;
644
645 switch (widget) {
646 case WID_VT_ORDER_VIEW: // Order view button
647 ShowOrdersWindow(v);
648 break;
649
650 case WID_VT_TIMETABLE_PANEL: { // Main panel.
651 int selected = GetOrderFromTimetableWndPt(pt.y, v);
652
653 this->CloseChildWindows();
654 this->sel_index = (selected == OrderID::Invalid().base() || selected == this->sel_index) ? -1 : selected;
655 break;
656 }
657
658 case WID_VT_START_DATE: // Change the date that the timetable starts.
659 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
661 this->change_timetable_all = _ctrl_pressed;
663 } else {
665 }
666 break;
667
668 case WID_VT_CHANGE_TIME: { // "Wait For" button.
670 int selected = this->sel_index;
671 VehicleOrderID real = (selected + 1) / 2;
672
673 if (real >= v->GetNumOrders()) real = 0;
674
675 const Order *order = v->GetOrder(real);
676 std::string current;
677
678 if (order != nullptr) {
679 uint time = (selected % 2 != 0) ? order->GetTravelTime() : order->GetWaitTime();
680 time /= TicksPerTimetableUnit();
681
682 if (time != 0) {
683 current = GetString(STR_JUST_INT, time);
684 }
685 }
686
687 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
689 break;
690 }
691
692 case WID_VT_CHANGE_SPEED: { // Change max speed button.
694 int selected = this->sel_index;
695 VehicleOrderID real = (selected + 1) / 2;
696
697 if (real >= v->GetNumOrders()) real = 0;
698
699 std::string current;
700 const Order *order = v->GetOrder(real);
701 if (order != nullptr) {
702 if (order->GetMaxSpeed() != UINT16_MAX) {
704 }
705 }
706
707 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
709 break;
710 }
711
712 case WID_VT_CLEAR_TIME: { // Clear waiting time.
713 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, false);
714 if (_ctrl_pressed) {
716 } else {
718 }
719 break;
720 }
721
722 case WID_VT_CLEAR_SPEED: { // Clear max speed button.
723 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, true);
724 if (_ctrl_pressed) {
726 } else {
728 }
729 break;
730 }
731
732 case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
734 break;
735
736 case WID_VT_AUTOFILL: { // Autofill the timetable.
738 break;
739 }
740
741 case WID_VT_EXPECTED:
742 this->show_expected = !this->show_expected;
743 break;
744
746 ShowVehicleListWindow(v);
747 break;
748 }
749
750 this->SetDirty();
751 }
752
753 void OnQueryTextFinished(std::optional<std::string> str) override
754 {
755 if (!str.has_value()) return;
756
757 const Vehicle *v = this->vehicle;
758 uint64_t val = str->empty() ? 0 : std::strtoul(str->c_str(), nullptr, 10);
759 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, query_widget == WID_VT_CHANGE_SPEED);
760
761 switch (query_widget) {
762 case WID_VT_CHANGE_SPEED: {
764
765 if (this->change_timetable_all) {
767 } else {
769 }
770 break;
771 }
772
775
776 if (this->change_timetable_all) {
778 } else {
780 }
781 break;
782
783 case WID_VT_START_DATE: {
786 break;
787 }
788
789 default:
790 NOT_REACHED();
791 }
792 }
793
794 void OnResize() override
795 {
796 /* Update the scroll bar */
797 this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.Vertical());
798 }
799
808
813 if (_settings_client.gui.timetable_mode == TimetableMode::Seconds) {
814 this->SetDirty();
815 }
816 }};
817};
818
819static constexpr NWidgetPart _nested_timetable_widgets[] = {
821 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
822 NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION),
823 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetStringTip(STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
824 NWidget(WWT_SHADEBOX, COLOUR_GREY),
825 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
826 NWidget(WWT_STICKYBOX, COLOUR_GREY),
827 EndContainer(),
829 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_TIMETABLE_PANEL), SetMinimalSize(388, 82), SetResize(1, 10), SetToolTip(STR_TIMETABLE_TOOLTIP), SetScrollbar(WID_VT_SCROLLBAR), EndContainer(),
832 EndContainer(),
834 EndContainer(),
839 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),
840 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),
841 EndContainer(),
843 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),
844 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),
845 EndContainer(),
847 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_START_DATE), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_START, STR_TIMETABLE_START_TOOLTIP),
848 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),
849 EndContainer(),
851 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetStringTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
853 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetToolTip(STR_TIMETABLE_EXPECTED_TOOLTIP),
854 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
855 EndContainer(),
856 EndContainer(),
857 EndContainer(),
859 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),
860 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
861 EndContainer(),
862 EndContainer(),
863};
864
865static WindowDesc _timetable_desc(
866 WDP_AUTO, "view_vehicle_timetable", 400, 130,
869 _nested_timetable_widgets
870);
871
877{
880 AllocateWindowDescFront<TimetableWindow>(_timetable_desc, v->index);
881}
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:2447
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:2521
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:29
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:215
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:923
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
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:658
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:943
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(std::string_view 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:18
static const VehicleOrderID INVALID_VEH_ORDER_ID
Invalid vehicle order index (sentinel)
Definition order_type.h:40
@ ONSF_NO_STOP_AT_DESTINATION_STATION
The vehicle will stop at any station it passes except the destination.
Definition order_type.h:91
@ ONSF_STOP_EVERYWHERE
The vehicle will stop at any station it passes and the destination.
Definition order_type.h:89
ModifyTimetableFlags
Enumeration for the data to set in CmdChangeTimetable.
Definition order_type.h:187
@ MTF_TRAVEL_TIME
Set travel time.
Definition order_type.h:189
@ MTF_WAIT_TIME
Set wait time.
Definition order_type.h:188
@ MTF_TRAVEL_SPEED
Set max travel speed.
Definition order_type.h:190
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:57
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
uint64_t GetParamMaxValue(uint64_t max_value, uint min_count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:248
uint ConvertDisplaySpeedToKmhishSpeed(uint speed, VehicleType type)
Convert the given display speed to the km/h-ish speed.
Definition strings.cpp:987
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
uint ConvertKmhishSpeedToDisplaySpeed(uint speed, VehicleType type)
Convert the given km/h-ish speed to the display speed.
Definition strings.cpp:977
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
uint64_t GetParamMaxDigits(uint count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:230
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:371
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:294
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.
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
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.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
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.
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:274
void ReInit(int rx=0, int ry=0, bool reposition=false)
Re-initialize a window, and optionally change its size.
Definition window.cpp:955
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1736
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:744
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:502
void CloseChildWindows(WindowClass wc=WC_INVALID) const
Close all children a window might have in a head-recursive manner.
Definition window.cpp:1038
ResizeInfo resize
Resize information.
Definition window_gui.h:315
void DisableWidget(WidgetID widget_index)
Sets a widget to disabled.
Definition window_gui.h:392
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1726
Owner owner
The owner of the content shown in this window. Company colour is acquired from this variable.
Definition window_gui.h:317
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:442
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
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:382
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Stuff related to the text buffer GUI.
@ AcceptUnchanged
return success even when the text didn't change
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:18
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.
std::pair< StringParameter, StringParameter > GetTimetableParameters(TimerGameTick::Ticks ticks)
Get parameters to format timetable time.
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.
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:65
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:56
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:54
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:75
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:67
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:58
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:55
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:70
@ SZSP_NONE
Display plane with zero size in both directions (none filling and resizing).
@ EqualSize
Containers should keep all their (resizing) children equally large.
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:1143
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: