Skip to content

Commit 7550b02

Browse files
committed
Merge pull request #77117 from Calinou/richtextlabel-add-pulse-effect
Add a `[pulse]` built-in effect to RichTextLabel
2 parents b4a1129 + 70e6c3c commit 7550b02

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

scene/gui/rich_text_label.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,11 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
10971097
ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
10981098

10991099
font_color = font_color.from_hsv(item_rainbow->frequency * (item_rainbow->elapsed_time + ((p_ofs.x + gloff.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
1100+
} else if (item_fx->type == ITEM_PULSE) {
1101+
ItemPulse *item_pulse = static_cast<ItemPulse *>(item_fx);
1102+
1103+
const float sined_time = (Math::ease(Math::pingpong(item_pulse->elapsed_time, 1.0 / item_pulse->frequency) * item_pulse->frequency, item_pulse->ease));
1104+
font_color = font_color.lerp(font_color * item_pulse->color, sined_time);
11001105
}
11011106
}
11021107

@@ -1315,6 +1320,11 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
13151320
ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
13161321

13171322
font_color = font_color.from_hsv(item_rainbow->frequency * (item_rainbow->elapsed_time + ((p_ofs.x + off.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
1323+
} else if (item_fx->type == ITEM_PULSE) {
1324+
ItemPulse *item_pulse = static_cast<ItemPulse *>(item_fx);
1325+
1326+
const float sined_time = (Math::ease(Math::pingpong(item_pulse->elapsed_time, 1.0 / item_pulse->frequency) * item_pulse->frequency, item_pulse->ease));
1327+
font_color = font_color.lerp(font_color * item_pulse->color, sined_time);
13181328
}
13191329
}
13201330

@@ -1675,7 +1685,7 @@ void RichTextLabel::_update_fx(RichTextLabel::ItemFrame *p_frame, double p_delta
16751685
while (it) {
16761686
ItemFX *ifx = nullptr;
16771687

1678-
if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW) {
1688+
if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW || it->type == ITEM_PULSE) {
16791689
ifx = static_cast<ItemFX *>(it);
16801690
}
16811691

@@ -2616,7 +2626,7 @@ bool RichTextLabel::_find_strikethrough(Item *p_item) {
26162626
void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack) {
26172627
Item *item = p_item;
26182628
while (item) {
2619-
if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW) {
2629+
if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW || item->type == ITEM_PULSE) {
26202630
r_stack.push_back(static_cast<ItemFX *>(item));
26212631
}
26222632

@@ -3494,6 +3504,17 @@ void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_freq
34943504
_add_item(item, true);
34953505
}
34963506

3507+
void RichTextLabel::push_pulse(const Color &p_color, float p_frequency, float p_ease) {
3508+
_stop_thread();
3509+
MutexLock data_lock(data_mutex);
3510+
3511+
ItemPulse *item = memnew(ItemPulse);
3512+
item->color = p_color;
3513+
item->frequency = p_frequency;
3514+
item->ease = p_ease;
3515+
_add_item(item, true);
3516+
}
3517+
34973518
void RichTextLabel::push_bgcolor(const Color &p_color) {
34983519
_stop_thread();
34993520
MutexLock data_lock(data_mutex);
@@ -4677,7 +4698,29 @@ void RichTextLabel::append_text(const String &p_bbcode) {
46774698
pos = brk_end + 1;
46784699
tag_stack.push_front("rainbow");
46794700
set_process_internal(true);
4701+
} else if (bbcode_name == "pulse") {
4702+
Color color = Color(1, 1, 1, 0.25);
4703+
OptionMap::Iterator color_option = bbcode_options.find("color");
4704+
if (color_option) {
4705+
color = Color::from_string(color_option->value, color);
4706+
}
4707+
4708+
float frequency = 1.0;
4709+
OptionMap::Iterator freq_option = bbcode_options.find("freq");
4710+
if (freq_option) {
4711+
frequency = freq_option->value.to_float();
4712+
}
46804713

4714+
float ease = -2.0;
4715+
OptionMap::Iterator ease_option = bbcode_options.find("ease");
4716+
if (ease_option) {
4717+
ease = ease_option->value.to_float();
4718+
}
4719+
4720+
push_pulse(color, frequency, ease);
4721+
pos = brk_end + 1;
4722+
tag_stack.push_front("pulse");
4723+
set_process_internal(true);
46814724
} else if (tag.begins_with("bgcolor=")) {
46824725
String color_str = tag.substr(8, tag.length()).unquote();
46834726
Color color = Color::from_string(color_str, theme_cache.default_color);

scene/gui/rich_text_label.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class RichTextLabel : public Control {
7070
ITEM_WAVE,
7171
ITEM_TORNADO,
7272
ITEM_RAINBOW,
73+
ITEM_PULSE,
7374
ITEM_BGCOLOR,
7475
ITEM_FGCOLOR,
7576
ITEM_META,
@@ -343,6 +344,14 @@ class RichTextLabel : public Control {
343344
ItemRainbow() { type = ITEM_RAINBOW; }
344345
};
345346

347+
struct ItemPulse : public ItemFX {
348+
Color color = Color(1.0, 1.0, 1.0, 0.25);
349+
float frequency = 1.0f;
350+
float ease = -2.0f;
351+
352+
ItemPulse() { type = ITEM_PULSE; }
353+
};
354+
346355
struct ItemBGColor : public Item {
347356
Color color;
348357
ItemBGColor() { type = ITEM_BGCOLOR; }
@@ -611,6 +620,7 @@ class RichTextLabel : public Control {
611620
void push_wave(float p_frequency, float p_amplitude, bool p_connected);
612621
void push_tornado(float p_frequency, float p_radius, bool p_connected);
613622
void push_rainbow(float p_saturation, float p_value, float p_frequency);
623+
void push_pulse(const Color &p_color, float p_frequency, float p_ease);
614624
void push_bgcolor(const Color &p_color);
615625
void push_fgcolor(const Color &p_color);
616626
void push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment);

0 commit comments

Comments
 (0)