mirror of
https://github.com/ppy/osu.git
synced 2025-01-31 13:33:20 +08:00
Refactor BeatmapAttributeText to compute values on the fly
This commit is contained in:
parent
80c77e6e05
commit
102f55a213
@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
@ -35,25 +33,6 @@ namespace osu.Game.Skinning.Components
|
|||||||
[Resolved]
|
[Resolved]
|
||||||
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
|
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
|
||||||
|
|
||||||
private readonly Dictionary<BeatmapAttribute, LocalisableString> valueDictionary = new Dictionary<BeatmapAttribute, LocalisableString>();
|
|
||||||
|
|
||||||
private static readonly ImmutableDictionary<BeatmapAttribute, LocalisableString> label_dictionary = new Dictionary<BeatmapAttribute, LocalisableString>
|
|
||||||
{
|
|
||||||
[BeatmapAttribute.CircleSize] = BeatmapsetsStrings.ShowStatsCs,
|
|
||||||
[BeatmapAttribute.Accuracy] = BeatmapsetsStrings.ShowStatsAccuracy,
|
|
||||||
[BeatmapAttribute.HPDrain] = BeatmapsetsStrings.ShowStatsDrain,
|
|
||||||
[BeatmapAttribute.ApproachRate] = BeatmapsetsStrings.ShowStatsAr,
|
|
||||||
[BeatmapAttribute.StarRating] = BeatmapsetsStrings.ShowStatsStars,
|
|
||||||
[BeatmapAttribute.Title] = EditorSetupStrings.Title,
|
|
||||||
[BeatmapAttribute.Artist] = EditorSetupStrings.Artist,
|
|
||||||
[BeatmapAttribute.DifficultyName] = EditorSetupStrings.DifficultyHeader,
|
|
||||||
[BeatmapAttribute.Creator] = EditorSetupStrings.Creator,
|
|
||||||
[BeatmapAttribute.Source] = EditorSetupStrings.Source,
|
|
||||||
[BeatmapAttribute.Length] = ArtistStrings.TracklistLength.ToTitle(),
|
|
||||||
[BeatmapAttribute.RankedStatus] = BeatmapDiscussionsStrings.IndexFormBeatmapsetStatusDefault,
|
|
||||||
[BeatmapAttribute.BPM] = BeatmapsetsStrings.ShowStatsBpm,
|
|
||||||
}.ToImmutableDictionary();
|
|
||||||
|
|
||||||
private readonly OsuSpriteText text;
|
private readonly OsuSpriteText text;
|
||||||
|
|
||||||
public BeatmapAttributeText()
|
public BeatmapAttributeText()
|
||||||
@ -74,52 +53,130 @@ namespace osu.Game.Skinning.Components
|
|||||||
{
|
{
|
||||||
base.LoadComplete();
|
base.LoadComplete();
|
||||||
|
|
||||||
Attribute.BindValueChanged(_ => updateLabel());
|
Attribute.BindValueChanged(_ => updateText());
|
||||||
Template.BindValueChanged(_ => updateLabel());
|
Template.BindValueChanged(_ => updateText());
|
||||||
beatmap.BindValueChanged(b =>
|
beatmap.BindValueChanged(b => updateText());
|
||||||
{
|
|
||||||
updateBeatmapContent(b.NewValue);
|
updateText();
|
||||||
updateLabel();
|
|
||||||
}, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateBeatmapContent(WorkingBeatmap workingBeatmap)
|
private void updateText()
|
||||||
{
|
|
||||||
valueDictionary[BeatmapAttribute.Title] = new RomanisableString(workingBeatmap.BeatmapInfo.Metadata.TitleUnicode, workingBeatmap.BeatmapInfo.Metadata.Title);
|
|
||||||
valueDictionary[BeatmapAttribute.Artist] = new RomanisableString(workingBeatmap.BeatmapInfo.Metadata.ArtistUnicode, workingBeatmap.BeatmapInfo.Metadata.Artist);
|
|
||||||
valueDictionary[BeatmapAttribute.DifficultyName] = workingBeatmap.BeatmapInfo.DifficultyName;
|
|
||||||
valueDictionary[BeatmapAttribute.Creator] = workingBeatmap.BeatmapInfo.Metadata.Author.Username;
|
|
||||||
valueDictionary[BeatmapAttribute.Source] = workingBeatmap.BeatmapInfo.Metadata.Source;
|
|
||||||
valueDictionary[BeatmapAttribute.Length] = TimeSpan.FromMilliseconds(workingBeatmap.BeatmapInfo.Length).ToFormattedDuration();
|
|
||||||
valueDictionary[BeatmapAttribute.RankedStatus] = workingBeatmap.BeatmapInfo.Status.GetLocalisableDescription();
|
|
||||||
valueDictionary[BeatmapAttribute.BPM] = workingBeatmap.BeatmapInfo.BPM.ToLocalisableString(@"F2");
|
|
||||||
valueDictionary[BeatmapAttribute.CircleSize] = ((double)workingBeatmap.BeatmapInfo.Difficulty.CircleSize).ToLocalisableString(@"F2");
|
|
||||||
valueDictionary[BeatmapAttribute.HPDrain] = ((double)workingBeatmap.BeatmapInfo.Difficulty.DrainRate).ToLocalisableString(@"F2");
|
|
||||||
valueDictionary[BeatmapAttribute.Accuracy] = ((double)workingBeatmap.BeatmapInfo.Difficulty.OverallDifficulty).ToLocalisableString(@"F2");
|
|
||||||
valueDictionary[BeatmapAttribute.ApproachRate] = ((double)workingBeatmap.BeatmapInfo.Difficulty.ApproachRate).ToLocalisableString(@"F2");
|
|
||||||
valueDictionary[BeatmapAttribute.StarRating] = workingBeatmap.BeatmapInfo.StarRating.ToLocalisableString(@"F2");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateLabel()
|
|
||||||
{
|
{
|
||||||
string numberedTemplate = Template.Value
|
string numberedTemplate = Template.Value
|
||||||
.Replace("{", "{{")
|
.Replace("{", "{{")
|
||||||
.Replace("}", "}}")
|
.Replace("}", "}}")
|
||||||
.Replace(@"{{Label}}", "{0}")
|
.Replace(@"{{Label}}", "{0}")
|
||||||
.Replace(@"{{Value}}", $"{{{1 + (int)Attribute.Value}}}");
|
.Replace(@"{{Value}}", "{1}");
|
||||||
|
|
||||||
object?[] args = valueDictionary.OrderBy(pair => pair.Key)
|
List<object?> values = new List<object?>
|
||||||
.Select(pair => pair.Value)
|
{
|
||||||
.Prepend(label_dictionary[Attribute.Value])
|
getLabelString(Attribute.Value),
|
||||||
.Cast<object?>()
|
getValueString(Attribute.Value)
|
||||||
.ToArray();
|
};
|
||||||
|
|
||||||
foreach (var type in Enum.GetValues<BeatmapAttribute>())
|
foreach (var type in Enum.GetValues<BeatmapAttribute>())
|
||||||
{
|
{
|
||||||
numberedTemplate = numberedTemplate.Replace($"{{{{{type}}}}}", $"{{{1 + (int)type}}}");
|
numberedTemplate = numberedTemplate.Replace($"{{{{{type}}}}}", $"{{{values.Count}}}");
|
||||||
|
values.Add(getValueString(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
text.Text = LocalisableString.Format(numberedTemplate, args);
|
text.Text = LocalisableString.Format(numberedTemplate, values.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalisableString getLabelString(BeatmapAttribute attribute)
|
||||||
|
{
|
||||||
|
switch (attribute)
|
||||||
|
{
|
||||||
|
case BeatmapAttribute.CircleSize:
|
||||||
|
return BeatmapsetsStrings.ShowStatsCs;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Accuracy:
|
||||||
|
return BeatmapsetsStrings.ShowStatsAccuracy;
|
||||||
|
|
||||||
|
case BeatmapAttribute.HPDrain:
|
||||||
|
return BeatmapsetsStrings.ShowStatsDrain;
|
||||||
|
|
||||||
|
case BeatmapAttribute.ApproachRate:
|
||||||
|
return BeatmapsetsStrings.ShowStatsAr;
|
||||||
|
|
||||||
|
case BeatmapAttribute.StarRating:
|
||||||
|
return BeatmapsetsStrings.ShowStatsStars;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Title:
|
||||||
|
return EditorSetupStrings.Title;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Artist:
|
||||||
|
return EditorSetupStrings.Artist;
|
||||||
|
|
||||||
|
case BeatmapAttribute.DifficultyName:
|
||||||
|
return EditorSetupStrings.DifficultyHeader;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Creator:
|
||||||
|
return EditorSetupStrings.Creator;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Source:
|
||||||
|
return EditorSetupStrings.Source;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Length:
|
||||||
|
return ArtistStrings.TracklistLength.ToTitle();
|
||||||
|
|
||||||
|
case BeatmapAttribute.RankedStatus:
|
||||||
|
return BeatmapDiscussionsStrings.IndexFormBeatmapsetStatusDefault;
|
||||||
|
|
||||||
|
case BeatmapAttribute.BPM:
|
||||||
|
return BeatmapsetsStrings.ShowStatsBpm;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalisableString getValueString(BeatmapAttribute attribute)
|
||||||
|
{
|
||||||
|
switch (attribute)
|
||||||
|
{
|
||||||
|
case BeatmapAttribute.Title:
|
||||||
|
return new RomanisableString(beatmap.Value.BeatmapInfo.Metadata.TitleUnicode, beatmap.Value.BeatmapInfo.Metadata.Title);
|
||||||
|
|
||||||
|
case BeatmapAttribute.Artist:
|
||||||
|
return new RomanisableString(beatmap.Value.BeatmapInfo.Metadata.ArtistUnicode, beatmap.Value.BeatmapInfo.Metadata.Artist);
|
||||||
|
|
||||||
|
case BeatmapAttribute.DifficultyName:
|
||||||
|
return beatmap.Value.BeatmapInfo.DifficultyName;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Creator:
|
||||||
|
return beatmap.Value.BeatmapInfo.Metadata.Author.Username;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Source:
|
||||||
|
return beatmap.Value.BeatmapInfo.Metadata.Source;
|
||||||
|
|
||||||
|
case BeatmapAttribute.Length:
|
||||||
|
return TimeSpan.FromMilliseconds(beatmap.Value.BeatmapInfo.Length).ToFormattedDuration();
|
||||||
|
|
||||||
|
case BeatmapAttribute.RankedStatus:
|
||||||
|
return beatmap.Value.BeatmapInfo.Status.GetLocalisableDescription();
|
||||||
|
|
||||||
|
case BeatmapAttribute.BPM:
|
||||||
|
return beatmap.Value.BeatmapInfo.BPM.ToLocalisableString(@"F2");
|
||||||
|
|
||||||
|
case BeatmapAttribute.CircleSize:
|
||||||
|
return ((double)beatmap.Value.BeatmapInfo.Difficulty.CircleSize).ToLocalisableString(@"F2");
|
||||||
|
|
||||||
|
case BeatmapAttribute.HPDrain:
|
||||||
|
return ((double)beatmap.Value.BeatmapInfo.Difficulty.DrainRate).ToLocalisableString(@"F2");
|
||||||
|
|
||||||
|
case BeatmapAttribute.Accuracy:
|
||||||
|
return ((double)beatmap.Value.BeatmapInfo.Difficulty.OverallDifficulty).ToLocalisableString(@"F2");
|
||||||
|
|
||||||
|
case BeatmapAttribute.ApproachRate:
|
||||||
|
return ((double)beatmap.Value.BeatmapInfo.Difficulty.ApproachRate).ToLocalisableString(@"F2");
|
||||||
|
|
||||||
|
case BeatmapAttribute.StarRating:
|
||||||
|
return beatmap.Value.BeatmapInfo.StarRating.ToLocalisableString(@"F2");
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void SetFont(FontUsage font) => text.Font = font.With(size: 40);
|
protected override void SetFont(FontUsage font) => text.Font = font.With(size: 40);
|
||||||
|
Loading…
Reference in New Issue
Block a user