Add auto translate mode for cells in Tree

This commit is contained in:
Haoyu Qiu 2024-09-23 15:44:12 +08:00
parent e3213aaef5
commit 3c365a7fa5
3 changed files with 68 additions and 4 deletions

View file

@ -73,6 +73,13 @@
Removes the button at index [param button_index] in column [param column].
</description>
</method>
<method name="get_auto_translate_mode" qualifiers="const">
<return type="int" enum="Node.AutoTranslateMode" />
<param index="0" name="column" type="int" />
<description>
Returns the column's auto translate mode.
</description>
</method>
<method name="get_autowrap_mode" qualifiers="const">
<return type="int" enum="TextServer.AutowrapMode" />
<param index="0" name="column" type="int" />
@ -493,6 +500,15 @@
Selects the given [param column].
</description>
</method>
<method name="set_auto_translate_mode">
<return type="void" />
<param index="0" name="column" type="int" />
<param index="1" name="mode" type="int" enum="Node.AutoTranslateMode" />
<description>
Sets the given column's auto translate mode to [param mode].
All columns use [constant Node.AUTO_TRANSLATE_MODE_INHERIT] by default, which uses the same auto translate mode as the [Tree] itself.
</description>
</method>
<method name="set_autowrap_mode">
<return type="void" />
<param index="0" name="column" type="int" />

View file

@ -185,6 +185,26 @@ TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
return cells[p_column].mode;
}
/* auto translate mode */
void TreeItem::set_auto_translate_mode(int p_column, Node::AutoTranslateMode p_mode) {
ERR_FAIL_INDEX(p_column, cells.size());
if (cells[p_column].auto_translate_mode == p_mode) {
return;
}
cells.write[p_column].auto_translate_mode = p_mode;
cells.write[p_column].dirty = true;
cells.write[p_column].cached_minimum_size_dirty = true;
_changed_notify(p_column);
}
Node::AutoTranslateMode TreeItem::get_auto_translate_mode(int p_column) const {
ERR_FAIL_INDEX_V(p_column, cells.size(), Node::AUTO_TRANSLATE_MODE_INHERIT);
return cells[p_column].auto_translate_mode;
}
/* multiline editable */
void TreeItem::set_edit_multiline(int p_column, bool p_multiline) {
ERR_FAIL_INDEX(p_column, cells.size());
@ -247,6 +267,24 @@ void TreeItem::propagate_check(int p_column, bool p_emit_signal) {
_propagate_check_through_parents(p_column, p_emit_signal);
}
String TreeItem::atr(int p_column, const String &p_text) const {
ERR_FAIL_INDEX_V(p_column, cells.size(), tree->atr(p_text));
switch (cells[p_column].auto_translate_mode) {
case Node::AUTO_TRANSLATE_MODE_INHERIT: {
return tree->atr(p_text);
} break;
case Node::AUTO_TRANSLATE_MODE_ALWAYS: {
return tree->tr(p_text);
} break;
case Node::AUTO_TRANSLATE_MODE_DISABLED: {
return p_text;
} break;
}
ERR_FAIL_V_MSG(tree->atr(p_text), "Unexpected auto translate mode: " + itos(cells[p_column].auto_translate_mode));
}
void TreeItem::_propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal) {
TreeItem *current = get_first_child();
while (current) {
@ -323,7 +361,7 @@ void TreeItem::set_text(int p_column, String p_text) {
} 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);
cells.write[p_column].xl_text = atr(p_column, p_text);
} else {
cells.write[p_column].xl_text = p_text;
}
@ -1621,6 +1659,9 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "column", "mode"), &TreeItem::set_auto_translate_mode);
ClassDB::bind_method(D_METHOD("get_auto_translate_mode", "column"), &TreeItem::get_auto_translate_mode);
ClassDB::bind_method(D_METHOD("set_edit_multiline", "column", "multiline"), &TreeItem::set_edit_multiline);
ClassDB::bind_method(D_METHOD("is_edit_multiline", "column"), &TreeItem::is_edit_multiline);
@ -2009,7 +2050,7 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) {
int option = (int)p_item->cells[p_col].val;
valtext = atr(ETR("(Other)"));
valtext = p_item->atr(p_col, ETR("(Other)"));
Vector<String> strings = p_item->cells[p_col].text.split(",");
for (int j = 0; j < strings.size(); j++) {
int value = j;
@ -2017,7 +2058,7 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) {
value = strings[j].get_slicec(':', 1).to_int();
}
if (option == value) {
valtext = atr(strings[j].get_slicec(':', 0));
valtext = p_item->atr(p_col, strings[j].get_slicec(':', 0));
break;
}
}
@ -2028,7 +2069,7 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) {
} 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 (!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);
p_item->cells.write[p_col].xl_text = p_item->atr(p_col, p_item->cells[p_col].text);
} else {
p_item->cells.write[p_col].xl_text = p_item->cells[p_col].text;
}

View file

@ -64,6 +64,7 @@ private:
Rect2i icon_region;
String text;
String xl_text;
Node::AutoTranslateMode auto_translate_mode = Node::AUTO_TRANSLATE_MODE_INHERIT;
bool edit_multiline = false;
String suffix;
Ref<TextParagraph> text_buf;
@ -210,6 +211,10 @@ public:
void set_cell_mode(int p_column, TreeCellMode p_mode);
TreeCellMode get_cell_mode(int p_column) const;
/* auto translate mode */
void set_auto_translate_mode(int p_column, Node::AutoTranslateMode p_mode);
Node::AutoTranslateMode get_auto_translate_mode(int p_column) const;
/* multiline editable */
void set_edit_multiline(int p_column, bool p_multiline);
bool is_edit_multiline(int p_column) const;
@ -222,6 +227,8 @@ public:
void propagate_check(int p_column, bool p_emit_signal = true);
String atr(int p_column, const String &p_text) const;
private:
// Check helpers.
void _propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal);