Adjust timetable GUI widget sizes and get rid of non-functional 'tiny' versions of...
[openttd-joker.git] / src / timetable_gui.cpp
blob0b150dc5b437cc8c2cf30b5b3ab6cedb37391d5b
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file timetable_gui.cpp GUI for time tabling. */
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "gui.h"
15 #include "window_gui.h"
16 #include "window_func.h"
17 #include "textbuf_gui.h"
18 #include "strings_func.h"
19 #include "vehicle_base.h"
20 #include "string_func.h"
21 #include "gfx_func.h"
22 #include "company_func.h"
23 #include "date_func.h"
24 #include "date_gui.h"
25 #include "vehicle_gui.h"
26 #include "settings_type.h"
27 #include "viewport_func.h"
28 #include "timetable.h"
30 #include "widgets/timetable_widget.h"
32 #include "table/sprites.h"
33 #include "table/strings.h"
34 #include "widgets/dropdown_func.h"
37 /** Entries for mode selection dropdown list. Order must be identical to the one in #TTSepMode */
38 static const StringID TimetableSeparationDropdownOptions[6] = {
39 STR_TTSEPARATION_AUTO,
40 STR_TTSEPARATION_OFF,
41 STR_TTSEPARATION_MAN_TIME,
42 STR_TTSEPARATION_MAN_NUM,
43 STR_TTSEPARATION_BUFFERED_AUTO,
44 INVALID_STRING_ID,
47 #include "safeguards.h"
49 /** Container for the arrival/departure dates of a vehicle */
50 struct TimetableArrivalDeparture {
51 Ticks arrival; ///< The arrival time
52 Ticks departure; ///< The departure time
55 /**
56 * Set the timetable parameters in the ticks (minutes) format.
57 * @param param1 the first of three successive DParam to fill
58 * @param ticks the number of ticks to 'draw'
60 void SetTimetableParams(Ticks ticks, int param1)
62 SetDParam(param1, STR_TIMETABLE_TICKS_MINUTES);
63 SetDParam(param1 + 1, ticks);
64 SetDParam(param1 + 2, ticks);
67 /**
68 * Check whether it is possible to determine how long the order takes.
69 * @param order the order to check.
70 * @param travelling whether we are interested in the travel or the wait part.
71 * @return true if the travel/wait time can be used.
73 static bool CanDetermineTimeTaken(const Order *order, bool travelling)
75 /* Current order is conditional */
76 if (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT)) return false;
77 /* No travel time and we have not already finished travelling */
78 if (travelling && !order->IsTravelTimetabled()) return false;
79 /* No wait time but we are loading at this timetabled station */
80 if (!travelling && !order->IsWaitTimetabled() && order->IsType(OT_GOTO_STATION) &&
81 !(order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) {
82 return false;
85 return true;
89 /**
90 * Fill the table with arrivals and departures
91 * @param v Vehicle which must have at least 2 orders.
92 * @param start order index to start at
93 * @param travelling Are we still in the travelling part of the start order
94 * @param table Fill in arrival and departures including intermediate orders
95 * @param offset Add this value to result and all arrivals and departures
97 static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, TimetableArrivalDeparture *table, Ticks offset)
99 assert(table != NULL);
100 assert(v->GetNumOrders() >= 2);
101 assert(start < v->GetNumOrders());
103 Ticks sum = offset;
104 VehicleOrderID i = start;
105 const Order *order = v->GetOrder(i);
107 /* Pre-initialize with unknown time */
108 for (int i = 0; i < v->GetNumOrders(); ++i) {
109 table[i].arrival = table[i].departure = INVALID_TICKS;
112 /* Cyclically loop over all orders until we reach the current one again.
113 * As we may start at the current order, do a post-checking loop */
114 do {
115 /* Automatic orders don't influence the overall timetable;
116 * they just add some untimetabled entries, but the time till
117 * the next non-implicit order can still be known. */
118 if (!order->IsType(OT_IMPLICIT)) {
119 if (travelling || i != start) {
120 if (!CanDetermineTimeTaken(order, true)) return;
121 sum += order->GetTimetabledTravel();
122 table[i].arrival = sum;
125 if (!CanDetermineTimeTaken(order, false)) return;
126 sum += order->GetTimetabledWait();
127 table[i].departure = sum;
130 ++i;
131 order = order->next;
132 if (i >= v->GetNumOrders()) {
133 i = 0;
134 assert(order == NULL);
135 order = v->GetFirstOrder();
137 } while (i != start);
139 /* When loading at a scheduled station we still have to treat the
140 * travelling part of the first order. */
141 if (!travelling) {
142 if (!CanDetermineTimeTaken(order, true)) return;
143 sum += order->GetTimetabledTravel();
144 table[i].arrival = sum;
149 struct TimetableWindow : Window {
150 int sel_index;
151 const Vehicle *vehicle; ///< Vehicle monitored by the window.
152 bool show_expected; ///< Whether we show expected arrival or scheduled
153 uint deparr_time_width; ///< The width of the departure/arrival time
154 uint deparr_abbr_width; ///< The width of the departure/arrival abbreviation
155 Scrollbar *vscroll;
156 bool query_is_speed_query; ///< The currently open query window is a speed query and not a time query
157 bool query_is_bulk_query; ///< The currently open query window applies to all relevant orders.
158 TTSepSettings new_sep_settings; ///< Contains new separation settings.
159 VehicleTimetableWidgets query_widget; ///< Required to determinate source of input query
161 TimetableWindow(WindowDesc *desc, WindowNumber window_number) :
162 Window(desc),
163 sel_index(-1),
164 vehicle(Vehicle::Get(window_number)),
165 show_expected(true)
167 this->new_sep_settings = vehicle->GetTimetableSeparationSettings();
168 this->CreateNestedTree();
169 this->vscroll = this->GetScrollbar(WID_VT_SCROLLBAR);
170 this->UpdateSelectionStates();
171 this->FinishInitNested(window_number);
173 this->owner = this->vehicle->owner;
176 ~TimetableWindow()
178 if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
179 MarkAllRouteStepsDirty(this->vehicle);
184 * Build the arrival-departure list for a given vehicle
185 * @param v the vehicle to make the list for
186 * @param table the table to fill
187 * @return if next arrival will be early
189 static bool BuildArrivalDepartureList(const Vehicle *v, TimetableArrivalDeparture *table)
191 assert(HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED));
193 bool travelling = (!(v->current_order.IsType(OT_LOADING) || v->current_order.IsType(OT_WAITING)) || v->current_order.GetNonStopType() == ONSF_STOP_EVERYWHERE);
194 Ticks start_time = GetCurrentTickCount() - v->current_order_time;
196 FillTimetableArrivalDepartureTable(v, v->cur_real_order_index % v->GetNumOrders(), travelling, table, start_time);
198 return (travelling && v->lateness_counter < 0);
201 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
203 switch (widget) {
204 case WID_VT_ARRIVAL_DEPARTURE_PANEL:
205 SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR, 0, FS_SMALL);
206 this->deparr_time_width = GetStringBoundingBox(STR_JUST_TIME).width;
207 this->deparr_abbr_width = max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION).width);
208 size->width = WD_FRAMERECT_LEFT + this->deparr_abbr_width + 10 + this->deparr_time_width + 10 + WD_FRAMERECT_RIGHT;
209 FALLTHROUGH;
211 case WID_VT_ARRIVAL_DEPARTURE_SELECTION:
212 case WID_VT_TIMETABLE_PANEL:
213 resize->height = FONT_HEIGHT_NORMAL;
214 size->height = WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM;
215 break;
217 case WID_VT_SUMMARY_PANEL:
218 size->height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
219 break;
223 int GetOrderFromTimetableWndPt(int y, const Vehicle *v)
225 int sel = (y - this->GetWidget<NWidgetBase>(WID_VT_TIMETABLE_PANEL)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
227 if ((uint)sel >= this->vscroll->GetCapacity()) return INVALID_ORDER;
229 sel += this->vscroll->GetPosition();
231 return (sel < v->GetNumOrders() * 2 && sel >= 0) ? sel : INVALID_ORDER;
235 * Some data on this window has become invalid.
236 * @param data Information about the changed data.
237 * @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.
239 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
241 this->new_sep_settings = vehicle->GetTimetableSeparationSettings();
243 switch (data) {
244 case VIWD_AUTOREPLACE:
245 /* Autoreplace replaced the vehicle */
246 this->vehicle = Vehicle::Get(this->window_number);
247 break;
249 case VIWD_REMOVE_ALL_ORDERS:
250 /* Removed / replaced all orders (after deleting / sharing) */
251 if (this->sel_index == -1) break;
253 this->DeleteChildWindows();
254 this->sel_index = -1;
255 break;
257 case VIWD_MODIFY_ORDERS:
258 if (!gui_scope) break;
259 this->UpdateSelectionStates();
260 this->ReInit();
261 break;
263 default: {
264 if (gui_scope) break; // only do this once; from command scope
266 /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
267 * the order is being created / removed */
268 if (this->sel_index == -1) break;
270 VehicleOrderID from = GB(data, 0, 8);
271 VehicleOrderID to = GB(data, 8, 8);
273 if (from == to) break; // no need to change anything
275 /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
276 uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
278 VehicleOrderID selected_order = (this->sel_index + 1) / 2;
279 if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
281 bool travel = HasBit(this->sel_index, 0);
283 if (from != selected_order) {
284 /* Moving from preceding order? */
285 selected_order -= (int)(from <= selected_order);
286 /* Moving to preceding order? */
287 selected_order += (int)(to <= selected_order);
288 } else {
289 /* Now we are modifying the selected order */
290 if (to == INVALID_VEH_ORDER_ID) {
291 /* Deleting selected order */
292 this->DeleteChildWindows();
293 this->sel_index = -1;
294 break;
295 } else {
296 /* Moving selected order */
297 selected_order = to;
301 /* recompute new sel_index */
302 this->sel_index = 2 * selected_order - (int)travel;
303 /* travel time of first order needs special handling */
304 if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
305 break;
311 virtual void OnPaint()
313 const Vehicle *v = this->vehicle;
314 int selected = this->sel_index;
316 this->vscroll->SetCount(v->GetNumOrders() * 2);
318 if (v->owner == _local_company) {
319 bool disable = IsActionDisabled(v, selected);
320 bool disable_speed = disable || selected % 2 != 1 || v->type == VEH_AIRCRAFT;
322 this->SetWidgetDisabledState(WID_VT_CHANGE_TIME, disable);
323 this->SetWidgetDisabledState(WID_VT_CLEAR_TIME, disable);
324 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED, disable_speed);
325 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
326 this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST, !v->HasSharedOrdersList());
328 this->SetWidgetDisabledState(WID_VT_CONFIRM_ALL, !v->HasOrdersList());
329 this->SetWidgetDisabledState(WID_VT_RESET_LATENESS, !v->HasOrdersList());
330 this->SetWidgetDisabledState(WID_VT_AUTOMATE, !v->HasOrdersList());
331 } else {
332 this->DisableWidget(WID_VT_CONFIRM_ALL);
333 this->DisableWidget(WID_VT_CHANGE_TIME);
334 this->DisableWidget(WID_VT_CLEAR_TIME);
335 this->DisableWidget(WID_VT_CHANGE_SPEED);
336 this->DisableWidget(WID_VT_CLEAR_SPEED);
337 this->DisableWidget(WID_VT_RESET_LATENESS);
338 this->DisableWidget(WID_VT_AUTOMATE);
339 this->DisableWidget(WID_VT_SHARED_ORDER_LIST);
342 /* We can only set parameters if we're in one of the manual modes. */
343 bool enabled_state = (this->new_sep_settings.mode == TTS_MODE_MAN_N) || (this->new_sep_settings.mode == TTS_MODE_MAN_T);
345 this->SetWidgetDisabledState(WID_VT_TTSEP_SET_PARAMETER, !enabled_state);
347 this->SetWidgetLoweredState(WID_VT_AUTOMATE, HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE));
349 this->DrawWidgets();
352 virtual void SetStringParameters(int widget) const
354 switch (widget) {
355 case WID_VT_CAPTION: SetDParam(0, this->vehicle->index); break;
356 case WID_VT_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
357 case WID_VT_TTSEP_MODE_DROPDOWN: SetDParam(0, TimetableSeparationDropdownOptions[this->new_sep_settings.mode]); break;
358 case WID_VT_TTSEP_SET_PARAMETER: SetDParam(0, (this->new_sep_settings.mode == TTS_MODE_MAN_N) ? STR_TTSEPARATION_SET_NUM : STR_TTSEPARATION_SET_TIME); break;
362 virtual void DrawWidget(const Rect &r, int widget) const
364 const Vehicle *v = this->vehicle;
365 int selected = this->sel_index;
367 switch (widget) {
368 case WID_VT_TIMETABLE_PANEL: {
369 int y = r.top + WD_FRAMERECT_TOP;
370 int i = this->vscroll->GetPosition();
371 VehicleOrderID order_id = (i + 1) / 2;
372 bool final_order = false;
374 bool rtl = _current_text_dir == TD_RTL;
375 SetDParamMaxValue(0, v->GetNumOrders(), 2);
376 int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
377 int middle = rtl ? r.right - WD_FRAMERECT_RIGHT - index_column_width : r.left + WD_FRAMERECT_LEFT + index_column_width;
379 const Order *order = v->GetOrder(order_id);
380 while (order != NULL) {
381 /* Don't draw anything if it extends past the end of the window. */
382 if (!this->vscroll->IsVisible(i)) break;
384 if (i % 2 == 0) {
385 DrawOrderString(v, order, order_id, y, i == selected, true, r.left + WD_FRAMERECT_LEFT, middle, r.right - WD_FRAMERECT_RIGHT);
387 order_id++;
389 if (order_id >= v->GetNumOrders()) {
390 order = v->GetOrder(0);
391 final_order = true;
392 } else {
393 order = order->next;
395 } else {
396 StringID string;
397 TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
398 if (order->IsType(OT_CONDITIONAL)) {
399 string = STR_TIMETABLE_NO_TRAVEL;
400 } else if (order->IsType(OT_IMPLICIT)) {
401 string = STR_TIMETABLE_NOT_TIMETABLEABLE;
402 colour = ((i == selected) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
403 } else if (!order->IsTravelTimetabled()) {
404 if (order->GetTravelTime() > 0) {
405 SetTimetableParams(order->GetTravelTime());
406 string = order->GetMaxSpeed() != UINT16_MAX ?
407 STR_TIMETABLE_TRAVEL_FOR_MINUTES_SPEED_ESTIMATED :
408 STR_TIMETABLE_TRAVEL_FOR_MINUTES_ESTIMATED;
409 } else {
410 string = order->GetMaxSpeed() != UINT16_MAX ?
411 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED_SPEED :
412 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
414 } else {
415 SetTimetableParams(order->GetTimetabledTravel());
416 string = order->GetMaxSpeed() != UINT16_MAX ?
417 STR_TIMETABLE_TRAVEL_FOR_MINUTES_SPEED : STR_TIMETABLE_TRAVEL_FOR_MINUTES;
419 SetDParam(3, order->GetMaxSpeed());
421 DrawString(rtl ? r.left + WD_FRAMERECT_LEFT : middle, rtl ? middle : r.right - WD_FRAMERECT_LEFT, y, string, colour);
423 if (final_order) break;
426 i++;
427 y += FONT_HEIGHT_NORMAL;
429 break;
432 case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
433 /* Arrival and departure times are handled in an all-or-nothing approach,
434 * i.e. are only shown if we can calculate all times.
435 * Excluding order lists with only one order makes some things easier.
437 Ticks total_time = v->GetTimetableDurationIncomplete();
438 if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
440 TimetableArrivalDeparture *arr_dep = AllocaM(TimetableArrivalDeparture, v->GetNumOrders());
441 const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
443 VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
445 int y = r.top + WD_FRAMERECT_TOP;
447 bool show_late = this->show_expected && v->lateness_counter > _settings_client.gui.ticks_per_minute;
448 Ticks offset = show_late ? 0 : -v->lateness_counter;
450 if (HasBit(v->vehicle_flags, VF_SEPARATION_IN_PROGRESS)) {
451 show_late = false;
454 bool rtl = _current_text_dir == TD_RTL;
455 int abbr_left = rtl ? r.right - WD_FRAMERECT_RIGHT - this->deparr_abbr_width : r.left + WD_FRAMERECT_LEFT;
456 int abbr_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->deparr_abbr_width;
457 int time_left = rtl ? r.right - WD_FRAMERECT_RIGHT - r.left - WD_FRAMERECT_RIGHT - this->deparr_abbr_width - 10 - this->deparr_time_width : r.left + WD_FRAMERECT_RIGHT + this->deparr_abbr_width + 10;
458 int time_right = rtl ? r.right - WD_FRAMERECT_RIGHT - this->deparr_abbr_width - 10 : r.left + WD_FRAMERECT_RIGHT + r.left + WD_FRAMERECT_RIGHT + this->deparr_abbr_width + 10 + this->deparr_time_width;
460 for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
461 /* Don't draw anything if it extends past the end of the window. */
462 if (!this->vscroll->IsVisible(i)) break;
464 if (i % 2 == 0) {
465 if (arr_dep[i / 2].arrival != INVALID_TICKS) {
466 DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
467 if (this->show_expected && i / 2 == earlyID) {
468 SetDParam(0, arr_dep[i / 2].arrival);
469 DrawString(time_left, time_right, y, STR_JUST_TIME, TC_GREEN);
470 } else {
471 SetDParam(0, arr_dep[i / 2].arrival + offset);
472 DrawString(time_left, time_right, y, STR_JUST_TIME, show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
476 else {
477 if (arr_dep[i / 2].departure != INVALID_TICKS) {
478 DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
479 SetDParam(0, arr_dep[i / 2].departure + offset);
480 DrawString(time_left, time_right, y, STR_JUST_TIME,
481 show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
484 y += FONT_HEIGHT_NORMAL;
486 break;
489 case WID_VT_SUMMARY_PANEL: {
490 int y = r.top + WD_FRAMERECT_TOP;
492 Ticks total_time = v->GetTimetableDurationIncomplete();
493 if (total_time != 0) {
494 SetTimetableParams(total_time);
495 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, v->HasCompleteTimetable() ? STR_TIMETABLE_TOTAL_TIME_MINUTES : STR_TIMETABLE_TOTAL_TIME_MINUTES_INCOMPLETE);
497 y += FONT_HEIGHT_NORMAL;
499 auto lateness_counter = HasBit(v->vehicle_flags, VF_SEPARATION_IN_PROGRESS) ? 0 : v->lateness_counter;
501 if (v->timetable_start != 0) {
502 /* We are running towards the first station so we can start the
503 * timetable at the given time. */
504 SetDParam(0, STR_JUST_DATE);
505 SetDParam(1, v->timetable_start);
506 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_START_AT);
508 else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
509 /* We aren't running on a timetable yet, so how can we be "on time"
510 * when we aren't even "on service"/"on duty"? */
511 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_NOT_STARTED);
513 else if (lateness_counter == 0) {
514 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_ON_TIME);
516 else {
517 SetTimetableParams(abs(lateness_counter));
518 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY_MINUTES : STR_TIMETABLE_STATUS_LATE_MINUTES);
520 break;
523 case WID_VT_TTSEP_PANEL_TEXT: {
524 int y = r.top + WD_FRAMERECT_TOP; // Represents the current vertical position
525 const int left_border = r.left + WD_FRAMERECT_LEFT; // Represents the left border of the separation display frame
526 const int right_border = r.right - WD_FRAMERECT_RIGHT; // Represents the right border of the separation display frame.
528 /* If separation is inactive, we can stop here. */
529 if (!_settings_game.order.automatic_timetable_separation || !this->vehicle->HasOrdersList())
530 break;
532 /* If the new mode is OFF... */
533 if (this->new_sep_settings.mode == TTS_MODE_OFF) {
534 /* ... skip description lines. */
535 int offset = GetStringBoundingBox(STR_TTSEPARATION_REQ_TIME_DESC).height;
537 y = y + GetStringBoundingBox(STR_TTSEPARATION_REQ_NUM_DESC).height + offset;
540 else {
541 uint64 par;
543 // If separation hasn't just been switched off, we need to draw various description lines.
544 // The first line is the amount of separation which is either saved in the struct or must
545 // be calculated on the fly.
546 if (this->new_sep_settings.mode == TTS_MODE_MAN_T ||
547 this->new_sep_settings.mode == TTS_MODE_AUTO ||
548 this->new_sep_settings.mode == TTS_MODE_BUFFERED_AUTO) {
549 par = this->new_sep_settings.sep_ticks;
551 else {
552 par = this->vehicle->GetTimetableTotalDuration() / max(1u, this->new_sep_settings.num_veh);
555 if (this->new_sep_settings.mode == TTS_MODE_MAN_T ||
556 (this->vehicle->HasCompleteTimetable() && this->vehicle->IsTimetableSeparationValid())) {
558 SetDParam(0, par);
559 SetDParam(1, par);
560 DrawString(left_border, right_border, y, STR_TTSEPARATION_REQ_TIME_DESC, TC_BLACK);
561 y += GetStringBoundingBox(STR_TTSEPARATION_REQ_TIME_DESC).height;
563 DrawString(left_border, right_border, y, STR_TTSEPARATION_BETWEEN, TC_BLACK);
564 y += GetStringBoundingBox(STR_TTSEPARATION_BETWEEN).height;
566 else {
567 y += GetStringBoundingBox(STR_TTSEPARATION_REQ_TIME_DESC).height;
570 // Print either the chosen amount of vehicles (when in MAN_N mode) or the calculated result...
571 // Do not print anything at all if we do not have a valid timetable yet.
572 if (this->new_sep_settings.mode == TTS_MODE_MAN_N ||
573 this->new_sep_settings.mode == TTS_MODE_AUTO ||
574 this->new_sep_settings.mode == TTS_MODE_BUFFERED_AUTO) {
575 par = this->new_sep_settings.num_veh;
577 else {
578 par = this->vehicle->GetTimetableTotalDuration() / max(1u, this->new_sep_settings.sep_ticks);
581 if (this->new_sep_settings.mode == TTS_MODE_MAN_N ||
582 (this->vehicle->HasCompleteTimetable() && this->vehicle->IsTimetableSeparationValid())) {
583 SetDParam(0, par);
584 DrawString(left_border, right_border, y, STR_TTSEPARATION_REQ_NUM_DESC, TC_BLACK);
587 y += GetStringBoundingBox(STR_TTSEPARATION_REQ_NUM_DESC).height;
590 /* If separation is switched on at all... */
591 if (this->vehicle->IsTimetableSeparationOn()) {
592 if (!this->vehicle->HasCompleteTimetable()) {
593 SetDParam(0, STR_TTSEPARATION_STATUS_WAITING_FOR_TIMETABLE);
595 else {
596 /* ... set displayed status to either "Running" or "Initializing" */
597 SetDParam(0, (this->vehicle->IsTimetableSeparationValid()) ? STR_TTSEPARATION_STATUS_RUNNING : STR_TTSEPARATION_STATUS_INIT);
600 else {
601 /* If separation is switched off, show this instead. */
602 SetDParam(0, STR_TTSEPARATION_STATUS_OFF);
605 y += FONT_HEIGHT_NORMAL;
607 /* Print status description. */
608 DrawStringMultiLine(left_border, right_border, y, r.bottom - WD_FRAMERECT_BOTTOM, STR_TTSEPARATION_STATUS_DESC);
613 static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected, bool speed)
615 uint order_number = (selected + 1) / 2;
616 ModifyTimetableFlags mtf = (selected % 2 == 1) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME;
618 if (order_number >= v->GetNumOrders()) order_number = 0;
620 return v->index | (order_number << 20) | (mtf << 28);
623 static inline bool IsActionDisabled(const Vehicle *v, int selected)
625 bool disabled = true;
626 if (selected != -1) {
627 const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
628 if (selected % 2 == 1) {
629 disabled = order != NULL && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT));
631 else {
632 disabled = order == NULL || ((!(order->IsType(OT_GOTO_STATION) || (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_HALT))) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
635 return disabled;
638 virtual void OnClick(Point pt, int widget, int click_count)
640 const Vehicle *v = this->vehicle;
642 this->DeleteChildWindows(WC_QUERY_STRING);
644 switch (widget) {
645 case WID_VT_ORDER_VIEW: // Order view button
646 ShowOrdersWindow(v);
647 break;
649 case WID_VT_TIMETABLE_PANEL: { // Main panel.
650 int selected = GetOrderFromTimetableWndPt(pt.y, v);
652 /* Allow change time by double-clicking order */
653 if (click_count == 2) {
654 this->sel_index = selected == INVALID_ORDER ? -1 : selected;
655 this->OnClick(pt, WID_VT_CHANGE_TIME, click_count);
656 return;
657 } else {
658 this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
661 this->DeleteChildWindows();
662 break;
665 case WID_VT_CONFIRM_ALL: { // Confirm all estimated times as timetabled.
666 DoCommandP(0, v->index, 0, CMD_CONFIRM_ALL | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
667 break;
670 case WID_VT_CHANGE_TIME: { // "Wait For" button.
671 int selected = this->sel_index;
672 VehicleOrderID real = (selected + 1) / 2;
674 if (real >= v->GetNumOrders()) real = 0;
676 const Order *order = v->GetOrder(real);
677 StringID current = STR_EMPTY;
679 if (order != NULL) {
680 uint time = (selected % 2 == 1) ? order->GetTravelTime() : order->GetWaitTime();
682 if (time != 0) {
683 SetDParam(0, time);
684 current = STR_JUST_INT;
688 this->query_widget = WID_VT_CHANGE_TIME;
689 this->query_is_speed_query = false;
690 this->query_is_bulk_query = _ctrl_pressed;
691 ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
692 break;
695 case WID_VT_CHANGE_SPEED: { // Change max speed button.
696 int selected = this->sel_index;
697 VehicleOrderID real = (selected + 1) / 2;
699 if (real >= v->GetNumOrders()) real = 0;
701 StringID current = STR_EMPTY;
702 const Order *order = v->GetOrder(real);
703 if (order != NULL) {
704 if (order->GetMaxSpeed() != UINT16_MAX) {
705 SetDParam(0, ConvertKmhishSpeedToDisplaySpeed(order->GetMaxSpeed()));
706 current = STR_JUST_INT;
710 this->query_widget = WID_VT_CHANGE_SPEED;
711 this->query_is_speed_query = true;
712 this->query_is_bulk_query = _ctrl_pressed;
713 ShowQueryString(current, STR_TIMETABLE_CHANGE_SPEED, 31, this, CS_NUMERAL, QSF_NONE);
714 break;
717 case WID_VT_CLEAR_TIME: { // Clear waiting time.
718 uint32 p1 = PackTimetableArgs(v, this->sel_index, false);
719 DoCommandP(0, p1, 0, (_ctrl_pressed ? CMD_BULK_CHANGE_TIMETABLE : CMD_CHANGE_TIMETABLE) | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
720 break;
723 case WID_VT_CLEAR_SPEED: { // Clear max speed button.
724 uint32 p1 = PackTimetableArgs(v, this->sel_index, true);
725 DoCommandP(0, p1, UINT16_MAX, (_ctrl_pressed ? CMD_BULK_CHANGE_TIMETABLE : CMD_CHANGE_TIMETABLE) | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
726 break;
729 case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
730 DoCommandP(0, v->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
731 break;
733 case WID_VT_AUTOMATE: { // Automate the timetable.
734 uint32 p2 = 0;
735 if (!HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) SetBit(p2, 0);
736 if (_ctrl_pressed) SetBit(p2, 1);
737 DoCommandP(0, v->index, p2, CMD_AUTOMATE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
738 break;
741 case WID_VT_EXPECTED:
742 this->show_expected = !this->show_expected;
743 break;
745 case WID_VT_SHARED_ORDER_LIST:
746 ShowVehicleListWindow(v);
747 break;
749 case WID_VT_TTSEP_MODE_DROPDOWN: {
750 ShowDropDownMenu(this, TimetableSeparationDropdownOptions, this->new_sep_settings.mode, WID_VT_TTSEP_MODE_DROPDOWN, 0, 0);
751 break;
754 case WID_VT_TTSEP_SET_PARAMETER: {
755 this->query_widget = WID_VT_TTSEP_SET_PARAMETER;
756 SetDParam(0, (this->new_sep_settings.mode == TTS_MODE_MAN_N) ? this->new_sep_settings.num_veh : this->new_sep_settings.sep_ticks);
757 ShowQueryString(STR_JUST_INT, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_NONE);
758 break;
762 this->SetDirty();
765 virtual void OnDropdownSelect(int widget, int index)
767 assert(widget == WID_VT_TTSEP_MODE_DROPDOWN);
769 this->new_sep_settings = this->vehicle->GetTimetableSeparationSettings();
770 this->new_sep_settings.mode = (TTSepMode)index;
771 this->vehicle->SetTimetableSeparationSettings(this->new_sep_settings);
772 this->InvalidateData();
776 virtual void OnQueryTextFinished(char *str)
778 if (str == NULL || StrEmpty(str))
779 return;
781 switch (this->query_widget) {
782 case WID_VT_CHANGE_TIME:
783 case WID_VT_CHANGE_SPEED: {
784 const Vehicle *v = this->vehicle;
786 uint32 p1 = PackTimetableArgs(v, this->sel_index, this->query_is_speed_query);
788 uint64 val = StrEmpty(str) ? 0 : strtoul(str, NULL, 10);
789 if (this->query_is_speed_query) {
790 val = ConvertDisplaySpeedToKmhishSpeed(val);
793 uint32 p2 = minu(val, UINT16_MAX);
795 DoCommandP(0, p1, p2, (this->query_is_bulk_query ? CMD_BULK_CHANGE_TIMETABLE : CMD_CHANGE_TIMETABLE) | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
796 break;
798 case WID_VT_TTSEP_SET_PARAMETER: {
799 int value = atoi(str);
801 switch (this->new_sep_settings.mode)
803 case TTS_MODE_AUTO:
804 case TTS_MODE_BUFFERED_AUTO:
805 case TTS_MODE_OFF:
806 break;
808 case TTS_MODE_MAN_N:
809 this->new_sep_settings.num_veh = Clamp(value, 1, 65535);
810 break;
812 case TTS_MODE_MAN_T:
813 this->new_sep_settings.sep_ticks = Clamp(value, 1, 65535);
814 break;
816 default:
817 NOT_REACHED();
818 break;
821 this->vehicle->SetTimetableSeparationSettings(this->new_sep_settings);
822 this->InvalidateData();
823 break;
825 default:
826 NOT_REACHED();
827 break;
831 virtual void OnResize()
833 /* Update the scroll bar */
834 this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
838 * Update the selection state of the arrival/departure data
840 void UpdateSelectionStates()
842 this->GetWidget<NWidgetStacked>(WID_VT_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
843 this->GetWidget<NWidgetStacked>(WID_VT_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
846 virtual void OnFocus(Window *previously_focused_window)
848 if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
849 MarkAllRoutePathsDirty(this->vehicle);
850 MarkAllRouteStepsDirty(this->vehicle);
854 virtual void OnFocusLost(Window *newly_focused_window)
856 if (HasFocusedVehicleChanged(this->window_number, newly_focused_window)) {
857 MarkAllRoutePathsDirty(this->vehicle);
858 MarkAllRouteStepsDirty(this->vehicle);
862 const Vehicle *GetVehicle()
864 return this->vehicle;
868 static const NWidgetPart _nested_timetable_widgets[] = {
869 NWidget(NWID_HORIZONTAL),
870 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
871 NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
872 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
873 NWidget(WWT_SHADEBOX, COLOUR_GREY),
874 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
875 NWidget(WWT_STICKYBOX, COLOUR_GREY),
876 EndContainer(),
877 NWidget(NWID_HORIZONTAL),
878 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(),
879 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_ARRIVAL_DEPARTURE_SELECTION),
880 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_ARRIVAL_DEPARTURE_PANEL), SetMinimalSize(85, 0), SetFill(0, 1), SetDataTip(STR_NULL, STR_TIMETABLE_TOOLTIP), SetScrollbar(WID_VT_SCROLLBAR), EndContainer(),
881 EndContainer(),
882 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_VT_SCROLLBAR),
883 NWidget(WWT_PANEL, COLOUR_GREY),
884 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_TTSEPARATION_SETTINGS_DESC, STR_NULL), SetPadding(3),
885 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_VT_TTSEP_MODE_DROPDOWN), SetDataTip(STR_JUST_STRING, STR_TIMETABLE_TOOLTIP),
886 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_TTSEP_SET_PARAMETER), SetFill(1, 0), SetDataTip(STR_TTSEPARATION_SET_XX, STR_TIMETABLE_TOOLTIP),
887 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_TTSEP_PANEL_TEXT), SetFill(1, 1), SetResize(0, 1), SetMinimalSize(225, 44), EndContainer(),
888 EndContainer(),
889 EndContainer(),
890 EndContainer(),
891 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
892 NWidget(NWID_HORIZONTAL),
893 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
894 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
895 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),
896 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),
897 EndContainer(),
898 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
899 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),
900 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),
901 EndContainer(),
902 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
903 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CONFIRM_ALL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CONFIRM_ALL, STR_TIMETABLE_CONFIRM_ALL_TOOLTIP),
904 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),
905 EndContainer(),
906 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
907 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOMATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOMATE, STR_TIMETABLE_AUTOMATE_TOOLTIP),
908 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_EXPECTED_SELECTION),
909 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
910 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
911 EndContainer(),
912 EndContainer(),
913 EndContainer(),
914 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
915 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),
916 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
917 EndContainer(),
918 EndContainer(),
921 static WindowDesc _timetable_desc(
922 WDP_AUTO, "view_vehicle_timetable", 400, 130,
923 WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
924 WDF_CONSTRUCTION,
925 _nested_timetable_widgets, lengthof(_nested_timetable_widgets)
929 * Show the timetable for a given vehicle.
930 * @param v The vehicle to show the timetable for.
932 void ShowTimetableWindow(const Vehicle *v)
934 DeleteWindowById(WC_VEHICLE_DETAILS, v->index, false);
935 DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
936 AllocateWindowDescFront<TimetableWindow>(&_timetable_desc, v->index);