Skip to content

Commit

Permalink
Merge pull request #311 from andy840119/lyric-editor/make-time-tag-ou…
Browse files Browse the repository at this point in the history
…tside

Make time-tag outside the drawable lyric.
  • Loading branch information
andy840119 authored Dec 13, 2020
2 parents 15648bc + d04c8ca commit 2982aa3
Showing 1 changed file with 81 additions and 74 deletions.
155 changes: 81 additions & 74 deletions osu.Game.Rulesets.Karaoke/Edit/Lyrics/Components/LyricControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Drawables;
using osu.Game.Rulesets.Karaoke.Skinning.Components;
using osuTK;

namespace osu.Game.Rulesets.Karaoke.Edit.Lyrics.Components
{
public class LyricControl : Container
{
private const int time_tag_spacing = 4;

private readonly DrawableEditorLyric drawableLyric;
private readonly Container timeTagContainer;
private readonly Container timeTagCursorContainer;

public Lyric Lyric { get; }

Expand All @@ -27,10 +32,36 @@ public LyricControl(Lyric lyric)
CornerRadius = 5;
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Bottom = 10 };
InternalChildren = new Drawable[]
Children = new Drawable[]
{
drawableLyric = new DrawableEditorLyric(lyric)
{
ApplyFontAction = () =>
{
// todo : need to delay until karaoke text has been calculated.
ScheduleAfterChildren(UpdateTimeTags);
}
},
timeTagContainer = new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Scale = new Vector2(2)
},
timeTagCursorContainer = new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Scale = new Vector2(2)
}
};

drawableLyric.TimeTagsBindable.BindValueChanged(e =>
{
ScheduleAfterChildren(UpdateTimeTags);
});
}

[BackgroundDependencyLoader(true)]
Expand All @@ -39,60 +70,66 @@ private void load(IFrameBasedClock framedClock, TimeTagManager timeTagManager)
drawableLyric.Clock = framedClock;
timeTagManager?.BindableCursorPosition.BindValueChanged(e =>
{
drawableLyric?.UpdateTimeTagCursoe(e.NewValue);
UpdateTimeTagCursoe(e.NewValue);
}, true);
}

public class DrawableEditorLyric : DrawableLyric
public void UpdateTimeTagCursoe(TimeTag cursor)
{
private const int time_tag_spacing = 4;

private readonly Container timeTagContainer;
private readonly Container timeTagCursorContainer;

public DrawableEditorLyric(Lyric lyric)
: base(lyric)
timeTagCursorContainer.Clear();
if (drawableLyric.TimeTagsBindable.Value.Contains(cursor))
{
AddRangeInternal(new[]
var spacing = timeTagPosition(cursor);
timeTagCursorContainer.Add(new DrawableTimeTagCursor(cursor)
{
timeTagContainer = new Container
{
RelativeSizeAxes = Axes.Both
},
timeTagCursorContainer = new Container
{
RelativeSizeAxes = Axes.Both
}
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
X = spacing
});

DisplayRuby = true;
DisplayRomaji = true;
}
}

public void UpdateTimeTagCursoe(TimeTag cursor)
{
timeTagCursorContainer.Clear();
protected void UpdateTimeTags()
{
timeTagContainer.Clear();
var timeTags = drawableLyric.TimeTagsBindable.Value;
if (timeTags == null)
return;

if (TimeTagsBindable.Value.Contains(cursor))
foreach (var timeTag in timeTags)
{
var spacing = timeTagPosition(timeTag);
timeTagContainer.Add(new DrawableTimeTag(timeTag)
{
var spacing = timeTagPosition(cursor);
timeTagCursorContainer.Add(new DrawableTimeTagCursor(cursor)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
X = spacing
});
}
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
X = spacing
});
}
}

protected override void LoadComplete()
{
base.LoadComplete();
private float timeTagPosition(TimeTag timeTag)
{
var index = Math.Min(timeTag.Index.Index, Lyric.Text.Length - 1);
var isStart = timeTag.Index.State == TimeTagIndex.IndexState.Start;
var percentage = isStart ? 0 : 1;
var position = drawableLyric.GetPercentageWidth(index, index + 1, percentage);

var timeTags = isStart ? drawableLyric.TimeTagsBindable.Value.Reverse() : drawableLyric.TimeTagsBindable.Value;
var duplicatedTagAmount = timeTags.SkipWhile(t => t != timeTag).Count(x => x.Index == timeTag.Index) - 1;
var spacing = duplicatedTagAmount * time_tag_spacing * (isStart ? 1 : -1);
return position + spacing;
}

TimeTagsBindable.BindValueChanged(e =>
{
UpdateTimeTags();
}, true);
public class DrawableEditorLyric : DrawableLyric
{
public Action ApplyFontAction;

public DrawableEditorLyric(Lyric lyric)
: base(lyric)
{
DisplayRuby = true;
DisplayRomaji = true;
}

protected override void ApplyFont(KaraokeFont font)
Expand All @@ -102,8 +139,7 @@ protected override void ApplyFont(KaraokeFont font)
if (TimeTagsBindable.Value == null)
return;

// todo : need to delay until karaoke text has been calculated.
ScheduleAfterChildren(UpdateTimeTags);
ApplyFontAction?.Invoke();
}

protected override void ApplyLayout(KaraokeLayout layout)
Expand All @@ -129,37 +165,8 @@ public override double LifetimeEnd
set => base.LifetimeEnd = double.MaxValue;
}

protected void UpdateTimeTags()
{
timeTagContainer.Clear();
var timeTags = TimeTagsBindable.Value;
if (timeTags == null)
return;

foreach (var timeTag in timeTags)
{
var spacing = timeTagPosition(timeTag);
timeTagContainer.Add(new DrawableTimeTag(timeTag)
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
X = spacing
});
}
}

private float timeTagPosition(TimeTag timeTag)
{
var index = Math.Min(timeTag.Index.Index, HitObject.Text.Length - 1);
var isStart = timeTag.Index.State == TimeTagIndex.IndexState.Start;
var percentage = isStart ? 0 : 1;
var position = karaokeText.GetPercentageWidth(index, index + 1, percentage);

var timeTags = isStart ? TimeTagsBindable.Value.Reverse() : TimeTagsBindable.Value;
var duplicatedTagAmount = timeTags.SkipWhile(t => t != timeTag).Count(x => x.Index == timeTag.Index) - 1;
var spacing = duplicatedTagAmount * time_tag_spacing * (isStart ? 1 : -1);
return position + spacing;
}
public float GetPercentageWidth(int startIndex, int endIndex, float percentage = 0)
=> karaokeText.GetPercentageWidth(startIndex, endIndex, percentage);
}
}
}

0 comments on commit 2982aa3

Please sign in to comment.