Skip to content

Commit

Permalink
Merge pull request #1500 from andy840119/implement-change-the-time-by…
Browse files Browse the repository at this point in the history
…-reference-lyric
  • Loading branch information
andy840119 authored Aug 6, 2022
2 parents 302d06c + 743c8a3 commit 3e316e0
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers.Notes;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Helper;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.ChangeHandlers.Notes
{
Expand All @@ -16,8 +19,7 @@ public void TestSplit()
PrepareHitObject(new Note
{
Text = "カラオケ",
StartTime = 1000,
Duration = 1000,
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("カラオケ", 1000, 1000)
});

TriggerHandlerChanged(c => c.Split());
Expand All @@ -30,6 +32,8 @@ public void TestSplit()
var firstNote = actualNotes[0];
var secondNote = actualNotes[1];

Assert.AreSame(firstNote.ReferenceLyric, secondNote.ReferenceLyric);

Assert.AreEqual("カラオケ", firstNote.Text);
Assert.AreEqual(1000, firstNote.StartTime);
Assert.AreEqual(500, firstNote.Duration);
Expand All @@ -43,7 +47,7 @@ public void TestSplit()
[Test]
public void TestCombine()
{
var lyric = new Lyric();
var lyric = TestCaseNoteHelper.CreateLyricForNote("カラオケ", 1000, 1000);

// note that lyric and notes should in the selection.
PrepareHitObject(lyric);
Expand All @@ -53,17 +57,15 @@ public void TestCombine()
{
Text = "カラ",
RubyText = "から",
StartTime = 1000,
Duration = 500,
ReferenceLyric = lyric,
ReferenceTimeTagIndex = 0
},
new Note
{
Text = "オケ",
RubyText = "おけ",
StartTime = 1500,
Duration = 500,
ReferenceLyric = lyric,
ReferenceTimeTagIndex = 0
}
});

Expand Down Expand Up @@ -158,16 +160,12 @@ public void TestClear()
{
Text = "カラ",
RubyText = "から",
StartTime = 1000,
Duration = 500,
ReferenceLyric = lyric,
},
new Note
{
Text = "オケ",
RubyText = "おけ",
StartTime = 1500,
Duration = 500,
ReferenceLyric = lyric,
}
}, false);
Expand Down
25 changes: 25 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Helper/TestCaseNoteHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Helper
{
public static class TestCaseNoteHelper
{
public static Lyric CreateLyricForNote(string text, double startTime, double duration)
{
return new Lyric
{
Text = text,
TimeTags = new List<TimeTag>
{
new(new TextIndex(0), startTime),
new(new TextIndex(text.Length - 1, TextIndex.IndexState.End), startTime + duration)
}
};
}
}
}
77 changes: 69 additions & 8 deletions osu.Game.Rulesets.Karaoke.Tests/Objects/NoteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Helper;

namespace osu.Game.Rulesets.Karaoke.Tests.Objects
{
Expand All @@ -11,18 +13,15 @@ public class NoteTest
[TestCase]
public void TestClone()
{
var referenceLyric = new Lyric();

var note = new Note
{
Text = "ノート",
RubyText = "Note",
Display = true,
StartTime = 1000,
Duration = 500,
StartTimeOffset = 100,
EndTimeOffset = -100,
ReferenceLyric = referenceLyric
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("ノート", 1000, 1000),
ReferenceTimeTagIndex = 0,
};

var clonedNote = note.DeepClone();
Expand All @@ -41,13 +40,13 @@ public void TestClone()

// note time will not being copied because the time is based on the time-tag in the lyric.
Assert.AreNotSame(clonedNote.StartTimeBindable, note.StartTimeBindable);
Assert.AreEqual(clonedNote.StartTime, 0);
Assert.AreEqual(clonedNote.StartTime, clonedNote.StartTime);

// note time will not being copied because the time is based on the time-tag in the lyric.
Assert.AreEqual(clonedNote.Duration, 0);
Assert.AreEqual(clonedNote.Duration, clonedNote.Duration);

// note time will not being copied because the time is based on the time-tag in the lyric.
Assert.AreEqual(clonedNote.EndTime, 0);
Assert.AreEqual(clonedNote.EndTime, clonedNote.EndTime);

Assert.AreEqual(clonedNote.StartTimeOffset, note.StartTimeOffset);

Expand All @@ -58,5 +57,67 @@ public void TestClone()
Assert.AreNotSame(clonedNote.ReferenceTimeTagIndexBindable, note.ReferenceTimeTagIndexBindable);
Assert.AreEqual(clonedNote.ReferenceTimeTagIndex, note.ReferenceTimeTagIndex);
}

[TestCase]
public void TestReferenceTime()
{
var note = new Note();

// Should not have the time.
Assert.AreEqual(0, note.StartTime);
Assert.AreEqual(0, note.Duration);
Assert.AreEqual(0, note.EndTime);

const double first_time_tag_time = 1000;
const double second_time_tag_time = 3000;
const double duration = second_time_tag_time - first_time_tag_time;
var lyric = TestCaseNoteHelper.CreateLyricForNote("Lyric", first_time_tag_time, duration);
note.ReferenceLyric = lyric;

// Should have calculated time.
Assert.AreEqual(first_time_tag_time, note.StartTime);
Assert.AreEqual(duration, note.Duration);

const double time_tag_offset_time = 500;
lyric.TimeTags.ForEach(x => x.Time += time_tag_offset_time);

// Should change the time if time-tag time has been changed.
Assert.AreEqual(first_time_tag_time + time_tag_offset_time, note.StartTime);
Assert.AreEqual(duration, note.Duration);

note.ReferenceTimeTagIndex = 1;

// Duration will be zero if there's no next time-tag.
Assert.AreEqual(second_time_tag_time + time_tag_offset_time, note.StartTime);
Assert.AreEqual(0, note.Duration);

note.ReferenceTimeTagIndex = 2;

// Time will be zero if there's no matched time-tag.
Assert.AreEqual(0, note.StartTime);
Assert.AreEqual(0, note.Duration);

const double note_start_offset_time = 500;
const double note_end_offset_time = 500;
note.ReferenceTimeTagIndex = 0;
note.StartTimeOffset = note_start_offset_time;
note.EndTimeOffset = note_end_offset_time;

// start time and end time will apply the offset time.
Assert.AreEqual(first_time_tag_time + time_tag_offset_time + note_start_offset_time, note.StartTime);
Assert.AreEqual(duration + time_tag_offset_time - note_end_offset_time, note.Duration);

note.EndTimeOffset = -100000;

// duration should not be empty.
Assert.AreEqual(first_time_tag_time + time_tag_offset_time + note_start_offset_time, note.StartTime);
Assert.AreEqual(0, note.Duration);

note.ReferenceLyric = null;

// time will be zero if lyric has been removed.
Assert.AreEqual(0, note.StartTime);
Assert.AreEqual(0, note.Duration);
}
}
}
29 changes: 13 additions & 16 deletions osu.Game.Rulesets.Karaoke.Tests/Replays/TestSceneAutoGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using osu.Game.Rulesets.Karaoke.Beatmaps;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Replays;
using osu.Game.Rulesets.Karaoke.Tests.Helper;
using osu.Game.Rulesets.Replays;
using osu.Game.Tests.Visual;

Expand All @@ -23,11 +24,10 @@ public void TestSingleShortNote()
var beatmap = new KaraokeBeatmap();
beatmap.HitObjects.Add(new Note
{
Display = true,
StartTime = 1000,
Duration = 50,
Text = "karaoke!",
Tone = new Tone(0, true)
Display = true,
Tone = new Tone(0, true),
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("karaoke!", 1000, 50)
});

var generated = new KaraokeAutoGenerator(beatmap).Generate();
Expand All @@ -49,11 +49,10 @@ public void TestSingleNoteWithLongTime()
var beatmap = new KaraokeBeatmap();
beatmap.HitObjects.Add(new Note
{
Display = true,
StartTime = 1000,
Duration = 1000,
Text = "karaoke!",
Tone = new Tone(0, true)
Display = true,
Tone = new Tone(0, true),
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("karaoke!", 1000, 1000)
});

var generated = new KaraokeAutoGenerator(beatmap).Generate();
Expand All @@ -75,19 +74,17 @@ public void TestNoteStair()
var beatmap = new KaraokeBeatmap();
beatmap.HitObjects.Add(new Note
{
Display = true,
StartTime = 1000,
Duration = 50,
Text = "kara",
Tone = new Tone(0, true)
Display = true,
Tone = new Tone(0, true),
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("karaoke!", 1000, 50)
});
beatmap.HitObjects.Add(new Note
{
Display = true,
StartTime = 1050,
Duration = 50,
Text = "oke!",
Tone = new Tone(1, true)
Display = true,
Tone = new Tone(1, true),
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("karaoke!", 1050, 50)
});

var generated = new KaraokeAutoGenerator(beatmap).Generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Karaoke.UI.Components;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.UI.Scrolling;
using osuTK.Graphics;

Expand Down Expand Up @@ -45,11 +44,6 @@ private void load()
{
c.Add(CreateHitObject().With(h =>
{
h.HitObject.StartTime = START_TIME;

if (h.HitObject is IHasDuration hasDurationHitObject)
hasDurationHitObject.Duration = DURATION;

h.AccentColour.Value = Color4.Orange;
}));
})
Expand Down
5 changes: 2 additions & 3 deletions osu.Game.Rulesets.Karaoke.Tests/Skinning/TestSceneNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Drawables;
using osu.Game.Rulesets.Karaoke.Tests.Helper;
using osu.Game.Rulesets.Objects.Drawables;

namespace osu.Game.Rulesets.Karaoke.Tests.Skinning
Expand All @@ -29,10 +30,8 @@ protected override DrawableHitObject CreateHitObject()
{
var note = new Note
{
StartTime = 100,
Duration = 800,
Text = "カラオケ",
ReferenceLyric = new Lyric()
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("カラオケ", 100, 800),
};
note.ApplyDefaults(new ControlPointInfo(), new BeatmapDifficulty());

Expand Down
10 changes: 5 additions & 5 deletions osu.Game.Rulesets.Karaoke.Tests/UI/TestSceneNotePlayfield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Drawables;
using osu.Game.Rulesets.Karaoke.Replays;
using osu.Game.Rulesets.Karaoke.Tests.Helper;
using osu.Game.Rulesets.Karaoke.UI;
using osu.Game.Rulesets.Karaoke.UI.Components;
using osu.Game.Rulesets.Karaoke.UI.Position;
Expand Down Expand Up @@ -112,12 +113,11 @@ private void createNote(double increaseTime = 2000, double duration = 1000, int
{
var note = new Note
{
StartTime = Time.Current + increaseTime,
Duration = duration,
Tone = new Tone { Scale = tone },
Text = "Here",
ReferenceLyric = new Lyric(),
Display = true
Display = true,
Tone = new Tone { Scale = tone },
ReferenceLyric = TestCaseNoteHelper.CreateLyricForNote("Here", Time.Current + increaseTime, duration),
ReferenceTimeTagIndex = 0
};
note.ApplyDefaults(new ControlPointInfo(), new BeatmapDifficulty());

Expand Down
24 changes: 16 additions & 8 deletions osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@

using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Tests.Helper;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Tests.Utils
{
public class NoteUtilsTest
{
[TestCase(new double[] { 1000, 3000 }, 0, 1, new double[] { 1000, 3000 })]
[TestCase(new double[] { 1000, 3000 }, 0, 0.5, new double[] { 1000, 1500 })]
[TestCase(new double[] { 1000, 3000 }, 0.5, 0.5, new double[] { 2500, 1500 })]
[TestCase(new double[] { 1000, 3000 }, 0.3, 0.4, new double[] { 1900, 1200 })]
[TestCase(new double[] { 1000, 3000 }, 0.3, 1, null)] // start + duration should not exceed 1
public void TestSliceNoteTime(double[] time, double startPercentage, double durationPercentage, double[]? expected)
[TestCase(0, 1, new double[] { 1000, 3000 })]
[TestCase(0, 0.5, new double[] { 1000, 1500 })]
[TestCase(0.5, 0.5, new double[] { 2500, 1500 })]
[TestCase(0.3, 0.4, new double[] { 1900, 1200 })]
[TestCase(0.3, 1, null)] // start + duration should not exceed 1
public void TestSliceNoteTime(double startPercentage, double durationPercentage, double[]? expected)
{
var lyric = new Lyric
{
TimeTags = TestCaseTagHelper.ParseTimeTags(new[] { "[0,start]:1000", "[1,start]:4000" }),
};

// start time will be 1000, and duration will be 3000.
var note = new Note
{
StartTime = time[0],
Duration = time[1],
ReferenceLyric = lyric,
ReferenceTimeTagIndex = 0
};

if (expected != null)
{
var sliceNote = NoteUtils.SliceNote(note, startPercentage, durationPercentage);

Assert.AreEqual(expected[0], sliceNote.StartTime);
Assert.AreEqual(expected[1], sliceNote.Duration);
}
Expand Down
Loading

0 comments on commit 3e316e0

Please sign in to comment.