GFramework/GFramework.Godot/Text/RichTextEffectsController.cs
GeWuYou e5ad29314e feat(text): 添加富文本效果系统和颜色标记功能
- 实现 RichTextEffectBase 基类提供统一的标签命名和参数读取逻辑
- 添加 RichTextBlueEffect、RichTextGoldEffect、RichTextGreenEffect 和 RichTextRedEffect 颜色标记效果
- 添加 RichTextFadeInEffect、RichTextFlyInEffect、RichTextJitterEffect 和 RichTextSineEffect 动画效果
- 实现 DefaultRichTextEffectRegistry 默认效果注册表
- 创建 GfRichTextLabel 富文本标签宿主组件
- 添加 IRichTextEffectRegistry 效果注册表接口
- 实现 RichTextEffectEntry、RichTextEffectsController 和 RichTextProfile 配置管理类
- 添加 RichTextMarkup 语义化标签构建辅助方法
- 创建 RichTextMarkupTests 和 RichTextProfileTests 单元测试
2026-04-18 11:58:40 +08:00

71 lines
2.8 KiB
C#

using Array = Godot.Collections.Array;
namespace GFramework.Godot.Text;
/// <summary>
/// 负责把配置、开关和注册表装配为宿主标签的实际效果集合。
/// 该控制器是组合式扩展的装配中心,使 <see cref="GfRichTextLabel" /> 保持轻量。
/// </summary>
internal sealed class RichTextEffectsController
{
private readonly Func<bool> _animatedEffectsEnabledAccessor;
private readonly Func<bool> _frameworkEffectsEnabledAccessor;
private readonly RichTextLabel _host;
private readonly Func<RichTextProfile?> _profileAccessor;
private readonly IRichTextEffectRegistry _registry;
/// <summary>
/// 初始化控制器实例。
/// </summary>
/// <param name="host">目标富文本标签。</param>
/// <param name="registry">效果注册表。</param>
/// <param name="profileAccessor">当前配置访问器。</param>
/// <param name="frameworkEffectsEnabledAccessor">框架效果总开关访问器。</param>
/// <param name="animatedEffectsEnabledAccessor">字符动画开关访问器。</param>
public RichTextEffectsController(
RichTextLabel host,
IRichTextEffectRegistry registry,
Func<RichTextProfile?> profileAccessor,
Func<bool> frameworkEffectsEnabledAccessor,
Func<bool> animatedEffectsEnabledAccessor)
{
_host = host ?? throw new ArgumentNullException(nameof(host));
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
_profileAccessor = profileAccessor ?? throw new ArgumentNullException(nameof(profileAccessor));
_frameworkEffectsEnabledAccessor = frameworkEffectsEnabledAccessor
?? throw new ArgumentNullException(nameof(frameworkEffectsEnabledAccessor));
_animatedEffectsEnabledAccessor = animatedEffectsEnabledAccessor
?? throw new ArgumentNullException(nameof(animatedEffectsEnabledAccessor));
}
/// <summary>
/// 初始化并立即刷新宿主标签的效果集合。
/// </summary>
public void Initialize()
{
RefreshEffects();
}
/// <summary>
/// 根据当前配置和开关重建宿主标签上的 <see cref="RichTextLabel.CustomEffects" />。
/// </summary>
public void RefreshEffects()
{
if (!_frameworkEffectsEnabledAccessor())
{
_host.CustomEffects = new Array();
return;
}
var profile = _profileAccessor() ?? RichTextProfile.CreateBuiltInDefault();
var effects = _registry.CreateEffects(profile, _animatedEffectsEnabledAccessor());
var customEffects = new Array();
foreach (var effect in effects)
{
customEffects.Add(effect);
}
_host.CustomEffects = customEffects;
}
}