From 0e89bba2f977533eab68bb5ba55ae077ecc6fbeb Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 13 Dec 2020 23:07:50 +0900 Subject: [PATCH] Nove split note into utils. --- .../Utils/NoteUtilsTest.cs | 9 +++ .../Formats/KaraokeLegacyBeatmapDecoder.cs | 2 +- .../Notes/NoteSelectionBlueprint.cs | 10 ++-- osu.Game.Rulesets.Karaoke/Objects/Note.cs | 30 ---------- osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs | 59 +++++++++++++++++++ 5 files changed, 75 insertions(+), 35 deletions(-) create mode 100644 osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs create mode 100644 osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs b/osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs new file mode 100644 index 000000000..4c5179e1e --- /dev/null +++ b/osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs @@ -0,0 +1,9 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +namespace osu.Game.Rulesets.Karaoke.Tests.Utils +{ + public class NoteUtilsTest + { + } +} diff --git a/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs b/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs index 5f291123a..8d6734bae 100644 --- a/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs +++ b/osu.Game.Rulesets.Karaoke/Beatmaps/Formats/KaraokeLegacyBeatmapDecoder.cs @@ -168,7 +168,7 @@ private void processNotes(Beatmap beatmap, IList lines) } // Split note and apply them - var splitDefaultNote = defaultNote.CopyByPercentage(startPercentage, percentage); + var splitDefaultNote = NoteUtils.SplitNote(defaultNote, startPercentage, percentage); startPercentage += percentage; if (!string.IsNullOrEmpty(ruby)) splitDefaultNote.Text = ruby; diff --git a/osu.Game.Rulesets.Karaoke/Edit/Blueprints/Notes/NoteSelectionBlueprint.cs b/osu.Game.Rulesets.Karaoke/Edit/Blueprints/Notes/NoteSelectionBlueprint.cs index df617991b..76265199d 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Blueprints/Notes/NoteSelectionBlueprint.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Blueprints/Notes/NoteSelectionBlueprint.cs @@ -1,6 +1,7 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. +using System; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.UserInterface; @@ -8,6 +9,7 @@ using osu.Game.Rulesets.Karaoke.Edit.Blueprints.Notes.Components; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Karaoke.Objects.Drawables; +using osu.Game.Rulesets.Karaoke.Utils; using osu.Game.Screens.Edit; using osuTK; @@ -32,10 +34,10 @@ public class NoteSelectionBlueprint : KaraokeSelectionBlueprint private void splitNote() { // TODO : percentage should be enter by dialog - var splittedNote = HitObject.CopyByPercentage(0.5); - EditorBeatmap?.Add(splittedNote); - // Change object's duration - HitObject.Duration = HitObject.Duration - splittedNote.Duration; + var (firstNote, secondNote) = NoteUtils.SplitNote(HitObject, 0.5); + EditorBeatmap?.Add(firstNote); + EditorBeatmap?.Add(secondNote); + EditorBeatmap?.Remove(HitObject); } public void ChangeDisplay(bool display) diff --git a/osu.Game.Rulesets.Karaoke/Objects/Note.cs b/osu.Game.Rulesets.Karaoke/Objects/Note.cs index 6a336a6cb..024ef59b7 100644 --- a/osu.Game.Rulesets.Karaoke/Objects/Note.cs +++ b/osu.Game.Rulesets.Karaoke/Objects/Note.cs @@ -88,36 +88,6 @@ public virtual Tone Tone public Lyric ParentLyric { get; set; } - public Note CopyByPercentage(double startPercentage = 0, double durationPercentage = 0.5) - { - if (startPercentage < 0 || startPercentage + durationPercentage > 1) - throw new ArgumentOutOfRangeException($"{nameof(Note)} cannot assign split range of start from {startPercentage} and duration {durationPercentage}"); - - var startTime = StartTime + Duration * startPercentage; - var duration = Duration * durationPercentage; - - return CopyByTime(startTime, duration); - } - - public Note CopyByTime(double startTime, double duration) - { - if (startTime < StartTime || startTime + duration > EndTime) - throw new ArgumentOutOfRangeException($"{nameof(Note)} cannot assign split range of start from {startTime} and duration {duration}"); - - return new Note - { - StartTime = startTime, - Duration = duration, - StartIndex = StartIndex, - EndIndex = EndIndex, - Text = Text, - Singers = Singers, - Display = Display, - Tone = Tone, - ParentLyric = ParentLyric - }; - } - public override Judgement CreateJudgement() => new KaraokeNoteJudgement(); } } diff --git a/osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs b/osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs new file mode 100644 index 000000000..ecf6e5a6f --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs @@ -0,0 +1,59 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Game.Rulesets.Karaoke.Objects; +using System; + +namespace osu.Game.Rulesets.Karaoke.Utils +{ + public static class NoteUtils + { + public static Note SplitNote(Note note, double startPercentage, double durationPercentage) + { + if (startPercentage < 0 || startPercentage + durationPercentage > 1) + throw new ArgumentOutOfRangeException($"{nameof(Note)} cannot assign split range of start from {startPercentage} and duration {durationPercentage}"); + + var startTime = note.StartTime + note.Duration * startPercentage; + var duration = note.Duration * durationPercentage; + + return copyByTime(note, startTime, duration); + } + public static Tuple SplitNote(Note note, double percentage = 0.5) + { + if (percentage <= 0 || percentage >= 1) + throw new ArgumentOutOfRangeException(nameof(Note)); + + var firstNoteStartTime = note.StartTime; + var firstNoteDuration = note.Duration * percentage; + + var secondNoteStartTime = firstNoteStartTime + firstNoteDuration; + var secondNoteDuration = note.Duration * (1 - percentage); + + var firstNote = copyByTime(note, firstNoteStartTime, firstNoteDuration); + var secondNote = copyByTime(note, secondNoteStartTime, secondNoteDuration); + + return new Tuple(firstNote, secondNote); + } + + private static Note copyByTime(Note oritinNote, double startTime, double duration) + { + return new Note + { + StartTime = startTime, + Duration = duration, + StartIndex = oritinNote.StartIndex, + EndIndex = oritinNote.EndIndex, + Text = oritinNote.Text, + Singers = oritinNote.Singers, + Display = oritinNote.Display, + Tone = oritinNote.Tone, + ParentLyric = oritinNote.ParentLyric + }; + } + + public static Note CombineNote(Note firstLyric, Note secondLyric) + { + return null; + } + } +}