Skip to content

Commit

Permalink
Nove split note into utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Dec 13, 2020
1 parent edbb0de commit 0e89bba
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 35 deletions.
9 changes: 9 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Utils/NoteUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) andy840119 <[email protected]>. 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
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void processNotes(Beatmap beatmap, IList<string> 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) andy840119 <[email protected]>. 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;
using osu.Game.Graphics.UserInterface;
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;

Expand All @@ -32,10 +34,10 @@ public class NoteSelectionBlueprint : KaraokeSelectionBlueprint<Note>
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)
Expand Down
30 changes: 0 additions & 30 deletions osu.Game.Rulesets.Karaoke/Objects/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
59 changes: 59 additions & 0 deletions osu.Game.Rulesets.Karaoke/Utils/NoteUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) andy840119 <[email protected]>. 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<Note, Note> 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<Note, Note>(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;
}
}
}

0 comments on commit 0e89bba

Please sign in to comment.