Skip to content

Commit

Permalink
Merge pull request #77802 from dalexeev/tree-add-autowrap-mode
Browse files Browse the repository at this point in the history
Tree: Add ability to configure text autowrap mode for individual cells
  • Loading branch information
akien-mga committed Jun 7, 2023
2 parents a9350d4 + 010829f commit bd62d8e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
15 changes: 15 additions & 0 deletions doc/classes/TreeItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
Removes the button at index [param button_index] in column [param column].
</description>
</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">
<return type="Texture2D" />
<param index="0" name="column" type="int" />
Expand Down Expand Up @@ -448,6 +455,14 @@
Selects the given [param column].
</description>
</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">
<return type="void" />
<param index="0" name="column" type="int" />
Expand Down
41 changes: 40 additions & 1 deletion scene/gui/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,25 @@ Control::TextDirection TreeItem::get_text_direction(int p_column) const {
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) {
ERR_FAIL_INDEX(p_column, cells.size());

Expand Down Expand Up @@ -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("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("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);

Expand Down Expand Up @@ -1948,7 +1970,24 @@ void Tree::update_item_cell(TreeItem *p_item, int p_col) {
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->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));
p_item->cells.write[p_col].dirty = false;
}
Expand Down
4 changes: 4 additions & 0 deletions scene/gui/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TreeItem : public Object {
TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
Array st_args;
Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
bool dirty = true;
double min = 0.0;
double max = 100.0;
Expand Down Expand Up @@ -227,6 +228,9 @@ class TreeItem : public Object {
void set_text_direction(int p_column, Control::TextDirection p_text_direction);
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);
TextServer::StructuredTextParser get_structured_text_bidi_override(int p_column) const;

Expand Down

0 comments on commit bd62d8e

Please sign in to comment.