Tree: Add ability to configure text autowrap mode for individual cells

This commit is contained in:
Danil Alexeev 2023-06-03 15:17:35 +03:00
parent 543750a1b3
commit 010829f962
No known key found for this signature in database
GPG Key ID: 124453E157DA8DC7
3 changed files with 59 additions and 1 deletions

View File

@ -73,6 +73,13 @@
Removes the button at index [param button_index] in column [param column]. Removes the button at index [param button_index] in column [param column].
</description> </description>
</method> </method>
<method name="get_autowrap_mode" qualifiers="const">
<return type="int" enum="TextServer.AutowrapMode" />
<param index="0" name="column" type="int" />
<description>
Returns the text autowrap mode in the given [param column]. By default it is [constant TextServer.AUTOWRAP_OFF].
</description>
</method>
<method name="get_button" qualifiers="const"> <method name="get_button" qualifiers="const">
<return type="Texture2D" /> <return type="Texture2D" />
<param index="0" name="column" type="int" /> <param index="0" name="column" type="int" />
@ -448,6 +455,14 @@
Selects the given [param column]. Selects the given [param column].
</description> </description>
</method> </method>
<method name="set_autowrap_mode">
<return type="void" />
<param index="0" name="column" type="int" />
<param index="1" name="autowrap_mode" type="int" enum="TextServer.AutowrapMode" />
<description>
Sets the autowrap mode in the given [param column]. If set to something other than [constant TextServer.AUTOWRAP_OFF], the text gets wrapped inside the cell's bounding rectangle.
</description>
</method>
<method name="set_button"> <method name="set_button">
<return type="void" /> <return type="void" />
<param index="0" name="column" type="int" /> <param index="0" name="column" type="int" />

View File

@ -332,6 +332,25 @@ Control::TextDirection TreeItem::get_text_direction(int p_column) const {
return cells[p_column].text_direction; return cells[p_column].text_direction;
} }
void TreeItem::set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode) {
ERR_FAIL_INDEX(p_column, cells.size());
ERR_FAIL_COND(p_mode < TextServer::AUTOWRAP_OFF || p_mode > TextServer::AUTOWRAP_WORD_SMART);
if (cells[p_column].autowrap_mode == p_mode) {
return;
}
cells.write[p_column].autowrap_mode = p_mode;
cells.write[p_column].dirty = true;
_changed_notify(p_column);
cells.write[p_column].cached_minimum_size_dirty = true;
}
TextServer::AutowrapMode TreeItem::get_autowrap_mode(int p_column) const {
ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::AUTOWRAP_OFF);
return cells[p_column].autowrap_mode;
}
void TreeItem::set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser) { void TreeItem::set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser) {
ERR_FAIL_INDEX(p_column, cells.size()); ERR_FAIL_INDEX(p_column, cells.size());
@ -1483,6 +1502,9 @@ void TreeItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction); ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction);
ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction); ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction);
ClassDB::bind_method(D_METHOD("set_autowrap_mode", "column", "autowrap_mode"), &TreeItem::set_autowrap_mode);
ClassDB::bind_method(D_METHOD("get_autowrap_mode", "column"), &TreeItem::get_autowrap_mode);
ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override); ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override);
ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override); ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);
@ -1948,7 +1970,24 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) {
font_size = theme_cache.font_size; font_size = theme_cache.font_size;
} }
p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, p_item->cells[p_col].language); p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, p_item->cells[p_col].language);
p_item->cells.write[p_col].text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE);
BitField<TextServer::LineBreakFlag> break_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_EDGE_SPACES;
switch (p_item->cells.write[p_col].autowrap_mode) {
case TextServer::AUTOWRAP_OFF:
break;
case TextServer::AUTOWRAP_ARBITRARY:
break_flags.set_flag(TextServer::BREAK_GRAPHEME_BOUND);
break;
case TextServer::AUTOWRAP_WORD:
break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
break;
case TextServer::AUTOWRAP_WORD_SMART:
break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
break_flags.set_flag(TextServer::BREAK_ADAPTIVE);
break;
}
p_item->cells.write[p_col].text_buf->set_break_flags(break_flags);
TS->shaped_text_set_bidi_override(p_item->cells[p_col].text_buf->get_rid(), structured_text_parser(p_item->cells[p_col].st_parser, p_item->cells[p_col].st_args, valtext)); TS->shaped_text_set_bidi_override(p_item->cells[p_col].text_buf->get_rid(), structured_text_parser(p_item->cells[p_col].st_parser, p_item->cells[p_col].st_args, valtext));
p_item->cells.write[p_col].dirty = false; p_item->cells.write[p_col].dirty = false;
} }

View File

@ -69,6 +69,7 @@ private:
TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT; TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
Array st_args; Array st_args;
Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED; Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
bool dirty = true; bool dirty = true;
double min = 0.0; double min = 0.0;
double max = 100.0; double max = 100.0;
@ -227,6 +228,9 @@ public:
void set_text_direction(int p_column, Control::TextDirection p_text_direction); void set_text_direction(int p_column, Control::TextDirection p_text_direction);
Control::TextDirection get_text_direction(int p_column) const; Control::TextDirection get_text_direction(int p_column) const;
void set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode);
TextServer::AutowrapMode get_autowrap_mode(int p_column) const;
void set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser); void set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser);
TextServer::StructuredTextParser get_structured_text_bidi_override(int p_column) const; TextServer::StructuredTextParser get_structured_text_bidi_override(int p_column) const;