Skip to content

Commit

Permalink
Merge pull request #326 from andy840119/lyric-editor/create-delete-by…
Browse files Browse the repository at this point in the history
…-mouse-position

Lyric editor/create delete by mouse position.
  • Loading branch information
andy840119 authored Dec 19, 2020
2 parents 3f55d66 + 8719eeb commit e0d9c29
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 37 deletions.
98 changes: 69 additions & 29 deletions osu.Game.Rulesets.Karaoke/Edit/Lyrics/LyricEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LyricEditor : Container
[Resolved(canBeNull: true)]
private TimeTagManager timeTagManager { get; set; }

private LyricEditorStateManager lyricEditorStateManager;
private LyricEditorStateManager stateManager;

private readonly KaraokeLyricEditorSkin skin;
private readonly DrawableLyricEditList container;
Expand Down Expand Up @@ -50,7 +50,7 @@ protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnl
private void load()
{
// todo : might not place into here.
dependencies.Cache(lyricEditorStateManager = new LyricEditorStateManager(beatmap));
dependencies.Cache(stateManager = new LyricEditorStateManager(beatmap));

foreach (var obj in beatmap.HitObjects)
Schedule(() => addHitObject(obj));
Expand All @@ -63,65 +63,105 @@ protected override void LoadComplete()
beatmap.HitObjectAdded += addHitObject;
beatmap.HitObjectRemoved += removeHitObject;

lyricEditorStateManager.MoveCursor(CursorAction.First);
stateManager.MoveCursor(CursorAction.First);
}

protected override bool OnKeyDown(KeyDownEvent e)
{
if (timeTagManager == null)
return false;

var isMoving = HandleMovingEvent(e.Key);
if (isMoving)
return true;

switch (stateManager.Mode)
{
case Mode.ViewMode:
return false;

case Mode.EditMode:
return false;

case Mode.RecordMode:
return HandleSetTimeEvent(e.Key);

case Mode.TimeTagEditMode:
return HandleCreateOrDeleterTimeTagEvent(e.Key);

default:
return false;
}
}

protected bool HandleMovingEvent(Key key)
{
// moving cursor action
switch (e.Key)
switch (key)
{
case Key.Up:
return lyricEditorStateManager.MoveCursor(CursorAction.MoveUp);
return stateManager.MoveCursor(CursorAction.MoveUp);

case Key.Down:
return lyricEditorStateManager.MoveCursor(CursorAction.MoveDown);
return stateManager.MoveCursor(CursorAction.MoveDown);

case Key.Left:
return lyricEditorStateManager.MoveCursor(CursorAction.MoveLeft);
return stateManager.MoveCursor(CursorAction.MoveLeft);

case Key.Right:
return lyricEditorStateManager.MoveCursor(CursorAction.MoveRight);
return stateManager.MoveCursor(CursorAction.MoveRight);

case Key.PageUp:
return lyricEditorStateManager.MoveCursor(CursorAction.First);
return stateManager.MoveCursor(CursorAction.First);

case Key.PageDown:
return lyricEditorStateManager.MoveCursor(CursorAction.Last);
return stateManager.MoveCursor(CursorAction.Last);

default:
return false;
}
}

if (lyricEditorStateManager.Mode != Mode.TimeTagEditMode)
return false;
protected bool HandleSetTimeEvent(Key key)
{
var currentTimeTag = stateManager.BindableRecordCursorPosition.Value;

// edit time tag action
var currentTimeTag = lyricEditorStateManager.BindableRecordCursorPosition.Value;
if (timeTagManager == null)
return false;

switch (e.Key)
switch (key)
{
case Key.BackSpace:
return (bool)timeTagManager?.ClearTimeTagTime(currentTimeTag);
return timeTagManager.ClearTimeTagTime(currentTimeTag);

case Key.Space:
var setTimeSuccess = (bool)timeTagManager?.SetTimeTagTime(currentTimeTag);
var setTimeSuccess = timeTagManager.SetTimeTagTime(currentTimeTag);
if (setTimeSuccess)
lyricEditorStateManager.MoveCursor(CursorAction.MoveRight);
stateManager.MoveCursor(CursorAction.MoveRight);
return setTimeSuccess;

default:
return false;
}
}

protected bool HandleCreateOrDeleterTimeTagEvent(Key key)
{
var position = stateManager.BindableCursorPosition.Value;

if (timeTagManager == null)
return false;

switch (key)
{
case Key.N:
var createdTimeTag = timeTagManager?.AddTimeTag(currentTimeTag);
if (createdTimeTag != null)
lyricEditorStateManager.MoveRecordCursorToTargetPosition(createdTimeTag);
return createdTimeTag != null;
return timeTagManager.AddTimeTagByPosition(position);

case Key.Delete:
lyricEditorStateManager.MoveCursor(CursorAction.MoveRight);
return (bool)timeTagManager?.RemoveTimeTag(currentTimeTag);
return timeTagManager.RemoveTimeTagByPosition(position);

default:
return base.OnKeyDown(e);
return false;
}
}

Expand Down Expand Up @@ -161,14 +201,14 @@ public float FontSize

public Mode Mode
{
get => lyricEditorStateManager.Mode;
set => ScheduleAfterChildren(() => lyricEditorStateManager.SetMode(value));
get => stateManager.Mode;
set => ScheduleAfterChildren(() => stateManager.SetMode(value));
}

public LyricFastEditMode LyricFastEditMode
{
get => lyricEditorStateManager.FastEditMode;
set => ScheduleAfterChildren(() => lyricEditorStateManager.SetFastEditMode(value));
get => stateManager.FastEditMode;
set => ScheduleAfterChildren(() => stateManager.SetFastEditMode(value));
}
}
}
17 changes: 9 additions & 8 deletions osu.Game.Rulesets.Karaoke/Edit/Lyrics/Lyrics/LyricControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using osu.Game.Rulesets.Karaoke.Edit.Lyrics.Lyrics.Components;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Utils;
using osuTK.Input;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.Lyrics
{
Expand All @@ -26,6 +27,9 @@ public class LyricControl : Container
[Resolved(canBeNull: true)]
private LyricManager lyricManager { get; set; }

[Resolved(canBeNull: true)]
private TimeTagManager timeTagManager { get; set; }

[Resolved]
private LyricEditorStateManager stateManager { get; set; }

Expand Down Expand Up @@ -102,28 +106,25 @@ protected override bool OnClick(ClickEvent e)
var timeTagIndex = stateManager.BindableHoverCursorPosition.Value.Index;
stateManager.MoveCursorToTargetPosition(Lyric, timeTagIndex);

return base.OnClick(e);
return true;
}

protected override bool OnDoubleClick(DoubleClickEvent e)
{
if (!isTrigger(stateManager.Mode))
return false;

// split or set time-tag by double click
var timeTagIndex = stateManager.BindableHoverCursorPosition.Value.Index;
// todo : not really sure is ok to split time-tag by double cliek?
// need to make an ux research.
var position = stateManager.BindableHoverCursorPosition.Value;

switch (stateManager.Mode)
{
case Mode.EditMode:
var splitPosition = TimeTagIndexUtils.ToLyricIndex(timeTagIndex);
var splitPosition = TimeTagIndexUtils.ToLyricIndex(position.Index);
lyricManager?.SplitLyric(Lyric, splitPosition);
return true;

case Mode.TimeTagEditMode:
// todo : might add or remove time-tag in here.
return false;

default:
return base.OnDoubleClick(e);
}
Expand Down
45 changes: 45 additions & 0 deletions osu.Game.Rulesets.Karaoke/Edit/Lyrics/TimeTagManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,51 @@ public bool RemoveTimeTag(TimeTag timeTag)
return true;
}

public bool AddTimeTagByPosition(CursorPosition position)
{
var lyric = position.Lyric;
var timeTagIndex = position.Index;
if (!beatmap.HitObjects.Contains(lyric))
return false;

var timeTags = lyric.TimeTags.ToList();
var targetTimeTag = timeTags.FirstOrDefault(x => x.Index >= timeTagIndex) ?? timeTags.LastOrDefault();
if (targetTimeTag == null)
return false;

changeHandler?.BeginChange();

var insertIndex = timeTags.IndexOf(targetTimeTag);
timeTags.Insert(insertIndex, new TimeTag(timeTagIndex));
lyric.TimeTags = timeTags.ToArray();

changeHandler?.EndChange();

return false;
}

public bool RemoveTimeTagByPosition(CursorPosition position)
{
var lyric = position.Lyric;
var timeTagIndex = position.Index;
if (!beatmap.HitObjects.Contains(lyric))
return false;

var timeTags = lyric.TimeTags.ToList();
var targetTimeTag = timeTags.FirstOrDefault(x => x.Index == timeTagIndex);
if (targetTimeTag == null)
return false;

changeHandler?.BeginChange();

timeTags.Remove(targetTimeTag);
lyric.TimeTags = timeTags.ToArray();

changeHandler?.EndChange();

return false;
}

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

Expand Down

0 comments on commit e0d9c29

Please sign in to comment.