Codechange: Refactor timetable GUI (#10613)

This commit is contained in:
Tyler Trahan 2023-04-21 11:31:26 -04:00 committed by GitHub
parent 7b539fa7c9
commit 394192dde8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 205 additions and 147 deletions

View File

@ -13,6 +13,8 @@
#include "date_type.h"
#include "vehicle_type.h"
static const uint8 MAX_TIMETABLE_START_YEARS = 15; ///< The maximum start date offset, in years.
void ShowTimetableWindow(const Vehicle *v);
void UpdateVehicleTimetable(Vehicle *v, bool travelling);
void SetTimetableParams(int param1, int param2, Ticks ticks);

View File

@ -14,6 +14,7 @@
#include "window_func.h"
#include "vehicle_base.h"
#include "timetable_cmd.h"
#include "timetable.h"
#include "table/strings.h"
@ -303,7 +304,7 @@ CommandCost CmdSetTimetableStart(DoCommandFlag flags, VehicleID veh_id, bool tim
/* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
if (start_date - _date > MAX_TIMETABLE_START_YEARS * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
if (timetable_all && !v->orders->IsCompleteTimetable()) return CMD_ERROR;
if (timetable_all && start_date + total_duration / DAY_TICKS > MAX_DAY) return CMD_ERROR;

View File

@ -23,6 +23,7 @@
#include "vehicle_gui.h"
#include "settings_type.h"
#include "timetable_cmd.h"
#include "timetable.h"
#include <cstdint>
#include "widgets/timetable_widget.h"
@ -150,14 +151,14 @@ static void ChangeTimetableStartCallback(const Window *w, Date date, void *data)
struct TimetableWindow : Window {
int sel_index;
VehicleTimetableWidgets query_widget; ///< Which button was clicked to open the query text input?
const Vehicle *vehicle; ///< Vehicle monitored by the window.
bool show_expected; ///< Whether we show expected arrival or scheduled
bool show_expected; ///< Whether we show expected arrival or scheduled.
uint deparr_time_width; ///< The width of the departure/arrival time
uint deparr_abbr_width; ///< The width of the departure/arrival abbreviation
Scrollbar *vscroll;
bool query_is_speed_query; ///< The currently open query window is a speed query and not a time query.
bool set_start_date_all; ///< Set start date using minutes text entry for all timetable entries (ctrl-click) action
bool change_timetable_all; ///< Set wait time or speed for all timetable entries (ctrl-click) action
Scrollbar *vscroll; ///< The scrollbar.
bool set_start_date_all; ///< Set start date using minutes text entry for all timetable entries (ctrl-click) action.
bool change_timetable_all; ///< Set wait time or speed for all timetable entries (ctrl-click) action.
TimetableWindow(WindowDesc *desc, WindowNumber window_number) :
Window(desc),
@ -351,17 +352,18 @@ struct TimetableWindow : Window {
}
}
void DrawWidget(const Rect &r, int widget) const override
/**
* Helper function to draw the timetable panel.
* @param r The rect to draw within.
*/
void DrawTimetablePanel(const Rect &r) const
{
const Vehicle *v = this->vehicle;
int selected = this->sel_index;
switch (widget) {
case WID_VT_TIMETABLE_PANEL: {
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
int i = this->vscroll->GetPosition();
VehicleOrderID order_id = (i + 1) / 2;
bool final_order = false;
int selected = this->sel_index;
bool rtl = _current_text_dir == TD_RTL;
SetDParamMaxValue(0, v->GetNumOrders(), 2);
@ -418,21 +420,27 @@ struct TimetableWindow : Window {
i++;
tr.top += FONT_HEIGHT_NORMAL;
}
break;
}
case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
/**
* Helper function to draw the arrival and departure panel.
* @param r The rect to draw within.
*/
void DrawArrivalDeparturePanel(const Rect &r) const
{
const Vehicle *v = this->vehicle;
/* Arrival and departure times are handled in an all-or-nothing approach,
* i.e. are only shown if we can calculate all times.
* Excluding order lists with only one order makes some things easier.
*/
* Excluding order lists with only one order makes some things easier. */
Ticks total_time = v->orders != nullptr ? v->orders->GetTimetableDurationIncomplete() : 0;
if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
std::vector<TimetableArrivalDeparture> arr_dep(v->GetNumOrders());
const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
int selected = this->sel_index;
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
bool show_late = this->show_expected && v->lateness_counter > DAY_TICKS;
@ -447,31 +455,48 @@ struct TimetableWindow : Window {
if (!this->vscroll->IsVisible(i)) break;
if (i % 2 == 0) {
/* Draw an arrival time. */
if (arr_dep[i / 2].arrival != INVALID_TICKS) {
/* First draw the arrival abbreviation. */
DrawString(abbr.left, abbr.right, tr.top, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
/* First set the offset and text colour based on the expected/scheduled mode and some other things. */
Ticks this_offset;
TextColour colour;
if (this->show_expected && i / 2 == earlyID) {
SetDParam(0, _date + arr_dep[i / 2].arrival / DAY_TICKS);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY, TC_GREEN);
/* Show expected arrival. */
this_offset = 0;
colour = TC_GREEN;
} else {
SetDParam(0, _date + (arr_dep[i / 2].arrival + offset) / DAY_TICKS);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY,
show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
/* Show scheduled arrival. */
this_offset = offset;
colour = show_late ? TC_RED : (i == selected ? TC_WHITE : TC_BLACK);
}
/* Now actually draw the arrival time. */
SetDParam(0, _date + (arr_dep[i / 2].arrival + this_offset) / DAY_TICKS);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY, colour);
}
} else {
/* Draw a departure time. */
if (arr_dep[i / 2].departure != INVALID_TICKS) {
DrawString(abbr.left, abbr.right, tr.top, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
TextColour colour = show_late ? TC_RED : (i == selected ? TC_WHITE : TC_BLACK);
SetDParam(0, _date + (arr_dep[i / 2].departure + offset) / DAY_TICKS);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY,
show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY, colour);
}
}
tr.top += FONT_HEIGHT_NORMAL;
}
break;
}
case WID_VT_SUMMARY_PANEL: {
/**
* Helper function to draw the summary panel.
* @param r The rect to draw within.
*/
void DrawSummaryPanel(const Rect &r) const
{
const Vehicle *v = this->vehicle;
Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
Ticks total_time = v->orders != nullptr ? v->orders->GetTimetableDurationIncomplete() : 0;
@ -497,6 +522,23 @@ struct TimetableWindow : Window {
SetTimetableParams(0, 1, abs(v->lateness_counter));
DrawString(tr, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
}
}
void DrawWidget(const Rect &r, int widget) const override
{
switch (widget) {
case WID_VT_TIMETABLE_PANEL: {
this->DrawTimetablePanel(r);
break;
}
case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
this->DrawArrivalDeparturePanel(r);
break;
}
case WID_VT_SUMMARY_PANEL: {
this->DrawSummaryPanel(r);
break;
}
}
@ -530,10 +572,11 @@ struct TimetableWindow : Window {
}
case WID_VT_START_DATE: // Change the date that the timetable starts.
ShowSetDateWindow(this, v->index, _date, _cur_year, _cur_year + 15, ChangeTimetableStartCallback, reinterpret_cast<void *>(static_cast<uintptr_t>(v->orders->IsCompleteTimetable() && _ctrl_pressed)));
ShowSetDateWindow(this, v->index, _date, _cur_year, _cur_year + MAX_TIMETABLE_START_YEARS, ChangeTimetableStartCallback, reinterpret_cast<void *>(static_cast<uintptr_t>(v->orders->IsCompleteTimetable() && _ctrl_pressed)));
break;
case WID_VT_CHANGE_TIME: { // "Wait For" button.
this->query_widget = WID_VT_CHANGE_TIME;
int selected = this->sel_index;
VehicleOrderID real = (selected + 1) / 2;
@ -552,13 +595,13 @@ struct TimetableWindow : Window {
}
}
this->query_is_speed_query = false;
this->change_timetable_all = _ctrl_pressed && (order != nullptr);
ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
break;
}
case WID_VT_CHANGE_SPEED: { // Change max speed button.
this->query_widget = WID_VT_CHANGE_SPEED;
int selected = this->sel_index;
VehicleOrderID real = (selected + 1) / 2;
@ -573,7 +616,6 @@ struct TimetableWindow : Window {
}
}
this->query_is_speed_query = true;
this->change_timetable_all = _ctrl_pressed && (order != nullptr);
ShowQueryString(current, STR_TIMETABLE_CHANGE_SPEED, 31, this, CS_NUMERAL, QSF_NONE);
break;
@ -625,21 +667,34 @@ struct TimetableWindow : Window {
if (str == nullptr) return;
const Vehicle *v = this->vehicle;
uint64 val = StrEmpty(str) ? 0 : strtoul(str, nullptr, 10);
if (this->query_is_speed_query) {
val = ConvertDisplaySpeedToKmhishSpeed(val, v->type);
} else {
if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS;
}
auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, query_widget == WID_VT_CHANGE_SPEED);
auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, this->query_is_speed_query);
switch (query_widget) {
case WID_VT_CHANGE_SPEED: {
val = ConvertDisplaySpeedToKmhishSpeed(val, v->type);
if (this->change_timetable_all) {
Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min<uint32>(val, UINT16_MAX));
} else {
Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min<uint32>(val, UINT16_MAX));
}
break;
}
case WID_VT_CHANGE_TIME:
if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS;
if (this->change_timetable_all) {
Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min<uint32>(val, UINT16_MAX));
} else {
Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min<uint32>(val, UINT16_MAX));
}
break;
default:
NOT_REACHED();
}
}
void OnResize() override