Skip to content

Commit 7c1649d

Browse files
xJayMorexJohn Taylorfrosch123
authored
Fix OpenTTD#13955: Make graphs respect RTL (OpenTTD#13957)
Co-authored-by: John Taylor <[email protected]> Co-authored-by: frosch <[email protected]>
1 parent b25daba commit 7c1649d

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

src/graph_gui.cpp

+55-16
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,14 @@ struct BaseGraphWindow : Window {
332332
static_assert(GRAPH_MAX_DATASETS >= (int)NUM_CARGO && GRAPH_MAX_DATASETS >= (int)MAX_COMPANIES);
333333
assert(this->num_vert_lines > 0);
334334

335+
bool rtl = _current_text_dir == TD_RTL;
336+
335337
/* Rect r will be adjusted to contain just the graph, with labels being
336338
* placed outside the area. */
337339
r.top += ScaleGUITrad(5) + GetCharacterHeight(FS_SMALL) / 2;
338340
r.bottom -= (this->draw_dates ? 2 : 1) * GetCharacterHeight(FS_SMALL) + ScaleGUITrad(4);
339-
r.left += ScaleGUITrad(9);
340-
r.right -= ScaleGUITrad(5);
341+
r.left += ScaleGUITrad(rtl ? 5 : 9);
342+
r.right -= ScaleGUITrad(rtl ? 9 : 5);
341343

342344
/* Initial number of horizontal lines. */
343345
int num_hori_lines = 160 / ScaleGUITrad(MIN_GRID_PIXEL_SIZE);
@@ -349,14 +351,22 @@ struct BaseGraphWindow : Window {
349351

350352
int label_width = GetYLabelWidth(interval, num_hori_lines);
351353

352-
r.left += label_width;
354+
if (rtl) {
355+
r.right -= label_width;
356+
} else {
357+
r.left += label_width;
358+
}
353359

354360
int x_sep = (r.right - r.left) / this->num_vert_lines;
355361
int y_sep = (r.bottom - r.top) / num_hori_lines;
356362

357363
/* Redetermine right and bottom edge of graph to fit with the integer
358364
* separation values. */
359-
r.right = r.left + x_sep * this->num_vert_lines;
365+
if (rtl) {
366+
r.left = r.right - x_sep * this->num_vert_lines;
367+
} else {
368+
r.right = r.left + x_sep * this->num_vert_lines;
369+
}
360370
r.bottom = r.top + y_sep * num_hori_lines;
361371

362372
OverflowSafeInt64 interval_size = interval.highest + abs(interval.lowest);
@@ -369,7 +379,12 @@ struct BaseGraphWindow : Window {
369379
/* Draw the vertical grid lines. */
370380

371381
/* Don't draw the first line, as that's where the axis will be. */
372-
x = r.left + x_sep;
382+
if (rtl) {
383+
x_sep = -x_sep;
384+
x = r.right + x_sep;
385+
} else {
386+
x = r.left + x_sep;
387+
}
373388

374389
int grid_colour = GRAPH_GRID_COLOUR;
375390
for (int i = 1; i < this->num_vert_lines + 1; i++) {
@@ -385,7 +400,11 @@ struct BaseGraphWindow : Window {
385400
y = r.bottom;
386401

387402
for (int i = 0; i < (num_hori_lines + 1); i++) {
388-
GfxFillRect(r.left - ScaleGUITrad(3), y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
403+
if (rtl) {
404+
GfxFillRect(r.right + 1, y, r.right + ScaleGUITrad(3), y, GRAPH_AXIS_LINE_COLOUR);
405+
} else {
406+
GfxFillRect(r.left - ScaleGUITrad(3), y, r.left - 1, y, GRAPH_AXIS_LINE_COLOUR);
407+
}
389408
GfxFillRect(r.left, y, r.right, y, GRAPH_GRID_COLOUR);
390409
y -= y_sep;
391410
}
@@ -409,24 +428,36 @@ struct BaseGraphWindow : Window {
409428
y = r.top - GetCharacterHeight(FS_SMALL) / 2;
410429

411430
for (int i = 0; i < (num_hori_lines + 1); i++) {
412-
DrawString(r.left - label_width - ScaleGUITrad(4), r.left - ScaleGUITrad(4), y,
413-
GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, y_label),
414-
GRAPH_AXIS_LABEL_COLOUR, SA_RIGHT);
431+
if (rtl) {
432+
DrawString(r.right + ScaleGUITrad(4), r.right + label_width + ScaleGUITrad(4), y,
433+
GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, y_label),
434+
GRAPH_AXIS_LABEL_COLOUR, SA_RIGHT | SA_FORCE);
435+
} else {
436+
DrawString(r.left - label_width - ScaleGUITrad(4), r.left - ScaleGUITrad(4), y,
437+
GetString(STR_GRAPH_Y_LABEL, this->format_str_y_axis, y_label),
438+
GRAPH_AXIS_LABEL_COLOUR, SA_RIGHT | SA_FORCE);
439+
}
415440

416441
y_label -= y_label_separation;
417442
y += y_sep;
418443
}
419444

420445
/* Draw x-axis labels and markings for graphs based on financial quarters and years. */
421446
if (this->draw_dates) {
422-
x = r.left;
447+
x = rtl ? r.right : r.left;
423448
y = r.bottom + ScaleGUITrad(2);
424449
TimerGameEconomy::Month month = this->month;
425450
TimerGameEconomy::Year year = this->year;
426451
for (int i = 0; i < this->num_on_x_axis; i++) {
427-
DrawStringMultiLine(x, x + x_sep, y, this->height,
428-
GetString(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, STR_MONTH_ABBREV_JAN + month, year),
429-
GRAPH_AXIS_LABEL_COLOUR, SA_LEFT);
452+
if (rtl) {
453+
DrawStringMultiLine(x + x_sep, x, y, this->height,
454+
GetString(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, STR_MONTH_ABBREV_JAN + month, year),
455+
GRAPH_AXIS_LABEL_COLOUR, SA_LEFT);
456+
} else {
457+
DrawStringMultiLine(x, x + x_sep, y, this->height,
458+
GetString(month == 0 ? STR_GRAPH_X_LABEL_MONTH_YEAR : STR_GRAPH_X_LABEL_MONTH, STR_MONTH_ABBREV_JAN + month, year),
459+
GRAPH_AXIS_LABEL_COLOUR, SA_LEFT);
460+
}
430461

431462
month += this->month_increment;
432463
if (month >= 12) {
@@ -440,7 +471,7 @@ struct BaseGraphWindow : Window {
440471
}
441472
} else {
442473
/* Draw x-axis labels for graphs not based on quarterly performance (cargo payment rates, and all graphs when using wallclock units). */
443-
x = r.left;
474+
x = rtl ? r.right : r.left;
444475
y = r.bottom + ScaleGUITrad(2);
445476

446477
int16_t iterator;
@@ -454,7 +485,11 @@ struct BaseGraphWindow : Window {
454485
}
455486

456487
for (int i = 0; i < this->num_on_x_axis; i++) {
457-
DrawString(x + 1, x + x_sep - 1, y, GetString(STR_GRAPH_Y_LABEL_NUMBER, label), GRAPH_AXIS_LABEL_COLOUR, SA_HOR_CENTER);
488+
if (rtl) {
489+
DrawString(x + x_sep + 1, x - 1, y, GetString(STR_GRAPH_Y_LABEL_NUMBER, label), GRAPH_AXIS_LABEL_COLOUR, SA_HOR_CENTER);
490+
} else {
491+
DrawString(x + 1, x + x_sep - 1, y, GetString(STR_GRAPH_Y_LABEL_NUMBER, label), GRAPH_AXIS_LABEL_COLOUR, SA_HOR_CENTER);
492+
}
458493

459494
label += iterator;
460495
x += x_sep;
@@ -471,7 +506,11 @@ struct BaseGraphWindow : Window {
471506
if (HasBit(this->excluded_range, dataset.range_bit)) continue;
472507

473508
/* Centre the dot between the grid lines. */
474-
x = r.left + (x_sep / 2);
509+
if (rtl) {
510+
x = r.right + (x_sep / 2);
511+
} else {
512+
x = r.left + (x_sep / 2);
513+
}
475514

476515
uint prev_x = INVALID_DATAPOINT_POS;
477516
uint prev_y = INVALID_DATAPOINT_POS;

0 commit comments

Comments
 (0)