From 141d6e3ffbdde075b6a612c8ccda24b089ec1f1e Mon Sep 17 00:00:00 2001 From: andy840119 Date: Fri, 11 Dec 2020 23:34:13 +0900 Subject: [PATCH] Make time-tag outside. --- .../Edit/Lyrics/Components/LyricControl.cs | 154 +++++++++--------- 1 file changed, 81 insertions(+), 73 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke/Edit/Lyrics/Components/LyricControl.cs b/osu.Game.Rulesets.Karaoke/Edit/Lyrics/Components/LyricControl.cs index 80be160ea..d44459f12 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Lyrics/Components/LyricControl.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Lyrics/Components/LyricControl.cs @@ -10,6 +10,7 @@ using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Rulesets.Karaoke.Objects.Drawables; using osu.Game.Rulesets.Karaoke.Skinning.Components; +using osuTK; using System; using System.Linq; @@ -17,7 +18,11 @@ 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; } @@ -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)] @@ -39,59 +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; } + } + + protected void UpdateTimeTags() + { + timeTagContainer.Clear(); + var timeTags = drawableLyric.TimeTagsBindable.Value; + if (timeTags == null) + return; - public void UpdateTimeTagCursoe(TimeTag cursor) + foreach (var timeTag in timeTags) { - timeTagCursorContainer.Clear(); - if (TimeTagsBindable.Value.Contains(cursor)) + 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) @@ -101,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) @@ -128,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); } } }