From 010829f96233f3d8ed5dc405ec7c7f61a84a3059 Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Sat, 3 Jun 2023 15:17:35 +0300 Subject: [PATCH] Tree: Add ability to configure text autowrap mode for individual cells --- doc/classes/TreeItem.xml | 15 +++++++++++++++ scene/gui/tree.cpp | 41 +++++++++++++++++++++++++++++++++++++++- scene/gui/tree.h | 4 ++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/doc/classes/TreeItem.xml b/doc/classes/TreeItem.xml index 33b69929bd9..559d38e5cca 100644 --- a/doc/classes/TreeItem.xml +++ b/doc/classes/TreeItem.xml @@ -73,6 +73,13 @@ Removes the button at index [param button_index] in column [param column]. + + + + + Returns the text autowrap mode in the given [param column]. By default it is [constant TextServer.AUTOWRAP_OFF]. + + @@ -448,6 +455,14 @@ Selects the given [param column]. + + + + + + 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. + + diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 8afd4af19d4..c6584eec757 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -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()); @@ -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); @@ -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 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; } diff --git a/scene/gui/tree.h b/scene/gui/tree.h index a5122bb1a79..c0814c651d6 100644 --- a/scene/gui/tree.h +++ b/scene/gui/tree.h @@ -69,6 +69,7 @@ private: 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; @@ -227,6 +228,9 @@ public: 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;