Skip to content

Commit

Permalink
Merge pull request #2135 from andy840119/adjust-time-tag-romaji-chang…
Browse files Browse the repository at this point in the history
…e-handler

Add time-tag romaji generator into generator change handler.
  • Loading branch information
andy840119 authored Sep 27, 2023
2 parents 5c96bef + 825847a commit 2085cab
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void TestAutoGenerateRomajiTagsWithNonSupportedLyric()

#endregion

#region TimeTag
#region Time-tag

[Test]
public void TestAutoGenerateTimeTags()
Expand Down Expand Up @@ -241,6 +241,44 @@ public void TestAutoGenerateTimeTagsWithNonSupportedLyric()

#endregion

#region Time-tag romaji

[Test]
public void TestAutoGenerateTimeTagRomaji()
{
PrepareHitObject(() => new Lyric
{
Text = "カラオケ",
Language = new CultureInfo(17),
TimeTags = TestCaseTagHelper.ParseTimeTags(new[] { "[0,start]", "[3,end]" }),
});

TriggerHandlerChanged(c => c.AutoGenerate(AutoGenerateType.AutoGenerateTimeTagRomaji));

AssertSelectedHitObject(h =>
{
Assert.AreEqual("karaoke", h.TimeTags[0].RomajiText);
});
}

[Test]
public void TestAutoGenerateTimeTagRomajiWithNonSupportedLyric()
{
PrepareHitObjects(() => new[]
{
new Lyric
{
Text = "カラオケ",
Language = new CultureInfo(17),
// with no time-tag.
},
});

TriggerHandlerChangedWithException<GeneratorNotSupportedException>(c => c.AutoGenerate(AutoGenerateType.AutoGenerateTimeTagRomaji));
}

#endregion

#region Note

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers;
/// <typeparam name="TEnum"></typeparam>
public interface IEnumAutoGenerateChangeHandler<in TEnum> where TEnum : Enum
{
bool CanGenerate(TEnum property);
bool CanGenerate(TEnum type);

void AutoGenerate(TEnum property);
void AutoGenerate(TEnum type);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ public enum AutoGenerateType

AutoGenerateTimeTags,

AutoGenerateTimeTagRomaji,

AutoGenerateNotes,
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Language;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Notes;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.ReferenceLyric;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.Romajies;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.RomajiTags;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.RubyTags;
using osu.Game.Rulesets.Karaoke.Edit.Generator.Lyrics.TimeTags;
Expand All @@ -27,16 +28,16 @@ public partial class LyricPropertyAutoGenerateChangeHandler : LyricPropertyChang
{
// should change this flag if wants to change property in the lyrics.
// Not a good to waite a global property for that but there's no better choice.
private AutoGenerateType? currentAutoGenerateProperty;
private AutoGenerateType? currentAutoGenerateType;

[Resolved]
private EditorBeatmap beatmap { get; set; } = null!;

public bool CanGenerate(AutoGenerateType autoGenerateProperty)
public bool CanGenerate(AutoGenerateType type)
{
currentAutoGenerateProperty = autoGenerateProperty;
currentAutoGenerateType = type;

switch (autoGenerateProperty)
switch (type)
{
case AutoGenerateType.DetectReferenceLyric:
var referenceLyricDetector = getDetector<Lyric?, ReferenceLyricDetectorConfig>(HitObjects);
Expand All @@ -58,12 +59,16 @@ public bool CanGenerate(AutoGenerateType autoGenerateProperty)
var timeTagGenerator = getSelector<TimeTag[], TimeTagGeneratorConfig>();
return canGenerate(timeTagGenerator);

case AutoGenerateType.AutoGenerateTimeTagRomaji:
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>();
return canGenerate(timeTagRomajiGenerator);

case AutoGenerateType.AutoGenerateNotes:
var noteGenerator = getGenerator<Note[], NoteGeneratorConfig>();
return canGenerate(noteGenerator);

default:
throw new ArgumentOutOfRangeException(nameof(autoGenerateProperty));
throw new ArgumentOutOfRangeException(nameof(type));
}

bool canDetect<T>(PropertyDetector<Lyric, T> detector)
Expand All @@ -73,11 +78,11 @@ bool canGenerate<T>(PropertyGenerator<Lyric, T> generator)
=> HitObjects.Where(x => !IsWritePropertyLocked(x)).Any(generator.CanGenerate);
}

public IDictionary<Lyric, LocalisableString> GetGeneratorNotSupportedLyrics(AutoGenerateType autoGenerateProperty)
public IDictionary<Lyric, LocalisableString> GetGeneratorNotSupportedLyrics(AutoGenerateType type)
{
currentAutoGenerateProperty = autoGenerateProperty;
currentAutoGenerateType = type;

switch (autoGenerateProperty)
switch (type)
{
case AutoGenerateType.DetectReferenceLyric:
var referenceLyricDetector = getDetector<Lyric?, ReferenceLyricDetectorConfig>(HitObjects);
Expand All @@ -99,12 +104,16 @@ public IDictionary<Lyric, LocalisableString> GetGeneratorNotSupportedLyrics(Auto
var timeTagGenerator = getSelector<TimeTag[], TimeTagGeneratorConfig>();
return getInvalidMessageFromGenerator(timeTagGenerator);

case AutoGenerateType.AutoGenerateTimeTagRomaji:
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>();
return getInvalidMessageFromGenerator(timeTagRomajiGenerator);

case AutoGenerateType.AutoGenerateNotes:
var noteGenerator = getGenerator<Note[], NoteGeneratorConfig>();
return getInvalidMessageFromGenerator(noteGenerator);

default:
throw new ArgumentOutOfRangeException(nameof(autoGenerateProperty));
throw new ArgumentOutOfRangeException(nameof(type));
}

IDictionary<Lyric, LocalisableString> getInvalidMessageFromDetector<T>(PropertyDetector<Lyric, T> detector)
Expand All @@ -124,11 +133,11 @@ IDictionary<Lyric, LocalisableString> getInvalidMessageFromGenerator<T>(Property
}
}

public void AutoGenerate(AutoGenerateType autoGenerateProperty)
public void AutoGenerate(AutoGenerateType type)
{
currentAutoGenerateProperty = autoGenerateProperty;
currentAutoGenerateType = type;

switch (autoGenerateProperty)
switch (type)
{
case AutoGenerateType.DetectReferenceLyric:
var referenceLyricDetector = getDetector<Lyric?, ReferenceLyricDetectorConfig>(HitObjects);
Expand Down Expand Up @@ -178,6 +187,21 @@ public void AutoGenerate(AutoGenerateType autoGenerateProperty)
});
break;

case AutoGenerateType.AutoGenerateTimeTagRomaji:
var timeTagRomajiGenerator = getSelector<IReadOnlyDictionary<TimeTag, RomajiGenerateResult>, RomajiGeneratorConfig>();
PerformOnSelection(lyric =>
{
var results = timeTagRomajiGenerator.Generate(lyric);

foreach (var (key, value) in results)
{
var matchedTimeTag = lyric.TimeTags.Single(x => x == key);
matchedTimeTag.InitialRomaji = value.InitialRomaji;
matchedTimeTag.RomajiText = value.RomajiText;
}
});
break;

case AutoGenerateType.AutoGenerateNotes:
var noteGenerator = getGenerator<Note[], NoteGeneratorConfig>();
PerformOnSelection(lyric =>
Expand All @@ -192,21 +216,22 @@ public void AutoGenerate(AutoGenerateType autoGenerateProperty)
break;

default:
throw new ArgumentOutOfRangeException(nameof(autoGenerateProperty));
throw new ArgumentOutOfRangeException(nameof(type));
}
}

public override bool IsSelectionsLocked()
=> throw new InvalidOperationException("Auto-generator does not support this check method.");

protected override bool IsWritePropertyLocked(Lyric lyric) =>
currentAutoGenerateProperty switch
currentAutoGenerateType switch
{
AutoGenerateType.DetectReferenceLyric => HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.ReferenceLyric), nameof(Lyric.ReferenceLyricConfig)),
AutoGenerateType.DetectLanguage => HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.Language)),
AutoGenerateType.AutoGenerateRubyTags => HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.RubyTags)),
AutoGenerateType.AutoGenerateRomajiTags => HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.RomajiTags)),
AutoGenerateType.AutoGenerateTimeTags => HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.TimeTags)),
AutoGenerateType.AutoGenerateTimeTagRomaji => HitObjectWritableUtils.IsWriteLyricPropertyLocked(lyric, nameof(Lyric.TimeTags)),
AutoGenerateType.AutoGenerateNotes => HitObjectWritableUtils.IsCreateOrRemoveNoteLocked(lyric),
_ => throw new ArgumentOutOfRangeException(),
};
Expand Down

0 comments on commit 2085cab

Please sign in to comment.