Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTL] Add pop_all, push_context and pop_context methods, and use it for print_rich to avoid unclosed tags. #79011

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@
Terminates the current tag. Use after [code]push_*[/code] methods to close BBCodes manually. Does not need to follow [code]add_*[/code] methods.
</description>
</method>
<method name="pop_all">
<return type="void" />
<description>
Terminates all tags opened by [code]push_*[/code] methods.
</description>
</method>
<method name="pop_context">
<return type="void" />
<description>
Terminates tags opened after the last [method push_context] call (including context marker), or all tags if there's no context marker on the stack.
</description>
</method>
<method name="push_bgcolor">
<return type="void" />
<param index="0" name="bgcolor" type="Color" />
Expand Down Expand Up @@ -298,6 +310,12 @@
Adds a [code][color][/code] tag to the tag stack.
</description>
</method>
<method name="push_context">
<return type="void" />
<description>
Adds a context marker to the tag stack. See [method pop_context].
</description>
</method>
<method name="push_customfx">
<return type="void" />
<param index="0" name="effect" type="RichTextEffect" />
Expand Down
8 changes: 1 addition & 7 deletions editor/editor_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,7 @@ void EditorLog::_add_log_line(LogMessage &p_message, bool p_replace_previous) {
} else {
log->add_text(p_message.text);
}

// Need to use pop() to exit out of the RichTextLabels current "push" stack.
// We only "push" in the above switch when message type != STD and RICH, so only pop when that is the case.
if (p_message.type != MSG_TYPE_STD && p_message.type != MSG_TYPE_STD_RICH) {
log->pop();
}

log->pop_all(); // Pop all unclosed tags.
log->add_newline();

if (p_replace_previous) {
Expand Down
43 changes: 43 additions & 0 deletions scene/gui/rich_text_label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,7 @@ void RichTextLabel::push_fade(int p_start_index, int p_length) {
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemFade *item = memnew(ItemFade);
item->starting_index = p_start_index;
item->length = p_length;
Expand All @@ -3454,6 +3455,7 @@ void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f, bool p
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemShake *item = memnew(ItemShake);
item->strength = p_strength;
item->rate = p_rate;
Expand All @@ -3465,6 +3467,7 @@ void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemWave *item = memnew(ItemWave);
item->frequency = p_frequency;
item->amplitude = p_amplitude;
Expand All @@ -3476,6 +3479,7 @@ void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemTornado *item = memnew(ItemTornado);
item->frequency = p_frequency;
item->radius = p_radius;
Expand All @@ -3487,6 +3491,7 @@ void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_freq
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemRainbow *item = memnew(ItemRainbow);
item->frequency = p_frequency;
item->saturation = p_saturation;
Expand Down Expand Up @@ -3520,6 +3525,7 @@ void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionar
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemCustomFX *item = memnew(ItemCustomFX);
item->custom_effect = p_custom_effect;
item->char_fx_transform->environment = p_environment;
Expand All @@ -3528,6 +3534,15 @@ void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionar
set_process_internal(true);
}

void RichTextLabel::push_context() {
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_COND(current->type == ITEM_TABLE);
ItemContext *item = memnew(ItemContext);
_add_item(item, true);
}

void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio) {
_stop_thread();
MutexLock data_lock(data_mutex);
Expand Down Expand Up @@ -3621,6 +3636,31 @@ void RichTextLabel::pop() {
current = current->parent;
}

void RichTextLabel::pop_context() {
_stop_thread();
MutexLock data_lock(data_mutex);

ERR_FAIL_NULL(current->parent);

while (current->parent && current != main) {
if (current->type == ITEM_FRAME) {
current_frame = static_cast<ItemFrame *>(current)->parent_frame;
} else if (current->type == ITEM_CONTEXT) {
current = current->parent;
return;
}
current = current->parent;
}
}

void RichTextLabel::pop_all() {
_stop_thread();
MutexLock data_lock(data_mutex);

current = main;
current_frame = main;
}

void RichTextLabel::clear() {
_stop_thread();
MutexLock data_lock(data_mutex);
Expand Down Expand Up @@ -5508,7 +5548,10 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("push_fgcolor", "fgcolor"), &RichTextLabel::push_fgcolor);
ClassDB::bind_method(D_METHOD("push_bgcolor", "bgcolor"), &RichTextLabel::push_bgcolor);
ClassDB::bind_method(D_METHOD("push_customfx", "effect", "env"), &RichTextLabel::push_customfx);
ClassDB::bind_method(D_METHOD("push_context"), &RichTextLabel::push_context);
ClassDB::bind_method(D_METHOD("pop_context"), &RichTextLabel::pop_context);
ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
ClassDB::bind_method(D_METHOD("pop_all"), &RichTextLabel::pop_all);

ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);

Expand Down
10 changes: 9 additions & 1 deletion scene/gui/rich_text_label.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class RichTextLabel : public Control {
ITEM_META,
ITEM_HINT,
ITEM_DROPCAP,
ITEM_CUSTOMFX
ITEM_CUSTOMFX,
ITEM_CONTEXT
};

enum MenuItems {
Expand Down Expand Up @@ -370,6 +371,10 @@ class RichTextLabel : public Control {
}
};

struct ItemContext : public Item {
ItemContext() { type = ITEM_CONTEXT; }
};

ItemFrame *main = nullptr;
Item *current = nullptr;
ItemFrame *current_frame = nullptr;
Expand Down Expand Up @@ -614,6 +619,7 @@ class RichTextLabel : public Control {
void push_bgcolor(const Color &p_color);
void push_fgcolor(const Color &p_color);
void push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment);
void push_context();
void set_table_column_expand(int p_column, bool p_expand, int p_ratio = 1);
void set_cell_row_background_color(const Color &p_odd_row_bg, const Color &p_even_row_bg);
void set_cell_border_color(const Color &p_color);
Expand All @@ -622,6 +628,8 @@ class RichTextLabel : public Control {
int get_current_table_column() const;
void push_cell();
void pop();
void pop_context();
void pop_all();

void clear();

Expand Down