Skip to content

Commit

Permalink
Merge pull request #1537 from andy840119/prepare-sync-the-reference-l…
Browse files Browse the repository at this point in the history
…yric

Prepare sync the reference lyric
  • Loading branch information
andy840119 authored Aug 29, 2022
2 parents e4d2f22 + 43fd3b3 commit ea5a554
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 62 deletions.
33 changes: 33 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Objects/RomajiTagTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Objects
{
public class RomajiTagTest
{
[TestCase]
public void TestClone()
{
var romajiTag = new RomajiTag
{
Text = "romaji",
StartIndex = 1,
EndIndex = 2
};

var clonedRomajiTag = romajiTag.DeepClone();

Assert.AreNotSame(clonedRomajiTag.TextBindable, romajiTag.TextBindable);
Assert.AreEqual(clonedRomajiTag.Text, romajiTag.Text);

Assert.AreNotSame(clonedRomajiTag.StartIndexBindable, romajiTag.StartIndexBindable);
Assert.AreEqual(clonedRomajiTag.StartIndex, romajiTag.StartIndex);

Assert.AreNotSame(clonedRomajiTag.EndIndexBindable, romajiTag.EndIndexBindable);
Assert.AreEqual(clonedRomajiTag.EndIndex, romajiTag.EndIndex);
}
}
}
33 changes: 33 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Objects/RubyTagTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using NUnit.Framework;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Objects
{
public class RubyTagTest
{
[TestCase]
public void TestClone()
{
var rubyTag = new RubyTag
{
Text = "ruby",
StartIndex = 1,
EndIndex = 2
};

var clonedRubyTag = rubyTag.DeepClone();

Assert.AreNotSame(clonedRubyTag.TextBindable, rubyTag.TextBindable);
Assert.AreEqual(clonedRubyTag.Text, rubyTag.Text);

Assert.AreNotSame(clonedRubyTag.StartIndexBindable, rubyTag.StartIndexBindable);
Assert.AreEqual(clonedRubyTag.StartIndex, rubyTag.StartIndex);

Assert.AreNotSame(clonedRubyTag.EndIndexBindable, rubyTag.EndIndexBindable);
Assert.AreEqual(clonedRubyTag.EndIndex, rubyTag.EndIndex);
}
}
}
25 changes: 25 additions & 0 deletions osu.Game.Rulesets.Karaoke.Tests/Objects/TimeTagTest.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 NUnit.Framework;
using osu.Framework.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Objects
{
public class TimeTagTest
{
[TestCase]
public void TestClone()
{
var timeTag = new TimeTag(new TextIndex(1, TextIndex.IndexState.End), 1000);

var clonedTimeTag = timeTag.DeepClone();

Assert.AreEqual(clonedTimeTag.Index, timeTag.Index);

Assert.AreNotSame(clonedTimeTag.TimeBindable, timeTag.TimeBindable);
Assert.AreEqual(clonedTimeTag.Time, timeTag.Time);
}
}
}
64 changes: 5 additions & 59 deletions osu.Game.Rulesets.Karaoke/Objects/Lyric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
Expand All @@ -24,7 +22,7 @@

namespace osu.Game.Rulesets.Karaoke.Objects
{
public class Lyric : KaraokeHitObject, IHasDuration, IHasSingers, IHasOrder, IHasLock, IHasPrimaryKey, IDeepCloneable<Lyric>
public partial class Lyric : KaraokeHitObject, IHasDuration, IHasSingers, IHasOrder, IHasLock, IHasPrimaryKey, IDeepCloneable<Lyric>
{
/// <summary>
/// Primary key.
Expand Down Expand Up @@ -215,6 +213,9 @@ public Lyric? ReferenceLyric
set => ReferenceLyricBindable.Value = value;
}

[JsonIgnore]
public readonly Bindable<int> ReferenceLyricConfigVersion = new();

[JsonIgnore]
public readonly Bindable<IReferenceLyricPropertyConfig?> ReferenceLyricConfigBindable = new();

Expand All @@ -229,62 +230,7 @@ public IReferenceLyricPropertyConfig? ReferenceLyricConfig

public Lyric()
{
TimeTagsBindable.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in args.NewItems.Cast<TimeTag>())
c.Changed += invalidate;
break;

case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var c in args.OldItems.Cast<TimeTag>())
c.Changed -= invalidate;
break;
}

void invalidate() => TimeTagsVersion.Value++;
};

RubyTagsBindable.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in args.NewItems.Cast<RubyTag>())
c.Changed += invalidate;
break;

case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var c in args.OldItems.Cast<RubyTag>())
c.Changed -= invalidate;
break;
}

void invalidate() => RubyTagsVersion.Value++;
};

RomajiTagsBindable.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in args.NewItems.Cast<RomajiTag>())
c.Changed += invalidate;
break;

case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var c in args.OldItems.Cast<RomajiTag>())
c.Changed -= invalidate;
break;
}

void invalidate() => RomajiTagsVersion.Value++;
};
initInternalBindingEvent();
}

public override Judgement CreateJudgement() => new KaraokeLyricJudgement();
Expand Down
86 changes: 86 additions & 0 deletions osu.Game.Rulesets.Karaoke/Objects/Lyric_Binding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.Specialized;
using System.Linq;

namespace osu.Game.Rulesets.Karaoke.Objects
{
public partial class Lyric
{
private void initInternalBindingEvent()
{
TimeTagsBindable.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in args.NewItems.Cast<TimeTag>())
c.Changed += invalidate;
break;

case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var c in args.OldItems.Cast<TimeTag>())
c.Changed -= invalidate;
break;
}

void invalidate() => TimeTagsVersion.Value++;
};

RubyTagsBindable.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in args.NewItems.Cast<RubyTag>())
c.Changed += invalidate;
break;

case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var c in args.OldItems.Cast<RubyTag>())
c.Changed -= invalidate;
break;
}

void invalidate() => RubyTagsVersion.Value++;
};

RomajiTagsBindable.CollectionChanged += (_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var c in args.NewItems.Cast<RomajiTag>())
c.Changed += invalidate;
break;

case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var c in args.OldItems.Cast<RomajiTag>())
c.Changed -= invalidate;
break;
}

void invalidate() => RomajiTagsVersion.Value++;
};

ReferenceLyricConfigBindable.ValueChanged += e =>
{
if (e.OldValue != null)
{
e.OldValue.Changed -= invalidate;
}

if (e.NewValue != null)
{
e.NewValue.Changed += invalidate;
}

void invalidate() => ReferenceLyricConfigVersion.Value++;
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;

namespace osu.Game.Rulesets.Karaoke.Objects.Properties
{
public interface IReferenceLyricPropertyConfig
{
double OffsetTime { get; set; }

public event Action? Changed;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// 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 Newtonsoft.Json;
using osu.Framework.Bindables;

namespace osu.Game.Rulesets.Karaoke.Objects.Properties
{
public class ReferenceLyricConfig : IReferenceLyricPropertyConfig
{
public event Action? Changed;

public ReferenceLyricConfig()
{
OffsetTimeBindable.ValueChanged += _ => Changed?.Invoke();
}

[JsonIgnore]
public readonly Bindable<double> OffsetTimeBindable = new BindableDouble();

Expand Down
10 changes: 10 additions & 0 deletions osu.Game.Rulesets.Karaoke/Objects/Properties/SyncLyricConfig.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.ComponentModel;
using System.Text.Json.Serialization;
using osu.Framework.Bindables;
Expand All @@ -9,6 +10,15 @@ namespace osu.Game.Rulesets.Karaoke.Objects.Properties
{
public class SyncLyricConfig : IReferenceLyricPropertyConfig
{
public event Action? Changed;

public SyncLyricConfig()
{
OffsetTimeBindable.ValueChanged += _ => Changed?.Invoke();
SyncSingerPropertyBindable.ValueChanged += _ => Changed?.Invoke();
SyncTimeTagPropertyBindable.ValueChanged += _ => Changed?.Invoke();
}

[JsonIgnore]
public readonly Bindable<double> OffsetTimeBindable = new BindableDouble();

Expand Down
11 changes: 10 additions & 1 deletion osu.Game.Rulesets.Karaoke/Objects/RomajiTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Game.Rulesets.Karaoke.Objects.Types;
using osu.Game.Utils;

namespace osu.Game.Rulesets.Karaoke.Objects
{
public class RomajiTag : ITextTag
public class RomajiTag : ITextTag, IDeepCloneable<RomajiTag>
{
/// <summary>
/// Invoked when any property of this <see cref="RomajiTag"/> is changed.
Expand Down Expand Up @@ -57,5 +58,13 @@ public int EndIndex
get => EndIndexBindable.Value;
set => EndIndexBindable.Value = value;
}

public RomajiTag DeepClone()
=> new()
{
Text = Text,
StartIndex = StartIndex,
EndIndex = EndIndex,
};
}
}
11 changes: 10 additions & 1 deletion osu.Game.Rulesets.Karaoke/Objects/RubyTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using Newtonsoft.Json;
using osu.Framework.Bindables;
using osu.Game.Rulesets.Karaoke.Objects.Types;
using osu.Game.Utils;

namespace osu.Game.Rulesets.Karaoke.Objects
{
public class RubyTag : ITextTag
public class RubyTag : ITextTag, IDeepCloneable<RubyTag>
{
/// <summary>
/// Invoked when any property of this <see cref="RubyTag"/> is changed.
Expand Down Expand Up @@ -57,5 +58,13 @@ public int EndIndex
get => EndIndexBindable.Value;
set => EndIndexBindable.Value = value;
}

public RubyTag DeepClone()
=> new()
{
Text = Text,
StartIndex = StartIndex,
EndIndex = EndIndex,
};
}
}
Loading

0 comments on commit ea5a554

Please sign in to comment.