From 02d711eb610a49c0632b874f0de3e868b074f091 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 31 Jan 2017 22:57:46 +0100 Subject: [PATCH] RichTextLabel add function remove_line (cherry picked from commit c20b186e7324616843701ee044e87e72b737b47b) --- scene/gui/rich_text_label.cpp | 43 +++++++++++++++++++++++++++++++++++ scene/gui/rich_text_label.h | 2 ++ 2 files changed, 45 insertions(+) diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp index c6a44f3f04..f126145b1d 100644 --- a/scene/gui/rich_text_label.cpp +++ b/scene/gui/rich_text_label.cpp @@ -1216,6 +1216,28 @@ void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) } +void RichTextLabel::_remove_item(Item* p_item, const int p_line, const int p_subitem_line) { + + + int size = p_item->subitems.size(); + if (size == 0) { + p_item->parent->subitems.erase(p_item); + if (p_item->type == ITEM_NEWLINE) { + current_frame->lines.remove(p_line); + for (int i = p_subitem_line; i < current->subitems.size(); i++) { + if (current->subitems[i]->line > 0) + current->subitems[i]->line--; + } + } + } + else { + for (int i = 0; i < size; i++) { + _remove_item(p_item->subitems.front()->get(), p_line, p_subitem_line); + } + } + +} + void RichTextLabel::add_image(const Ref& p_image) { if (current->type==ITEM_TABLE) @@ -1240,6 +1262,26 @@ void RichTextLabel::add_newline() { } +bool RichTextLabel::remove_line(const int p_line) { + + if (p_line >= current_frame->lines.size() || p_line < 0) + return false; + + int lines = p_line * 2; + + if (current->subitems[lines]->type != ITEM_NEWLINE) + _remove_item(current->subitems[lines], current->subitems[lines]->line, lines); + + _remove_item(current->subitems[lines], current->subitems[lines]->line, lines); + + if (p_line == 0) { + main->lines[0].from = main; + } + + main->first_invalid_line = 0; + return true; +} + void RichTextLabel::push_font(const Ref& p_font) { ERR_FAIL_COND(current->type==ITEM_TABLE); @@ -1908,6 +1950,7 @@ void RichTextLabel::_bind_methods() { ObjectTypeDB::bind_method(_MD("add_text","text"),&RichTextLabel::add_text); ObjectTypeDB::bind_method(_MD("add_image","image:Texture"),&RichTextLabel::add_image); ObjectTypeDB::bind_method(_MD("newline"),&RichTextLabel::add_newline); + ObjectTypeDB::bind_method(_MD("remove_line"),&RichTextLabel::remove_line); ObjectTypeDB::bind_method(_MD("push_font","font"),&RichTextLabel::push_font); ObjectTypeDB::bind_method(_MD("push_color","color"),&RichTextLabel::push_color); ObjectTypeDB::bind_method(_MD("push_align","align"),&RichTextLabel::push_align); diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h index 1ac008a951..ae1ac48824 100644 --- a/scene/gui/rich_text_label.h +++ b/scene/gui/rich_text_label.h @@ -217,6 +217,7 @@ private: void _validate_line_caches(ItemFrame *p_frame); void _add_item(Item *p_item, bool p_enter=false,bool p_ensure_newline=false); + void _remove_item(Item *p_item, const int p_line, const int p_subitem_line); @@ -284,6 +285,7 @@ public: void add_text(const String& p_text); void add_image(const Ref& p_image); void add_newline(); + bool remove_line(const int p_line); void push_font(const Ref& p_font); void push_color(const Color& p_color); void push_underline();