Skip to content

Commit

Permalink
Merge pull request #304 from andy840119/lyric-editor/press-space-to-s…
Browse files Browse the repository at this point in the history
…et-time-tag-time

Press space to set current time in time-tag
  • Loading branch information
andy840119 authored Dec 12, 2020
2 parents 4475574 + be7e310 commit 507cfcf
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
22 changes: 22 additions & 0 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,6 +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:
return timeTagManager?.ClearTimeTagTime(currentTimeTag) ?? false;
case Key.Space:
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
100 changes: 99 additions & 1 deletion osu.Game.Rulesets.Karaoke/Edit/Lyrics/TimeTagManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Timing;
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 @@ -23,11 +25,16 @@ public class TimeTagManager : Component
[Resolved]
private EditorBeatmap beatmap { get; set; }

[Resolved(CanBeNull = true)]
[Resolved(canBeNull: true)]
private IEditorChangeHandler changeHandler { get; set; }

[Resolved(canBeNull: true)]
private IFrameBasedClock framedClock { get; set; }

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 All @@ -50,6 +57,95 @@ public void AutoGenerateTimeTags()
changeHandler?.EndChange();
}

public bool SetTimeTagTime(TimeTag timeTag)
{
if (framedClock == null)
return false;

var currentLyric = timeTagInLyric(timeTag);
if (currentLyric == null)
return false;

changeHandler?.BeginChange();

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

changeHandler?.EndChange();

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

public bool ClearTimeTagTime(TimeTag timeTag)
{
if (framedClock == null)
return false;

var currentLyric = timeTagInLyric(timeTag);
if (currentLyric == null)
return false;

changeHandler?.BeginChange();

timeTag.Time = null;
refreshTimeTag(currentLyric);

changeHandler?.EndChange();

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 @@ -185,6 +281,8 @@ public TimeTag[] GenerateTimeTags(Lyric lyric)
}
}
}

#endregion
}

public enum CursorAction
Expand Down

0 comments on commit 507cfcf

Please sign in to comment.