Skip to content

Commit

Permalink
Merge pull request #1538 from andy840119/implement-sync-reference-lyr…
Browse files Browse the repository at this point in the history
…ic-properties

Implement sync reference lyric properties.
  • Loading branch information
andy840119 authored Aug 29, 2022
2 parents ea5a554 + a61e99d commit 74e315e
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Editor.ChangeHandlers.Lyrics
public abstract class LyricPropertyChangeHandlerTest<TChangeHandler> : BaseHitObjectChangeHandlerTest<TChangeHandler, Lyric>
where TChangeHandler : LyricPropertyChangeHandler, new()
{
protected void PrepareLyricWithSyncConfig(Lyric hitObject, bool selected = true)
protected Lyric PrepareLyricWithSyncConfig(Lyric referencedLyric, IReferenceLyricPropertyConfig? config = null, bool selected = true)
{
// allow to pre-assign the config.
hitObject.ReferenceLyric = new Lyric();
hitObject.ReferenceLyricConfig ??= new SyncLyricConfig();
var lyric = new Lyric
{
ReferenceLyric = referencedLyric,
ReferenceLyricConfig = config ?? new SyncLyricConfig()
};

PrepareHitObjects(new[] { hitObject }, selected);
PrepareHitObjects(new[] { lyric }, selected);

return lyric;
}

protected void TriggerHandlerChangedWithChangeForbiddenException(Action<TChangeHandler> c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,9 @@ public void TestWithReferenceLyric(bool syncSinger)
{
Name = "Singer1",
};
PrepareLyricWithSyncConfig(new Lyric
PrepareLyricWithSyncConfig(new Lyric(), new SyncLyricConfig
{
ReferenceLyricConfig = new SyncLyricConfig
{
SyncSingerProperty = syncSinger
}
SyncSingerProperty = syncSinger
});

if (syncSinger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,29 @@ public void TestShiftingException(TextIndex.IndexState state, ShiftingDirection
[TestCase(false)]
public void TestWithReferenceLyric(bool syncTimeTag)
{
var timeTag = new TimeTag(new TextIndex(), 1000);
PrepareLyricWithSyncConfig(new Lyric
var lyric = PrepareLyricWithSyncConfig(new Lyric
{
Text = "カラオケ",
TimeTags = new[]
{
timeTag
},
ReferenceLyricConfig = new SyncLyricConfig
{
SyncTimeTagProperty = syncTimeTag
new TimeTag(new TextIndex(), 1000)
}
}, new SyncLyricConfig
{
SyncTimeTagProperty = syncTimeTag
});

// should add the time-tag by hand because it does not sync from thr referenced lyric.
if (!syncTimeTag)
{
lyric.TimeTags = new[]
{
new TimeTag(new TextIndex(), 2000)
};
}

var timeTag = lyric.TimeTags.First();

if (syncTimeTag)
{
TriggerHandlerChangedWithChangeForbiddenException(c => c.SetTimeTagTime(timeTag, 2000));
Expand Down
170 changes: 170 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Objects/LyricTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Objects
{
public class LyricTest
{
#region Clone

[TestCase]
public void TestClone()
{
Expand Down Expand Up @@ -89,5 +91,173 @@ public void TestClone()
Assert.AreNotSame(clonedLyric.ReferenceLyricConfig, lyric.ReferenceLyricConfig);
Assert.AreEqual(clonedLyric.ReferenceLyricConfig?.OffsetTime, lyric.ReferenceLyricConfig?.OffsetTime);
}

#endregion

#region Reference lyric

[Test]
public void TestSyncFromReferenceLyric()
{
var referencedLyric = new Lyric
{
Text = "karaoke",
TimeTags = TestCaseTagHelper.ParseTimeTags(new[] { "[0,start]:1100" }),
RubyTags = TestCaseTagHelper.ParseRubyTags(new[] { "[0,1]:か" }),
RomajiTags = TestCaseTagHelper.ParseRomajiTags(new[] { "[0,1]:ka" }),
Singers = new[] { 1 },
Translates = new Dictionary<CultureInfo, string>
{
{ new CultureInfo(17), "からおけ" }
},
Language = new CultureInfo(17)
};

var lyric = new Lyric
{
ReferenceLyric = referencedLyric,
ReferenceLyricConfig = new SyncLyricConfig(),
};

Assert.AreEqual(referencedLyric.Text, lyric.Text);
TimeTagAssert.ArePropertyEqual(referencedLyric.TimeTags, lyric.TimeTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RubyTags, lyric.RubyTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RomajiTags, lyric.RomajiTags);
Assert.AreEqual(referencedLyric.Singers, lyric.Singers);
Assert.AreEqual(referencedLyric.Translates, lyric.Translates);
Assert.AreEqual(referencedLyric.Language, lyric.Language);
}

[Test]
public void TestReferenceLyricPropertyChanged()
{
var referencedLyric = new Lyric();

var lyric = new Lyric
{
ReferenceLyric = referencedLyric,
ReferenceLyricConfig = new SyncLyricConfig(),
};

referencedLyric.Text = "karaoke";
referencedLyric.TimeTags = TestCaseTagHelper.ParseTimeTags(new[] { "[0,start]:1100" });
referencedLyric.RubyTags = TestCaseTagHelper.ParseRubyTags(new[] { "[0,1]:か" });
referencedLyric.RomajiTags = TestCaseTagHelper.ParseRomajiTags(new[] { "[0,1]:ka" });
referencedLyric.Singers = new[] { 1 };
referencedLyric.Translates = new Dictionary<CultureInfo, string>
{
{ new CultureInfo(17), "からおけ" }
};
referencedLyric.Language = new CultureInfo(17);

Assert.AreEqual(referencedLyric.Text, lyric.Text);
TimeTagAssert.ArePropertyEqual(referencedLyric.TimeTags, lyric.TimeTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RubyTags, lyric.RubyTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RomajiTags, lyric.RomajiTags);
Assert.AreEqual(referencedLyric.Singers, lyric.Singers);
Assert.AreEqual(referencedLyric.Translates, lyric.Translates);
Assert.AreEqual(referencedLyric.Language, lyric.Language);
}

[Test]
public void TestReferenceLyricListPropertyChanged()
{
// test modify property inside the list.
// ruby, romaji tag time-tag.
var timeTag = TestCaseTagHelper.ParseTimeTag("[0,start]:1100");
var rubyTag = TestCaseTagHelper.ParseRubyTag("[0,1]:か");
var romajiTag = TestCaseTagHelper.ParseRomajiTag("[0,1]:ka");

var referencedLyric = new Lyric
{
Text = "karaoke",
TimeTags = new[] { timeTag },
RubyTags = new[] { rubyTag },
RomajiTags = new[] { romajiTag },
Singers = new[] { 1 },
Translates = new Dictionary<CultureInfo, string>
{
{ new CultureInfo(17), "からおけ" }
},
Language = new CultureInfo(17)
};

var lyric = new Lyric
{
ReferenceLyric = referencedLyric,
ReferenceLyricConfig = new SyncLyricConfig(),
};

// property should be the same
TimeTagAssert.ArePropertyEqual(referencedLyric.TimeTags, lyric.TimeTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RubyTags, lyric.RubyTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RomajiTags, lyric.RomajiTags);

// and because there's no change inside the tag, so there's version change.
Assert.AreEqual(0, lyric.TimeTagsVersion.Value);
Assert.AreEqual(0, lyric.RubyTagsVersion.Value);
Assert.AreEqual(0, lyric.RomajiTagsVersion.Value);

// it's time to change the property in the list.
timeTag.Time = 2000;
rubyTag.Text = "ruby";
romajiTag.Text = "romaji";

// property should be equal.
TimeTagAssert.ArePropertyEqual(referencedLyric.TimeTags, lyric.TimeTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RubyTags, lyric.RubyTags);
TextTagAssert.ArePropertyEqual(referencedLyric.RomajiTags, lyric.RomajiTags);

// and note that because only one property is different, so version should change once.
Assert.AreEqual(1, lyric.TimeTagsVersion.Value);
Assert.AreEqual(1, lyric.RubyTagsVersion.Value);
Assert.AreEqual(1, lyric.RomajiTagsVersion.Value);
}

[Test]
public void TestConfigChange()
{
// change the config from reference lyric into sync lyric config.
// than, should auto sync the value.
var config = new SyncLyricConfig
{
SyncSingerProperty = false,
SyncTimeTagProperty = false,
};

var referencedLyric = new Lyric
{
Text = "karaoke",
TimeTags = TestCaseTagHelper.ParseTimeTags(new[] { "[0,start]:1100" }),
RubyTags = TestCaseTagHelper.ParseRubyTags(new[] { "[0,1]:か" }),
RomajiTags = TestCaseTagHelper.ParseRomajiTags(new[] { "[0,1]:ka" }),
Singers = new[] { 1 },
Translates = new Dictionary<CultureInfo, string>
{
{ new CultureInfo(17), "からおけ" }
},
Language = new CultureInfo(17)
};

var lyric = new Lyric
{
ReferenceLyric = referencedLyric,
ReferenceLyricConfig = config
};

// the property should not same as the reference reference because those properties are not sync.
Assert.IsEmpty(lyric.TimeTags);
Assert.AreNotEqual(referencedLyric.Singers, lyric.Singers);

// it's time to open the config.
config.SyncSingerProperty = true;
config.SyncTimeTagProperty = true;

// after open the config, the property should sync from the reference lyric now.
TimeTagAssert.ArePropertyEqual(referencedLyric.TimeTags, lyric.TimeTags);
Assert.AreEqual(referencedLyric.Singers, lyric.Singers);
}

#endregion
}
}
1 change: 1 addition & 0 deletions osu.Game.Rulesets.Karaoke/Objects/Lyric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public IReferenceLyricPropertyConfig? ReferenceLyricConfig
public Lyric()
{
initInternalBindingEvent();
initReferenceLyricEvent();
}

public override Judgement CreateJudgement() => new KaraokeLyricJudgement();
Expand Down
Loading

0 comments on commit 74e315e

Please sign in to comment.