Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: Simplify drawing of timetables #10687

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lang/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4571,8 +4571,8 @@ STR_TIMETABLE_EXPECTED :{BLACK}Expected
STR_TIMETABLE_SCHEDULED :{BLACK}Scheduled
STR_TIMETABLE_EXPECTED_TOOLTIP :{BLACK}Switch between expected and scheduled

STR_TIMETABLE_ARRIVAL_ABBREVIATION :A:
STR_TIMETABLE_DEPARTURE_ABBREVIATION :D:
STR_TIMETABLE_ARRIVAL :A: {COLOUR}{DATE_TINY}
STR_TIMETABLE_DEPARTURE :D: {COLOUR}{DATE_TINY}


# Date window (for timetable)
Expand Down
6 changes: 6 additions & 0 deletions src/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,12 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
break;
}

case SCC_COLOUR: { // {COLOUR}
StringControlCode scc = (StringControlCode)(SCC_BLUE + args.GetInt32(SCC_COLOUR));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting a compile error on this line after the merge.

C:\Users\nielsm\Dev\OpenTTD\src\strings.cpp(1628): error C2660: 'StringParameters::GetInt32': function does not take 1 arguments
  C:\Users\nielsm\Dev\OpenTTD\src\strings_internal.h(91): note: see declaration of 'StringParameters::GetInt32'
  C:\Users\nielsm\Dev\OpenTTD\src\strings.cpp(1628): note: while trying to match the argument list '(StringControlCode)'

As far as I can tell, StringParameters::GetInt32() has never existed in a version that takes a parameter.

if (IsInsideMM(scc, SCC_BLUE, SCC_COLOUR)) builder.Utf8Encode(scc);
break;
}

default:
builder.Utf8Encode(b);
break;
Expand Down
1 change: 1 addition & 0 deletions src/table/control_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum StringControlCode {
SCC_GRAY,
SCC_DKBLUE,
SCC_BLACK,
SCC_COLOUR,
SCC_PUSH_COLOUR,
SCC_POP_COLOUR,

Expand Down
1 change: 1 addition & 0 deletions src/table/strgen_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static const CmdStruct _cmd_structs[] = {
{"GRAY", EmitSingleChar, SCC_GRAY, 0, -1, C_DONTCOUNT},
{"DKBLUE", EmitSingleChar, SCC_DKBLUE, 0, -1, C_DONTCOUNT},
{"BLACK", EmitSingleChar, SCC_BLACK, 0, -1, C_DONTCOUNT},
{"COLOUR", EmitSingleChar, SCC_COLOUR, 1, -1, C_NONE},
{"PUSH_COLOUR", EmitSingleChar, SCC_PUSH_COLOUR, 0, -1, C_DONTCOUNT},
{"POP_COLOUR", EmitSingleChar, SCC_POP_COLOUR, 0, -1, C_DONTCOUNT},

Expand Down
31 changes: 9 additions & 22 deletions src/timetable_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ struct TimetableWindow : Window {
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.
uint deparr_time_width; ///< The width of the departure/arrival time
uint deparr_abbr_width; ///< The width of the departure/arrival abbreviation
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.
Expand Down Expand Up @@ -195,10 +193,8 @@ struct TimetableWindow : Window {
{
switch (widget) {
case WID_VT_ARRIVAL_DEPARTURE_PANEL:
SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR, 0, FS_SMALL);
this->deparr_time_width = GetStringBoundingBox(STR_JUST_DATE_TINY).width;
this->deparr_abbr_width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION).width);
size->width = this->deparr_abbr_width + WidgetDimensions::scaled.hsep_wide + this->deparr_time_width + padding.width;
SetDParamMaxValue(1, MAX_YEAR * DAYS_IN_YEAR, 0, FS_SMALL);
size->width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE).width) + WidgetDimensions::scaled.hsep_wide + padding.width;
FALLTHROUGH;

case WID_VT_ARRIVAL_DEPARTURE_SELECTION:
Expand Down Expand Up @@ -442,44 +438,35 @@ struct TimetableWindow : Window {
bool show_late = this->show_expected && v->lateness_counter > DAY_TICKS;
Ticks offset = show_late ? 0 : -v->lateness_counter;

bool rtl = _current_text_dir == TD_RTL;
Rect abbr = tr.WithWidth(this->deparr_abbr_width, rtl);
Rect time = tr.WithWidth(this->deparr_time_width, !rtl);

for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
/* Don't draw anything if it extends past the end of the window. */
if (!this->vscroll->IsVisible(i)) break;

/* TC_INVALID will skip the colour change. */
SetDParam(0, show_late ? TC_RED : TC_INVALID);
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) {
/* Show expected arrival. */
this_offset = 0;
colour = TC_GREEN;
SetDParam(0, TC_GREEN);
} else {
/* 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, TimerGameCalendar::date + (arr_dep[i / 2].arrival + this_offset) / DAY_TICKS);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY, colour);
SetDParam(1, TimerGameCalendar::date + (arr_dep[i / 2].arrival + this_offset) / DAY_TICKS);
DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_ARRIVAL, i == selected ? TC_WHITE : TC_BLACK);
}
} 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, TimerGameCalendar::date + (arr_dep[i / 2].departure + offset) / DAY_TICKS);
DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY, colour);
SetDParam(1, TimerGameCalendar::date + (arr_dep[i / 2].departure + offset) / DAY_TICKS);
DrawString(tr.left, tr.right, tr.top, STR_TIMETABLE_DEPARTURE, i == selected ? TC_WHITE : TC_BLACK);
}
}
tr.top += FONT_HEIGHT_NORMAL;
Expand Down