Skip to content

Commit

Permalink
Create and delete time-tag works.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Dec 12, 2020
1 parent 848bfde commit be7e310
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
22 changes: 17 additions & 5 deletions osu.Game.Rulesets.Karaoke/Edit/Lyrics/LyricEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ protected override bool OnKeyDown(KeyDownEvent e)
if (timeTagManager == null)
return false;

// moving cursor action
switch (e.Key)
{
case Key.Up:
Expand All @@ -72,16 +73,27 @@ protected override bool OnKeyDown(KeyDownEvent e)
return timeTagManager.MoveCursor(CursorAction.First);
case Key.PageDown:
return timeTagManager.MoveCursor(CursorAction.Last);
}

// edit time tag action
var currentTimeTag = timeTagManager?.BindableCursorPosition?.Value;
switch (e.Key)
{
case Key.BackSpace:
case Key.Delete:
var currentTimeTag = timeTagManager?.BindableCursorPosition?.Value;
return timeTagManager?.ClearTimeTagTime(currentTimeTag) ?? false;
case Key.Space:
var timeTag = timeTagManager?.BindableCursorPosition?.Value;
var setTimeSuccess = timeTagManager?.SetTimeTagTime(timeTag) ?? false;
if(setTimeSuccess)
var setTimeSuccess = timeTagManager?.SetTimeTagTime(currentTimeTag) ?? false;
if (setTimeSuccess)
timeTagManager.MoveCursor(CursorAction.MoveRight);
return setTimeSuccess;
case Key.N:
var createdTimeTag = timeTagManager?.AddTimeTag(currentTimeTag);
if (createdTimeTag != null)
timeTagManager.MoveCursorToTargetPosition(createdTimeTag);
return createdTimeTag != null;
case Key.Delete:
timeTagManager?.MoveCursor(CursorAction.MoveRight);
return timeTagManager?.RemoveTimeTag(currentTimeTag) ?? false;
default:
return base.OnKeyDown(e);
}
Expand Down
62 changes: 61 additions & 1 deletion osu.Game.Rulesets.Karaoke/Edit/Lyrics/TimeTagManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags.Ja;
using osu.Game.Rulesets.Karaoke.Edit.Generator.TimeTags.Zh;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Screens.Edit;
using System;
using System.Linq;
Expand All @@ -32,6 +33,8 @@ public class TimeTagManager : Component

public Bindable<TimeTag> BindableCursorPosition { get; set; } = new Bindable<TimeTag>();

#region Edit Time Tag

/// <summary>
/// Will auto-detect each <see cref="Lyric"/> 's <see cref="Lyric.TimeTags"/> and apply on them.
/// </summary>
Expand Down Expand Up @@ -64,7 +67,10 @@ public bool SetTimeTagTime(TimeTag timeTag)
return false;

changeHandler?.BeginChange();

timeTag.Time = framedClock.CurrentTime;
refreshTimeTag(currentLyric);

changeHandler?.EndChange();

currentLyric.TimeTagsBindable.TriggerChange();
Expand All @@ -81,13 +87,65 @@ public bool ClearTimeTagTime(TimeTag timeTag)
return false;

changeHandler?.BeginChange();

timeTag.Time = null;
refreshTimeTag(currentLyric);

changeHandler?.EndChange();

currentLyric.TimeTagsBindable.TriggerChange();
return true;
}

public TimeTag AddTimeTag(TimeTag timeTag)
{
var currentLyric = timeTagInLyric(timeTag);
if (currentLyric == null)
return null;

var timeTags = currentLyric.TimeTags.ToList();
var targetIndex = timeTags.IndexOf(timeTag);
if (targetIndex < 0)
return null;

var newTimeTag = new TimeTag(timeTag.Index);
timeTags.Insert(targetIndex, newTimeTag);

changeHandler?.BeginChange();

currentLyric.TimeTags = timeTags.ToArray();
sortingTimeTag(currentLyric);

changeHandler?.EndChange();

return newTimeTag;
}

public bool RemoveTimeTag(TimeTag timeTag)
{
var currentLyric = timeTagInLyric(timeTag);
if (currentLyric == null)
return false;

changeHandler?.BeginChange();

// delete time tag from list
currentLyric.TimeTags = currentLyric.TimeTags.Where(x => x != timeTag).ToArray();

changeHandler?.EndChange();

return true;
}

private void refreshTimeTag(Lyric lyric)
=> lyric.TimeTags = lyric.TimeTags.ToArray();

private void sortingTimeTag(Lyric lyric)
=> lyric.TimeTags = TimeTagsUtils.Sort(lyric.TimeTags);

#endregion

#region Time Tag cursor

public bool MoveCursor(CursorAction action)
{
var currentTimeTag = BindableCursorPosition.Value;
Expand Down Expand Up @@ -223,6 +281,8 @@ public TimeTag[] GenerateTimeTags(Lyric lyric)
}
}
}

#endregion
}

public enum CursorAction
Expand Down

0 comments on commit be7e310

Please sign in to comment.