Refactor MetadataSection to use generics and inheritance

This commit is contained in:
Joseph Madamba 2022-12-21 10:18:47 -08:00
parent 01f09529a8
commit 1d39e8d0ce
8 changed files with 180 additions and 71 deletions

View File

@ -59,7 +59,7 @@ namespace osu.Game.Overlays.BeatmapSet
Child = new Container
{
RelativeSizeAxes = Axes.Both,
Child = new MetadataSection(MetadataType.Description),
Child = new MetadataSectionDescription(),
},
},
new Container
@ -78,10 +78,10 @@ namespace osu.Game.Overlays.BeatmapSet
Direction = FillDirection.Full,
Children = new[]
{
source = new MetadataSection(MetadataType.Source),
genre = new MetadataSection(MetadataType.Genre) { Width = 0.5f },
language = new MetadataSection(MetadataType.Language) { Width = 0.5f },
tags = new MetadataSection(MetadataType.Tags),
source = new MetadataSectionSource(),
genre = new MetadataSectionGenre { Width = 0.5f },
language = new MetadataSectionLanguage { Width = 0.5f },
tags = new MetadataSectionTags(),
},
},
},

View File

@ -1,10 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using Humanizer;
using osu.Framework.Extensions;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
@ -12,27 +9,45 @@ using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.Chat;
using osu.Game.Overlays.BeatmapListing;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Overlays.BeatmapSet
{
public partial class MetadataSection : Container
public abstract partial class MetadataSection : MetadataSection<string>
{
public override string Text
{
set
{
if (string.IsNullOrEmpty(value))
{
this.FadeOut(TRANSITION_DURATION);
return;
}
base.Text = value;
}
}
protected MetadataSection(MetadataType type, Action<string>? searchAction = null)
: base(type, searchAction)
{
}
}
public abstract partial class MetadataSection<T> : Container
{
private readonly FillFlowContainer textContainer;
private readonly MetadataType type;
private TextFlowContainer textFlow;
private TextFlowContainer? textFlow;
private readonly Action<string> searchAction;
protected readonly Action<T>? SearchAction;
private const float transition_duration = 250;
protected const float TRANSITION_DURATION = 250;
public MetadataSection(MetadataType type, Action<string> searchAction = null)
protected MetadataSection(MetadataType type, Action<T>? searchAction = null)
{
this.type = type;
this.searchAction = searchAction;
SearchAction = searchAction;
Alpha = 0;
@ -55,7 +70,7 @@ namespace osu.Game.Overlays.BeatmapSet
AutoSizeAxes = Axes.Y,
Child = new OsuSpriteText
{
Text = this.type.GetLocalisableDescription(),
Text = type.GetLocalisableDescription(),
Font = OsuFont.GetFont(weight: FontWeight.Bold, size: 14),
},
},
@ -63,23 +78,23 @@ namespace osu.Game.Overlays.BeatmapSet
};
}
public string Text
public virtual T Text
{
set
{
if (string.IsNullOrEmpty(value))
if (value == null)
{
this.FadeOut(transition_duration);
this.FadeOut(TRANSITION_DURATION);
return;
}
this.FadeIn(transition_duration);
this.FadeIn(TRANSITION_DURATION);
setTextAsync(value);
}
}
private void setTextAsync(string text)
private void setTextAsync(T text)
{
LoadComponentAsync(new LinkFlowContainer(s => s.Font = s.Font.With(size: 14))
{
@ -90,52 +105,15 @@ namespace osu.Game.Overlays.BeatmapSet
{
textFlow?.Expire();
switch (type)
{
case MetadataType.Tags:
string[] tags = text.Split(" ");
for (int i = 0; i <= tags.Length - 1; i++)
{
string tag = tags[i];
if (searchAction != null)
loaded.AddLink(tag, () => searchAction(tag));
else
loaded.AddLink(tag, LinkAction.SearchBeatmapSet, tag);
if (i != tags.Length - 1)
loaded.AddText(" ");
}
break;
case MetadataType.Source:
if (searchAction != null)
loaded.AddLink(text, () => searchAction(text));
else
loaded.AddLink(text, LinkAction.SearchBeatmapSet, text);
break;
case MetadataType.Genre:
loaded.AddLink(text.DehumanizeTo<SearchGenre>().GetLocalisableDescription(), LinkAction.FilterBeatmapSetGenre, text);
break;
case MetadataType.Language:
loaded.AddLink(text.DehumanizeTo<SearchLanguage>().GetLocalisableDescription(), LinkAction.FilterBeatmapSetLanguage, text);
break;
default:
loaded.AddText(text);
break;
}
AddMetadata(text, loaded);
textContainer.Add(textFlow = loaded);
// fade in if we haven't yet.
textContainer.FadeIn(transition_duration);
textContainer.FadeIn(TRANSITION_DURATION);
});
}
protected abstract void AddMetadata(T text, LinkFlowContainer loaded);
}
}

View File

@ -0,0 +1,21 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Game.Graphics.Containers;
namespace osu.Game.Overlays.BeatmapSet
{
public partial class MetadataSectionDescription : MetadataSection
{
public MetadataSectionDescription(Action<string>? searchAction = null)
: base(MetadataType.Description, searchAction)
{
}
protected override void AddMetadata(string text, LinkFlowContainer loaded)
{
loaded.AddText(text);
}
}
}

View File

@ -0,0 +1,25 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using Humanizer;
using osu.Framework.Extensions;
using osu.Game.Graphics.Containers;
using osu.Game.Online.Chat;
using osu.Game.Overlays.BeatmapListing;
namespace osu.Game.Overlays.BeatmapSet
{
public partial class MetadataSectionGenre : MetadataSection
{
public MetadataSectionGenre(Action<string>? searchAction = null)
: base(MetadataType.Genre, searchAction)
{
}
protected override void AddMetadata(string text, LinkFlowContainer loaded)
{
loaded.AddLink(text.DehumanizeTo<SearchGenre>().GetLocalisableDescription(), LinkAction.FilterBeatmapSetGenre, text);
}
}
}

View File

@ -0,0 +1,25 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using Humanizer;
using osu.Framework.Extensions;
using osu.Game.Graphics.Containers;
using osu.Game.Online.Chat;
using osu.Game.Overlays.BeatmapListing;
namespace osu.Game.Overlays.BeatmapSet
{
public partial class MetadataSectionLanguage : MetadataSection
{
public MetadataSectionLanguage(Action<string>? searchAction = null)
: base(MetadataType.Language, searchAction)
{
}
protected override void AddMetadata(string text, LinkFlowContainer loaded)
{
loaded.AddLink(text.DehumanizeTo<SearchLanguage>().GetLocalisableDescription(), LinkAction.FilterBeatmapSetLanguage, text);
}
}
}

View File

@ -0,0 +1,25 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Game.Graphics.Containers;
using osu.Game.Online.Chat;
namespace osu.Game.Overlays.BeatmapSet
{
public partial class MetadataSectionSource : MetadataSection
{
public MetadataSectionSource(Action<string>? searchAction = null)
: base(MetadataType.Source, searchAction)
{
}
protected override void AddMetadata(string text, LinkFlowContainer loaded)
{
if (SearchAction != null)
loaded.AddLink(text, () => SearchAction(text));
else
loaded.AddLink(text, LinkAction.SearchBeatmapSet, text);
}
}
}

View File

@ -0,0 +1,35 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Game.Graphics.Containers;
using osu.Game.Online.Chat;
namespace osu.Game.Overlays.BeatmapSet
{
public partial class MetadataSectionTags : MetadataSection
{
public MetadataSectionTags(Action<string>? searchAction = null)
: base(MetadataType.Tags, searchAction)
{
}
protected override void AddMetadata(string text, LinkFlowContainer loaded)
{
string[] tags = text.Split(" ");
for (int i = 0; i <= tags.Length - 1; i++)
{
string tag = tags[i];
if (SearchAction != null)
loaded.AddLink(tag, () => SearchAction(tag));
else
loaded.AddLink(tag, LinkAction.SearchBeatmapSet, tag);
if (i != tags.Length - 1)
loaded.AddText(" ");
}
}
}
}

View File

@ -141,9 +141,9 @@ namespace osu.Game.Screens.Select
LayoutEasing = Easing.OutQuad,
Children = new[]
{
description = new MetadataSection(MetadataType.Description, searchOnSongSelect),
source = new MetadataSection(MetadataType.Source, searchOnSongSelect),
tags = new MetadataSection(MetadataType.Tags, searchOnSongSelect),
description = new MetadataSectionDescription(searchOnSongSelect),
source = new MetadataSectionSource(searchOnSongSelect),
tags = new MetadataSectionTags(searchOnSongSelect),
},
},
},
@ -187,9 +187,9 @@ namespace osu.Game.Screens.Select
private void updateStatistics()
{
advanced.BeatmapInfo = BeatmapInfo;
description.Text = BeatmapInfo?.DifficultyName;
source.Text = BeatmapInfo?.Metadata.Source;
tags.Text = BeatmapInfo?.Metadata.Tags;
description.Text = BeatmapInfo?.DifficultyName ?? string.Empty;
source.Text = BeatmapInfo?.Metadata.Source ?? string.Empty;
tags.Text = BeatmapInfo?.Metadata.Tags ?? string.Empty;
// failTimes may have been previously fetched
if (ratings != null && failTimes != null)