Merge pull request #77819 from 0xafbf/char-fx-tests

Make it possible to change character transform in RichTextEffect
This commit is contained in:
Rémi Verschelde 2023-09-26 13:44:28 +02:00
commit 21cdedb79c
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 67 additions and 18 deletions

View File

@ -49,6 +49,9 @@
<member name="relative_index" type="int" setter="set_relative_index" getter="get_relative_index" default="0">
The character offset of the glyph, relative to the current [RichTextEffect] custom block. Setting this property won't affect drawing.
</member>
<member name="transform" type="Transform2D" setter="set_transform" getter="get_transform" default="Transform2D(1, 0, 0, 1, 0, 0)">
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.
</member>
<member name="visible" type="bool" setter="set_visibility" getter="is_visible" default="true">
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.
</member>

View File

@ -64,6 +64,9 @@ RichTextEffect::RichTextEffect() {
}
void CharFXTransform::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_transform"), &CharFXTransform::get_transform);
ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CharFXTransform::set_transform);
ClassDB::bind_method(D_METHOD("get_range"), &CharFXTransform::get_range);
ClassDB::bind_method(D_METHOD("set_range", "range"), &CharFXTransform::set_range);
@ -100,6 +103,7 @@ void CharFXTransform::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_font"), &CharFXTransform::get_font);
ClassDB::bind_method(D_METHOD("set_font", "font"), &CharFXTransform::set_font);
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "range"), "set_range", "get_range");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "elapsed_time"), "set_elapsed_time", "get_elapsed_time");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visibility", "is_visible");

View File

@ -41,6 +41,7 @@ protected:
static void _bind_methods();
public:
Transform2D transform;
Vector2i range;
bool visibility = true;
bool outline = false;
@ -57,6 +58,9 @@ public:
CharFXTransform();
~CharFXTransform();
void set_transform(const Transform2D &p_transform) { transform = p_transform; }
const Transform2D &get_transform() { return transform; }
Vector2i get_range() { return range; }
void set_range(const Vector2i &p_range) { range = p_range; }

View File

@ -36,6 +36,7 @@
#include "core/os/os.h"
#include "core/string/translation.h"
#include "scene/gui/label.h"
#include "scene/gui/rich_text_effect.h"
#include "scene/resources/atlas_texture.h"
#include "scene/scene_string_names.h"
#include "scene/theme/theme_db.h"
@ -46,6 +47,18 @@
#include "modules/regex/regex.h"
#endif
RichTextLabel::ItemCustomFX::ItemCustomFX() {
type = ITEM_CUSTOMFX;
char_fx_transform.instantiate();
}
RichTextLabel::ItemCustomFX::~ItemCustomFX() {
_clear_children();
char_fx_transform.unref();
custom_effect.unref();
}
RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) const {
if (p_free) {
if (p_item->subitems.size()) {
@ -1028,6 +1041,8 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
}
bool txt_visible = (font_outline_color.a != 0) || (font_shadow_color.a != 0);
Transform2D char_xform;
char_xform.set_origin(gloff + p_ofs);
for (int j = 0; j < fx_stack.size(); j++) {
ItemFX *item_fx = fx_stack[j];
@ -1051,10 +1066,12 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
charfx->glyph_count = gl_cn;
charfx->offset = fx_offset;
charfx->color = font_color;
charfx->transform = char_xform;
bool effect_status = custom_effect->_process_effect_impl(charfx);
custom_fx_ok = effect_status;
char_xform = charfx->transform;
fx_offset += charfx->offset;
font_color = charfx->color;
frid = charfx->font;
@ -1108,6 +1125,8 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
fx_offset = fx_offset.round();
}
Vector2i char_off = char_xform.get_origin();
// Draw glyph outlines.
const Color modulated_outline_color = font_outline_color * Color(1, 1, 1, font_color.a);
const Color modulated_shadow_color = font_shadow_color * Color(1, 1, 1, font_color.a);
@ -1116,13 +1135,24 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
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));
if (!skip && frid != RID()) {
if (modulated_shadow_color.a > 0) {
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, p_ofs + fx_offset + gloff + p_shadow_ofs, gl, modulated_shadow_color);
}
if (modulated_shadow_color.a > 0 && p_shadow_outline_size > 0) {
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);
Transform2D char_reverse_xform;
char_reverse_xform.set_origin(-char_off - p_shadow_ofs);
Transform2D char_final_xform = char_xform * char_reverse_xform;
char_final_xform.columns[2] += p_shadow_ofs;
draw_set_transform_matrix(char_final_xform);
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off + p_shadow_ofs, gl, modulated_shadow_color);
if (p_shadow_outline_size > 0) {
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);
}
}
if (modulated_outline_color.a != 0.0 && size > 0) {
TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, size, p_ofs + fx_offset + gloff, gl, modulated_outline_color);
Transform2D char_reverse_xform;
char_reverse_xform.set_origin(-char_off);
Transform2D char_final_xform = char_xform * char_reverse_xform;
draw_set_transform_matrix(char_final_xform);
TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, size, fx_offset + char_off, gl, modulated_outline_color);
}
}
processed_glyphs_ol++;
@ -1130,6 +1160,7 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
gloff.x += glyphs[i].advance;
}
}
draw_set_transform_matrix(Transform2D());
Vector2 fbg_line_off = off + p_ofs;
// Draw background color box
@ -1256,6 +1287,9 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
bool txt_visible = (font_color.a != 0);
Transform2D char_xform;
char_xform.set_origin(p_ofs + off);
for (int j = 0; j < fx_stack.size(); j++) {
ItemFX *item_fx = fx_stack[j];
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
charfx->glyph_count = gl_cn;
charfx->offset = fx_offset;
charfx->color = font_color;
charfx->transform = char_xform;
bool effect_status = custom_effect->_process_effect_impl(charfx);
custom_fx_ok = effect_status;
char_xform = charfx->transform;
fx_offset += charfx->offset;
font_color = charfx->color;
frid = charfx->font;
@ -1335,6 +1371,12 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
fx_offset = fx_offset.round();
}
Vector2i char_off = char_xform.get_origin();
Transform2D char_reverse_xform;
char_reverse_xform.set_origin(-char_off);
char_xform = char_xform * char_reverse_xform;
draw_set_transform_matrix(char_xform);
if (selected && use_selected_font_color) {
font_color = theme_cache.font_selected_color;
}
@ -1345,9 +1387,9 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
if (txt_visible) {
if (!skip) {
if (frid != RID()) {
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, p_ofs + fx_offset + off, gl, font_color);
TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
} 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)) {
TS->draw_hex_code_box(ci, glyphs[i].font_size, p_ofs + fx_offset + off, gl, font_color);
TS->draw_hex_code_box(ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
}
}
r_processed_glyphs++;
@ -1375,6 +1417,8 @@ int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_o
}
off.x += glyphs[i].advance;
}
draw_set_transform_matrix(Transform2D());
}
if (ul_started) {
ul_started = false;

View File

@ -33,10 +33,12 @@
#include "core/object/worker_thread_pool.h"
#include "scene/gui/popup_menu.h"
#include "scene/gui/rich_text_effect.h"
#include "scene/gui/scroll_bar.h"
#include "scene/resources/text_paragraph.h"
class CharFXTransform;
class RichTextEffect;
class RichTextLabel : public Control {
GDCLASS(RichTextLabel, Control);
@ -374,17 +376,9 @@ private:
Ref<CharFXTransform> char_fx_transform;
Ref<RichTextEffect> custom_effect;
ItemCustomFX() {
type = ITEM_CUSTOMFX;
char_fx_transform.instantiate();
}
ItemCustomFX();
virtual ~ItemCustomFX() {
_clear_children();
char_fx_transform.unref();
custom_effect.unref();
}
virtual ~ItemCustomFX();
};
struct ItemContext : public Item {