Skip to content

Commit b5710e8

Browse files
committed
feat: 特质支持显示描述文本
1 parent 46c81bc commit b5710e8

3 files changed

Lines changed: 59 additions & 7 deletions

File tree

Moder.Core/Models/Modifiers/LeafModifier.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public sealed class LeafModifier : IModifier, IEquatable<LeafModifier>
1616
public ModifierType Type => ModifierType.Leaf;
1717

1818
public const string CustomEffectTooltipKey = "custom_effect_tooltip";
19+
public const string CustomModifierTooltipKey = "custom_modifier_tooltip";
1920

2021
/// <summary>
2122
/// 从 <see cref="Leaf"/> 构建一个叶子修饰符, <see cref="LocalizationKey"/> 属性被设置为 <c>leaf.Key</c>

Moder.Core/Models/Vo/TraitVo.cs

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
1+
using System.Text;
12
using CommunityToolkit.Mvvm.ComponentModel;
23
using CommunityToolkit.Mvvm.Messaging;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.UI.Xaml.Controls;
56
using Microsoft.UI.Xaml.Documents;
67
using Moder.Core.Messages;
78
using Moder.Core.Models.Character;
9+
using Moder.Core.Parser;
810
using Moder.Core.Services;
11+
using Moder.Core.Services.GameResources;
912
using Moder.Language.Strings;
1013

1114
namespace Moder.Core.Models.Vo;
1215

13-
public sealed partial class TraitVo(Trait trait, string localisationName) : ObservableObject, IEquatable<TraitVo>
16+
public sealed partial class TraitVo(Trait trait, string localisationName)
17+
: ObservableObject,
18+
IEquatable<TraitVo>
1419
{
1520
public string Name => trait.Name;
1621
public Trait Trait => trait;
1722
public string LocalisationName => localisationName;
1823
public TextBlock Description => GetDescription();
1924

25+
private static readonly ModifierService ModifierService =
26+
App.Current.Services.GetRequiredService<ModifierService>();
27+
private static readonly LocalisationService LocalisationService =
28+
App.Current.Services.GetRequiredService<LocalisationService>();
29+
30+
private static readonly string Separator = new('-', 25);
31+
2032
/// <summary>
2133
/// 是否已选择, 当值改变时, 发送 <see cref="SelectedTraitChangedMessage"/> 通知
2234
/// </summary>
@@ -31,11 +43,7 @@ partial void OnIsSelectedChanged(bool value)
3143
private TextBlock GetDescription()
3244
{
3345
var textBox = new TextBlock();
34-
foreach (
35-
var inline in App
36-
.Current.Services.GetRequiredService<ModifierService>()
37-
.GetModifierInlines(trait.AllModifiers)
38-
)
46+
foreach (var inline in ModifierService.GetModifierInlines(trait.AllModifiers))
3947
{
4048
textBox.Inlines.Add(inline);
4149
}
@@ -45,9 +53,47 @@ var inline in App
4553
textBox.Inlines.Add(new Run { Text = Resource.ModifierDisplay_Empty });
4654
}
4755

56+
textBox.Inlines.Add(new LineBreak());
57+
textBox.Inlines.Add(new Run { Text = Separator });
58+
textBox.Inlines.Add(new LineBreak());
59+
60+
var traitDesc = LocalisationService.GetValue($"{trait.Name}_desc");
61+
62+
foreach (var chars in GetCleanText(traitDesc).Chunk(15))
63+
{
64+
textBox.Inlines.Add(new Run { Text = new string(chars) });
65+
textBox.Inlines.Add(new LineBreak());
66+
}
67+
68+
if (textBox.Inlines[^1] is LineBreak)
69+
{
70+
textBox.Inlines.RemoveAt(textBox.Inlines.Count - 1);
71+
}
4872
return textBox;
4973
}
5074

75+
private static string GetCleanText(string rawText)
76+
{
77+
string text;
78+
if (LocalizationFormatParser.TryParse(rawText, out var formats))
79+
{
80+
var sb = new StringBuilder();
81+
foreach (var format in formats)
82+
{
83+
sb.Append(
84+
format.Type == LocalizationFormatType.TextWithColor ? format.Text[1..] : format.Text
85+
);
86+
}
87+
text = sb.ToString();
88+
}
89+
else
90+
{
91+
text = rawText;
92+
}
93+
94+
return text;
95+
}
96+
5197
public bool Equals(TraitVo? other)
5298
{
5399
if (other is null)
@@ -72,4 +118,4 @@ public override int GetHashCode()
72118
{
73119
return Name.GetHashCode();
74120
}
75-
}
121+
}

Moder.Core/Services/ModifierService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public IReadOnlyCollection<Inline> GetModifierInlines(IEnumerable<IModifier> mod
157157
leafModifier.Key,
158158
LeafModifier.CustomEffectTooltipKey
159159
)
160+
||
161+
StringComparer.OrdinalIgnoreCase.Equals(
162+
leafModifier.Key,
163+
LeafModifier.CustomModifierTooltipKey
164+
)
160165
)
161166
{
162167
addedInlines = _localisationFormatService.GetTextWithColor(

0 commit comments

Comments
 (0)