Skip to content

Commit

Permalink
Merge pull request #1558 from andy840119/implement-allow-edit-by-edit…
Browse files Browse the repository at this point in the history
…-mode-utils

Implement allow edit by edit mode utils.
  • Loading branch information
andy840119 authored Sep 4, 2022
2 parents bf7269f + c0ab63d commit edd4d12
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Utils
{
public class HitObjectWritableUtilsTest
{
#region Lyric property

[Test]
public void TestIsWriteLyricPropertyLocked()
{
Expand Down Expand Up @@ -55,12 +57,22 @@ void test(Lyric lyric)
=> testEveryWritablePropertyInObject<Lyric, Lyric>(lyric, (l, propertyName) => HitObjectWritableUtils.GetLyricPropertyLockedReason(l, propertyName));
}

#endregion

#region Note property

[Test]
public void TestIsWriteNotePropertyLocked()
{
// standard.
test(new Note());

// test with reference lyric.
test(new Note
{
ReferenceLyric = new Lyric(),
});

void test(Note note)
=> testEveryWritablePropertyInObject<Note, Note>(note, (l, propertyName) => HitObjectWritableUtils.IsWriteNotePropertyLocked(l, propertyName));
}
Expand All @@ -71,16 +83,12 @@ public void TestGetNotePropertyLockedReason()
// standard.
test(new Note());

// test with reference lyric.
test(new Note
{
ReferenceLyric = new Lyric(),
});

void test(Note note)
=> testEveryWritablePropertyInObject<Note, Note>(note, (l, propertyName) => HitObjectWritableUtils.GetNotePropertyLockedReason(l, propertyName));
}

#endregion

private void testEveryWritablePropertyInObject<THitObject, TProperty>(TProperty property, Action<TProperty, string> action)
{
// the purpose of this test case if focus on checking every property in the hit-object should be able to know the writable or not.
Expand Down
33 changes: 33 additions & 0 deletions osu.Game.Rulesets.Karaoke/Edit/Lyrics/LyricEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Rulesets.Karaoke.Configuration;
using osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers.Lyrics;
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.Extends;
Expand All @@ -25,7 +26,9 @@
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.Extends.TimeTags;
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.States;
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.States.Modes;
using osu.Game.Rulesets.Karaoke.Edit.Utils;
using osu.Game.Rulesets.Karaoke.Extensions;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Timing;
using osu.Game.Rulesets.UI;
using osu.Game.Rulesets.UI.Scrolling;
Expand Down Expand Up @@ -356,5 +359,35 @@ private class LocalScrollingInfo : IScrollingInfo

public IScrollAlgorithm Algorithm { get; } = new SequentialScrollAlgorithm(new List<MultiplierControlPoint>());
}

public static LocalisableString? GetLyricPropertyLockedReason(Lyric lyric, LyricEditorMode mode)
{
var reasons = getLyricPropertyLockedReasons(lyric, mode);

return reasons switch
{
LockLyricPropertyBy.ReferenceLyricConfig => "Cannot modify this property due to this lyric is property is sync from another lyric.",
LockLyricPropertyBy.LockState => "This property is locked and not editable",
null => null,
_ => throw new ArgumentOutOfRangeException()
};
}

private static LockLyricPropertyBy? getLyricPropertyLockedReasons(Lyric lyric, LyricEditorMode mode)
{
return mode switch
{
LyricEditorMode.View => null,
LyricEditorMode.Texting => HitObjectWritableUtils.GetLyricPropertyLockedReasons(lyric, nameof(Lyric.Text), nameof(Lyric.RubyTags), nameof(Lyric.RomajiTags), nameof(Lyric.TimeTags)),
LyricEditorMode.Reference => HitObjectWritableUtils.GetLyricPropertyLockedReasons(lyric, nameof(Lyric.ReferenceLyric), nameof(Lyric.ReferenceLyricConfig)),
LyricEditorMode.Language => HitObjectWritableUtils.GetLyricPropertyLockedReason(lyric, nameof(Lyric.Language)),
LyricEditorMode.EditRuby => HitObjectWritableUtils.GetLyricPropertyLockedReason(lyric, nameof(Lyric.RubyTags)),
LyricEditorMode.EditRomaji => HitObjectWritableUtils.GetLyricPropertyLockedReason(lyric, nameof(Lyric.RomajiTags)),
LyricEditorMode.EditTimeTag => HitObjectWritableUtils.GetLyricPropertyLockedReason(lyric, nameof(Lyric.TimeTags)),
LyricEditorMode.EditNote => HitObjectWritableUtils.GetCreateOrRemoveNoteLockedReason(lyric),
LyricEditorMode.Singer => HitObjectWritableUtils.GetLyricPropertyLockedReason(lyric, nameof(Lyric.Singers)),
_ => throw new ArgumentOutOfRangeException(nameof(mode), mode, null)
};
}
}
}
28 changes: 26 additions & 2 deletions osu.Game.Rulesets.Karaoke/Edit/Utils/HitObjectWritableUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ public static bool IsWriteLyricPropertyLocked(Lyric lyric, params string[] prope
public static bool IsWriteLyricPropertyLocked(Lyric lyric, string propertyName)
=> GetLyricPropertyLockedReason(lyric, propertyName) != null;

public static LockLyricPropertyBy? GetLyricPropertyLockedReasons(Lyric lyric, params string[] propertyNames)
{
var reasons = propertyNames.Select(x => GetLyricPropertyLockedReason(lyric, x))
.Where(x => x != null)
.OfType<LockLyricPropertyBy>()
.ToArray();

if (reasons.Contains(LockLyricPropertyBy.ReferenceLyricConfig))
return LockLyricPropertyBy.ReferenceLyricConfig;

if (reasons.Contains(LockLyricPropertyBy.LockState))
return LockLyricPropertyBy.LockState;

return null;
}

public static LockLyricPropertyBy? GetLyricPropertyLockedReason(Lyric lyric, string propertyName)
{
bool lockedByConfig = isWriteLyricPropertyLockedByConfig(lyric.ReferenceLyricConfig, propertyName);
Expand Down Expand Up @@ -93,11 +109,18 @@ private static bool isWriteLyricPropertyLockedByConfig(IReferenceLyricPropertyCo
#region Create or remove notes.

public static bool IsCreateOrRemoveNoteLocked(Lyric lyric)
=> GetCreateOrRemoveNoteLockedReason(lyric) != null;

public static LockLyricPropertyBy? GetCreateOrRemoveNoteLockedReason(Lyric lyric)
{
return IsCreateOrRemoveNoteLocked(lyric.ReferenceLyricConfig);
bool lockedByConfig = isCreateOrRemoveNoteLocked(lyric.ReferenceLyricConfig);
if (lockedByConfig)
return LockLyricPropertyBy.ReferenceLyricConfig;

return null;
}

public static bool IsCreateOrRemoveNoteLocked(IReferenceLyricPropertyConfig? config)
private static bool isCreateOrRemoveNoteLocked(IReferenceLyricPropertyConfig? config)
{
// todo: implementation.
return config switch
Expand Down Expand Up @@ -139,6 +162,7 @@ private static bool isWriteNotePropertyLockedByReferenceLyric(Lyric lyric, strin
#endregion
}

[Flags]
public enum LockLyricPropertyBy
{
ReferenceLyricConfig,
Expand Down

0 comments on commit edd4d12

Please sign in to comment.