From 612eed164cda8bca1413470b03682d0e948ed4b6 Mon Sep 17 00:00:00 2001 From: Michael Alexsander Date: Fri, 1 Mar 2024 01:27:36 -0300 Subject: [PATCH] Add auto translation to `Tree` --- scene/gui/tree.cpp | 31 +++++++++++++++++++++++++------ scene/gui/tree.h | 2 ++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 0c841dabbeb..30a5433e458 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -315,6 +315,13 @@ void TreeItem::set_text(int p_column, String p_text) { cells.write[p_column].max = MAX(cells[p_column].max, value); } cells.write[p_column].step = 0; + } else { + // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user. + if (tree && (!cells[p_column].editable || cells[p_column].mode != TreeItem::CELL_MODE_STRING)) { + cells.write[p_column].xl_text = tree->atr(p_text); + } else { + cells.write[p_column].xl_text = p_text; + } } cells.write[p_column].cached_minimum_size_dirty = true; @@ -1933,7 +1940,8 @@ void Tree::update_column(int p_col) { columns.write[p_col].text_buf->set_direction((TextServer::Direction)columns[p_col].text_direction); } - columns.write[p_col].text_buf->add_string(columns[p_col].title, theme_cache.tb_font, theme_cache.tb_font_size, columns[p_col].language); + columns.write[p_col].xl_title = atr(columns[p_col].title); + columns.write[p_col].text_buf->add_string(columns[p_col].xl_title, theme_cache.tb_font, theme_cache.tb_font_size, columns[p_col].language); columns.write[p_col].cached_minimum_width_dirty = true; } @@ -1957,7 +1965,7 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) { value = strings[j].get_slicec(':', 1).to_int(); } if (option == value) { - valtext = strings[j].get_slicec(':', 0); + valtext = atr(strings[j].get_slicec(':', 0)); break; } } @@ -1966,11 +1974,21 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) { valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step)); } } else { - valtext = p_item->cells[p_col].text; + // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user. + if (!p_item->cells[p_col].editable || p_item->cells[p_col].mode != TreeItem::CELL_MODE_STRING) { + p_item->cells.write[p_col].xl_text = atr(p_item->cells[p_col].text); + } else { + p_item->cells.write[p_col].xl_text = p_item->cells[p_col].text; + } + + valtext = p_item->cells[p_col].xl_text; } if (!p_item->cells[p_col].suffix.is_empty()) { - valtext += " " + p_item->cells[p_col].suffix; + if (!valtext.is_empty()) { + valtext += " "; + } + valtext += p_item->cells[p_col].suffix; } if (p_item->cells[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) { @@ -2076,7 +2094,7 @@ int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 if (p_item->cells[i].expand_right) { int plus = 1; - while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null()) { + while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].xl_text.is_empty() && p_item->cells[i + plus].icon.is_null()) { item_width += get_column_width(i + plus); plus++; skip2++; @@ -4666,7 +4684,7 @@ int Tree::get_column_minimum_width(int p_column) const { // Check if the visible title of the column is wider. if (show_column_titles) { - min_width = MAX(theme_cache.font->get_string_size(columns[p_column].title, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT), min_width); + min_width = MAX(theme_cache.font->get_string_size(columns[p_column].xl_title, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT), min_width); } if (!columns[p_column].clip_content) { @@ -4921,6 +4939,7 @@ void Tree::set_column_title(int p_column, const String &p_title) { } columns.write[p_column].title = p_title; + columns.write[p_column].xl_title = atr(p_title); update_column(p_column); queue_redraw(); } diff --git a/scene/gui/tree.h b/scene/gui/tree.h index ee5f0d420ba..194de1e994d 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -62,6 +62,7 @@ private: Ref icon; Rect2i icon_region; String text; + String xl_text; bool edit_multiline = false; String suffix; Ref text_buf; @@ -457,6 +458,7 @@ private: bool expand = true; bool clip_content = false; String title; + String xl_title; HorizontalAlignment title_alignment = HORIZONTAL_ALIGNMENT_CENTER; Ref text_buf; String language;