Skip to content

Commit 3d4f1cd

Browse files
committed
Merge pull request godotengine#77819 from 0xafbf/char-fx-tests
Make it possible to change character transform in RichTextEffect
2 parents f9fa866 + bca435b commit 3d4f1cd

5 files changed

Lines changed: 67 additions & 18 deletions

File tree

doc/classes/CharFXTransform.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
<member name="relative_index" type="int" setter="set_relative_index" getter="get_relative_index" default="0">
5050
The character offset of the glyph, relative to the current [RichTextEffect] custom block. Setting this property won't affect drawing.
5151
</member>
52+
<member name="transform" type="Transform2D" setter="set_transform" getter="get_transform" default="Transform2D(1, 0, 0, 1, 0, 0)">
53+
The current transform of the current glyph. It can be overridden (for example, by driving the position and rotation from a curve). You can also alter the existing value to apply transforms on top of other effects.
54+
</member>
5255
<member name="visible" type="bool" setter="set_visibility" getter="is_visible" default="true">
5356
If [code]true[/code], the character will be drawn. If [code]false[/code], the character will be hidden. Characters around hidden characters will reflow to take the space of hidden characters. If this is not desired, set their [member color] to [code]Color(1, 1, 1, 0)[/code] instead.
5457
</member>

scene/gui/rich_text_effect.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ RichTextEffect::RichTextEffect() {
6464
}
6565

6666
void CharFXTransform::_bind_methods() {
67+
ClassDB::bind_method(D_METHOD("get_transform"), &CharFXTransform::get_transform);
68+
ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CharFXTransform::set_transform);
69+
6770
ClassDB::bind_method(D_METHOD("get_range"), &CharFXTransform::get_range);
6871
ClassDB::bind_method(D_METHOD("set_range", "range"), &CharFXTransform::set_range);
6972

@@ -100,6 +103,7 @@ void CharFXTransform::_bind_methods() {
100103
ClassDB::bind_method(D_METHOD("get_font"), &CharFXTransform::get_font);
101104
ClassDB::bind_method(D_METHOD("set_font", "font"), &CharFXTransform::set_font);
102105

106+
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
103107
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "range"), "set_range", "get_range");
104108
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "elapsed_time"), "set_elapsed_time", "get_elapsed_time");
105109
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visibility", "is_visible");

scene/gui/rich_text_effect.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CharFXTransform : public RefCounted {
4141
static void _bind_methods();
4242

4343
public:
44+
Transform2D transform;
4445
Vector2i range;
4546
bool visibility = true;
4647
bool outline = false;
@@ -57,6 +58,9 @@ class CharFXTransform : public RefCounted {
5758
CharFXTransform();
5859
~CharFXTransform();
5960

61+
void set_transform(const Transform2D &p_transform) { transform = p_transform; }
62+
const Transform2D &get_transform() { return transform; }
63+
6064
Vector2i get_range() { return range; }
6165
void set_range(const Vector2i &p_range) { range = p_range; }
6266

scene/gui/rich_text_label.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "core/os/os.h"
3737
#include "core/string/translation.h"
3838
#include "scene/gui/label.h"
39+
#include "scene/gui/rich_text_effect.h"
3940
#include "scene/resources/atlas_texture.h"
4041
#include "scene/scene_string_names.h"
4142
#include "scene/theme/theme_db.h"
@@ -46,6 +47,18 @@
4647
#include "modules/regex/regex.h"
4748
#endif
4849

50+
RichTextLabel::ItemCustomFX::ItemCustomFX() {
51+
type = ITEM_CUSTOMFX;
52+
char_fx_transform.instantiate();
53+
}
54+
55+
RichTextLabel::ItemCustomFX::~ItemCustomFX() {
56+
_clear_children();
57+
58+
char_fx_transform.unref();
59+
custom_effect.unref();
60+
}
61+
4962
RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) const {
5063
if (p_free) {
5164
if (p_item->subitems.size()) {
@@ -1028,6 +1041,8 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
10281041
}
10291042

10301043
bool txt_visible = (font_outline_color.a != 0) || (font_shadow_color.a != 0);
1044+
Transform2D char_xform;
1045+
char_xform.set_origin(gloff + p_ofs);
10311046

10321047
for (int j = 0; j < fx_stack.size(); j++) {
10331048
ItemFX *item_fx = fx_stack[j];
@@ -1051,10 +1066,12 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
10511066
charfx->glyph_count = gl_cn;
10521067
charfx->offset = fx_offset;
10531068
charfx->color = font_color;
1069+
charfx->transform = char_xform;
10541070

10551071
bool effect_status = custom_effect->_process_effect_impl(charfx);
10561072
custom_fx_ok = effect_status;
10571073

1074+
char_xform = charfx->transform;
10581075
fx_offset += charfx->offset;
10591076
font_color = charfx->color;
10601077
frid = charfx->font;
@@ -1108,6 +1125,8 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
11081125
fx_offset = fx_offset.round();
11091126
}
11101127

1128+
Vector2i char_off = char_xform.get_origin();
1129+
11111130
// Draw glyph outlines.
11121131
const Color modulated_outline_color = font_outline_color * Color(1, 1, 1, font_color.a);
11131132
const Color modulated_shadow_color = font_shadow_color * Color(1, 1, 1, font_color.a);
@@ -1116,20 +1135,32 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
11161135
bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
11171136
if (!skip && frid != RID()) {
11181137
if (modulated_shadow_color.a > 0) {
1119-
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, p_ofs + fx_offset + gloff + p_shadow_ofs, gl, modulated_shadow_color);
1120-
}
1121-
if (modulated_shadow_color.a > 0 && p_shadow_outline_size > 0) {
1122-
TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, p_shadow_outline_size, p_ofs + fx_offset + gloff + p_shadow_ofs, gl, modulated_shadow_color);
1138+
Transform2D char_reverse_xform;
1139+
char_reverse_xform.set_origin(-char_off - p_shadow_ofs);
1140+
Transform2D char_final_xform = char_xform * char_reverse_xform;
1141+
char_final_xform.columns[2] += p_shadow_ofs;
1142+
draw_set_transform_matrix(char_final_xform);
1143+
1144+
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off + p_shadow_ofs, gl, modulated_shadow_color);
1145+
if (p_shadow_outline_size > 0) {
1146+
TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, p_shadow_outline_size, fx_offset + char_off + p_shadow_ofs, gl, modulated_shadow_color);
1147+
}
11231148
}
11241149
if (modulated_outline_color.a != 0.0 && size > 0) {
1125-
TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, size, p_ofs + fx_offset + gloff, gl, modulated_outline_color);
1150+
Transform2D char_reverse_xform;
1151+
char_reverse_xform.set_origin(-char_off);
1152+
Transform2D char_final_xform = char_xform * char_reverse_xform;
1153+
draw_set_transform_matrix(char_final_xform);
1154+
1155+
TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, size, fx_offset + char_off, gl, modulated_outline_color);
11261156
}
11271157
}
11281158
processed_glyphs_ol++;
11291159
}
11301160
gloff.x += glyphs[i].advance;
11311161
}
11321162
}
1163+
draw_set_transform_matrix(Transform2D());
11331164

11341165
Vector2 fbg_line_off = off + p_ofs;
11351166
// Draw background color box
@@ -1256,6 +1287,9 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
12561287

12571288
bool txt_visible = (font_color.a != 0);
12581289

1290+
Transform2D char_xform;
1291+
char_xform.set_origin(p_ofs + off);
1292+
12591293
for (int j = 0; j < fx_stack.size(); j++) {
12601294
ItemFX *item_fx = fx_stack[j];
12611295
bool cn = cprev_cluster || (cprev_conn && item_fx->connected);
@@ -1278,10 +1312,12 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
12781312
charfx->glyph_count = gl_cn;
12791313
charfx->offset = fx_offset;
12801314
charfx->color = font_color;
1315+
charfx->transform = char_xform;
12811316

12821317
bool effect_status = custom_effect->_process_effect_impl(charfx);
12831318
custom_fx_ok = effect_status;
12841319

1320+
char_xform = charfx->transform;
12851321
fx_offset += charfx->offset;
12861322
font_color = charfx->color;
12871323
frid = charfx->font;
@@ -1335,6 +1371,12 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
13351371
fx_offset = fx_offset.round();
13361372
}
13371373

1374+
Vector2i char_off = char_xform.get_origin();
1375+
Transform2D char_reverse_xform;
1376+
char_reverse_xform.set_origin(-char_off);
1377+
char_xform = char_xform * char_reverse_xform;
1378+
draw_set_transform_matrix(char_xform);
1379+
13381380
if (selected && use_selected_font_color) {
13391381
font_color = theme_cache.font_selected_color;
13401382
}
@@ -1345,9 +1387,9 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
13451387
if (txt_visible) {
13461388
if (!skip) {
13471389
if (frid != RID()) {
1348-
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, p_ofs + fx_offset + off, gl, font_color);
1390+
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
13491391
} else if (((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[i].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) {
1350-
TS->draw_hex_code_box(ci, glyphs[i].font_size, p_ofs + fx_offset + off, gl, font_color);
1392+
TS->draw_hex_code_box(ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
13511393
}
13521394
}
13531395
r_processed_glyphs++;
@@ -1375,6 +1417,8 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
13751417
}
13761418
off.x += glyphs[i].advance;
13771419
}
1420+
1421+
draw_set_transform_matrix(Transform2D());
13781422
}
13791423
if (ul_started) {
13801424
ul_started = false;

scene/gui/rich_text_label.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@
3333

3434
#include "core/object/worker_thread_pool.h"
3535
#include "scene/gui/popup_menu.h"
36-
#include "scene/gui/rich_text_effect.h"
3736
#include "scene/gui/scroll_bar.h"
3837
#include "scene/resources/text_paragraph.h"
3938

39+
class CharFXTransform;
40+
class RichTextEffect;
41+
4042
class RichTextLabel : public Control {
4143
GDCLASS(RichTextLabel, Control);
4244

@@ -374,17 +376,9 @@ class RichTextLabel : public Control {
374376
Ref<CharFXTransform> char_fx_transform;
375377
Ref<RichTextEffect> custom_effect;
376378

377-
ItemCustomFX() {
378-
type = ITEM_CUSTOMFX;
379-
char_fx_transform.instantiate();
380-
}
381-
382-
virtual ~ItemCustomFX() {
383-
_clear_children();
379+
ItemCustomFX();
384380

385-
char_fx_transform.unref();
386-
custom_effect.unref();
387-
}
381+
virtual ~ItemCustomFX();
388382
};
389383

390384
struct ItemContext : public Item {

0 commit comments

Comments
 (0)