Add auto translation to `Tree`

This commit is contained in:
Michael Alexsander 2024-03-01 01:27:36 -03:00
parent 8e951fd0a9
commit 612eed164c
No known key found for this signature in database
GPG Key ID: A9C91EE110F4EABA
2 changed files with 27 additions and 6 deletions

View File

@ -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].max = MAX(cells[p_column].max, value);
} }
cells.write[p_column].step = 0; 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; 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->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; 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(); value = strings[j].get_slicec(':', 1).to_int();
} }
if (option == value) { if (option == value) {
valtext = strings[j].get_slicec(':', 0); valtext = atr(strings[j].get_slicec(':', 0));
break; 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)); valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step));
} }
} else { } 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()) { 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) { 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) { if (p_item->cells[i].expand_right) {
int plus = 1; 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); item_width += get_column_width(i + plus);
plus++; plus++;
skip2++; 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. // Check if the visible title of the column is wider.
if (show_column_titles) { 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) { 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].title = p_title;
columns.write[p_column].xl_title = atr(p_title);
update_column(p_column); update_column(p_column);
queue_redraw(); queue_redraw();
} }

View File

@ -62,6 +62,7 @@ private:
Ref<Texture2D> icon; Ref<Texture2D> icon;
Rect2i icon_region; Rect2i icon_region;
String text; String text;
String xl_text;
bool edit_multiline = false; bool edit_multiline = false;
String suffix; String suffix;
Ref<TextParagraph> text_buf; Ref<TextParagraph> text_buf;
@ -457,6 +458,7 @@ private:
bool expand = true; bool expand = true;
bool clip_content = false; bool clip_content = false;
String title; String title;
String xl_title;
HorizontalAlignment title_alignment = HORIZONTAL_ALIGNMENT_CENTER; HorizontalAlignment title_alignment = HORIZONTAL_ALIGNMENT_CENTER;
Ref<TextParagraph> text_buf; Ref<TextParagraph> text_buf;
String language; String language;