GFramework/GFramework.Godot/Text/Effects/RichTextJitterEffect.cs
GeWuYou 22882f68c4 feat(text): 添加富文本效果系统和抖动效果实现
- 创建 RichTextEffectBase 基类提供统一的标签命名和环境参数读取逻辑
- 实现 RichTextJitterEffect 抖动效果类,支持振幅和速度参数调节
- 添加 DefaultRichTextEffectRegistry 默认效果注册表管理内置效果映射
- 创建 GfRichTextLabel 组合式富文本标签宿主,集成效果装配逻辑
- 定义 IRichTextEffectRegistry 接口实现效果注册表抽象
- 开发 RichTextEffectsController 装配控制器负责效果集合管理
- 实现 RichTextMarkup 工具类提供语义化富文本标签构建辅助方法
- 添加相关单元测试验证效果控制器和标记工具的功能正确性
2026-04-18 14:17:09 +08:00

63 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace GFramework.Godot.Text.Effects;
/// <summary>
/// 为文本提供抖动效果。
/// </summary>
/// <remarks>
/// 默认注册表会在每次宿主刷新时为该效果创建独立实例。
/// <see cref="_ProcessCustomFX" /> 内部会复用并修改 <see cref="_noise" /> 的 Seed因此该类型假定 Godot 在主线程顺序
/// 执行字符效果,不支持跨多个 <see cref="RichTextLabel" /> 共享同一实例,也不保证并发调用下的线程安全。
/// </remarks>
[GlobalClass]
[Tool]
public partial class RichTextJitterEffect : RichTextEffectBase
{
private readonly bool _animatedEffectsEnabled;
private readonly FastNoiseLite _noise;
/// <summary>
/// 初始化抖动效果。
/// </summary>
/// <param name="animatedEffectsEnabled">是否允许动态效果实际生效。</param>
public RichTextJitterEffect(bool animatedEffectsEnabled = true)
{
_animatedEffectsEnabled = animatedEffectsEnabled;
_noise = new FastNoiseLite
{
NoiseType = FastNoiseLite.NoiseTypeEnum.Perlin,
FractalOctaves = 8,
FractalGain = 0.8f
};
}
/// <summary>
/// 获取标签名。
/// </summary>
protected override string TagName => "jitter";
/// <summary>
/// 应用抖动位移。
/// </summary>
/// <param name="charFx">当前字符上下文。</param>
/// <returns>始终返回 <see langword="true" />。</returns>
public override bool _ProcessCustomFX(CharFXTransform charFx)
{
if (!_animatedEffectsEnabled)
{
return true;
}
var amplitude = GetFloat(charFx, "amplitude", 3.0f);
var speed = GetFloat(charFx, "speed", 600.0f);
_noise.Seed = (charFx.RelativeIndex + 1) * 131;
var x = _noise.GetNoise1D((float)charFx.ElapsedTime * speed);
_noise.Seed = (charFx.RelativeIndex + 1) * 737;
var y = _noise.GetNoise1D((float)charFx.ElapsedTime * speed);
charFx.Offset += new Vector2(x, y) * amplitude;
ApplyVisibility(charFx);
return true;
}
}