Skip to content

Commit

Permalink
Merge pull request #1123 from andy840119/should-be-able-to-check-sele…
Browse files Browse the repository at this point in the history
…cted-hitobject-amount

Make sure only select only lyric in some cases in change handler.
  • Loading branch information
andy840119 authored Feb 13, 2022
2 parents 5df2006 + 1f8ac59 commit 4ce7e03
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public abstract class HitObjectChangeHandler<THitObject> : Component where THitO

protected IEnumerable<THitObject> HitObjects => beatmap.HitObjects.OfType<THitObject>();

protected void CheckExactlySelectedOneHitObject()
{
if (beatmap.SelectedHitObjects.OfType<THitObject>().Count() != 1)
throw new InvalidOperationException($"Should be exactly one {nameof(THitObject)} being selected.");
}

protected void PerformOnSelection(Action<THitObject> action) => beatmap.PerformOnSelection(h =>
{
if (h is THitObject tHitObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class LyricTextChangeHandler : HitObjectChangeHandler<Lyric>, ILyricTextC
{
public void InsertText(int index, string text)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
LyricUtils.AddText(lyric, index, text);
Expand All @@ -19,6 +21,8 @@ public void InsertText(int index, string text)

public void DeleteLyricText(int index)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
LyricUtils.RemoveText(lyric, index - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers.Lyrics
{
public void Add(TTextTag textTag)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
bool containsInLyric = ContainsInLyric(lyric, textTag);
Expand All @@ -26,6 +28,8 @@ public void Add(TTextTag textTag)

public void Remove(TTextTag textTag)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
bool containsInLyric = ContainsInLyric(lyric, textTag);
Expand All @@ -38,6 +42,8 @@ public void Remove(TTextTag textTag)

public void RemoveAll(IEnumerable<TTextTag> textTags)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
// should convert to array because enumerable might change while deleting.
Expand All @@ -54,6 +60,8 @@ public void RemoveAll(IEnumerable<TTextTag> textTags)

public void SetIndex(TTextTag textTag, int? startIndex, int? endIndex)
{
CheckExactlySelectedOneHitObject();

// note: it's ok not sort the text tag by index.
PerformOnSelection(lyric =>
{
Expand All @@ -71,6 +79,8 @@ public void SetIndex(TTextTag textTag, int? startIndex, int? endIndex)

public void ShiftingIndex(IEnumerable<TTextTag> textTags, int offset)
{
CheckExactlySelectedOneHitObject();

// note: it's ok not sort the text tag by index.
PerformOnSelection(lyric =>
{
Expand All @@ -89,6 +99,8 @@ public void ShiftingIndex(IEnumerable<TTextTag> textTags, int offset)

public void SetText(TTextTag textTag, string text)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
bool containsInLyric = ContainsInLyric(lyric, textTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public void AutoGenerate()

public void SetTimeTagTime(TimeTag timeTag, double time)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
bool containsInLyric = lyric.TimeTags?.Contains(timeTag) ?? false;
Expand All @@ -44,6 +46,8 @@ public void SetTimeTagTime(TimeTag timeTag, double time)

public void ClearTimeTagTime(TimeTag timeTag)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
bool containsInLyric = lyric.TimeTags?.Contains(timeTag) ?? false;
Expand All @@ -56,6 +60,8 @@ public void ClearTimeTagTime(TimeTag timeTag)

public void Add(TimeTag timeTag)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
bool containsInLyric = lyric.TimeTags.Contains(timeTag);
Expand All @@ -68,6 +74,8 @@ public void Add(TimeTag timeTag)

public void Remove(TimeTag timeTag)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
// delete time tag from list
Expand All @@ -77,6 +85,8 @@ public void Remove(TimeTag timeTag)

public void AddByPosition(TextIndex index)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
lyric.TimeTags.Add(new TimeTag(index));
Expand All @@ -85,6 +95,8 @@ public void AddByPosition(TextIndex index)

public void RemoveByPosition(TextIndex index)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
var matchedTimeTags = lyric.TimeTags.Where(x => x.Index == index).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class LyricTranslateChangeHandler : HitObjectChangeHandler<Lyric>, ILyric
{
public void UpdateTranslate(CultureInfo cultureInfo, string translate)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
// should not save translate if is null or empty or whitespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class LyricsChangeHandler : HitObjectChangeHandler<Lyric>, ILyricsChangeH
{
public void Split(int index)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
// Shifting order that order is larger than current lyric.
Expand All @@ -35,6 +37,8 @@ public void Split(int index)

public void Combine()
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
var previousLyric = HitObjects.GetPrevious(lyric);
Expand All @@ -57,6 +61,8 @@ public void Combine()

public void CreateAtPosition()
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(lyric =>
{
int order = lyric.Order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public void AutoGenerate()

public void Split(float percentage = 0.5f)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(note =>
{
var (firstNote, secondNote) = NotesUtils.SplitNote(note);
Expand Down Expand Up @@ -70,6 +72,8 @@ public void Combine()

public void ChangeText(string text)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(note =>
{
note.Text = text;
Expand All @@ -78,6 +82,8 @@ public void ChangeText(string text)

public void ChangeRubyText(string ruby)
{
CheckExactlySelectedOneHitObject();

PerformOnSelection(note =>
{
note.RubyText = ruby;
Expand Down

0 comments on commit 4ce7e03

Please sign in to comment.