Skip to content

Commit

Permalink
Merge pull request #2154 from andy840119/refactor-issue-icon
Browse files Browse the repository at this point in the history
Refactor issue icon.
  • Loading branch information
andy840119 authored Dec 17, 2023
2 parents 547eb67 + 982f209 commit affd087
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
48 changes: 46 additions & 2 deletions osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseCheckHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Rulesets.Karaoke.Beatmaps.Metadatas;
using osu.Game.Rulesets.Karaoke.Edit;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Stages.Classic;
using osu.Game.Rulesets.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Helper;

Expand All @@ -22,8 +28,46 @@ public static IEnumerable<ICheck> GetAllAvailableChecks()
return (List<ICheck>)field.GetValue(beatmapVerifier)!;
}

public static IEnumerable<IssueTemplate> GetAllAvailableIssueTemplates()
public static IReadOnlyDictionary<ICheck, IEnumerable<IssueTemplate>> GetAllAvailableIssueTemplates()
{
return GetAllAvailableChecks().SelectMany(x => x.PossibleTemplates);
return GetAllAvailableChecks().ToDictionary(k => k, v => v.PossibleTemplates);
}

public static IReadOnlyDictionary<ICheck, Issue[]> CreateAllAvailableIssues()
{
return GetAllAvailableIssueTemplates().ToDictionary(k => k.Key,
v => v.Value.Select(createIssueByIssueTemplate).ToArray());

Issue createIssueByIssueTemplate(IssueTemplate issueTemplate)
{
var method = issueTemplate.GetType().GetMethod("Create");
if (method == null)
throw new MissingMethodException("Test method is not exist.");

object[] paramsInfo = method.GetParameters().Select(generateDefaultTypeByParameterInfo).ToArray();
if (method.Invoke(issueTemplate, paramsInfo) is not Issue issue)
throw new InvalidOperationException("Issue should not be null.");

return issue;
}

object generateDefaultTypeByParameterInfo(ParameterInfo parameterInfos) =>
parameterInfos.ParameterType switch
{
// Metadata in the beatmap.
Type t when ReferenceEquals(t, typeof(Page)) => new Page(),
Type t when ReferenceEquals(t, typeof(ClassicLyricTimingPoint)) => new ClassicLyricTimingPoint(),
Type t when ReferenceEquals(t, typeof(IEnumerable<HitObject>)) => new List<HitObject>(),
// Hit-object.
Type t when ReferenceEquals(t, typeof(Lyric)) => new Lyric(),
Type t when ReferenceEquals(t, typeof(Note)) => new Note(),
Type t when ReferenceEquals(t, typeof(RubyTag)) => new RubyTag(),
Type t when ReferenceEquals(t, typeof(TimeTag)) => new TimeTag(new TextIndex(), 0),
Type t when ReferenceEquals(t, typeof(RomajiTag)) => new RomajiTag(),
// Other type.
Type t when ReferenceEquals(t, typeof(int)) => 0,
Type t when ReferenceEquals(t, typeof(CultureInfo)) => new CultureInfo("Ja-jp"),
_ => throw new InvalidOperationException(),
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Components.Issues;
using osu.Game.Rulesets.Karaoke.Tests.Helper;
using osu.Game.Tests.Visual;
Expand All @@ -14,12 +13,12 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Screens.Edit.Components.Issues;
[TestFixture]
public partial class TestSceneIssueIcon : OsuTestScene
{
private SpriteIcon icon = null!;
private IssueIcon icon = null!;

[SetUp]
public void SetUp() => Schedule(() =>
{
Child = icon = new SpriteIcon
Child = icon = new IssueIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Expand All @@ -28,19 +27,19 @@ public void SetUp() => Schedule(() =>
});

[Test]
public void DisplayIconByIssueTemplate()
public void DisplayIconByIssues()
{
var availableChecks = TestCaseCheckHelper.GetAllAvailableChecks();
var availableIssues = TestCaseCheckHelper.CreateAllAvailableIssues();

foreach (var check in availableChecks)
foreach (var (check, issues) in availableIssues)
{
AddLabel($"Check: {check.Metadata.Description}");

foreach (var template in check.PossibleTemplates)
foreach (var issue in issues)
{
AddStep($"Test lyric with template {template.UnformattedMessage}", () =>
AddStep($"Test lyric with template {issue.Template.UnformattedMessage}", () =>
{
icon.Icon = IssueIcon.GetIconByIssueTemplate(template);
icon.Issue = issue;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,41 @@ public virtual Issue? Issue

private void updateIssue()
{
InternalChild = Issue != null ? getDrawableByIssue(Issue) : null;
InternalChild = Issue != null ? createDrawableByIssue(Issue) : null;
}

private static Drawable getDrawableByIssue(Issue issue)
private static Drawable createDrawableByIssue(Issue issue)
{
return createIssueIcon(issue).With(x =>
return createDrawable(issue).With(x =>
{
x.Colour = issue.Template.Colour;
x.RelativeSizeAxes = Axes.Both;
});

static Drawable createIssueIcon(Issue issue)
{
var drawableByIssue = GetDrawableByIssue(issue);
if (drawableByIssue != null)
return drawableByIssue;

return new SpriteIcon
static Drawable createDrawable(Issue issue) =>
issue switch
{
Icon = GetIconByIssueTemplate(issue.Template),
LyricTimeTagIssue lyricTimeTagIssue => new DrawableTextIndex { State = lyricTimeTagIssue.TimeTag.Index.State },
_ => new SpriteIcon
{
Icon = getIconByIssue(issue),
},
};
}
}

internal static Drawable? GetDrawableByIssue(Issue issue) =>
issue switch
{
LyricTimeTagIssue lyricTimeTagIssue => new DrawableTextIndex { State = lyricTimeTagIssue.TimeTag.Index.State },
_ => null,
};
private static IconUsage getIconByIssue(Issue issue)
=> getIconByIssueTemplate(issue.Template);

internal static IconUsage GetIconByIssueTemplate(IssueTemplate issueTemplate)
=> GetIconUsageByIssueTemplate(issueTemplate) ?? GetIconUsageByCheck(issueTemplate.Check);
private static IconUsage getIconByIssueTemplate(IssueTemplate issueTemplate)
=> getIconUsageByIssueTemplate(issueTemplate) ?? getIconUsageByCheck(issueTemplate.Check);

internal static IconUsage? GetIconUsageByIssueTemplate(IssueTemplate issueTemplate)
private static IconUsage? getIconUsageByIssueTemplate(IssueTemplate issueTemplate)
{
// will override the icon if needed.
return null;
}

internal static IconUsage GetIconUsageByCheck(ICheck check) =>
private static IconUsage getIconUsageByCheck(ICheck check) =>
check switch
{
CheckBeatmapAvailableTranslates => FontAwesome.Solid.Language,
Expand Down

0 comments on commit affd087

Please sign in to comment.